Reputation: 864
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?
Upvotes: 1
Views: 876
Reputation: 38510
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 NSData
as you describe - dump it to a temporary location, then try to open it (perhaps with sqlite3_open()
, but hopefully you're using FMDB or similar) and if that fails, conclude that the data is invalid.
Upvotes: 1
Reputation: 3234
Below code will help u to check extension.
NSString *url = @"http://example.com/mydatabase.bdx";
if([url hasSuffix:@".db"])
NSData *dbfile = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
Upvotes: 1