from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.Qtsql import * import sys db=QsqlDatabase.addDatabase("QsqlITE") db.setDatabaseName('./a.db') if db.open(): print "db is open" class FF(QDialog): def __init__(self,parent=None): super(FF,self).__init__(parent) self.resize(300,300) self.model=QsqlTableModel(self) self.model.setTable("user") self.model.setHeaderData(0,Qt.Horizontal,QVariant("xuhao")) self.model.setHeaderData(1,QVariant("content")) self.model.select() self.view=QTableView(self) self.view.resize(300,300) self.view.setModel(self.model) self.view.resizeColumnsToContents() #基类是qmainwindow是用这个方法添加 #self.setCentralWidget(self.view) query=QsqlQuery() query.exec_("select * from user") if query.next(): print query.value(1).toString() app=QApplication(sys.argv) f=FF() f.show() app.exec_()原文链接:https://www.f2er.com/sqlite/198615.html