【问题标题】:Iam getting error after inserting data in to sqlite3将数据插入 sqlite3 后出现错误
【发布时间】:2011-01-10 14:29:26
【问题描述】:

我使用以下代码使用 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)

直到下一次应用程序才会运行,除非我从设备中删除应用程序,否则我认为数据库文件崩溃

【问题讨论】:

    标签: iphone xcode


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      相关资源
      最近更新 更多