MechMK1
MechMK1

Reputation: 3378

Is there a way to check if a file is an SQLite3 DB?

I am writing a simple C program to check if a File F is a usable SQLite3 database.

I tried writing gibberish into a file test.db, but sqlite3_open("test.db", &db) returned 0.

I suppose either sqlite3_status() or sqlite3_db_status() do what I want, but the documentation states something different.

Upvotes: 3

Views: 808

Answers (2)

Lawrence Barsanti
Lawrence Barsanti

Reputation: 33232

Check out this thread.

It looks like sqlite3_open will return SQLITE_OK even if the file is not a database. However, when you try execute a statement you will get SQLITE_NOTADB. Instead of reading the header, you could just do something like SELECT * FROM sqlite_master; after opening the database and check if SQLITE_NOTADB is returned. You could go a step further and use the data returned to make sure all your tables exists.

Upvotes: 4

andrew cooke
andrew cooke

Reputation: 46882

the file must begin with a fixed header. if you want to recognise valid files, that should be fine.

Upvotes: 1

Related Questions