Results 1 to 8 of 8

Thread: QSQLITE: multiple instructions in SQL query

  1. #1
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QSQLITE: multiple instructions in SQL query

    Hi,

    I'm porting a pure C++ application in Qt.
    These application create a SQLITE database structure in one instruction
    Qt Code:
    1. const char* sql =
    2. "create table T1 (id INTEGER, name TEXT); \n"
    3. "create table T2 (id INTEGER, name TEXT); \n";
    4.  
    5. int ans = sqlite_exec (db, sql, 0, 0, 0);
    6. if (SQLITE_OK != ans) {
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    writing this code
    Qt Code:
    1. const char* sql =
    2. "create table T1 (id INTEGER, name TEXT); \n"
    3. "create table T2 (id INTEGER, name TEXT); \n";
    4.  
    5. QSqlQuery q(db)
    6. if (!q.exec(sql)) {
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    QSqlQuery::exec returns true but only the first table is created.
    I think this is a QSqlDriver limit.

    Any suggestion?

    PS. At the moment i resolved creating an array of query and executing them one a time
    A camel can go 14 days without drink,
    I can't!!!

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSQLITE: multiple instructions in SQL query

    I'm not an expert on SQLite, but why don't you try the following:
    Qt Code:
    1. QString sql = "create table T1 (id INTEGER, name TEXT); create table T2 (id INTEGER, name TEXT);";
    2. QSqlQuery q(db);
    3. if (!q.exec(sql))
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Last edited by schnitzel; 9th March 2011 at 18:47. Reason: typo

  3. #3
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: QSQLITE: multiple instructions in SQL query

    Code of schnitzel is not work too.

    I'm looking code of sqlite plugin, but cann't find any mistake. This problem is very interesting for me, can anybody help?
    Try read Qt documentation before ask stupid question.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE: multiple instructions in SQL query

    You code doesn't work. Only T1 are created.
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSQLITE: multiple instructions in SQL query

    sorry, didn't actually try the code. I just tried it in mysql Query Browser and it didn't work there either.
    If you look at one of the sql examples:
    http://doc.qt.nokia.com/latest/sql-cachedtable.html
    ... it appears you can only do one statement per exec() call.

    However, can't you read multiple sql statements from a file using sqlite command line?
    Last edited by schnitzel; 9th March 2011 at 20:07. Reason: clarify

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE: multiple instructions in SQL query

    At the moment this behaviour isn't a real problem, I'm only curious about it because using sqlite API is possible to do this.
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSQLITE: multiple instructions in SQL query

    I'm not sure if the sqlite API supports reading sql statements from a file, you would have to check on sqlite website.

    What is wrong with using sqlite command line? You could use QProcess to do the tedious work of creating the db structure.

  8. #8
    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: QSQLITE: multiple instructions in SQL query

    The Sqlite API permits one statement at a a time through sqlite3_prepare16_v2(), which is the call that Qt uses because it permits parameter binding. The sqlite3_exec() API call, which does permit multiple statements, does not permit parameter binding AFAICT.

Similar Threads

  1. Dose the QSQLITE support the QUERY COMMAND LINE ?
    By wter27 in forum Qt Programming
    Replies: 1
    Last Post: 1st February 2011, 02:07
  2. bad instructions...
    By antsu in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 29th May 2010, 09:04
  3. QSqlite, multiple connections to in memory database.
    By adzajac in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2010, 22:35
  4. How to find result of a query in qsqlite?
    By newtowindows in forum Qt Programming
    Replies: 1
    Last Post: 28th October 2009, 11:40
  5. Error executing SELECT query with QSQLITE
    By garfield85 in forum Qt Programming
    Replies: 6
    Last Post: 25th May 2009, 18:05

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.