Results 1 to 4 of 4

Thread: How can i execute a database script within Qt?

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default How can i execute a database script within Qt?

    Hey @all,

    i have a sql script in the resource file, which creates all necessary database tables, indexes and insert some initial data.

    How can i execute these script?

    Here is an example of such script:
    Qt Code:
    1. CREATE TABLE "table1" (
    2. "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    3. "bla1" TEXT,
    4. );
    5. CREATE TABLE "table2" (
    6. "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    7. "bla2" TEXT,
    8. );
    To copy to clipboard, switch view to plain text mode 

    Best Regards
    NoRulez

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can i execute a database script within Qt?

    Qt Code:
    1. #include <QApplication>
    2. #include <QSqlDatabase>
    3. #include <QDebug>
    4. #include <QSqlTableModel>
    5. #include <QStringList>
    6. #include <QSqlQuery>
    7. #include <QTreeView>
    8. #include <QFile>
    9. #include <QSqlError>
    10.  
    11. int ExecuteSqlScriptFile(QSqlDatabase & db, const QString & fileName)
    12. {
    13. QFile file(fileName);
    14. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    15. return 0;
    16.  
    17. QTextStream in(&file);
    18. QString sql = in.readAll();
    19. QStringList sqlStatements = sql.split(';', QString::SkipEmptyParts);
    20. int successCount = 0;
    21.  
    22. foreach(const QString& statement, sqlStatements)
    23. {
    24. if (statement.trimmed() != "")
    25. {
    26. QSqlQuery query(db);
    27. if (query.exec(statement))
    28. successCount++;
    29. else
    30. qDebug() << "Failed:" << statement << "\nReason:" << query.lastError();
    31. }
    32. }
    33. return successCount;
    34. }
    35.  
    36.  
    37. int main(int argc, char * argv[])
    38. {
    39. QApplication a(argc, argv);
    40.  
    41. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    42. db.setDatabaseName(":memory:");
    43. db.open();
    44.  
    45. ExecuteSqlScriptFile(db, "sql.sql");
    46.  
    47. QSqlTableModel tbl(0, db);
    48. tbl.setTable("table2");
    49. tbl.select();
    50.  
    51. tv.setModel(&tbl);
    52. tv.show();
    53.  
    54. return a.exec();
    55. }
    To copy to clipboard, switch view to plain text mode 
    Tested with this script:
    Qt Code:
    1. CREATE TABLE `table1` (
    2. `id` INTEGER,
    3. `bla1` TEXT,
    4. PRIMARY KEY(`id`)
    5. );
    6. CREATE TABLE `table2` (
    7. `id` INTEGER,
    8. `bla2` TEXT,
    9. PRIMARY KEY(`id`)
    10. );
    11. INSERT INTO `table2` (bla2) VALUES ('Hello World!');
    To copy to clipboard, switch view to plain text mode 
    Note: If your script is many megabytes, you may want to load it incrementally.

  3. #3
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can i execute a database script within Qt?

    Note: The above won't work if there are embedded ; in sql strings. Be careful.

  4. #4
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can i execute a database script within Qt?

    OK, take two. This handles semicolons in single quoted strings. It still doesn't handle (rarer) double quoted strings or comments.
    Qt Code:
    1. #include <QApplication>
    2. #include <QSqlDatabase>
    3. #include <QDebug>
    4. #include <QSqlTableModel>
    5. #include <QStringList>
    6. #include <QSqlQuery>
    7. #include <QTreeView>
    8. #include <QFile>
    9. #include <QSqlError>
    10.  
    11.  
    12. int ParseSqlScriptFile(QSqlDatabase & db, const QString & fileName)
    13. {
    14. QFile file(fileName);
    15. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    16. return 0;
    17.  
    18. QTextStream in(&file);
    19. QString sql = in.readAll();
    20. if (sql.length() == 0)
    21. return 0;
    22.  
    23. QList<int> splitPoints;
    24. enum { IN_STR, IN_ESC, NORMAL } state = NORMAL;
    25. int successCount = 0;
    26.  
    27. for (int i = 0; i < sql.length(); i++)
    28. {
    29. const int character = sql.at(i).unicode();
    30. switch (state)
    31. {
    32. case IN_STR:
    33. switch (character)
    34. {
    35. case '\'':
    36. state = NORMAL;
    37. break;
    38. case '\\':
    39. state = IN_ESC;
    40. break;
    41. }
    42. break;
    43.  
    44. case IN_ESC:
    45. state = IN_STR;
    46. break;
    47.  
    48. case NORMAL:
    49. switch (character)
    50. {
    51. case ';':
    52. splitPoints.push_back(i);
    53. break;
    54.  
    55. case '\'':
    56. state = IN_STR;
    57. break;
    58. }
    59. }
    60. }
    61.  
    62. splitPoints.push_back(sql.length() - 1);
    63.  
    64. for (int i = 0, j = 0; i < splitPoints.length(); i++)
    65. {
    66. QString statement = sql.mid(j, splitPoints.at(i) - j + 1);
    67. j = splitPoints.at(i) + 1;
    68.  
    69. if (statement.trimmed().length() > 0)
    70. {
    71. QSqlQuery query(db);
    72. if (query.exec(statement))
    73. successCount++;
    74. else
    75. qDebug() << "Failed:" << statement << "\nReason:" << query.lastError();
    76. }
    77. }
    78.  
    79. return successCount;
    80. }
    81.  
    82. int main(int argc, char * argv[])
    83. {
    84. QApplication a(argc, argv);
    85.  
    86. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    87. db.setDatabaseName(":memory:");
    88. db.open();
    89.  
    90. int queryCount = ParseSqlScriptFile(db, "sql.sql");
    91. qDebug() << "Successful queries:" << queryCount;
    92.  
    93. QSqlTableModel tbl(0, db);
    94. tbl.setTable("table2");
    95. tbl.select();
    96.  
    97. tv.setModel(&tbl);
    98. tv.show();
    99.  
    100. return a.exec();
    101. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Can execute examples
    By gyre in forum Qwt
    Replies: 12
    Last Post: 26th February 2010, 08:51
  2. Can I include a script from script?
    By yycking in forum Qt Programming
    Replies: 1
    Last Post: 24th April 2009, 03:01
  3. How to execute an exe file from Qt application
    By maveric in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2008, 10:24
  4. Replies: 2
    Last Post: 5th March 2008, 10:39
  5. How to Execute Qt script from Qt
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 7th May 2007, 09:12

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.