python 用Python的Sqlite3。 #python#sqlite3 #database
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用Python的Sqlite3。 #python#sqlite3 #database相关的知识,希望对你有一定的参考价值。
import sqlite3
DB_FILE = 'database.db'
def db_init():
"""
Initialize the sqlite3 database if it doesn't exist or the tables are
missing..
:return: n/a
"""
conn = sqlite3.connect(DB_FILE)
c = conn.cursor()
c.execute(
'CREATE TABLE if not exists results \
(tag text, num integer, status text')
conn.commit()
conn.close()
def db_write(tag, num, status):
global DEBUG
"""
Writes results from a single boot to the database.
:param data: Data to write.
:param boot: Boot count.
:return: n/a
"""
conn = sqlite3.connect(DB_FILE)
c = conn.cursor()
row = (tag, num, status)
c.execute('INSERT INTO results VALUES (?, ?, ?)', row)
conn.commit()
conn.close()
def db_read(key, filter_value):
if not os.path.isfile(DB_FILE):
print('No db file detected!')
exit()
conn = sqlite3.connect(DB_FILE)
conn.row_factory = sqlite3.Row
c = conn.cursor()
cmd = 'SELECT * FROM results'
if key != '':
cmd = cmd + ' WHERE tag = \"%s\" ' % key
try:
c.execute(cmd)
except sqlite3.OperationalError:
print cmd
tbl = from_db_cursor(c)
print(tbl)
conn.close()
def db_delete_tag(key):
conn = sqlite3.connect(DB_FILE)
conn.row_factory = sqlite3.Row
c = conn.cursor()
if DEBUG: print('Deleting %s from database' % key)
c.execute('DELETE FROM results WHERE tag = \'%s\'' % key)
conn.commit()
conn.close()
以上是关于python 用Python的Sqlite3。 #python#sqlite3 #database的主要内容,如果未能解决你的问题,请参考以下文章
几行代码轻松搞定python的sqlite3的存取
python sqlite3的问题
最近学python和bottle框架,用sqlite3,网页中文显示乱码,咋办捏?
Python爬虫编程思想(83):用Python操作SQLite数据库
Python爬虫编程思想(83):用Python操作SQLite数据库
Python sqlite3 占位符