0

我正在开发一个应用程序,我需要在其中将大量数据插入 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;

}

提前致谢

4

1 回答 1

0

我认为您可以使用调度队列

    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
        });
    });
于 2012-08-14T06:56:47.900 回答