[Qt-interest] SQLite and create new database file

Robert Hairgrove evorgriahr at hispeed.ch
Mon Jan 19 18:26:02 CET 2009


Israel Brewster wrote:
> On Jan 18, 2009, at 10:09 PM, Christoph Duelli wrote:
> 
>>>>> QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
>>>>> db.setDatabaseName(fileName);
>>>>> db.open();
>>
>> note that addDatabase and setDatabaseName both *do not* create tables 
>> in the
>> (or a) database.
>> QDatabase is a database *connection* (and said calls just establish 
>> and name
>> a connection to a database).
>>
>> Creating a table in a database is done with SQL (see QSqlQuery):
>> QSqlQuery q; // pass your QSqlDatabase if more than one connection
>> q.exec("your sql command");
>> ...
> 
> For what it is worth, I have been working on a SQLite QT application for 
> the last month or so now, and in my code the above referenced code block 
> DOES create a new database file. Granted, it is empty, has no tables, 
> and is not a valid database in any way (until I run a QSqlQuery to 
> create some), but the file itself is created. Actually, this behavior 
> caused me some problems in initial testing, as a file was created but 
> not populated, and then when my application tried to open it again it 
> got an error because it wasn't a valid database file :) Keep in mind, of 
> course, the pathing issue- unless "fileName" is the full path to the 
> file, it will be created in the current directory, which may not be what 
> you are expecting.


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.



More information about the Qt-interest-old mailing list