在 nsthread 上运行的 iPhone Sqlite 插入
Posted
技术标签:
【中文标题】在 nsthread 上运行的 iPhone Sqlite 插入【英文标题】:iPhone Sqlite insert running on nsthread 【发布时间】:2012-08-14 06:41:38 【问题描述】:我正在开发一个应用程序,我需要在其中将大量数据插入到 sqlite 数据库中。数据被正确插入,但问题是有时我得到一个异常
Assertion failure in -[AttendeeDetails addData:anddbpath:], /Users/abhisheknaidu/Desktop/EventApp/AttendeeDetails.m:118
2012-08-14 11:54:24.937 EventApp[3586:1770b]
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while creating add statement. 'SQL logic error or missing database''
这种情况只会发生几次..
这里的任何人都可以帮助我吗..
我使用下面的代码插入
- (void) addData:(NSMutableArray *)attendeeArray anddbpath:(NSString *)dbPath
NSString *str = @"Not Available";
if(addStmt == nil)
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
const char *sql = "insert into AttendeeDetails(AtQRid , EventId , ImageUrl , FirstName , LastName , Company , Email , PhoneNumber , City , State , ATBarcodeID) Values(?,?,?,?,?,?,?,?,?,?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
sqlite3_bind_text(addStmt, 1, [[attendeeArray valueForKey:@"Attendee_QR_ID"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [[[[AppDelegate SharedInstance].DetailsOfEventArray valueForKey:@"Event_ID"] objectAtIndex:0] UTF8String], -1, SQLITE_TRANSIENT);
if ([attendeeArray valueForKey:@"Image_URL"] == [NSNull null])
sqlite3_bind_text(addStmt, 3, [str UTF8String], -1, SQLITE_TRANSIENT);
else
sqlite3_bind_text(addStmt, 3, [[attendeeArray valueForKey:@"Image_URL"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 4, [[attendeeArray valueForKey:@"FirstName"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 5, [[attendeeArray valueForKey:@"LastName"] UTF8String], -1, SQLITE_TRANSIENT);
if ([attendeeArray valueForKey:@"Company"] == [NSNull null])
sqlite3_bind_text(addStmt, 6, [str UTF8String], -1, SQLITE_TRANSIENT);
else
sqlite3_bind_text(addStmt, 6, [[attendeeArray valueForKey:@"Company"] UTF8String], -1, SQLITE_TRANSIENT);
if ([attendeeArray valueForKey:@"Email"] == [NSNull null])
sqlite3_bind_text(addStmt, 7, [str UTF8String], -1, SQLITE_TRANSIENT);
else
sqlite3_bind_text(addStmt, 7, [[attendeeArray valueForKey:@"Email"] UTF8String], -1, SQLITE_TRANSIENT);
if ([attendeeArray valueForKey:@"PhoneNo"] == [NSNull null])
sqlite3_bind_text(addStmt, 8, [str UTF8String], -1, SQLITE_TRANSIENT);
else
sqlite3_bind_text(addStmt, 8, [[attendeeArray valueForKey:@"PhoneNo"] UTF8String], -1, SQLITE_TRANSIENT);
if ([attendeeArray valueForKey:@"City"] == [NSNull null])
sqlite3_bind_text(addStmt, 9, [str UTF8String], -1, SQLITE_TRANSIENT);
else
sqlite3_bind_text(addStmt, 9, [[attendeeArray valueForKey:@"City"] UTF8String], -1, SQLITE_TRANSIENT);
if ([attendeeArray valueForKey:@"State"] == [NSNull null])
sqlite3_bind_text(addStmt, 10, [str UTF8String], -1, SQLITE_TRANSIENT);
else
sqlite3_bind_text(addStmt, 10, [[attendeeArray valueForKey:@"State"] UTF8String], -1, SQLITE_TRANSIENT);
if ([attendeeArray valueForKey:@"State"] == [NSNull null])
sqlite3_bind_text(addStmt, 10, [str UTF8String], -1, SQLITE_TRANSIENT);
else
sqlite3_bind_text(addStmt, 10, [[attendeeArray valueForKey:@"State"] UTF8String], -1, SQLITE_TRANSIENT);
if ([attendeeArray valueForKey:@"State"] == [NSNull null])
sqlite3_bind_text(addStmt, 11, [str UTF8String], -1, SQLITE_TRANSIENT);
else
sqlite3_bind_text(addStmt, 11, [[attendeeArray valueForKey:@"State"] UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
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);
//Reset the add statement.
sqlite3_reset(addStmt);
addStmt = nil;
提前致谢
【问题讨论】:
【参考方案1】:我认为你可以使用调度队列
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^
//this will call when you inserting in bg
dispatch_async(dispatch_get_main_queue(), ^
//this will call when inserting is finished
);
);
【讨论】:
@ankur 我仍然得到与 *** Assertion failure in -[AttendeeDetails addData:anddbpath:], /Users/abhisheknaidu/Desktop/EventApp/AttendeeDetails.m:118 2012-08- 相同的异常14 12:46:25.771 EventApp[1020:1a03] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“创建添加语句时出错。 'SQL 逻辑错误或缺少数据库'' 我正在使用这个以上是关于在 nsthread 上运行的 iPhone Sqlite 插入的主要内容,如果未能解决你的问题,请参考以下文章
用于 CoreMotion 和准确计时目的的 NSThread、NSOperation 或 GCD?