我为我的应用程序使用了 sqlite DB,我需要同时调用两个查询,但我给出错误:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“错误准备语句”
我每 10 秒运行一次计时器,用于在按钮事件后从数据库中选择行和在数据库中插入行。
我的代码片段为:
在viewWillAppear
:
int result2 = sqlite3_open([dbPath UTF8String], &database);
if (result2 != SQLITE_OK) {
NSLog(@"Failure in connecting to the database with result %d",result2);
}
else {
NSLog(@ "Succesfully opened connection to DB") ;
}
并在viewWillDisappear
:
int result = sqlite3_close(database);
if (result != SQLITE_OK){
NSLog(@"Failure in closing connection to database. Result %d",result);
}
else {
NSLog(@"Successfully closed DB connection") ;
}
对于Inserting rows
:
NSString *queryInsert = [NSString stringWithFormat: @"insert into mail_snoozlist (msgBody,msgSubject, msgSender,msgTo,msgDate,snoozTime) values('%@','%@','%@','%@','%@','%@')",strBody,msgSub,msgFrom,msgTo,strMsgDate,stringFromDate];
NSLog(@"queryInsert:%@",queryInsert);
const char *sql = [queryInsert UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
sqlite3_step(statement);
sqlite3_reset(statement);
} else {
NSAssert1(0,@"error preparing statement",sqlite3_errmsg(database));
return;
}
sqlite3_finalize(statement);
和Selecting rows
:
NSString *querySQL2 = [NSString stringWithFormat: @"Select * from mail_snoozlist WHERE snoozTime = '%@'",_snoozTime];
NSLog(@"querySql:%@",querySQL2);
if (sqlite3_prepare_v2(database, [querySQL2 UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
Message *obj = [[Message alloc] init];
NSString *msgBody=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
obj.msgBody= msgBody;
NSString *msgSub=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
obj.msgSub= msgSub;
NSString *msgSender=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
obj.msgFrom= msgSender;
NSString *msgTo=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
obj.msgTo= msgTo;
NSString *msgDate=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];
obj.msgDate= msgDate;
[listOfItems addObject:obj];
[self.tableView reloadData];
}
sqlite3_reset(statement);
sqlite3_finalize(statement);
任何人都请帮我解决这个问题。
谢谢!