[Qt-interest] SQLite and create new database file

Constantin Makshin dinosaur-rus at users.sourceforge.net
Tue Jan 20 22:45:54 CET 2009


Obviously, a file can't be considered a database until you create at least  
one table. So, IMHO, instead of storing an empty (but valid) database in  
resources and then extracting it when the file doesn't exist, you can do  
one of these things immediately after successfully opening/creating the  
database file:
1) check if required table exists with  
QSqlDatabase::tables().contains(tableName) and, if it fails (the table is  
not present in the database), create one;
2) execute "CREATE TABLE IF NOT EXISTS ..." query.

Since SQLite supports "IF NOT EXISTS" clause of "CREATE TABLE" statement,  
the second variant should be OK.

On Mon, 19 Jan 2009 20:26:02 +0300, Robert Hairgrove  
<evorgriahr at hispeed.ch> wrote:
> Thanks to everyone for replying!
>
> After some more experimentation running sqlite3 from a terminal with the
> name of a non-existent database, I discovered that sometimes a file of
> size 1 byte containing the letter "S" is created if I do not create a
> dummy table and then drop it. Obviously, that 1 byte file is not going
> to be a valid database! I still haven't been able to make Qt create any
> files, valid or not, with QSqlDatabase functions alone.
>
> So it looks like the suggestion to keep an initial valid database file
> as a binary resource in the application will be the way to go.

-- 
Constantin "Dinosaur" Makshin



More information about the Qt-interest-old mailing list