Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSqlite not able to retrieve data



  • 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


  • Lifetime Qt Champion

    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.


Log in to reply