hi everyone,
i have init a database and i want to insert data to this database ,i write a code as below, when compiler without any error but can not insert any data
please help me
Qt Code:
  1. void MainForm::creat_db()
  2. {
  3. db = QSqlDatabase::addDatabase("QSQLITE");
  4. #ifdef Q_OS_LINUX
  5. // store database file into user home folder in Linux
  6. QString path(QDir::home().path());
  7. path.append(QDir::separator()).append("my.db.sqlite");
  8. path = QDir::toNativeSeparators(path);
  9. db.setDatabaseName(path);
  10. #else
  11. // file exists in the application private folder
  12. db.setDatabaseName("my.db.sqlite");
  13. #endif
  14. // Open databasee
  15. if(!db.open())
  16. {
  17. QMessageBox::warning(0, QObject::tr("database error"),db.lastError().text());
  18. }
  19.  
  20. QSqlQuery query;
  21. query.exec("create table db_table "
  22. "(id integer primary key, "
  23. "start_time varchar(20), "
  24. "decription varchar(30), "
  25. "finish_time varchar(20))");
  26. }
  27.  
  28. int MainForm::insert_to_db(QString start_time, QString description, QString finish_time)
  29. {
  30. int newId = -1;
  31. bool ret = false;
  32. if (db.isOpen())
  33. {
  34. // NULL = is the keyword for the autoincrement to generate next value
  35. QSqlQuery query;
  36. ret = query.exec(QString("insert into db_table(NULL,'%1','%2','%3')")
  37. .arg(start_time).arg(description).arg(finish_time));
  38. // Get database given autoincrement value
  39. if (ret)
  40. {
  41. newId = query.lastInsertId().toInt();
  42. }
  43. }
  44. return newId;
  45. }
To copy to clipboard, switch view to plain text mode 

and I call them in mainform
Qt Code:
  1. MainForm::MainForm(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::MainForm)
  4. {
  5. ui-> setupUi(this);
  6. creat_db();
  7. insert_to_db("10:10","too high","10:12"); //only test
  8. }
  9. ...........
To copy to clipboard, switch view to plain text mode