1.数据库创建及打开
使用模块 import sqlite3
def open(self):
self.__connect = sqlite3.connect('data.db')
2.表创建
def initTable(self):
cur = self.__connect.cursor()
constraint = 'IF NOT EXISTS '
sql = 'CREATE TABLE ' + constraint + '''biz_table (id INTEGER PRIMARY KEY AUTOINCREMENT ,
biz_type TEXT,
biz_data TEXT,
biz_name TEXT,
save_time TEXT,
status TEXT,
remark1 TEXT,
remark2 TEXT,
remark3 TEXT,
remark4 TEXT,
remark5 TEXT,
remark6 TEXT,
remark7 TEXT,
remark8 TEXT,
remark9 TEXT,
remark10 TEXT);'''
cur.execute(sql)
# cur.close()
self.__connect.commit()
3.插入数据
def insertData(self,biz_data):
cur = self.__connect.cursor()
cur.execute('INSERT into biz_table (biz_data) values (?)',(biz_data,))
self.__connect.commit()
更新及删除,会sql即可。
4.关闭数据库操作
def close(self):
self.__connect.cursor().close()
self.__connect.close()
5.测试源码
import sqlite3
class SqlManager:
__connect = None
def open(self):
self.__connect = sqlite3.connect('data.db')
def initTable(self):
cur = self.__connect.cursor()
constraint = 'IF NOT EXISTS '
sql = 'CREATE TABLE ' + constraint + '''biz_table (id INTEGER PRIMARY KEY AUTOINCREMENT ,
biz_type TEXT,
biz_data TEXT,
biz_name TEXT,
save_time TEXT,
status TEXT,
remark1 TEXT,
remark2 TEXT,
remark3 TEXT,
remark4 TEXT,
remark5 TEXT,
remark6 TEXT,
remark7 TEXT,
remark8 TEXT,
remark9 TEXT,
remark10 TEXT);'''
cur.execute(sql)
# cur.close()
self.__connect.commit()
def insertData(self,biz_data):
cur = self.__connect.cursor()
cur.execute('INSERT into biz_table (biz_data) values (?)',(biz_data,))
self.__connect.commit()
def close(self):
self.__connect.cursor().close()
self.__connect.close()
if __name__ == "__main__":
sql = SqlManager()
sql.open()
sql.initTable()
sql.insertData('12321213121')
sql.close()