Hi all,

i can't understand where's my mistake.

Qt Code:
  1. #include <QApplication>
  2. #include <QtSql>
  3. #include <QtWidgets/QMessageBox>
  4.  
  5. bool m_hastransaction = false;
  6. bool m_startedtransaction = false;
  7.  
  8.  
  9. void connection()
  10. {
  11. db = QSqlDatabase::addDatabase("QSQLITE");
  12. db.setDatabaseName(":memory:");
  13. m_startedtransaction = false;
  14. m_hastransaction = false;
  15. if (!db.open()) {
  16. QMessageBox::critical(0, qApp->tr("Cannot open database"),
  17. qApp->tr("Unable to establish a database connection.\n"
  18. "This example needs SQLite support. Please read "
  19. "the Qt SQL driver documentation for information how "
  20. "to build it.\n\n"
  21. "Click Cancel to exit."), QMessageBox::Ok);
  22. return;
  23. }
  24. m_hastransaction = db.driver()->hasFeature(QSqlDriver::Transactions);
  25. }
  26.  
  27. void startTransaction()
  28. {
  29. if(m_hastransaction)
  30. m_startedtransaction = db.transaction();
  31. }
  32.  
  33. bool endTransaction(bool value)
  34. {
  35. if(!m_hastransaction || !m_startedtransaction)
  36. return value;
  37. else{
  38. if(value)
  39. {
  40. db.commit();
  41. }else{
  42. db.rollback();
  43. }
  44. m_startedtransaction = false;
  45. }
  46. return value;
  47. }
  48.  
  49. bool UnloggedQueryExec(QString query,bool setForward)
  50. {//it doesn't work
  51. startTransaction();
  52. QSqlQuery q(query);//QSqlQuery q = QSqlQuery(query);//it's the same
  53. q.setForwardOnly(setForward);
  54. bool ret = q.exec();
  55. if(!ret){
  56. qDebug() << "*** UnloggedQueryExec: query failed: " << query << q.lastError().text();
  57. }else{
  58. qDebug() << "*** UnloggedQueryExec: query ok";
  59. }
  60. return endTransaction(ret);
  61. }
  62.  
  63. bool UnloggedQueryExec1(QString query,bool setForward)
  64. {//it works
  65. startTransaction();
  66. q.setForwardOnly(setForward);
  67. bool ret = q.exec(query);
  68. if(!ret){
  69. qDebug() << "*** UnloggedQueryExec1: query failed: " << query << q.lastError().text();
  70. }else{
  71. qDebug() << "*** UnloggedQueryExec1: query ok";
  72. }
  73. return endTransaction(ret);
  74. }
  75.  
  76. int main(int argc, char *argv[])
  77. {
  78. QApplication a(argc,argv);
  79.  
  80. connection();
  81. UnloggedQueryExec("CREATE TABLE log(id INTEGER PRIMARY KEY,descr varchar(30))",true);
  82. UnloggedQueryExec1("CREATE TABLE log(id INTEGER PRIMARY KEY,descr varchar(30))",true);
  83.  
  84. return a.exec();
  85. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. Output:
  2. *** UnloggedQueryExec: query failed: "CREATE TABLE log(id INTEGER PRIMARY KEY,descr varchar(30))" "table tipolog already exists Unable to fetch row"
  3. *** UnloggedQueryExec1: query ok
To copy to clipboard, switch view to plain text mode 

EDIT: ops i forget pro file, even if it should not matter:

Qt Code:
  1. QT += core widgets sql
  2. CONFIG += console
  3. TARGET = test
  4. TEMPLATE = app
  5.  
  6. SOURCES += main.cpp
  7.  
  8. OBJECTS_DIR = build/o
  9. MOC_DIR = build/moc
  10. UI_DIR = build/ui
  11. RCC_DIR = build/rcc
  12. DESTDIR = bin
To copy to clipboard, switch view to plain text mode 

Someone could be so kind to help me?
Thanks in advance,
Alberto