我想从数据库表中获取记录列表。这些是插入数据库的两个列名“id”和“lable”的值。
INSERT INTO "phone_number_type" VALUES(1,'Home');
INSERT INTO "phone_number_type" VALUES(2,'Work');
INSERT INTO "phone_number_type" VALUES(3,'Mobile');
INSERT INTO "phone_number_type" VALUES(4,'Mistress');
INSERT INTO "phone_number_type" VALUES(5,'Meth Dealer');
我只想将列名“标签”放入 QComboBox。这是我的代码:
import sys
import sqlite3
from PyQt5 import QtSql
from PyQt5.QtSql import QSqlDatabase
from PyQt5.QtWidgets import QMainWindow, QApplication
from combobox import Ui_MainWindow
class fetchrec(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("ttest.db")
self.model = QtSql.QSqlTableModel(self)
self.model.setTable("phone_number_type")
self.comboBox.setModelColumn(self.model.fieldIndex("lable"))
self.comboBox.setModel(self.model)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
userins = fetchrec()
userins.show()
sys.exit(app.exec_())