0

我有如下功能

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() 

将值插入 DB 有效,但不能从 DB 检索信息。我究竟做错了什么 ?似乎选择查询根本没有检索到任何东西

4

1 回答 1

0

放在query.first()前面while (query1.next()):但是,第一行不会被使用。

更好的:

query.first()
while query.isValid():
    # Your Code
    query.next()
于 2017-06-05T15:35:57.263 回答