在項目研發中,發如今利用QT操做sqlite3數據庫的時候,在本身封裝的工具類裏面,進行delete操做的時候,數據庫報錯,老是說參數不匹配,可是發現本身傳入的sql語句,以及bindValue都是對的。html
無奈之下,經過在overstackflow查找答案,發現問題。sql
必須按照官網給的例子操做順序來執行。數據庫
先prepare,再bindvalue ,再exec執行。個人實現就是沒有按照這個順序,因此出現這個錯誤。app
問題回答原網站:https://stackoverflow.com/questions/20786003/qt-qsqlquery-bindvalue-works-with-but-not-with-placeholders工具
7down votefavoritepost |
I'm working with SQLite, doing insert into table. Folowwingfetch QSqlQuery testQuery(QString("INSERT INTO test(testcol) VALUES(?)")); testQuery.bindValue(0, someQStringObg); testQuery.exec(); works, but網站 QSqlQuery testQuery(QString("INSERT INTO test(testcol) VALUES(:val)")); testQuery.bindValue(":val", someQStringObg); testQuery.exec(); don't. testQuery.lastError().text() returns No query Unable to fetch rowthis Have no clue why things are that way, but really want to find out.spa |
||||||||||||||||
add a comment |
up vote7down voteaccepted |
Please use prepare as the official example: QSqlQuery testQuery; testQuery.prepare("INSERT INTO test(testcol) VALUES(:val)"); testQuery.bindValue(":val", someQStringObj); testQuery.exec(); The reason for the error is that the query was executed before binding to the corresponding placeholder. You can see the relevant part of the constructor documentation:
|