1 Attachment(s)
INSERT query with MySQL problem
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):
Code:
query.exec("INSERT INTO TradeData (symbol,price,size,time) "
"VALUES ( '"+symbol+"' , '"+price+"' , '"+size+"' , '"+timestamp+"' )");
attached is a screencap of my compile error
thanks
Re: INSERT query with MySQL problem
You pass std::string to QSqlQuery::exec(), but it expects a QString.
P.S. Don't construct your queries like that, use QSqlQuery::bindValue().
Re: INSERT query with MySQL problem
Quote:
Originally Posted by
jacek
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?
Re: INSERT query with MySQL problem
Code:
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
Re: INSERT query with MySQL problem
thats exactly what i tried and i got compile error
Code:
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
Re: INSERT query with MySQL problem
Quote:
Originally Posted by
timmyg
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".
Re: INSERT query with MySQL problem
Yes, the problem is exactly that std::string.
I never tried with std::string, but usually it is very easy to convert to QString:
so try putting your std::string in QString variables and try putting those in the bindValue, then let us know how it goes.
Re: INSERT query with MySQL problem
Quote:
Originally Posted by
Raccoon29
Yes, the problem is exactly that std::string.
I never tried with std::string, but usually it is very easy to convert to QString:
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:
Code:
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
Re: INSERT query with MySQL problem
i got it to work by doing this
Code:
QString var
= stdstringvar.
c_str();
thanks for the help
:D
Re: INSERT query with MySQL problem
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
Re: INSERT query with MySQL problem
This is the way to convert it correct:
QString::fromStdString(your_std_string);