Results 1 to 2 of 2

Thread: SOLVED : SqlQuery can't insert data.

  1. #1
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default SOLVED : SqlQuery can't insert data.

    Hello, I am trying to insert a new record into my db from lineedits. Everything works fine. But if there is some word in Double quotes (" ") the query fails with Sql syntax error. How can i insert data that contains double quotes?
    Qt Code:
    1. void MainWindow::insertRow()
    2. {
    3. QString sqlstr=QString("INSERT INTO sianimtable (CIPHER,CIPHERKP,TITLE,MSGON,MSGOFF) VALUES(\"%1\",\"%2\",\"%3\",\"%4\",\"%5\");")
    4. .arg(le2->text())
    5. .arg(le1->text())
    6. .arg(le3->text())
    7. .arg(le4->text())
    8. .arg(le5->text());
    9.  
    10. QSqlQuery *q = new QSqlQuery();
    11. if(q->exec(sqlstr)==false)
    12. {
    13. QMessageBox *pmsg = new QMessageBox;
    14. pmsg->setText("can't connect with db");
    15. pmsg->setInformativeText(q->lastError().text());
    16. pmsg->exec();
    17. return;
    18. }
    19. delete q;
    20. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by naptizaN; 18th July 2012 at 14:01.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SqlQuery can't insert data.

    Use named placeholders, something like this :
    Qt Code:
    1. QSqlQuery *q = new QSqlQuery();
    2. q->prepare("INSERT INTO sianimtable (CIPHER,CIPHERKP,TITLE,MSGON,MSGOFF) VALUES(:le1,:le2,:le3,:le4,:le5);");
    3. q->bindValue(":le1",le1->text());
    4. q->bindValue(":le2",le2->text());
    5. q->bindValue(":le3",le3->text());
    6. q->bindValue(":le4",le4->text());
    7. q->bindValue(":le5",le5->text());
    8. if(q->exec(sqlstr)==false)
    9. {
    10. QMessageBox *pmsg = new QMessageBox;
    11. pmsg->setText("can't connect with db");
    12. pmsg->setInformativeText(q->lastError().text());
    13. pmsg->exec();
    14. }
    15. delete q;
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lesiok for this useful post:

    naptizaN (18th July 2012)

Similar Threads

  1. Replies: 7
    Last Post: 2nd April 2012, 23:35
  2. Insert data in a text file
    By korwin in forum Newbie
    Replies: 7
    Last Post: 15th July 2011, 02:48
  3. Insert data to QTableView
    By Lodhart in forum Qt Programming
    Replies: 1
    Last Post: 23rd April 2009, 09:38
  4. insert additional data into qdirmodel
    By killerwookie99 in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2008, 15:09
  5. QAbstractTableModel data insert issues
    By nategoofs in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 09:16

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.