Results 1 to 5 of 5

Thread: qsqlquery insert error

  1. #1
    Join Date
    Feb 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default qsqlquery insert error

    Hello all,

    I am new in using qt and i would like some help. Currently i am trying to insert data to an sqlite db and i I get error, any ideas into what I might be doing wrong? The strings come from a Form's LineEdit fields. Please see below the code:

    Qt Code:
    1. void Register::Register1()
    2. {
    3. QSqlQuery query;
    4. query.prepare("INSERT INTO User (pk, name, psw) "
    5. "VALUES ( :name, :psw)");
    6.  
    7. query.bindValue( ":name", ui.RUserNameLineEdit->text() );
    8. query.bindValue( ":psw",ui.RPassword->text() );
    9.  
    10.  
    11. if( !query.exec() )
    12.  
    13. qDebug() << "> Query exec() error." << query.lastError().type();
    14.  
    15. else
    16.  
    17. qDebug() << ">Query exec() success.";
    To copy to clipboard, switch view to plain text mode 

    Thank you !

  2. #2
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: qsqlquery insert error

    You should bind pk value

    If pk is autoincrement key, use
    Qt Code:
    1. INSERT INTO User (pk, name, psw) VALUES (NULL, :name, :psw)
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: qsqlquery insert error

    Hello unit,

    Thank you for your response. Actually i removed totally the primary key but i still get the "Query exec () error. 2".

    Do you have any idea? Shall i send you more info(code)?


    Added after 22 minutes:


    Hello unit,

    Thank you for your response. Actually i removed totally the primary key but i still get the "Query exec () error. 2".

    Do you have any idea? Shall i send you more info(code)?
    Last edited by fantom; 23rd February 2011 at 15:48.

  4. #4
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: qsqlquery insert error

    2 is SQL statement syntax error (from enum QSqlError::ErrorType).

    Try:

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("INSERT INTO User (pk, name, psw) "
    3. "VALUES (NULL, :name, :psw)");
    4. query.bindValue( ":name", ui.RUserNameLineEdit->text() );
    5. query.bindValue( ":psw",ui.RPassword->text() );
    To copy to clipboard, switch view to plain text mode 

    and also you should use QSqlDatabase for QSqlQuery object


    Qt Code:
    1. db = QSqlDatabase::addDatabase("QMYSQL", "_default_");
    2. db.setHostName("127.0.0.1");
    3. db.setDatabaseName("testpay");
    4. db.setUserName("mypay");
    5. db.setPassword("test123");
    6. if(db.open())
    7. {
    8. QSqlQuery query(db);
    9. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: qsqlquery insert error

    Thank you again for the response but still i am getting the error. Please find below the main.cpp and the register.cpp file.

    main.cpp

    Qt Code:
    1. #include "disp.h"
    2. #include "login.h"
    3. #include <QtGui/QApplication>
    4. #include <QSqlDatabase>
    5. #include <QSqlDriver>
    6.  
    7. void initDB()
    8. {
    9. }
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QApplication a(argc, argv);
    14.  
    15. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    16. db.setDatabaseName("d:/database/disp");
    17. bool ok = db.open();
    18.  
    19. Login w;
    20. w.show();
    21. return a.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    and register.cpp

    Qt Code:
    1. #include <QtGui/QWidget>
    2. #include <QSqlQuery>
    3. #include <QString>
    4. #include <QDebug>
    5. #include <QSqlError>
    6. #include "register.h"
    7.  
    8. Register::Register(QWidget *parent)
    9. : QDialog(parent)
    10. {
    11. ui.setupUi(this);
    12. connect(ui.RegisterOK, SIGNAL( clicked() ), this, SLOT( Register1() ) ) ;
    13. }
    14.  
    15. Register::~Register()
    16. {
    17.  
    18. }
    19.  
    20.  
    21. void Register::Register1()
    22. {
    23. QSqlQuery query;
    24. query.prepare("INSERT INTO User (name, psw, cpsw, dID) "
    25. "VALUES ( :name, :psw, :cpsw, :dID)");
    26.  
    27. query.bindValue( ":name", ui.RUserNameLineEdit->text() );
    28. query.bindValue( ":psw",ui.RPassword->text() );
    29. query.bindValue( ":cpsw",ui.ConfirmPassword->text() );
    30. query.bindValue( ":dID",ui.RPatientLineEdit->text() );
    31.  
    32.  
    33. if( !query.exec() )
    34.  
    35. qDebug() << "> Query exec() error." << query.lastError().type();
    36.  
    37. else
    38.  
    39. qDebug() << ">Query exec() success.";
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 

    I really appreciate your patience. I am new in Qt and i am having some troubles


    Added after 15 minutes:


    Thanks Unit i just found the error the query needs to be :

    Qt Code:
    1. SqlQuery query;
    2. query.prepare("INSERT INTO User VALUES ( :name, :psw, :cpsw, :dID)");
    To copy to clipboard, switch view to plain text mode 
    Last edited by fantom; 23rd February 2011 at 17:15.

Similar Threads

  1. error with QSqlQuery
    By mmm286 in forum Newbie
    Replies: 5
    Last Post: 26th May 2010, 00:27
  2. QSqlQuery.exec() weird error
    By MarkoSan in forum Qt Programming
    Replies: 3
    Last Post: 25th May 2010, 13:02
  3. Replies: 1
    Last Post: 7th April 2010, 21:46
  4. Replies: 3
    Last Post: 25th August 2009, 13:03
  5. QSqlQuery error
    By skuda in forum Qt Programming
    Replies: 2
    Last Post: 2nd November 2007, 08:43

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.