I Spent days trying to fix this code and I give up !! What I'm doing is taking input from user and insert it at database if he press "save" button. when I run this code and After I pressed "save" button. 1- I can't quit from the View ( to another tab ). 2- the inserting didn't applied!!
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];
sqlite3 *database;
//Copy the database from the package to the usrrs filesystem
if (sqlite3_open([dbPath UTF8String], &database)== SQLITE_OK) {//start if
NSLog(@"dataBaseOpen");
const char *sqlStatement = "insert into Location (Latitude,Longitude,LocationName,Tag) VALUES (?,?,?,?)";
//
sqlite3_stmt *compileStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compileStatement, NULL) == SQLITE_OK) { // start if 2
if(SQLITE_DONE!=sqlite3_step(compileStatement))
{
sqlite3_bind_double(compileStatement, 1, [latitudeTextField.text doubleValue]);
sqlite3_bind_double(compileStatement, 2, [longitudeTextField.text doubleValue]);
sqlite3_bind_text( compileStatement, 3, [lName.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compileStatement, 4, [myText.text UTF8String] ,-1, SQLITE_TRANSIENT);
NSLog( @" successfully done" );
if(sqlite3_step(compileStatement) != SQLITE_DONE ) {
NSLog( @" not done" );
}
}
}
}
and I put in appDelegate code for coping in the file manager:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (success) {
NSLog(@"we have the database");
} else {
NSLog(@"we have no database");
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];
BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];
if (moved) {
NSLog(@"database copied");
}
}
Did I miss something !! what is the problem !!