Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlite not able to retrieve data
QtWS25 Last Chance

QSqlite not able to retrieve data

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqldatabase
2 Posts 2 Posters 819 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SolaVitae
    wrote on 17 Mar 2018, 04:39 last edited by
    #1

    Im currently trying to pull information from a SQLITE db with the QSqlDatabase driver, and i am unable to get individual selections of data.

    I am trying to select one row from my DB based on an integer identifier, but for some reason when i try to execute the select statement, it never returns any data.

    the statement is formatted with snprintf, and is

    SELECT * FROM 'families' WHERE id='5482555'
    

    and as can be seen in this image
    alt text
    that row with that ID does exist in the database, but for some reason QT Doesnt seem to be able to select it.
    I added a breakpoint with a lastQuery() function to see if the query was correct, and it is, i can even copy and paste the query without any editing into my 3rd party DB Browser and the select statement works perfectly. If i run a

    SELECT * FROM 'families'
    

    it will correctly pull all the information from the database, I have tried just about everything i can think of that could cause this issue to no avail, I've tried using the built in .bindValue function for the query, but i can't seem to get it to actually bind a value, it just leaves the placeholder in the query and does nothing no matter which way i try to do it (named placeholders, ? place holders, using positional binding, as well as named binding) they all just leave the query with the placeholders.

    The code ive been using to try to bind values to the query is

    QSqlQuery q;
    q.prepare("SELECT * FROM families WHERE id=?");
    q.bindValue(0, uuidTemp);
    

    Which results in my query being set to SELECT * FROM families WHERE id=?
    uuidTemp is a QString with a numerical value.

    The code that ive been primarily using to format the query is

    char query[256];
    snprintf(query, sizeof(query), "SELECT * FROM 'families' WHERE id='%ld'", uuid);
    q.prepare(query);
    

    Which results in my query being set to SELECT * FROM 'families' WHERE id='5482555'
    Which despite me being able to copy and paste straight from visual studio into my DB browser program and functioning properly, will not work inside QT.

    The entire code for this is

    char query[256];
    snprintf(query, sizeof(query), "SELECT * FROM 'families' WHERE id='%ld'", uuid);
    
    QSqlQuery q;
    q.prepare(query);
    
    if (q.exec())
    {
    	QString temp = q.lastQuery();
    	bool active = q.isActive();
    	if (q.next()) {
    		QMap<QString, int> mmap = client::stringToFamilyMemberMap(q.value(2).toString());
    	}
    }
    

    There is a breakpoint set on the QString temp line and the QMap line, the QMap line is never reached, ive also confirmed that the connection to the database is valid and functioning, as well as tried the exact same equivalent in python just to make sure, and it works perfectly in python

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Mar 2018, 22:06 last edited by
      #2

      Hi,

      What version of Qt are you using ?

      You should print the error you get if exec fails. That will give you clues as to what is happening.

      On a side note, QString already provides easy to use arguments handling so you don't have to use snprintf.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2

      2/2

      17 Mar 2018, 22:06

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved