Results 1 to 4 of 4

Thread: n00b mysql and c++/qt question

  1. #1
    Join Date
    Aug 2013
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default n00b mysql and c++/qt question

    if i do

    Qt Code:
    1. QSqlQuery get_company_id;
    2.  
    3. QString stmt_get_company_id = "Select id from company where (name = ?) ;";
    4.  
    5. get_company_id.prepare(stmt_get_company_id);
    6. get_company_id.bindValue(0, details[0]);
    7. int int_company_id = get_company_id.exec();
    8.  
    9. qDebug() << " int_company_id = " + int_company_id;
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Aug 2013
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: n00b mysql and c++/qt question

    tried another version i used with searching and strings...

    Qt Code:
    1. int int_contact_id ;
    2.  
    3. if (get_contact_id.exec())
    4. {
    5. if (get_contact_id.next())
    6. {
    7. int_contact_id = get_contact_id.value(0).toInt() ;
    8. }
    9. }
    10.  
    11. qDebug() << " get_int_contact_id: " + int_contact_id;
    To copy to clipboard, switch view to plain text mode 

    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..
    Qt Code:
    1. Starting /home/stingray/Qt/projects/build-MediaDB-Desktop_Qt_5_1_0_GCC_32bit-Debug/MediaDB...
    2. ompany_id =
    3. "fname: xxxx, lname: xxxxx, email: xxxxx@xxxxx.com, phone: 20001221"
    4. act_id:
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: n00b mysql and c++/qt question

    Could you post a minimal compilable example reproducing the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: n00b mysql and c++/qt question

    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.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. MySQL question using QT
    By sujan.dasmahapatra in forum General Programming
    Replies: 5
    Last Post: 21st June 2012, 07:37
  2. Replies: 0
    Last Post: 15th October 2011, 14:25
  3. A different MySql Question (windows performance)
    By LWakeland in forum Qt Programming
    Replies: 0
    Last Post: 6th October 2009, 01:45
  4. Replies: 2
    Last Post: 31st July 2009, 20:30
  5. Mysql query question
    By twells55555 in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 23:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.