I am trying to download database (SQLite) which has to be used in app. Before saving the downloaded data, which comes as NSData, I want to check if the downloaded data is an SQLite database. How can I check.
For example:
The correct URLl of the database is http://example.com/mydatabase.db
If somehow URL is typed as http://example.com/mydatabase.bdx, which does not exist, I still receive some data in NSData.
NSData *dbfile = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://example.com/mydatabase.db"]];
I surely can check after saving file and trying to open and if it fails to open it is having some problem. But I would like to check in NSData without the need to save the file if correct data is not downloaded.
How can I check without saving?
Looking through the sqlite3 interface documentation, all I can find is
sqlite3_open()and friends, which all take a filename, rather than a chunk of data.Thus, short of shipping your own custom sqlite3 library, I think you’re going to have to save your downloaded
NSDataas you describe – dump it to a temporary location, then try to open it (perhaps withsqlite3_open(), but hopefully you’re using FMDB or similar) and if that fails, conclude that the data is invalid.