将数据插入sqlite3后出现错误

时间:2011-01-10 14:29:26

标签: iphone xcode

我使用以下代码使用iphone插入sqllite - (无效)保存{

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);

} //保存方法结束

调用此方法时,应用程序崩溃并发生以下错误

2011-01-10 19:23:56.307约会[521:207] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'* + [NSString stringWithUTF8String:]:NULL cString “ 2011-01-10 19:23:56.321任命[521:207]筹码:(     843263261,     825818644,     842812211,     842812115,     863177559,     21701,     844154820,     19557,     846200172,     843020431,     844097412,     844097260,     844097204,     844096268,     844356084,     846188672,     862896011,     843011267,     843009055,     860901832,     843738160,     843731504,     10565,     10480 ) 抛出'NSException'实例后终止调用 程序收到信号:“SIGABRT”。 (gdb)

和应用程序下次不运行,除非我从我的设备中删除应用程序,我认为数据库文件崩溃

1 个答案:

答案 0 :(得分:1)

你应该使用sqlite3_finalize(compiledStatement)而不是compiledStatement = NULL; 只需阅读文档,您就会明白原因。或者你可以使用另一个变量sqlite3_stmt,用于INSERT。