Reputation: 11610
I use following code to insert into sqllite using iphone -(void)save{
AlarmData *alarm=[CallSettings getAlarm];
alarm.subject=subjectText.text;
alarm.sound=tempDefault;
NSDate *dat=[datePicker date];
NSString *da=(NSString *)[NSString stringWithFormat:@"%@",dat];
alarm.date=da;
//alarmData.date=da;
NSLog([CallSettings getAlarm].subject);
NSLog([CallSettings getAlarm].no1);
NSLog([CallSettings getAlarm].lname);
NSLog([CallSettings getAlarm].name);
NSLog([CallSettings getAlarm].date);
NSLog([CallSettings getAlarm].sound);
NSString *fullName=[NSString stringWithFormat:@"%@ %@",alarm.name,alarm.lname];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"appointment.sql"];
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select * from Alarm";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
compiledStatement=NULL;
const char *sql = "insert into Alarm(name) values ('Ahmed')";
if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) != SQLITE_OK);
else {
if(SQLITE_DONE != sqlite3_step(compiledStatement))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
//coffeeID = sqlite3_last_insert_rowid(database);
NSLog(@"Value is inserted");
// sqlite3_reset(compiledStatement);
}
}
}
}
sqlite3_close(database);
}// end of the save method
When call this method app crashes and following error occurs
2011-01-10 19:23:56.307 Appointment[521:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSString stringWithUTF8String:]: NULL cString' 2011-01-10 19:23:56.321 Appointment[521:207] Stack: ( 843263261, 825818644, 842812211, 842812115, 863177559, 21701, 844154820, 19557, 846200172, 843020431, 844097412, 844097260, 844097204, 844096268, 844356084, 846188672, 862896011, 843011267, 843009055, 860901832, 843738160, 843731504, 10565, 10480 ) terminate called after throwing an instance of 'NSException' Program received signal: “SIGABRT”. (gdb)
and apps dont run next time untill and unless i delete the app from my device I think database file crashes
Upvotes: 0
Views: 289
Reputation: 4159
You should use sqlite3_finalize(compiledStatement) instead of compiledStatement = NULL; Just read the documentation and you'll understand why. Or you could use another variable for sqlite3_stmt, for INSERT.
Upvotes: 1