0

I seek help for this issue I couldn't find an answer anywhere. I have a program that loads multiple string variables and then I am trying to paste them into a sqlite3 database, everything seems good, I have a conection, but this piece of code gives me an error : expected ';' before string constant (it asks for it before the farba variable. Any ideas?

QSqlQuery query;
query.exec("INSERT INTO spz VALUES") "(Meno,Priezvisko,Datum,Bydlisko,COP,Znacka,Model,Farba,Objem,Rok,SPZ) VALUES('"  meno  "','"  priezvisko  "','"  vek  "','"  bydlisko  "','"  cop  "','"  vyrobca  "','"  model  "','"  farba  "','"  objem  "','"  rok  "','"  znacka"');");
4
  • Does that string look right to you? Commented May 23, 2015 at 18:05
  • I now see that I've messed it up but even the below mentioned code does not work for me. Commented May 23, 2015 at 18:09
  • What I sometimes do is create the string in a variable before calling the query.exec() function. That way you can print it out to see if it is building correctly. Commented May 23, 2015 at 18:13
  • Some of your variables that you are putting into database fields may contain characters that can upset the SQL string. For example if they contain quotes. So you may need to escape some of your fields. Printing the SQL out should highlight such problems. Commented May 23, 2015 at 18:15

1 Answer 1

1

Try this

QSqlQuery query;
query.exec("INSERT INTO spz(Meno,Priezvisko,Datum,Bydlisko,COP,Znacka,Model,Farba,Objem,Rok,SPZ) VALUES('" + meno + "','" + priezvisko + "','"  + vek + "','" + bydlisko + "','" + cop + "','" + vyrobca + "','" + model + "','" + farba + "','" + objem + "','" + rok  "','" + znacka +"');");

Other Method:

QSqlQuery query;
query.prepare("INSERT INTO spz(Meno,Priezvisko,Datum,Bydlisko,COP,Znacka,Model,Farba,Objem,Rok,SPZ) VALUES (:meno, :priezvisko, :vek, :bydlisko, :cop, :vyrobca, :model, :farba, :objem, :rok, :znacka)");
query.bindValue(":meno", meno);
query.bindValue(":vek", vek);
query.bindValue(":bydlisko", bydlisko);
query.bindValue(":cop", cop);
query.bindValue(":vyrobca", vyrobca);
query.bindValue(":model", model);
query.bindValue(":farba", farba);
query.bindValue(":objem", objem);
query.bindValue(":rok", rok);
query.bindValue(":znacka", znacka);
query.exec();
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, but this one gave back Incopatible types string and integer because of the + char.
I don't know the datatypes of meno, priezvisko, vek, ... variables. You need to make the proper casting for each datatype.
also the datatypes are all string. If you dont mind me asking, what do you mean by proper casting?
I thought that the parameters are not of string data type. I added another method that will let you avoid string concatenation. both should work for you. Please mention the error message you got if you still have issue with my code.
std::string to query.bindValue(), but it expects a QString. you can try converting each variable before passing for example: QString vv = meno.c_str();query.bindValue(":meno", vv);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.