Results 1 to 7 of 7

Thread: Execute a .sql file with more than one query

  1. #1
    Join Date
    Aug 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Execute a .sql file with more than one query

    Hello, I'm trying to do anything like this

    Qt Code:
    1. QSqlQuery *query = new QSqlQuery();
    2. QFile file("querys.sql");
    3. file.open(QFile::ReadOnly|QFile::Text);
    4. query.exec(file.readAll());
    5.  
    6.  
    7. //querys.sql
    8. CREATE TABLE foo(id int, name varchar);
    9. CREATE TABLE bar(id int, name varchar);
    10. ....
    To copy to clipboard, switch view to plain text mode 

    but only the first query is executed

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Execute a .sql file with more than one query

    normally all should be executed, so please debug if the file is read correctly and post the error from your sql driver.

  3. #3
    Join Date
    Aug 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Execute a .sql file with more than one query

    strange, the file is read correctly and query.lastError().text() is empty.

    I've tested a query.exec() with 2 inserts but only the first is executed.

    I think that query.exec() only execute one query per time

  4. #4
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Execute a .sql file with more than one query

    After you read all text from file, you split all sql statements by semicolon. Then you can execute every sql statement one by one.

  5. #5
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Execute a .sql file with more than one query

    I should think you would also be able to loop through the file contents and execute each query iteratively.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Execute a .sql file with more than one query

    Maybe it is SQL driver dependent. On PostgreSQL it is working.

  7. #7
    Join Date
    Aug 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Execute a .sql file with more than one query

    yeah, in ruby the driver exec only one query per time.

    Now I'm spliting the file and running a query per exec. Thx to all

Similar Threads

  1. How to execute a QProgressDialog once
    By franco.amato in forum Newbie
    Replies: 4
    Last Post: 16th March 2010, 16:36
  2. How to query data by id in a text file
    By SamSong in forum Qt Programming
    Replies: 4
    Last Post: 26th May 2009, 10:58
  3. cannot execute binary file
    By mgturner in forum Installation and Deployment
    Replies: 1
    Last Post: 16th March 2009, 17:04
  4. How to execute an exe file from Qt application
    By maveric in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2008, 10:24
  5. Replies: 2
    Last Post: 5th March 2008, 10:39

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.