문제

I am working on a simple gui with pyqt and trying to update my database without any luck so far. 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.

While working with python (3) and sqltie3 I could update my db like this:

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

But with pyqt it's only working like this:

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

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top