PDA

View Full Version : Test for existence of SQLite db



scott_hollen
3rd February 2011, 02:17
Hey all --

I'm very, very adverse to posting questions on technical forums mainly because I like to figure things out myself, but with the project I'm currently working on and the time constraints it presents, I must throw questions out and subject myself to the abuse I might endure as a result :) Trust me I've tried to figure this out...

Is there any way with Qt's libraries to ping a SQLite database for it's existence prior to creating it? SQLite will try to create a db if it doesn't exist but I don't want that to happen -- it looks like there's ways to do it with their libraries but as of a few days ago I'd prefer to stay with the Qt ones...I'm trying to perform the following and the first line is the keystone...

if (DB doesn't exist) then
if (username/password is the default administrator pairing) then
go to the Admin form for DB and user creation
else
display Not Authorized-type error message and kick 'em out
end if
else /* DB does exist */
verify user credentials by querying DB
If (not authorized) then
display Not Authorized-type error message and kick 'em out
else
set up proper roles for this user
end if
end if


If I've overlooked something in my searched, please be kind :)


scott

looks like I lost my formatting -- sorry!

norobro
3rd February 2011, 02:44
Have a look at: QFile::exists()

ChrisW67
3rd February 2011, 03:24
I do a simple file existence check. If the file exists then I try to open it as an Sqlite DB and check that there are tables if there should be. Unfortunately a zero-byte file can be successfully opened but may not be a useful database.

Another option is to read the first 16 bytes of the file. The well known 16-byte sequence that begins every SQLite database file is:
0x53 0x51 0x4c 0x69 0x74 0x65 0x20 0x66 0x6f 0x72 0x6d 0x61 0x74 0x20 0x33 0x00
That's "SQLITE3 format 3\0"

scott_hollen
3rd February 2011, 04:01
Thanks for the replies...I was thinking about the "file exists" approach but didn't want to go off in that direction without an outside opinion...The way this apps going to work even if it's not a real SQLite file I'll be able to handle it...I'll just shut the app down :)

thanks again!


-- scott

Lykurg
3rd February 2011, 07:14
I do a simple file existence check. If the file exists then I try to open it as an Sqlite DB and check that there are tables if there should be. Unfortunately a zero-byte file can be successfully opened but may not be a useful database.This is exactly like I do it. As for the zero-byte file (or sqlite files that does not belong to my application) I also check after opening the database if it has the right structure using the table sqlite_master.