PyQt5操作SQLite数据库

1、操作SQLite数据库
import sys
from PyQt5.QtSql import QSqlDatabase,QSqlQuery
from PyQt5.QtCore import *

def creatDB():
db=QSqlDatabase.addDatabase("QSQLITE")
#指定SQLite数据库的文件名
db.setDatabaseName("./db/database.db")
if not db.open():
print("无法建立与数据库的连接")
return False
qurey=QSqlQuery()
qurey.exec('create table people(id int primary key,name varchar(10),address varchar(50))')
qurey.exec('insert into people values(1,"李宁","Shenyang")')
db.close()
return True

if __name__=="__main__":
creatDB()

posted @ 2020-02-18 11:30  The-Chosen-One  阅读(3136)  评论(0编辑  收藏  举报