PyQt5 (一百零六)操作SQLite数据库 案例创建sqllite数据库并插入数据

PyQt5 操作SQLite数据库 案例
创建sqllite数据库并插入数据

import sys

from PyQt5.QtSql import QSqlDatabase, QSqlQuery

'''
PyQt5 操作SQLite数据库 案例
创建sqllite数据库并插入数据
'''

def createDB():
    db = QSqlDatabase.addDatabase('QSQLITE')
    # 指定SQLite数据库的文件名
    db.setDatabaseName('dbSqllite.db')
    if not db.open():
        print('无法建立与数据库的连接!')
        return False

    query = QSqlQuery()
    query.exec('create table people(id int primary key,name varchar(10),address varchar(50))')
    query.exec('insert into people values(1,"李易峰","济南")')
    query.exec('insert into people values(2,"蜘蛛侠","青岛")')
    db.close()
    return True

if __name__ == '__main__':
    createDB()









你可能感兴趣的:(pyqt5,sqlite,数据库,sql,python,pyqt5)