gpt4 book ai didi

iphone - iOS:Sqlite 数据库错误:由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因: 'error preparing statement'

转载 作者:行者123 更新时间:2023-11-29 03:44:20 25 4
gpt4 key购买 nike

我在我的应用程序中使用了 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") ;
}

对于插入行:

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

以及选择行:

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

请大家帮我解决一下这个问题。

谢谢!

最佳答案

您应该更改您的 NSAssert 语句以包含错误消息:

NSAssert1(0, @"error preparing statement: %s", sqlite3_errmsg(database));

一旦您这样做,您应该会得到有意义的响应,这将帮助您诊断问题。

如果不查看sqlite3_errmsg消息,就很难诊断问题。它可能像列名或表名中的拼写错误一样简单,也可能像由于创建数据库时未找到而找不到表一样复杂,因此创建了一个空白数据库(没有该表)。在我们看到错误消息之前很难说。


顺便说一句,您不应该使用 stringWithFormat 构建 SQL,因为您容易遭受 SQL 注入(inject)攻击,并且如果这些文本值中的任何一个包含撇号,也会出现问题。您应该使用 ? 占位符而不是 printf 样式的格式化程序,然后使用 sqlite3_bind_text 调用将值绑定(bind)到这些列:

NSString *queryInsert = @"insert into mail_snoozlist (msgBody,msgSubject, msgSender,msgTo,msgDate,snoozTime) values(?, ?, ?, ?, ?, ?)";

if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK) {
NSAssert1(0,@"error preparing statement: %s",sqlite3_errmsg(database));
return;
}

// for these 6 sqlite3_bind function calls, if any of these strings can be `nil`, then you'd
// want to call sqlite3_bind_null if that's the case, rather than sqlite3_bind_text

if (sqlite3_bind_text(statement, 1, [strBody UTF8String], -1, NULL) != SQLITE_OK) {
NSAssert1(0,@"error binding 1: %s",sqlite3_errmsg(database));
sqlite3_finalize(statement);
return;
}

if (sqlite3_bind_text(statement, 2, [msgSub UTF8String], -1, NULL) != SQLITE_OK) {
NSAssert1(0,@"error binding 2: %s",sqlite3_errmsg(database));
sqlite3_finalize(statement);
return;
}

if (sqlite3_bind_text(statement, 3, [msgFrom UTF8String], -1, NULL) != SQLITE_OK) {
NSAssert1(0,@"error binding 3: %s",sqlite3_errmsg(database));
sqlite3_finalize(statement);
return;
}

if (sqlite3_bind_text(statement, 4, [msgTo UTF8String], -1, NULL) != SQLITE_OK) {
NSAssert1(0,@"error binding 4: %s",sqlite3_errmsg(database));
sqlite3_finalize(statement);
return;
}

if (sqlite3_bind_text(statement, 5, [strMsgDate UTF8String], -1, NULL) != SQLITE_OK) {
NSAssert1(0,@"error binding 5: %s",sqlite3_errmsg(database));
sqlite3_finalize(statement);
return;
}

if (sqlite3_bind_text(statement, 6, [stringFromDate UTF8String], -1, NULL) != SQLITE_OK) {
NSAssert1(0,@"error binding 6: %s",sqlite3_errmsg(database));
sqlite3_finalize(statement);
return;
}

if (sqlite3_step(statement) != SQLITE_DONE) {
NSAssert1(0,@"error stepping: %s",sqlite3_errmsg(database));
}

sqlite3_finalize(statement);

我用 insert 语句说明了这个问题,但同样也应该用 select 语句来完成。

关于iphone - iOS:Sqlite 数据库错误:由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因: 'error preparing statement',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17940220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com