Results 1 to 4 of 4

Thread: working with SQLite

  1. #1
    Join Date
    Aug 2006
    Posts
    25
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default working with SQLite

    I'm wondering if you can take some SQLite databases setup like this:
    Qt Code:
    1. QSqlDatabase sql = QSqlDatabase::addDatabase("QSQLITE","pva");
    2. sql.setDatabaseName("pva.db");
    3.  
    4. QSqlDatabase sql2 = QSqlDatabase::addDatabase("QSQLITE","pva-cache");
    5. sql2.setDatabaseName(":memory:");
    To copy to clipboard, switch view to plain text mode 

    and then "copy" the one to the other?

    and is there a way to store a ":memory:" database
    as a file when done working with it?

  2. #2
    Join Date
    Sep 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: working with SQLite

    In fact database in SQLite is a file! If you copy e file pva.db you have a new copy of database.

  3. #3
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: working with SQLite

    Quote Originally Posted by bhs-ittech View Post
    I'm wondering if you can take some SQLite databases setup like this:
    Qt Code:
    1. QSqlDatabase sql = QSqlDatabase::addDatabase("QSQLITE","pva");
    2. sql.setDatabaseName("pva.db");
    3.  
    4. QSqlDatabase sql2 = QSqlDatabase::addDatabase("QSQLITE","pva-cache");
    5. sql2.setDatabaseName(":memory:");
    To copy to clipboard, switch view to plain text mode 

    and then "copy" the one to the other?

    and is there a way to store a ":memory:" database
    as a file when done working with it?
    You can do it by youself. Here is an example of code to do that:
    Qt Code:
    1. SKGError SKGServices::copySqliteDatabase(const QString& iFileDbFile, QSqlDatabase* iFileDb, QSqlDatabase* iMemoryDb, bool iFromFileToMemory)
    2. {
    3. SKGError err;
    4. SKGTRACEINRC(10, "SKGServices::copySqliteDatabase", err);
    5. SKGTRACEL(10) << "Input parameter [iFileDbFile]=[" << iFileDbFile << "]" << endl;
    6. SKGTRACEL(10) << "Input parameter [iFromFileToMemory]=[" << (iFromFileToMemory ? "FILE->MEMORY" : "MEMORY->FILE") << "]" << endl;
    7.  
    8. // Copy the schema
    9. SKGTRACEL(20) << "Coping the schema..." << endl;
    10. //TODO: How to copy the statistics too
    11. SKGStringListList listSqlOrder;
    12. err=SKGServices::executeSelectSqliteOrder((iFromFileToMemory ? iFileDb: iMemoryDb),
    13. "SELECT sql FROM sqlite_master WHERE sql NOT NULL and name NOT IN ('sqlite_stat1', 'sqlite_sequence')",
    14. listSqlOrder);
    15. SKGStringListListIterator it = listSqlOrder.begin();
    16. ++it; //To Forget the header
    17. for (; err.isSucceeded() &&it != listSqlOrder.end(); ++it) {
    18. err=SKGServices::executeSqliteOrder((iFromFileToMemory ? iMemoryDb: iFileDb), *(it->begin()));
    19. }
    20.  
    21. // Attach the file iFileDbFile to the target
    22. SKGTRACEL(20) << "Attaching the file..." << endl;
    23. if (err.isSucceeded()) {
    24. err = SKGServices::executeSqliteOrder(iMemoryDb, "ATTACH DATABASE '" + iFileDbFile + "' as source");
    25. if (err.isSucceeded()) {
    26. // Copy the DATA from the source to the target
    27. SKGTRACEL(20) << "Coping data..." << endl;
    28. err = SKGServices::executeSqliteOrder(iMemoryDb, "BEGIN");
    29. if (err.isSucceeded()) {
    30. SKGStringListList listSqlOrder;
    31. err=SKGServices::executeSelectSqliteOrder(iMemoryDb,
    32. "SELECT name FROM source.sqlite_master WHERE type='table' and name NOT IN ('sqlite_stat1', 'sqlite_sequence')",
    33. listSqlOrder);
    34. SKGStringListListIterator it = listSqlOrder.begin();
    35. ++it; //To Forget the header
    36. for (; err.isSucceeded() &&it != listSqlOrder.end(); ++it) {
    37. QString val=*(it->begin());
    38. if (iFromFileToMemory) {
    39. err=SKGServices::executeSqliteOrder(iMemoryDb,
    40. "insert into main."+val+" select * from source."+val);
    41. } else {
    42. QString val=*(it->begin());
    43. err=SKGServices::executeSqliteOrder(iMemoryDb,
    44. "insert into source."+val+" select * from main."+val);
    45. }
    46. }
    47.  
    48. }
    49. if (err.isSucceeded()) {
    50. err = SKGServices::executeSqliteOrder(iMemoryDb, "COMMIT");
    51. }
    52.  
    53. //Detach
    54. SKGServices::executeSqliteOrder(iMemoryDb, "DETACH DATABASE source");
    55. }
    56. }
    57. if (err.isFailed()) err.addError(SQLLITEERROR + ERR_FAIL, tr("copySqliteDatabase::copySqliteDatabase() failed"));
    58. return err;
    59. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 15th September 2008 at 22:26. Reason: wrapped too long line

  4. #4
    Join Date
    Aug 2006
    Posts
    25
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: working with SQLite

    Thanks miraks,
    this looks simple indeed, however what is a SKGServices object?

    My guess is it's part of 'SKG' API but what's that, and how/where do I get it?

    if possible is there a way do this using only Qt?

    Problem is the database is very big, and takes ages if I have to add row by row
    of the database if the database is a file. However if the database is in memory
    that happens alot faster. ( the dataset it 1 table with 32000+ rows)
    Last edited by bhs-ittech; 15th September 2008 at 08:28.

Similar Threads

  1. QDevelop and CTags -> Not working
    By philwinder in forum Qt-based Software
    Replies: 13
    Last Post: 9th May 2008, 21:40
  2. SQLite
    By cyberboy in forum Installation and Deployment
    Replies: 1
    Last Post: 15th April 2008, 19:46
  3. sqlbrowser and sqlite
    By janus in forum Installation and Deployment
    Replies: 2
    Last Post: 31st March 2008, 14:59
  4. Don't want QTextBrowser to look in working directory
    By magland in forum Qt Programming
    Replies: 1
    Last Post: 20th October 2007, 23:14
  5. GUI thread and Working thread comunication
    By FasTTo in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 15:31

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.