I am trying to update the table column by appending with new data of QtSQl database. I need to update the column imgpath
by appending with new data.
Below is the code, but it always fails, what could be the issue?.
QSqlQuery query(db);
query.exec("create table table1 (id integer primary key autoincrement, time varchar(20), imgpath varchar(20))");
query.exec("insert into table1 values(NULL,'00:15:25','img0.jpg')");
query.exec("insert into table1 values(NULL,'00:15:25','img1.jpg')");
bool up = query.exec("update table1 set imgpath=concat(';newImage.jpg',imgpath) where ID=1");
if(up==false)
qDebug()<<"Update failed";
Update:
Complete code:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./newDB");
db.open();
QSqlQuery query(db);
query.exec("create table table1 (id integer primary key autoincrement, time varchar(20), imgpath varchar(20))");
query.exec("insert into table1 values(NULL,'00:15:25','img0.jpg')");
query.exec("insert into table1 values(NULL,'00:15:25','img1.jpg')");
//bool up = query.exec("update table1 set imgpath='newimage.jpg',time='' where ID=1");
bool up = query.exec("update table1 set imgpath=concat(';newImage.jpg',imgpath)");
if(up==false){
qDebug()<<"Update failed";
qDebug() << db.lastError();
}
query.exec("SELECT * FROM table1 limit 100");
QVector<QStringList> lst;
while (query.next())
{
QSqlRecord record = query.record();
QStringList tmp;
for(int i=0; i < record.count(); i++)
{
tmp << record.value(i).toString();
}
lst.append(tmp);
}
foreach (const QStringList &var, lst) {
qDebug() << var;
}
Aucun commentaire:
Enregistrer un commentaire