qt sqlite addBindValue 提示错误:Parameter count mismatch

原代码:

    sql_query.exec("CREATE TABLE wieght ("
                       "id INTEGER PRIMARY KEY AUTOINCREMENT, "
                       "datestr VARCHAR(100) NOT NULL, "
                       "value VARCHAR(150) NOT NULL) ");
     db.transaction();
        sql_query.prepare("insert into wieght values(?,?)");
        sql_query.addBindValue(datestr);
        sql_query.addBindValue(values);
        if(!sql_query.execBatch()){
            qDebug() << "execBatch fail:" << sql_query.lastError().text();
        }
        if(!db.commit()){
            qDebug() << "commit fail:" << sql_query.lastError().text();
        }

 

抛出错误:

Parameter count mismatch

正确的代码:

    db.transaction();
    sql_query.prepare("insert into wieght (datestr,value) values(?,?)");
        sql_query.addBindValue(datestr);
        sql_query.addBindValue(values);
        if(!sql_query.execBatch()){
            qDebug() << "execBatch fail:" << sql_query.lastError().text();
        }
        if(!db.commit()){
            qDebug() << "commit fail:" << sql_query.lastError().text();
        }

 

posted @ 2021-11-16 16:10  浅笑19  阅读(1666)  评论(0)    收藏  举报