PDA

View Full Version : INSERT query with MySQL problem



timmyg
19th March 2008, 19:31
hey everybody
im a bit of a newb to Qt so keep that in mind
I'm trying to make an application that sends data to a MySQL database
i can successfully send data using the INSERT INTO statement, but it does not compile when i try to use variables for the data values
the variables i am using are strings and the fields i am trying to insert them into are all varchar(20)
here is my code (which i found by searching this forum):


QSqlQuery query;
query.exec("INSERT INTO TradeData (symbol,price,size,time) "
"VALUES ( '"+symbol+"' , '"+price+"' , '"+size+"' , '"+timestamp+"' )");

attached is a screencap of my compile error

thanks

jacek
19th March 2008, 19:41
You pass std::string to QSqlQuery::exec(), but it expects a QString.


P.S. Don't construct your queries like that, use QSqlQuery::bindValue().

timmyg
20th March 2008, 14:18
You pass std::string to QSqlQuery::exec(), but it expects a QString.

P.S. Don't construct your queries like that, use QSqlQuery::bindValue().

ok
how do i do the bindvalue with variables?

Raccoon29
20th March 2008, 14:38
QSqlQuery query;
query.prepare("INSERT INTO TradeData (symbol,price,size,time) VALUES ( :symbol, :price, :size, :timestamp)");
query.bindValue(":symbol",symbol);
query.bindValue(":price",price);
query.bindValue(":size",size);
query.bindValue(":timestamp",timestamp);
query.exec();


It replaces corresponding strings with the value you want (formatting it accordingly to the database formats)

Greetings,
Raccoon29

timmyg
20th March 2008, 14:59
thats exactly what i tried and i got compile error


error: no matching function for call to 'QSqlQuery::bindValue(const char[8], std::string&)'

is this also because im using strings instead of Qstrings?
how do i convert a string to a Qstring?

thanks

jpn
20th March 2008, 16:15
how do i convert a string to a Qstring?

You might want to take a look at QString docs. Hint: search for "std::string" or "StdString".

Raccoon29
20th March 2008, 16:16
Yes, the problem is exactly that std::string.
I never tried with std::string, but usually it is very easy to convert to QString:

QString var=other_string;
so try putting your std::string in QString variables and try putting those in the bindValue, then let us know how it goes.

timmyg
20th March 2008, 16:45
Yes, the problem is exactly that std::string.
I never tried with std::string, but usually it is very easy to convert to QString:

QString var=other_string;
so try putting your std::string in QString variables and try putting those in the bindValue, then let us know how it goes.

i get compile error:


error: conversion from 'std::string' to non-scalar type 'QString' requested


im going to look into the QString documentation like jpn suggested and see if i can find anything

timmyg
20th March 2008, 17:13
i got it to work by doing this

QString var = stdstringvar.c_str();

thanks for the help
:D

Raccoon29
20th March 2008, 17:47
IMHO the Qt one is a great documentation, most of answers are in there; every documentation should learn by this one.

Glad you found your solution :)
Greetings

The Storm
20th March 2008, 21:52
This is the way to convert it correct:
QString::fromStdString(your_std_string);