강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

chokine12님의 프로필 이미지
chokine12

작성한 질문수

파이썬 입문 및 웹 크롤링을 활용한 다양한 자동화 어플리케이션 제작하기

데이터베이스 개요

purge를 쓰기만하면 계속 에러나오네요

작성

·

252

0

import sys

import io

from tinydb import TinyDB, Query

from tinydb.storages import MemoryStorage

import simplejson as json

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')

sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

#파일 DB 생성

# db = TinyDB('d:/python/section5/databases/database.db',default_table='todos') #테이블명 지정안하면 default로 생성

db = TinyDB('d:/python/section5/databases/database.db',default_table='users')

#메모리 DB 생성

db = TinyDB(storage=MemoryStorage, default_table='users')

#테이블 선택

users = db.table('users')

#테이블 데이터 전체 삽입1

with open('d:/python/section5/data/users.json','r') as infile:

    r = json.loads(infile.read())

    for p in r:

        users.insert(p)

#전체 데이터 출력

print(users.all())

#테이블 목록 조회

print(db.tables())

#전체 데이터 삭제

users.purge()

# todos.purge()

db.close()

코딩은 제가한 거랑 선생님께서 해주신거 전부다 에러네요

몇번을 아나콘다 아톰 지웠다가 깔았습니다 힘드네요

Traceback (most recent call last):
  File "D:\python\section5\5-2-1.py", line 26, in <module>
    users.purge()
AttributeError: 'Table' object has no attribute 'purge'
[Finished in 0.115s]

답변 1

0

좋은사람님의 프로필 이미지
좋은사람
지식공유자

안녕하세요.

tinyDB가 버전 업데이트 되서 레퍼런스를 보니

아래와 같네요.

db.truncate() 또는 users.truncate()로 전체 삭제가 가능합니다.

Removing
db.remove(query) Remove all documents matching the query
db.truncate() Remove all documents
chokine12님의 프로필 이미지
chokine12

작성한 질문수

질문하기