Results 1 to 5 of 5

Thread: qsqlquery insert error

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 

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.