PDA

View Full Version : n00b mysql and c++/qt question



stingray
15th September 2013, 13:03
if i do



QSqlQuery get_company_id;

QString stmt_get_company_id = "Select id from company where (name = ?) ;";

get_company_id.prepare(stmt_get_company_id);
get_company_id.bindValue(0, details[0]);
int int_company_id = get_company_id.exec();

qDebug() << " int_company_id = " + int_company_id;



atleast in my head i think it should pass the int value of the selected id to the int but it sure dont seem to do that, the qDebug() after says the int_company_id is null..

im pretty sure i've missed something basic on this one.. just cant get it do to what i want, as i have to insert in multiple instances because of foreign keys in the db, so i need this int, but dont know how to do it..

/Stingray

stingray
15th September 2013, 15:59
tried another version i used with searching and strings...



int int_contact_id ;

if (get_contact_id.exec())
{
if (get_contact_id.next())
{
int_contact_id = get_contact_id.value(0).toInt() ;
}
}

qDebug() << " get_int_contact_id: " + int_contact_id;


that one doesnt ether return a value to the int variable..
the only row that shows what it should is the middle one... the two other with the missing integers also missing chars in front of whats there..

and funny enough seems qDebug has gotten a real cold.. heres the debug output..


Starting /home/stingray/Qt/projects/build-MediaDB-Desktop_Qt_5_1_0_GCC_32bit-Debug/MediaDB...
ompany_id =
"fname: xxxx, lname: xxxxx, email: xxxxx@xxxxx.com, phone: 20001221"
act_id:

wysota
16th September 2013, 17:54
Could you post a minimal compilable example reproducing the problem?

ChrisW67
16th September 2013, 21:28
Your first effort does not work because QSqlQuery returns a bool indicating the success, not the results of the query. Then when you go to output the "result" you add an integer value to a const char* (i.e. your string literal), which adjusts the value of the pointer and has the effect of "removing" characters from the front of the string not magically converting the integer to a string and appending it.

Your second attempt will only set the int variable if the query is both successful and it returns a row: one does not necessarily follow from the other.

Try looking at QSqlQuery::lastError() if (when) exec() and next() fail.