SQLITE 更新查询失败
SQLITE update query fails
我正在尝试通过附加 QtSQl 数据库的新数据来更新 table 列。我需要通过附加新数据来更新列 imgpath
。
下面是代码,但总是失败,可能是什么问题?
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";
更新:
完整代码:
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;
}
SQLite 不支持 concat 函数。如果你删除
imgpath=concat(';newImage.jpg',imgpath)");
并将其替换为标准的 colName='Value' 语法 我打赌一切都会开始工作。如果您想将文本附加到当前值,您应该可以使用类似以下语法的方式来完成:
bool up = query.exec("update table1 set imgpath=';newImage.jpg'||imgpath");
我正在尝试通过附加 QtSQl 数据库的新数据来更新 table 列。我需要通过附加新数据来更新列 imgpath
。
下面是代码,但总是失败,可能是什么问题?
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";
更新:
完整代码:
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;
}
SQLite 不支持 concat 函数。如果你删除
imgpath=concat(';newImage.jpg',imgpath)");
并将其替换为标准的 colName='Value' 语法 我打赌一切都会开始工作。如果您想将文本附加到当前值,您应该可以使用类似以下语法的方式来完成:
bool up = query.exec("update table1 set imgpath=';newImage.jpg'||imgpath");