QUERY NOT WORKING
-
Hello people,I have a problem which needs your help.I have a query which should insert data in a 20 colunm table but instead is giving me parameter mismatch error. I have gone through the code i cannot find the error. My app is well connected to the database .I have tried to insert using the sqlite studio command its working. What can be the problem? Below is my code.
QSqlQuery qry; qry.prepare("INSERT INTO STAFF(SURNAME, LASTNAME, GENDER , BIRTH_D, M_STATUS, NRC_NO," " EMPLOYEE_NO, TS_NO, DATE_FIRST_APP, DATE_PRESENT_APP, POSITION , SUBJECT_ONE," " SUBJECT_TWO, DEPARTMENT_ONE, DEPARTMENT_TWO, QUALIFICATIONS , SALARY, CONFIRMED," " PHONE, E_MAIL)" "VALUES( :SURNAME, :LASTNAME, :GENDER, :BIRTH_D, :M_STATUS, :NRC_NO, :EMPLOYEE_NO, :TS_NO," " :DATE_FIRST_APP, :DATE_PRESENT_APP, :POSITION, :SUBJECT_ONE, :SUBJECT_TWO, :DEPARTMENT_ONE," " :DEPARTMENT_TWO, :QUALIFICATIONS, :SALARY, :CONFIRMED, :PHONE, :E_MAIL)"); qry.bindValue(":SURNAME",surname); qry.bindValue(":LASTNAME",lastname); qry.bindValue(":GENDER",sex); qry.bindValue(":BIRTH_D",dateofbirth); qry.bindValue(":M_STATUS",STATUS); qry.bindValue(":NRC_NO",NRC); qry.bindValue(":EMPLOYEE_NO",EMPLOYEE_NO); qry.bindValue(":TS_NO",TS_NO); qry.bindValue(":DATE_FIRST_APP",DATE_FIRST); qry.bindValue(":DATE_PRESENT_APP",DATE_SECOND); qry.bindValue(":POSITION",POSITION); qry.bindValue(":SUBJECT_ONE",SUB1); qry.bindValue(":SUBJECT_TWO",SUB2); qry.bindValue(":DEPARTMENT_ONE",DPT1); qry.bindValue(":DEPARTMENT_TWO",DPT2); qry.bindValue(":QUALIFICATIONS",QUALIFICATION); qry.bindValue(":SALARY",SALARY); qry.bindValue(":CONFIRMED",CONFIRMED); qry.bindValue(":PHONE",Phone); qry.bindValue(":E_MAIL",E_MAIL); qry.exec(); qDebug()<<qry.lastError();
These are error messages
QSqlError("", "Parameter count mismatch", "")
QSqlError("", "Parameter count mismatch", "")
QSqlError("", "Parameter count mismatch", "")Here is the table
-
Hi,
There might be some space missing in your query string.
You should also surround your query.exec call with an if or you will always print something.
You should also print the output of lastQuery.
On a side note, please don't write title in all caps. In written language it's considered shouting and rude.
-
The way you built your query string, "VALUES" is likely directly after the ")".
If (!query.exec()) { // put here the code that should run in case of failure }
-
Check the output of QSqlQuery::boundValues() (after the binding calls), QSqlQuery::lastQuery() and/or QSqlQuery::executedQuery() to see if anything looks different from what you expect.
-
@GREYON
@SGaist suggested you put a space into your)VALUES
, did you at least try that?Otherwise: reduce your statement down till it works. Try it with only the
SURNAME
/:SURNAME
column in it. Yes, theINSERT
will error before inserting because of a lack of columns, but you are only interested in trying to get as far as that, if you do not get theParameter count mismatch
error you have succeeded. Then add the other columns back in. -
So what was the actual error in your code above then?
-
@Christian-Ehrlicher it's like one of the column name was not correctly spelled though I couldn't pin point it exactly cause I was just going through them one by one until all of them worked.