我有一个如下函数
def updateExpenseEntryToDb (self):
self.day = self.line_edit1.text()
self.category = self.line_edit2.text()
self.amount = self.line_edit3.text()
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('expenses.db')
db.open()
query = QSqlQuery()
query.exec_("create table expense(date DATE primary key, "
"category varchar(20), amount varchar(20))")
query.exec_("insert into expense (date,category,amount) values('%s','%s','%s')" % (self.day, self.category, self.amount))
db.close()
db1 = QSqlDatabase.addDatabase('QSQLITE')
db1.setDatabaseName('expenses.db')
db1.open()
query1 = QSqlQuery()
query1.exec_("SELECT date, category, amount FROM expenses.expense")
while (query1.next()):
extractedDate = query1.value(0).toString()
extractedcategory = query1.value(1).toString()
extractedAmount = query1.value(2).toString()
self.line_edit1.setText(extractedDate)
self.line_edit2.setText(extractedcategory)
self.line_edit3.setText(extractedAmount)
db1.close()
将值插入数据库有效,但不能从数据库检索信息。我做错了什么?似乎选择查询根本没有检索到任何内容
把query.first()
放在while (query1.next()):
之前 但是,不会使用第一行。
更好:
query.first()
while query.isValid():
# Your Code
query.next()