PDA

View Full Version : Correct string syntax



poporacer
29th October 2010, 06:51
I am trying to use a SQLite UPDATE query and not sure of the proper syntax. I have tried a few differnt way but none works quite right. The issue is trying to include the single quote marks for the update query. I have 1 line edit and one combobox widget that contain the data for part of the string. Here is my last try:

Sqlstring=("UPDATE address set LName= '%1' WHERE id= %2")
.arg(ui->txtLName->text()).arg(ui->cmbName->itemData(Index).toString());
I get the error messeage:
request for member 'arg' in (UPDATE rider set LName= \'%1'\ WHERE id= %2) which is of non class type const char [24].

What would be the best method to create the correct string keeping in mind that the ui->txtLName->text() needs to be enclosed ina single quotation mark?

Thanks a bunch!

tbscope
29th October 2010, 06:54
That should be:


QString("%1").arg(something)

poporacer
29th October 2010, 06:57
I don't quite understand.... Sqlstring is declared as aQString. But how do I include the widgets and include one in a single quote?

tbscope
29th October 2010, 07:08
Learn about implicit conversion

Lykurg
29th October 2010, 07:36
...and you might want to escape the string values before using it with a sql statement.

wysota
29th October 2010, 12:27
Is there any specific reason for not using prepared sql statements? Not that it would directly solve the problem here (as it is syntax specific) but would surely be a correct approach to use sql.

poporacer
29th October 2010, 18:56
I appreciate all of your help. I am fairly new to QT and C++ but I am trying to learn. I didn't understand on escaping the string values before using them? I looked up implicit conversion but there were so many types that I wasn't sure what they meant. I think I am using a prepared sql statement. My code is:

bool MainWindow::save()
{
QString Sqlstring;
QSqlQuery query ;
Sqlstring=("UPDATE address set LName= '%1' WHERE id= %2")
.arg(ui->txtLName->text()).arg(ui->cmbName->itemData(Index).toString());

qDebug() << query.lastError();
query.prepare(Sqlstring);
query.exec();

return true;
}
Any links to what you are describing or hints to the correct code would be appreciated!
Thanks for all your input!

Rob

wysota
29th October 2010, 19:02
This is a correctly prepared statement:

QSqlQuery query;
query.prepare("UPDATE address set LName=:name WHERE id=:id");
query.bindValue(":name", ui->txtLName->text());
query.bindValue(":id", ui->cmbName->itemData(Index).toInt());
if(!query.exec()){
// error
}

poporacer
29th October 2010, 20:13
Thanks alot!
Does this also escape the string values as Lykurg suggested? Is this the implicit conversion that tbscope was talking about? I want to research these issues to better understand them and get a better knowelege!

Thanks
Rob

wysota
29th October 2010, 20:22
Does this also escape the string values as Lykurg suggested?
Yes.

Is this the implicit conversion that tbscope was talking about?
No.


I want to research these issues to better understand them and get a better knowelege!
Good luck.

poporacer
29th October 2010, 20:35
So what would be the implicit version? Which way is better?

wysota
29th October 2010, 20:58
The "implicit" thing is about casting between types in C++. It has nothing to do with SQL.

tbscope
30th October 2010, 05:47
So what would be the implicit version?

Learn the difference between:

QString string = "Hello world!";
QString string("Hello world!");
QString string = QString("Hello world!");
QString string2(string);
...

Which are initialisations, which are assignments, which are explicit, which are implicit, which use a copy constructor, ...?

This error tells you everything:

request for member 'arg' in (UPDATE rider set LName= \'%1'\ WHERE id= %2) which is of non class type const char [24].
You are requesting a member of an object which is not even a class.

You did:

const char *string = "UPDATE address set LName= '%1' WHERE id= %2".arg(...).arg(...);
There's no QString there.