python pyqt5 操作sqlite

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python pyqt5 操作sqlite相关的知识,希望对你有一定的参考价值。

参考技术A

\'\'\'
【简介】
PyQt5中 处理database 例子

\'\'\'

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtSql import QSqlDatabase , QSqlQuery

def createDB():
db = QSqlDatabase.addDatabase(\'QSQLITE\')
db.setDatabaseName(\'./db/database.db\')

if name == \' main \':
app = QApplication(sys.argv)
createDB()
sys.exit(app.exec_())

\'\'\'
【简介】
PyQt5中 处理database 例子

\'\'\'

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
from PyQt5.QtCore import Qt

def initializeModel(model):
model.setTable(\'people\')
model.setEditStrategy(QSqlTableModel.OnFieldChange)
model.select()
model.setHeaderData(0, Qt.Horizontal, "ID")
model.setHeaderData(1, Qt.Horizontal, "name")
model.setHeaderData(2, Qt.Horizontal, "address")

def createView(title, model):
view = QTableView()
view.setModel(model)
view.setWindowTitle(title)
return view

def addrow():
ret = model.insertRows(model.rowCount(), 1)
print(\'insertRows=%s\' % str(ret))

def findrow(i):
delrow = i.row()
print(\'del row=%s\' % str(delrow))

if name == \' main \':
app = QApplication(sys.argv)
db = QSqlDatabase.addDatabase(\'QSQLITE\')
db.setDatabaseName(\'./db/database.db\')
model = QSqlTableModel()
delrow = -1
initializeModel(model)
view1 = createView("Table Model (View 1)", model)
view1.clicked.connect(findrow)

以上是关于python pyqt5 操作sqlite的主要内容,如果未能解决你的问题,请参考以下文章

Python 小白的 PyQt5 项目实战安装与环境配置

python通过PyQt5实现登录界面

PyQt5简介及demo

PyQt5中Web操作与多线程定时器

Python3+Pycharm+PyQt5环境搭建

《PyQT5软件开发》第1章 PyQt5简介