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
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