Results 1 to 5 of 5

Thread: QSqlQuery

  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default QSqlQuery

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQuery

    What is unclear about the error message your database engine is giving you?
    Qt Code:
    1. Output:
    2. *** UnloggedQueryExec: query failed: "CREATE TABLE log(id INTEGER PRIMARY KEY,descr varchar(30))"
    3. "table tipolog already exists Unable to fetch row"
    To copy to clipboard, switch view to plain text mode 

    BTW: it helps if the code you post and the errors you present match

  3. #3
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QSqlQuery

    Thanks for your replay.

    Yes, furthermore It's unclear to me why it works if I create the query in this way:

    Qt Code:
    1. q.exec(query);
    To copy to clipboard, switch view to plain text mode 

    while it doesn't work if I create it in these other way:

    Qt Code:
    1. QSqlQuery q(query);
    2. QSqlQuery q = QSqlQuery(query);
    To copy to clipboard, switch view to plain text mode 

    This is not so clear even because there is an example in qt docs like mine(http://qt-project.org/doc/qt-5.0/qts...y.html#details):

    Qt Code:
    1. QSqlQuery query("SELECT country FROM artist");
    2. while (query.next()) {
    3. QString country = query.value(0).toString();
    4. doSomething(country);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Though the error message of database engine is not so clear as I never created log table before therefore It shouldn't happen that kind of error message.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQuery

    As described in the docs, QSqlQuery::QSqlQuery() with a string argument will create the query and execute it. Calling exec() a second time might have unintended consequences, like trying to create the same table again.

  5. #5
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Smile Re: QSqlQuery

    Ops i miss it: ", it will be executed".

    Thanks and sorry for noob question

Similar Threads

  1. Replies: 2
    Last Post: 9th April 2013, 10:15
  2. QSqlQuery
    By rajko in forum Newbie
    Replies: 7
    Last Post: 24th May 2012, 22:26
  3. Replies: 1
    Last Post: 18th July 2011, 13:12
  4. QSqlQuery
    By yasher in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2010, 15:25
  5. QSqlquery
    By codeman in forum Qt Programming
    Replies: 10
    Last Post: 4th June 2009, 13:57

Tags for this Thread

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.