Results 1 to 5 of 5

Thread: Database updating problem

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Database updating problem

    Hi there everyone. I'm writing an app that connects to an SQLite database. The connection to the database is fine but the problem come when I try updating the same database from a different class. The database does not update. Can somebody please tell me how one can reference one database from different classes in the program. Do I need to open the database every time I want to reference the database. here is my code.
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. this->setWindowTitle("Study Planner");
    10. JobsAtHandModel = new QStringListModel(this);
    11.  
    12. dataBase = QSqlDatabase::addDatabase("QSQLITE");
    13. dataBase.setDatabaseName("C:/My_Databases/Unisa_Database/UnisaDB.db");
    14. dataBase.open(); //This works
    15.  
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void MainWindow::on_pushButton_clicked()
    24. {
    25.  
    26.  
    27. }
    28.  
    29. void MainWindow::checkJobsAtHand()
    30. {
    31. /*QString sqlStr(QString("SELECT Name, Surname, Address, ContactNo FROM ClientTable")), tempStr;
    32.   QSqlQuery qry;
    33.   qry.prepare(sqlStr);
    34.  
    35.   if(qry.exec())
    36.   {
    37.   while(qry.next())
    38.   {
    39.   tempStr = QString("%1 %2 | %3 | %4").arg(qry.value(0).toString()).arg(qry.value(1).toString())
    40.   .arg(qry.value(2).toString()).arg(qry.value(3).toString());
    41.   job.append(tempStr);
    42.  
    43.   }
    44.   }
    45.   JobsAtHandModel->setStringList(job);
    46.   ui->lcdNumber->display(job.count());*/
    47. }
    48.  
    49. void MainWindow::checkJobsCompleted()
    50. {
    51.  
    52. }
    53.  
    54. void MainWindow::on_AddCourseBtn_clicked()
    55. {
    56. NewModule* newMod = new NewModule(this);
    57. newMod->setModal(true);
    58. newMod->exec();
    59. }
    To copy to clipboard, switch view to plain text mode 

    DIFFERENT CLASS

    Qt Code:
    1. #include "newmodule.h"
    2. #include "ui_newmodule.h"
    3.  
    4. NewModule::NewModule(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::NewModule)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. NewModule::~NewModule()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void NewModule::on_pushButton_clicked()
    17. {
    18.  
    19. }
    20.  
    21. void NewModule::on_SubmitBtn_clicked()
    22. {
    23. QSqlDatabase dataBase = QSqlDatabase::addDatabase("QSQLITE");
    24. dataBase.setDatabaseName("C:/My_Databases/Unisa_Database/UnisaDB.db");
    25. dataBase.open();
    26.  
    27. QString sqlStr(QString("INSERT INTO Modules VALUES(%1, %2, %3, %4").arg(ui->moduleCodeLE->text())
    28. .arg(ui->moduleNameLE->text()).arg(ui->LectureLE->text()).arg(ui->contactNo->text()));
    29. QSqlQuery qry;
    30. qry.prepare(sqlStr);
    31. qry.exec(); //This doesn't work
    32. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Database updating problem

    QSqlDatabase objects can be copied, you can pass the object to the "NewModule" instance from MainWindow.
    Another thing, if you create another database with the same connection name (default one in your case) then previous connection is removed, I don't think this is what you want.
    So instead of
    Qt Code:
    1. void NewModule::on_SubmitBtn_clicked()
    2. {
    3. QSqlDatabase dataBase = QSqlDatabase::addDatabase("QSQLITE");
    4. dataBase.setDatabaseName("C:/My_Databases/Unisa_Database/UnisaDB.db");
    5. dataBase.open();
    6. ...
    To copy to clipboard, switch view to plain text mode 
    try
    Qt Code:
    1. void NewModule::on_SubmitBtn_clicked()
    2. {
    3. QSqlDatabase dataBase = QSqlDatabase::database();
    4. ...
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: Database updating problem

    What does "does not work" ? What is a result from qry.lastError() and dataBase.lastError() ?

  4. #4
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Database updating problem

    My problem here is that the database object (i.e. QSqlDatabase) is declare in the scope of the class MainWindow which means the database object is inaccessible in the scope of the class NewModule. The issue here is that the sole purpose of the class NewModule is to add a new record in the database which happens to have been declared in the scope of the class MainWindow. That is my predicament. any suggestions will be appreciated.

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Database updating problem

    any suggestions will be appreciated.
    Suggestion is - pass the object down to the child class or use QSqlDatabase::database() method to get access to the previously created connection.

Similar Threads

  1. Replies: 0
    Last Post: 15th October 2011, 14:25
  2. Replies: 8
    Last Post: 30th March 2011, 20:06
  3. Updating SQL Database from QTableView
    By msweet in forum Newbie
    Replies: 5
    Last Post: 22nd June 2010, 22:18
  4. updating database with custom delegate
    By Shaitan in forum Qt Programming
    Replies: 4
    Last Post: 17th July 2007, 10:34
  5. updating database
    By locus in forum Qt Programming
    Replies: 3
    Last Post: 27th January 2007, 05:54

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.