QT6 SQLite and QSqlQuery "Parameter count mismatch" Error
-
wrote on 30 Jun 2022, 13:36 last edited by Meskaz285
I wrote this function to add data to a database:
void MainWindow::save1() {
QString stid,stname,stsur,stage,stgen,stbday,stcourses;
stid=ui->Stid->text();
stname=ui->name->text();
stsur=ui->surname->text();
stage=ui->age->text();
if(ui->male->isChecked())
stgen="male";
if(ui->female->isChecked())
stgen="female";
stbday=ui->birthday->text();
stcourses=ui->courses->text();
QSqlQuery qry;
qry.prepare("INSERT INTO Student (Student ID,Name,Surname,Age,Gender,Birthday,Courses) VALUES "
"(:stid,:stname,:stsur,:stage,:stgen,:stbday,:stcourses)");
qry.bindValue(":stid", stid);
qry.bindValue(":stname",stname);
qry.bindValue(":stsur",stsur);
qry.bindValue(":stage",stage);
qry.bindValue(":stgen",stgen);
qry.bindValue(":stbday",stbday);
qry.bindValue(":stcourses",stcourses);
qry.exec();
}I keep getting QSqlError("", "Parameter count mismatch", "").
I tried to find a solution but had no luck. I would like some help here -
I wrote this function to add data to a database:
void MainWindow::save1() {
QString stid,stname,stsur,stage,stgen,stbday,stcourses;
stid=ui->Stid->text();
stname=ui->name->text();
stsur=ui->surname->text();
stage=ui->age->text();
if(ui->male->isChecked())
stgen="male";
if(ui->female->isChecked())
stgen="female";
stbday=ui->birthday->text();
stcourses=ui->courses->text();
QSqlQuery qry;
qry.prepare("INSERT INTO Student (Student ID,Name,Surname,Age,Gender,Birthday,Courses) VALUES "
"(:stid,:stname,:stsur,:stage,:stgen,:stbday,:stcourses)");
qry.bindValue(":stid", stid);
qry.bindValue(":stname",stname);
qry.bindValue(":stsur",stsur);
qry.bindValue(":stage",stage);
qry.bindValue(":stgen",stgen);
qry.bindValue(":stbday",stbday);
qry.bindValue(":stcourses",stcourses);
qry.exec();
}I keep getting QSqlError("", "Parameter count mismatch", "").
I tried to find a solution but had no luck. I would like some help here@Meskaz285 said in QT6 SQLite and QSqlQuery "Parameter count mismatch" Error:
Student ID
You seem to have a space between Student and ID
-
@Meskaz285 said in QT6 SQLite and QSqlQuery "Parameter count mismatch" Error:
Student ID
You seem to have a space between Student and ID
-
wrote on 30 Jun 2022, 15:31 last edited by
@Meskaz285 You may need to wrap your names with a ` tick. I personally have had a lot of problems with bindValue so I switched to building the string to be executed.
e.g.:
QString sqlString = QString("INSERT INTO Student(`Student ID`, `Name`, `Surname`, `Age`, `Gender`, `Birthday`, `Courses`) VALUES ('%1', '%2', '%3', '%4', '%5', '%6', '%7')").arg(stid).arg(stname).arg(stsur).arg(stage).arg(stgen).arg(stbday).arg(stcourses); success = false; errorCount = 0; // success is a bool and errorCount is a int. while((success = sql.exec(sqlString)) == false && (errorCount++ < 10)) { QTime dieTime = QTime::currentTime().addSecs(1); while(QTime::currentTime() < dieTime) QCoreApplication::processEvents(QEventLoop::AllEvents, 100); } if((errorCount > 0) && (success == false)) { // Handle your failure here } else { // handle your success here }
I have found this method to work best for me. I always had issues with bindValue and the second part of the code you may not need if you use a local sqlServer. I was having issues when using a remote server with sql.exec failing if the network conditions was not perfect.
Hope that helps...
-
I wrote this function to add data to a database:
void MainWindow::save1() {
QString stid,stname,stsur,stage,stgen,stbday,stcourses;
stid=ui->Stid->text();
stname=ui->name->text();
stsur=ui->surname->text();
stage=ui->age->text();
if(ui->male->isChecked())
stgen="male";
if(ui->female->isChecked())
stgen="female";
stbday=ui->birthday->text();
stcourses=ui->courses->text();
QSqlQuery qry;
qry.prepare("INSERT INTO Student (Student ID,Name,Surname,Age,Gender,Birthday,Courses) VALUES "
"(:stid,:stname,:stsur,:stage,:stgen,:stbday,:stcourses)");
qry.bindValue(":stid", stid);
qry.bindValue(":stname",stname);
qry.bindValue(":stsur",stsur);
qry.bindValue(":stage",stage);
qry.bindValue(":stgen",stgen);
qry.bindValue(":stbday",stbday);
qry.bindValue(":stcourses",stcourses);
qry.exec();
}I keep getting QSqlError("", "Parameter count mismatch", "").
I tried to find a solution but had no luck. I would like some help herewrote on 30 Jun 2022, 15:49 last edited by JonB@Meskaz285
Before you decide to follow @Chrisw01's route and abandon bindings in favour of literal inlines (which has its own issues), from where you are now you have only to try:qry.prepare("INSERT INTO Student (`Student ID`,Name,Surname,Age,Gender,Birthday,Courses)
and the rest the same. You will always need to quote a column name which you have chosen to have a space in whenever you use it in SQL statements, e.g. when you get to your
SELECT
statements. And how you quote varies across SQL implementations. Up to you, but I wouldn't dream of choosing to have a space in a column name for the hassle :) Some people use underscore characters for that. -
@Meskaz285
Before you decide to follow @Chrisw01's route and abandon bindings in favour of literal inlines (which has its own issues), from where you are now you have only to try:qry.prepare("INSERT INTO Student (`Student ID`,Name,Surname,Age,Gender,Birthday,Courses)
and the rest the same. You will always need to quote a column name which you have chosen to have a space in whenever you use it in SQL statements, e.g. when you get to your
SELECT
statements. And how you quote varies across SQL implementations. Up to you, but I wouldn't dream of choosing to have a space in a column name for the hassle :) Some people use underscore characters for that.
1/6