PDA

View Full Version : How to validate QSQLITE file?



rakkar
23rd September 2009, 17:49
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "TEMP");
db.setDatabaseName(dbName);
if (db.open() && db.isOpenError()==false && db.isValid()==true)
{
// Opened
}
else
{
// Display error
}


I'm letting the user pick a file on the harddrive to open as an SQLite database. But I can pick ANY file and it will always reach the opened block. How do I check that a file is a valid SQLite database file?

Thanks

ChrisW67
24th September 2009, 01:05
Try to query a table from the expected schema or just retrieve the tables list. If you get an error then it is not an Sqlite DB. You will not get an error retrieving the table list if the file you opened is non-existent or zero sized. If a user mistypes the name of an existing file then Sqlite will just create it, which is both useful and annoying.