PDA

View Full Version : [Solved]Count parameter mismatch on QSqlQuery::exec()



adutzu89
13th March 2015, 15:12
So I just gone through regular expressions to understand them properly so I can use those to to split strings but I am having a bit of a step-back.
Here is the content of the function which does the job:


QSqlQuery adaug;
QRegExp rx("(\\d+),(.+)$");
int pos=0;
QString sirncateg="";
int nrccateg=0;
adaug.prepare("insert into categProd(codcat,numecateg,dataadaugare,datamodifi care) "
"values(?,?,datetime('now','localtime'),datetime('n ow','localtime'))");
//adaug.exec("begin");
for(int i=0; i<strl.size(); i++){

while((pos=rx.indexIn(strl[i],pos))!=-1){
nrccateg=rx.cap(1).toInt();
sirncateg=rx.cap(2).toLower();
pos+=rx.matchedLength();
}

adaug.bindValue(0,nrccateg);
adaug.bindValue(1,sirncateg);
adaug.exec();
pos=0;
if(adaug.lastError().isValid()==true){
QMessageBox::critical(this,"Eroare",adaug.lastError().text());
break;
}
}
if(adaug.lastError().isValid()==false){
//adaug.exec("commit");
QMessageBox::information(this,"Succes","Categoriile au fost importate cu succes.");
}

This code works good, but if I uncomment adaug.exec("begin") and adaug.exec("commit") I the following error: "Parameter count mismatch".
The lines are from a csv file containing: a number, a comma, and a string.
The QRegExp I use to catch the numbers(which are IDs) and the strings after the comma and insert them in the sqlite database, not using QString::split("delimiter") because the strings can contain commas also.
Anyone sees what am I doing wrong? Is it the regex

jefftee
13th March 2015, 15:41
You are preparing the SQL statement in line 6, but then you do execute a different query in line 8. Since you are using the same QSqlQuery, my guess is that the begin query is wiping out the fact that you had prepared the query, etc.

Either do your begin/commit with the QSqlDatabase instance, of move the prepare statement between the begin/commit statements (but outside of loop obviously).

Hope that helps.

P.S. I didn't even look at your regex. You are assuming that you're not finding data or finding the wrong data for bindValue, but I suspect your problem is inherently different than the data you're using for bindValue.

adutzu89
13th March 2015, 15:49
Thanks, indeed the issue is the location where I've put the prepared statement, don't know why I didn't seen it. I even checked on some older code where I had begin/commit and even though there I've used it correctly I didn't seen differences beetween that and the way I set up my prepared query here.