Hi everyone,

My small app has a button to start recording data into a database. A timer event happened every 1 second. My program write data to a database every second after the 'start' button has been clicked. A 'stop' button to stop recording data.

my problem is that after I click 'stop' button, and click 'start' again, i got a warning message from Qt creator:

"QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed."

I had a look the 'QSqlDatabase' class. the message indicates that a query on the database connection is still opened.

can anyone tell me how to stop the query in my program, please?
Thanks in advance.
here is part of my code:

Qt Code:
  1. void cycleScrDialog::on_pushButton_clicked()
  2. {
  3. // create new database
  4. QDate sDate = QDate::currentDate();
  5. QString dbName;
  6. dbName = QString(QApplication::applicationDirPath()).append("/I-" + sDate.toString("MMyyyy") + ".db");
  7. m4db = QSqlDatabase::addDatabase("QSQLITE");
  8. m4db.setDatabaseName(dbName);
  9.  
  10. if (!QFile::exists(dbName))
  11. {
  12. mydb.open();
  13. QSqlQuery query;
  14. query.exec("create table c1(ID int primary key unique, "
  15. "StartTime QString, StopTime QString)");
  16. mydb.close();
  17. }
  18.  
  19. if (!mydb.open())
  20. {
  21. QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while "
  22. "opening the connection: ") + m4db.lastError().text());
  23. return;
  24. }
  25. else
  26. {
  27. QSqlQuery query;
  28. query.exec("PRAGMA synchronous = OFF");
  29. model = new QSqlTableModel(this);
  30. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
  31.  
  32. model->setTable("c1");
  33. model->select();
  34. model->insertRow(0);
  35. model->setData(model->index(0, 1), String(qdatetime.toString(Qt::ISODate)));
  36. model->setData(model->index(0, 2), String(qdatetime.toString(Qt::ISODate)));
  37. model->submitAll();
  38. }
  39.  
  40. saveIni();
  41. model->setTable("c1");
  42. model->filter();
  43. model->select();
  44. }
  45.  
  46. void cycleScrDialog::timer1000Event()
  47. {
  48. model->setTable("c1");
  49. model->select();
  50. model->insertRow(0);
  51. model->setData(model->index(0, 1), String(qdatetime.toString(Qt::ISODate)));
  52. model->setData(model->index(0, 2), String(qdatetime.toString(Qt::ISODate)));
  53. model->submitAll();
  54. }
To copy to clipboard, switch view to plain text mode