Results 1 to 12 of 12

Thread: Database in a file?

  1. #1
    Join Date
    Jun 2010
    Posts
    142
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11
    Thanked 4 Times in 3 Posts

    Default Database in a file?

    How do I use QSqlQuery to manipulate a database that's stored in a single file, not access a already-running program like MySQL?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Database in a file?

    Use SQLite. A SQLite db is either a single file or in memory.

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

    Default Re: Database in a file?

    You can use sqlite database to stored data in a single file.

  4. #4
    Join Date
    Jun 2010
    Posts
    142
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11
    Thanked 4 Times in 3 Posts

    Default Re: Database in a file?

    Quote Originally Posted by tbscope View Post
    Use SQLite. A SQLite db is either a single file or in memory.
    Should I directly use SQLite's C++ API or use it as a "back end" for Qt's API (and if so, how)?

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

    Default Re: Database in a file?

    You can use QSQLITE driver to access sqlite database. Look at http://doc.trolltech.com/4.6/sql-cachedtable.html, especially in "Connecting to a Database" part.

  6. #6
    Join Date
    Jun 2010
    Posts
    142
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11
    Thanked 4 Times in 3 Posts

    Default Re: Database in a file?

    QSQLITE seems to work well.

    Now a slightly unrelated problem, but I don't think I should start another thread for it:

    How do I get a list of all the columns in a table?

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

    Default Re: Database in a file?

    Quote Originally Posted by MTK358 View Post
    How do I get a list of all the columns in a table?
    I guess you can do it in SQLite by queriing the tablemaster or simply fetch one row, and use QSqlRecord for getting the column names.

  8. #8
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Database in a file?

    i think is better approach use the class QSqlRecord to get the field names from a SQL table,

    Qt Code:
    1. QStringList MySQlClass::fieldNames(QString tableName)
    2. {
    3. QString query ("select * from ");
    4. query = query.append(tableName)
    5.  
    6. QSqlQuery sqlQuery(query);
    7. QSqlRecord record = sqlQuery.record(); //get the field information for the current query.
    8.  
    9. QStringList fieldNames;
    10. for ( int x = 0; x < record.count(); ++x)
    11. {
    12. fieldsName.append( record.fieldName(x) );
    13. }
    14. return fieldNames;
    15. }
    To copy to clipboard, switch view to plain text mode 

    this should work. and works on MySQL, Firebird, SQlite, etc

    if you need the database in one file and SQlite is a little small for yout project. firebird is a another option.
    Last edited by ecanela; 2nd August 2010 at 05:38. Reason: updated contents

  9. #9
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android
    Thanks
    16
    Thanked 5 Times in 5 Posts

    Default Re: Database in a file?

    Quote Originally Posted by saa7_go View Post
    You can use QSQLITE driver to access sqlite database. Look at http://doc.trolltech.com/4.6/sql-cachedtable.html, especially in "Connecting to a Database" part.
    Using QSQLITE driver, how do you persist the SQLite database to a file? That's the power of SQLite ... a serverless, self-contained, file-based SQL solution.

    I was able to get the Cached Table Example to compile and run, but I don't see how to save it to a file.

  10. #10
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android
    Thanks
    16
    Thanked 5 Times in 5 Posts

    Default Re: Database in a file?

    I was able to answer my own question with a little experimentation. Although I'd like to find this in the documentation ... still looking.

    The example, uses an in-memory database.

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(":memory:");
    To copy to clipboard, switch view to plain text mode 
    I noticed that changing the database name, resulted in using a file for the database. If it doesn't exist, one is created; if it exists, then that's the one that's loaded. Sweet!!!

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("filename.db");
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Database in a file?

    You will find this in the documentation of QSQLite: http://www.sqlite.org/docs.html.

  12. #12
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android
    Thanks
    16
    Thanked 5 Times in 5 Posts

    Default Re: Database in a file?

    Quote Originally Posted by Lykurg View Post
    You will find this in the documentation of QSQLite: http://www.sqlite.org/docs.html.
    I saw the SQLite documentation describing sqlite3_open(":memory:"), but I didn't correlate that to QSqlDatabase::setDatabaseName(QString&). I see it now though

Similar Threads

  1. Releasing database file with QSqlDatabase
    By JPNaude in forum Qt Programming
    Replies: 24
    Last Post: 24th May 2011, 09:33
  2. Replies: 9
    Last Post: 20th May 2010, 09:55
  3. QT Sqlite database import/ export csv file
    By logesh in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2010, 05:35
  4. Saving parts of an SQLITE database to a new file
    By zarkzervo in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2010, 00:59
  5. Database: How to use an external file?
    By goes2bob in forum Newbie
    Replies: 10
    Last Post: 23rd January 2008, 14:07

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
  •  
Qt is a trademark of The Qt Company.