C++ sqlite commit db after an update
-
wrote on 24 Aug 2016, 12:57 last edited by
QSqlDatabase db; QSqlQuery Csetp(db); Csetp.prepare("SELECT fq1, fq2, fq4, t1, t2, t3, t4 ,t5, t6, t7, t8 FROM defaultdata WHERE id_default =:id_default"); Csetp.bindValue(":id_default", programNU); qDebug() << "Csetp.exec(): " << Csetp.exec(); //qDebug() << "number of rows: " << Csetp.size(); qDebug() << "Csetp.next(): " << Csetp.next(); qDebug() << "error: " << Csetp.lastError().text(); //qDebug() << "number of rows: " << Csetp.size(); frq1 = Csetp.value(0).toInt(); frq2 = Csetp.value(1).toInt(); frq4 = Csetp.value(2).toInt(); tm1 = Csetp.value(3).toInt(); tm2 = Csetp.value(4).toInt(); tm3 = Csetp.value(5).toInt(); tm4 = Csetp.value(6).toInt(); tm5 = Csetp.value(7).toInt(); tm6 = Csetp.value(8).toInt(); tm7 = Csetp.value(9).toInt(); tm8 = Csetp.value(10).toInt(); qDebug() << "number of rows: " << Csetp.size(); //db.commit(); Csetp.finish(); //db.commit(); qDebug() << "error db on Csetp: " << db.lastError().text(); *******************output**************************** Csetp.exec(): true Csetp.next(): true error: " " number of rows: -1 /*<-------??????*/ error db on Csetp: "Driver not loaded Driver not loaded"
from doc: field 0 is forename and field 1 is surname. Using SELECT * is not recommended because the order of the fields in the query is undefined.
so I try with other query...
QSqlDatabase db; QSqlQuery queryM(db); queryM.prepare("UPDATE defaultdata SET id_default=:id_default, fq1=:fq1, fq2=:fq2, fq4=:fq4, t1=:t1, t2=:t2, t3=:t3, t4=:t4, t5=:t5, t6=:t6, t7=:t7, t8=:t8 WHERE id_default =:id_default"); queryM.bindValue(":id_default", NRPGR ); queryM.bindValue(":fq1",f1M); queryM.bindValue(":fq2",f2M); queryM.bindValue(":fq4",f4M); queryM.bindValue(":t1",t1M); queryM.bindValue(":t2",t2M); queryM.bindValue(":t3",t3M); queryM.bindValue(":t4",t4M); queryM.bindValue(":t5",t5M); queryM.bindValue(":t6",t6M); queryM.bindValue(":t7",t7M); queryM.bindValue(":t8",t8M); queryM.exec(); qDebug() << "queryM size: " << queryM.size(); queryM.next(); //db.commit(); queryM.finish(); db.commit(); qDebug() << "queryM: " << queryM.lastError().text(); qDebug() << "queryM-db: " << db.lastError().text(); *******************output**************************** queryM size: -1 queryM: " " queryM-db: "Driver not loaded Driver not loaded"
So it seems not see valid statement but it exec it (i control the new data in db with sqliteman gui after suthdown of my application. The result is always ok!!!!!).
But for me is impossible to use with success SELECT MAX(id_xxx) ...
and I have these terrible error "Driver not loaded Driver not loaded" (I seem to live in a film of horrors where the monster always comes out from behind the corner ...)regards
Giorgio -
@gfxx said:
QSqlDatabase db;
Make sure you dont have multiple
QSqlDatabase db around in code.
Just have 1 in class that last the life time of the application.So your UPDATE works but you SELECT returns nothing.
- (I seem to live in a film of horrors where the monster always comes out from behind the corner ...)
Good news is that most of the time, the monsters die and hero survives :)
- (I seem to live in a film of horrors where the monster always comes out from behind the corner ...)
-
@gfxx said:
QSqlDatabase db;
Make sure you dont have multiple
QSqlDatabase db around in code.
Just have 1 in class that last the life time of the application.So your UPDATE works but you SELECT returns nothing.
- (I seem to live in a film of horrors where the monster always comes out from behind the corner ...)
Good news is that most of the time, the monsters die and hero survives :)
wrote on 24 Aug 2016, 14:15 last edited bynews .... query.lasterror.text()
"cannot commit - no transaction is active Unable to commit transaction"
Make sure you dont have multiple
QSqlDatabase db around in code.eureka!!
I write only 4 app in qt with database ... The first is managing data from sensors ... What I have many updates and inserts, and many SELECT MAX ... other times it was enough to save the data before the shutdown .... so I never explored the use QSqldatabase ... I always put everything in mainwindows ...
thanks a lot
edit ... commit command is not necessary .... somewho it suggested my to use it in old app (3 years ago in QT4.x) ... only today I see the real work of these command ...
Best Regards
GiorgioI would say that the hero is you today .... next time I hope to be me .... thanks :)
- (I seem to live in a film of horrors where the monster always comes out from behind the corner ...)
-
Super :)
To use commit, one should start with
bool QSqlDatabase::transaction()
first. Else commit is not valid.Happy coding :)
21/24