The key is to examine the error message returned by sqlite3_errmsg
. You report that your NSAssert
line reports the message:
failed to prepare statement with message 'no such table: match'
That means that the database you've opened does not have a match
table. If you've run this on the simulator, the easiest thing to do is to open the database in your MacOS SQLite tool of choice (I use Base, you can use the sqlite3
command line tool; use whatever you want). The database can be found in your ~/Library/Application Support/iPhone Simulator
folder. To make it easier to find this database, you might want to unhide your ~/Library
folder by firing up the Terminal
app and then running the command chflags nohidden ~/Library/
.
Anyway, I think you'll find the match
table is not there (perhaps the entire database will be blank). A common reason for that is invoking sqlite_open
on a database that doesn't exist, in which case it will create a blank database for you.
If this is the case, you want to
remove any blank databases you might have in your Documents folder (the easiest way to do that is to remove the app from your device/simulator) and run it again; and
check your database opening logic, which should probably look something like:
NSString *filename = @"yourdb.sqlite"; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *bundlePath = [[NSBundle mainBundle] pathForResource:[filename stringByDeletingPathExtension] ofType:[filename pathExtension]]; NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSString *path = [documentsPath stringByAppendingPathComponent:filename]; if (![fileManager fileExistsAtPath:path isDirectory:NO]) { NSError *error = nil; [fileManager copyItemAtPath:bundlePath toPath:path error:&error]; NSAssert(error == nil, @"Unable to copy %@ to %@", bundlePath, path); }
Clearly, this assumes that you have a database ready for use in your bundle. If you're creating that programmatically, then just do that inside the if block where you determined that it's not the case that
fileExistsAtPath
.