How do I use QSqlQuery to manipulate a database that's stored in a single file, not access a already-running program like MySQL?
How do I use QSqlQuery to manipulate a database that's stored in a single file, not access a already-running program like MySQL?
Use SQLite. A SQLite db is either a single file or in memory.
You can use sqlite database to stored data in a single 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.
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?
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.
i think is better approach use the class QSqlRecord to get the field names from a SQL table,
Qt Code:
{ query = query.append(tableName) QStringList fieldNames; for ( int x = 0; x < record.count(); ++x) { fieldsName.append( record.fieldName(x) ); } return fieldNames; }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
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.
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:
db.setDatabaseName(":memory:");To copy to clipboard, switch view to plain text mode
Qt Code:
db.setDatabaseName("filename.db");To copy to clipboard, switch view to plain text mode
You will find this in the documentation of QSQLite: http://www.sqlite.org/docs.html.
Bookmarks