Results 1 to 2 of 2

Thread: QT SQLITE Query

  1. #1
    Join Date
    May 2015
    Qt products

    Default QT SQLITE Query

    I am quite confused what is going on here and I have to be completely missing something stupid, but...

    Qt Code:
    2. QSqlQuery query;
    3. query.prepare("select * from inventory");
    4. query.exec();
    To copy to clipboard, switch view to plain text mode 

    A simple query to my database that works in numerous other places, just does not function at all in one of my pushbutton slots.

    Qt Code:
    1. 0x14bfed30 //Query Result
    2. " " //Query Error
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. val = ui->listView->model()->data(index).toString();
    3. QSqlQuery query;
    4. query.prepare("select * from inventory where item ='"+val+"'");
    6. if(query.exec())
    7. {
    8. while(
    9. {
    10. ui->lineEdit_shelf->setText(query.value(1).toString());
    11. ui->lineEdit_container->setText(query.value(2).toString());
    12. ui->lineEdit_finished->setText(query.value(4).toString());
    13. ui->lineEdit_unfinished->setText(query.value(5).toString());
    14. ui->textEdit_notes->setText(query.value(6).toString());
    15. if(query.value(3) == 1)
    16. ui->checkBox->setChecked(true);
    17. else
    18. ui->checkBox->setChecked(false);
    19. }
    20. mydb.close();
    21. }
    22. else
    23. qDebug() << query.lastError().text();
    25. }
    To copy to clipboard, switch view to plain text mode 

    Example of setup in a listView_clicked slot. But it functions perfectly well. Any kind of guidance would be greatly appreciated, as this problem popped up while I was attempting to make an update query, and it seems to be a much bigger issue.
    Last edited by andrewhopps; 15th May 2015 at 00:30.

  2. #2
    Join Date
    Dec 2009
    New Orleans, Louisiana
    Thanked 153 Times in 150 Posts
    Qt products
    MacOS X

    Default Re: QT SQLITE Query

    A couple of general comments about your code shown above:

    • You don't show your call to QSqlDatabase::addDatabase
    • You don't verify that your database has even opened successfully. Obviously if that fails, nothing else can be expected to work.
    • You should use the QSqlQuery constructor that uses your QSqlDatabase object
    • You should not build your SQL statements using values obtained by the user. It leaves you susceptible to SQL injections. Use instead positional or named parameters for placeholders in your SQL statements, then QSqlQuery::prepare your SQL statement, then finally use QSqlQuery::bindValue to bind the values to their positional or named parameters before you QSqlQuery::exec the query.

    Make those changes and if you still have a problem, re-post please.

Similar Threads

  1. QSqlQuery update query not working using SQLite
    By Cyrebo in forum Qt Programming
    Replies: 2
    Last Post: 29th March 2013, 01:33
  2. how to send sqlite query CREATE TRIGGER in qt application
    By abdul_moiz in forum Qt Programming
    Replies: 2
    Last Post: 10th June 2011, 07:07
  3. SQLITE - UPDATE query problem
    By Tomasz in forum Newbie
    Replies: 12
    Last Post: 5th September 2010, 21:27
  4. Problem with A SQLite Query
    By maveric in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2008, 12:15
  5. [QT4][SQLITE] Database and query
    By agent007se in forum Newbie
    Replies: 10
    Last Post: 12th July 2006, 23:16


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.