I am a looking for advice of good tutorial or code example of SQLite in IOS to read and write in the database. Some people is talking about Core-data in iphone community, but I always prefer cross-platform solutions and SQlite looks android likes too.
I know Roxlu had made ofxSQLite, but it seems not working in IOS. I had tried with no success.
I managed to get write and read a SQlite, but mysteriously data is only storing temporally. I am using a nice wrapper library FMDB ( https://github.com/ccgus/fmdb ). I need to manage to store the data when app is close and open again with all data in. I think, I am doing something wrong about it. I imagine is something related with open the database file: “Datalist.sqlite”.
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // Get the path to the database file
NSString *documentPath = [searchPaths objectAtIndex:0];
NSString *databasePath = [documentPath stringByAppendingPathComponent:@"Datalist.sqlite"];
NSLog(@"d/b path: /%@", databasePath);
char * errmsg = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:databasePath error:NULL]; // <------------ delete d/b TESTING ONLY!
BOOL fileExists = [fileManager fileExistsAtPath:databasePath];
FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
if (![db open]) {
[db release];
NSLog(@"DB Error not open");
}
if ([db hadError]) {
NSLog(@"DB Error %d: %@", [db lastErrorCode], [db lastErrorMessage]);
}
[db executeUpdate:@"CREATE TABLE Datalist(pk INTEGER PRIMARY KEY, name VARCHAR(25), age INTEGER);"];
[db executeUpdate:@"INSERT INTO Datalist(name,age) VALUES('varvara',26);"];
[db executeUpdate:@"INSERT INTO Datalist(name,age) VALUES('mar',29);"];
FMResultSet *rs = [db executeQuery:@"select * from Datalist;"];
while ([rs next]) {
NSLog(@"%@", [rs stringForColumn:@"name"]);
}
[rs close];
[db close];
Maybe this is the source of yoru problems: You can’t write into the application-bundle on the device, you’ll have to write into your sandbox-documents folder.
Here’s a snippet to get the path to the documents-folder:
Thanks for suggestion. But still I didn’t make it work. The code not found the database. I add in project 'todo.sqlite" file and I am wondering what need more to do.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"todo.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"todo.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
I had the same issue last night. The solution is so simple just put ofxiOSGetDocumentsDirectory() mehod before your database path. You have to copy your database file in to bin/data folder. Then everything works smoothly;
sqlite = new ofxSQLite (ofxiOSGetDocumentsDirectory()+"buzdolabi_renk.db");