[ 기타 활동 ]/파이썬
python에서 tinyDB사용하기
유니시티황
2018. 7. 13. 02:54
from
tinydb
import
Query,TinyDB
filename
=
"test2.json"
db
=
TinyDB(filename)
#약간 connect 같은 느낌
db.purge_table(
'fruits'
)
#'fruits'라는테이블 삭제
table
=
db.table(
'fruits'
)
#테이블 생성
table.insert({
'name'
:
'사과'
,
'price'
:
5000
})
table.insert({
'name'
:
'바나나'
,
'price'
:
7000
})
table.insert({
'name'
:
'망고'
,
'price'
:
8000
})
table.insert({
'name'
:
'레몬'
,
'price'
:
5500
})
print
(table.
all
())
item
=
Query()
res
=
table.search(item.name
=
=
'사과'
)
#조건 문 사용
print
(res[
0
][
'name'
])
#반환은 리스트형태라서 res[0]을 붙임
res
=
table.search(item.price >
6000
)
for
i
in
res:
print
(
'-'
,i[
'name'
],i[
'price'
])
출처: http://hoony-gunputer.tistory.com/61?category=735007 [후니의 컴퓨터]