I’m using SQLite database. The problem is that data isn’t written to base. NSLog shows me “All ok” and insertSQL also is correct but still a record in the database does not happened. My code:
-(void)saveMessage:(id)sender
{
sqlite3 *database;
databaseName = @"BlogDatabase.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"insert into \"main\".\"MessagesData\" ( \"MessageText\", \"MessageDate\") values ( '%@', '%@');", _textField.text, [[NSDate alloc] init]];
NSLog(@"%@", insertSQL);
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, insert_stmt, -1, &compiledStatement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}else{
if (sqlite3_step(compiledStatement) == SQLITE_DONE)
{
NSLog(@"All ok");
} else {
NSLog(@"FAIL");
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
In which database you are looking for? app db ?
Can you check out the number of record, just count the records.
I doubt it whether you have any value in your _textField.text and date you are just initializing no setting it. and lets assume everything is fine, then
you should use BlogDatabase.MessagesData not main.MessagesData.
first try to run that insertQuery which you NSLog in your plain terminal.
sqlite3 [app_db_path]/BlogDatabase.sqlite