QSql bindValue issue



  • Hi,

    My name is Kaan and I'm student. I writing a program and i was see an error. While i look generated SQL, i see blank SQL values. Please help me. (Sorry for my bad English.)

    My code is here:

    #include <QCoreApplication>
    #include <QDebug>
    #include <QSql>
    #include <QSqlQuery>
    #include <QSqlDatabase>
    #include <QSqlError>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
        database.setDatabaseName("C:/Veritabani.db");
        database.open();
        qDebug() << database.lastError();
        QSqlQuery query;
        query.prepare("INSERT INTO Users(username, e_mail, password) VALUES(:username, :e_mail, :password)");
        query.bindValue(":username", "Kaan");
        query.bindValue(":e_mail", "kaansef@gmail.com");
        query.bindValue(":password", "123456");
        query.exec();
        qDebug() << query.executedQuery();
        return a.exec();
    }
    
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should check that your query executed successfully as well as the error message that it produced otherwise.

    One of the first thing that comes to mind is: Do you have that table in your database ?



  • Thank you very much but i'm sure my database table is true. How to see my query's prepared state?



  • @kaansef
    Whats the exact error you get here?
    Maybe you could add some more debug outputs there.
    What does query.lastError() return?



  • Thanks for replies. query.lastError() returns "Parameter count mismatch".



  • when query exec runs it prints some error informations to cerr, could you check your console for the answer?

    Also, are username, e_mail, password the only columns in that table?



  • @kaansef
    To add to @VRonin: can you maybe show the output of .schema TABLENAME (for SQLite) (or describe TABLENAME if its MySQL) ? This may help to solve your problem



  • I'm beginner Qt. So i understand you completely. My another project is QtWidgetApplication. My active project is QtWidgetApplication. Consoleapplication for you are understand more. How to cerr to cout redirect? Please help me.



  • Depends on your debugger, in Visual studio and qt creator it's the debug output window (normally at the bottom of the editor)


  • Lifetime Qt Champion

    How did you check that you are currently have that table in your database ? How did you add it to your database ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.