Results 1 to 2 of 2

Thread: Query on MS Access DB cause crash

  1. #1
    Join Date
    Sep 2016
    Posts
    8
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Query on MS Access DB cause crash

    Hello guys,
    i'm trying to make a query on an application that is connected with a .mdb file.
    In one of my dialogs i can populate a tableView with no problem and then close the dialog without any problems, let's say i'm reading records from the DB table "PEOPLE".
    In the same dialog i have a form to add rows to PEOPLE table. I can add 1-n° rows but when i close the dialog the application crashes EVERY time.
    Things like this never happened to me when i was using SQLite DB.

    Let me post some of my code, maybe you can help...

    This is the way i'm doing my connection with the ACCESS .mdb file
    Qt Code:
    1. bool connectionOpen(){
    2. static const QString path = PATH;
    3. mydb = QSqlDatabase::addDatabase("QODBC", "Contact");
    4. QString params="Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ="+ path;
    5. mydb.setDatabaseName(params);
    6.  
    7. if (!mydb.open())
    8. {
    9. qDebug() << "Error: connection with database fail";
    10. qDebug() << mydb.lastError().text();
    11. return false;
    12. }
    13. else
    14. {
    15. qDebug() << "Database: connection ok";
    16. return true;
    17. }
    18. return true;
    To copy to clipboard, switch view to plain text mode 

    This is showing the rows and is working as intended
    Qt Code:
    1. void class::on_pushButton_refreshListP_clicked()
    2. {
    3.  
    4.  
    5. QSqlDatabase db = QSqlDatabase::database("Contact");
    6.  
    7. if (db.isOpen())
    8. {
    9. QSqlTableModel *model = new QSqlTableModel(this,db);
    10. model->setTable("class");
    11. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    12.  
    13. model->setHeaderData(0, Qt::Horizontal, tr("a"));
    14. model->setHeaderData(1, Qt::Horizontal, tr("b"));
    15. model->setHeaderData(2, Qt::Horizontal, tr("c"));
    16. model->select();
    17.  
    18. ui->tableView->setModel(model);
    19. }
    To copy to clipboard, switch view to plain text mode 

    This is the part of code that is working till i close the dialog... Than the application crashes
    Qt Code:
    1. if (db.isOpen())
    2. {
    3. QSqlQuery query(db);
    4. // query.setForwardOnly(true);
    5. query.prepare("INSERT INTO class(a, b, c) "
    6. " VALUES ( :a,:b, :c)");
    7.  
    8.  
    9. query.bindValue(":a", a);
    10. query.bindValue(":b", b);
    11. query.bindValue(":c", c);
    12.  
    13.  
    14. if(query.exec())
    15. {
    16. qDebug() << "exec";
    17. QTimer::singleShot(0, this, SLOT(on_pushButton_refreshListP_clicked()));
    18. }
    To copy to clipboard, switch view to plain text mode 
    someone can tell me what im'm doing wrong? I'm missing some configuration for the MS ACCESS?

    Thank you for your time and answers
    Last edited by Bruschetta; 4th October 2016 at 15:38.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Query on MS Access DB cause crash

    In line 5 of the second piece of code, you are defining a local variable named "db". In line 1 of the third piece of code, you are using another variable named "db". If the one in the third piece of code is a member variable of your class (which can't be named "class" - that's a reserved word in C++ and not legal to use as a class name), then the one in the second piece of code is hiding that one, and therefore the one in the third piece of code is probably never initialized.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    Bruschetta (5th October 2016)

Similar Threads

  1. Replies: 0
    Last Post: 3rd November 2015, 17:58
  2. Replies: 7
    Last Post: 16th April 2015, 17:11
  3. Own widget access causes crash
    By p3t3 in forum Newbie
    Replies: 1
    Last Post: 24th March 2011, 01:13
  4. thread GUI access crash
    By liqxpil in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2010, 15:47
  5. QSqlTableModel submitAll access query
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 27th April 2007, 08:04

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.