简体   繁体   English

使用QSqlQuery更新sqlite3数据库

[英]Updating a sqlite3 Database with QSqlQuery

I am working on a simple gui with pyqt and trying to update my database without any luck so far. 我正在用pyqt开发一个简单的gui,并尝试更新我的数据库,到目前为止没有任何运气。 I am pretty new to qt, database and well also python ;) so I don't understand whats wrong with my code, maybe some one could help me a little bit. 我对qt,数据库以及python来说还很新;)所以我不明白我的代码有什么问题,也许有人可以帮助我一点点。

While working with python (3) and sqltie3 I could update my db like this: 在使用python(3)和sqltie3时,我可以像这样更新我的数据库:

cur.execute('''UPDATE news SET Raw=? WHERE id=?''', (raw, news_id))

But with pyqt it's only working like this: 但是使用pyqt只能像这样工作:

query = QSqlQuery()
query.exec_("UPDATE news SET Raw = 'test' WHERE Id = 9")

I tried: 我试过了:

query.exec_("UPDATE news SET Raw = ? WHERE Id = ?", (raw, news_id))

What lead to this error: 导致此错误的原因:

TypeError: arguments did not match any overloaded call:
QSqlQuery.exec_(str): too many arguments
QSqlQuery.exec_(): too many arguments

In the book rapid gui programming with qt he is doing his queries like this (not exactly like that but I tried to adapt it): 在使用qt进行快速gui编程的书中,他正在这样查询(不是完全一样,但我尝试对其进行调整):

query.exec_('''UPDATE news SET Raw={0} WHERE id={1}'''.format(raw, news_id))

It doesn't seem to do anything. 它似乎什么也没做。

and (doesn't seem to do anything either): 和(似乎也不做任何事情):

raw = 'test'
query.prepare("UPDATE news SET (Raw) VALUES (:raw) WHERE Id = 9 ")
query.bindValue(":raw", raw)
#query.bindValue(":news_id", 9)
query.exec_()

Well and some other things I have found here and elsewhere but without any luck so far. 好吧,我在这里和其他地方发现了一些其他东西,但是到目前为止还没有任何运气。

It seems that you have not yet tried the proper syntax which should be the following: 似乎您尚未尝试使用以下正确语法:

query.prepare(QString("UPDATE news SET Raw = :value WHERE id = :id "));
query.bindValue(":value", raw);
query.bindValue(":id", news_id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM