Results 1 to 4 of 4

Thread: QT SqlLite SELECT FROM does not update table

  1. #1
    Join Date
    Mar 2017
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default QT SqlLite SELECT FROM does not update table

    Hello there.

    I am building a small application just to learn how qt manages databases. Untill now I managed to display the db, insert new rows in it. Now I want to perform a query to select from that database and display the data in tableview. But after that, the table remains unchanged. Why is that?

    The select query is at void MainWindow:n_pushButton_3_clicked().

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QFileDialog>
    4. #include <QMessageBox>
    5. #include <QDesktopServices>
    6. #include <QUrl>
    7. #include <QProcess>
    8. #include <QSqlDatabase>
    9. #include <QtSql>
    10. #include <QDebug>
    11. #include <QFileInfo>
    12. #include <QtWidgets>
    13. #include <QLineEdit>
    14. #include "addrecipe.h"
    15.  
    16.  
    17. MainWindow::MainWindow(QWidget *parent) :
    18. QMainWindow(parent),
    19. ui(new Ui::MainWindow)
    20. {
    21. ui->setupUi(this);
    22.  
    23.  
    24. //setup database driver in main window
    25. QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
    26.  
    27.  
    28. }
    29.  
    30. MainWindow::~MainWindow()
    31. {
    32. delete ui;
    33. }
    34.  
    35. void MainWindow::on_pushButton_clicked()
    36. {
    37.  
    38. //create database connection and show its contents
    39. QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
    40. mydb.setDatabaseName("C:\\sqlite-tools-win32-x86-3170000\\recipedb.db");
    41. mydb.open();
    42. if(mydb.open()){
    43.  
    44. ui->label->setText("Database Connected");
    45.  
    46.  
    47. //setup table view
    48.  
    49. model->setQuery("SELECT empid, name, title FROM employee");
    50. model->setHeaderData(0, Qt::Horizontal, QObject::tr("empid"));
    51. model->setHeaderData(1, Qt::Horizontal, QObject::tr("name"));
    52. model->setHeaderData(2, Qt::Horizontal, QObject::tr("title"));
    53.  
    54. ui->tableView->setModel(model);
    55. ui->tableView->setColumnWidth(0,100);
    56. ui->tableView->setColumnWidth(1,100);
    57. ui->tableView->setColumnWidth(2,100);
    58. ui->tableView->horizontalHeader()->setStretchLastSection(true);
    59. ui->tableView->show();
    60.  
    61.  
    62.  
    63. }
    64.  
    65. }
    66.  
    67.  
    68.  
    69. //creates new database with sqlite and bath file
    70. void MainWindow::on_actionCreate_New_triggered()
    71. {
    72. QFileInfo cmdFile( "C:\\Windows\\system32\\cmd.exe");
    73. QProcess *process = new QProcess( this );
    74. process->execute(cmdFile.absoluteFilePath() + " /c C:\\sqlite-tools-win32-x86-3170000\\recipe.bat");
    75. process->waitForFinished() ;
    76. process->kill();
    77. }
    78.  
    79.  
    80.  
    81. //closing database connection
    82. void MainWindow::on_pushButton_2_clicked()
    83. {
    84. QSqlDatabase mydb = QSqlDatabase::database("QSQLITE");
    85.  
    86.  
    87. if (mydb.isOpen())
    88. mydb.close();
    89. QSqlDatabase::removeDatabase("C:\\sqlite-tools-win32-x86-3170000\\recipedb.db");
    90.  
    91. if(!mydb.open()){
    92.  
    93. ui->label->setText("Database NOT Connected");}
    94.  
    95. }
    96.  
    97.  
    98. //insert into database from input fields
    99. void MainWindow::on_pushButton_4_clicked()
    100. {
    101. QString empid, name, title;
    102. empid = ui->lineEdit->text();
    103. name = ui->lineEdit_2->text();
    104. title = ui->lineEdit_3->text();
    105.  
    106. QSqlQuery query;
    107. query.prepare("INSERT INTO employee (empid,name,title) VALUES ( :empid, :name, :title)");
    108. query.bindValue(":empid",empid);
    109. query.bindValue(":name",name);
    110. query.bindValue(":title",title);
    111. query.exec();
    112. }
    113.  
    114.  
    115.  
    116. void MainWindow::on_lineEdit_cursorPositionChanged(int arg1, int arg2)
    117. {
    118.  
    119. }
    120.  
    121.  
    122. //new recipe window
    123. void MainWindow::on_actionAdd_New_triggered()
    124. {
    125. AddRecipe mDialogAddRecipe;
    126. mDialogAddRecipe.setModal(true);
    127. mDialogAddRecipe.exec();
    128.  
    129. }
    130.  
    131.  
    132. //try to get select query
    133. void MainWindow::on_pushButton_3_clicked()
    134. {
    135.  
    136.  
    137. QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
    138. mydb.setDatabaseName("C:\\sqlite-tools-win32-x86-3170000\\recipedb.db");
    139. mydb.open();
    140.  
    141. QString name;
    142. name = ui->lineEdit->text();
    143. // name = ui->lineEdit_2->text();
    144. // title = ui->lineEdit_3->text();
    145.  
    146. QSqlQuery query;
    147. query.prepare("SELECT * FROM employee WHERE name = (:name)");
    148. query.bindValue(":name",name);
    149. // query.bindValue(":name",name);
    150. // query.bindValue(":title",title);
    151. query.exec();
    152. model->setHeaderData(0, Qt::Horizontal, QObject::tr("empid"));
    153. model->setHeaderData(1, Qt::Horizontal, QObject::tr("name"));
    154. model->setHeaderData(2, Qt::Horizontal, QObject::tr("title"));
    155. ui->tableView->horizontalHeader()->setStretchLastSection(true);
    156. ui->tableView->show();
    157.  
    158.  
    159. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT SqlLite SELECT FROM does not update table

    How do you imagine would the model magically know about the query object?
    How do you imagine would the view magically know about the model object?

    Cheers,
    _

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

    editheraven (4th April 2017)

  4. #3
    Join Date
    Mar 2017
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QT SqlLite SELECT FROM does not update table

    I have managed to get it working with

    Qt Code:
    1. model->setQuery("SELECT empid, name, title FROM employee where empid = 1");
    2. ...
    3. ui->tableView->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    But that is plain sql query.
    I can't pass values into that. Can I connect the model to query.prepare instead?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT SqlLite SELECT FROM does not update table

    Quote Originally Posted by editheraven View Post
    Can I connect the model to query.prepare instead?
    Yes, pass the query object to the model instead of the query string.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    editheraven (4th April 2017)

Similar Threads

  1. Replies: 2
    Last Post: 26th February 2014, 19:54
  2. how to select a particular indexed table row
    By gauravg in forum Qt Programming
    Replies: 1
    Last Post: 19th December 2011, 09:32
  3. Sql select table query error
    By nagabathula in forum Qt Programming
    Replies: 2
    Last Post: 4th July 2011, 11:12
  4. Replies: 1
    Last Post: 29th April 2011, 23:50
  5. select more than one row in table widget using pyqt
    By parkourpenner in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2009, 18:20

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.