0

我得到 exc_bad_access 代码=2。有时它的工作没有错误,但有时它会出错。我不知道为什么会这样。如果有人发现任何问题,请告诉我这是我的代码..

 if (sqlite3_open(dbpath, &adhDB) == SQLITE_OK)
            {
                sqlite3_stmt *compiledStatement;

                const char *sqlStatement = "INSERT INTO tblNotifications (NotificationID,Description,BeforeHrs,AfterHrs,PreparationID,PreparationName,ProcedureID,ProcedureName,ClientID,IsActive,UpdatedDateTime) VALUES (?,?,?,?,?,?,?,?,?,?,?)"; // Total 11 ?
                NSLog(@"sqlStatement = %s",sqlStatement);


                if(sqlite3_prepare_v2(adhDB, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
                {
                    int i = 0;

                    for (statusNotification in nsNotification)
                    {
                        NSString *NotificationID1 =[statusNotification objectForKey:@"NotificationID"];

                        NotificationID1 =  [NotificationID1 stringByTrimmingCharactersInSet:
                                           [NSCharacterSet whitespaceCharacterSet]];

                        NSString *Description1 = [statusNotification objectForKey:@"Description"];
                        Description1 =  [Description1 stringByTrimmingCharactersInSet:
                                        [NSCharacterSet whitespaceCharacterSet]];


                        NSString *BeforeHrs1 = [statusNotification objectForKey:@"BeforeHrs"];
                        BeforeHrs1 =  [BeforeHrs1 stringByTrimmingCharactersInSet:
                                      [NSCharacterSet whitespaceCharacterSet]];

                        NSString *AfterHrs1 = [statusNotification objectForKey:@"AfterHrs"];
                        AfterHrs1 =  [AfterHrs1 stringByTrimmingCharactersInSet:
                                     [NSCharacterSet whitespaceCharacterSet]];
                        NSString *PreparationID1 =[statusNotification objectForKey:@"PreparationID"];
                        PreparationID1 =  [PreparationID1 stringByTrimmingCharactersInSet:
                                          [NSCharacterSet whitespaceCharacterSet]];
                        NSString *PreparationName1 =[statusNotification objectForKey:@"PreparationName"];
                        PreparationName1 =  [PreparationName1 stringByTrimmingCharactersInSet:
                                            [NSCharacterSet whitespaceCharacterSet]];
                        NSString *ProcedureID1 =[statusNotification objectForKey:@"ProcedureID"];
                        ProcedureID1 =  [ProcedureID1 stringByTrimmingCharactersInSet:
                                        [NSCharacterSet whitespaceCharacterSet]];
                        NSString *ProcedureName1 =[statusNotification objectForKey:@"ProcedureName"];
                        ProcedureName1 =  [ProcedureName1 stringByTrimmingCharactersInSet:
                                          [NSCharacterSet whitespaceCharacterSet]];
                        NSString *ClientID1 =[statusNotification objectForKey:@"ClientID"];
                        ClientID1 =  [ClientID1 stringByTrimmingCharactersInSet:
                                     [NSCharacterSet whitespaceCharacterSet]];
                        NSString *IsActive1 =[statusNotification objectForKey:@"IsActive"];
                        IsActive1 =  [IsActive1 stringByTrimmingCharactersInSet:
                                     [NSCharacterSet whitespaceCharacterSet]];


                        NSString *UpdatedDateTime1 = [HomeScreen returnDateTime];

                        sqlite3_bind_text(compiledStatement, 1, [NotificationID1 UTF8String], -1, SQLITE_TRANSIENT);

                        sqlite3_bind_text(compiledStatement, 2, [Description1 UTF8String], -1, SQLITE_TRANSIENT);

                        sqlite3_bind_text(compiledStatement, 3, [BeforeHrs1 UTF8String], -1, SQLITE_TRANSIENT);

                        sqlite3_bind_text(compiledStatement, 4, [AfterHrs1 UTF8String], -1, SQLITE_TRANSIENT);


                        sqlite3_bind_text(compiledStatement, 5, [PreparationID1 UTF8String], -1, SQLITE_TRANSIENT);


                       sqlite3_bind_text(compiledStatement, 6, [PreparationName1 UTF8String], -1, SQLITE_TRANSIENT);


                       sqlite3_bind_text(compiledStatement, 7, [ProcedureID1 UTF8String], -1, SQLITE_TRANSIENT);


                        sqlite3_bind_text(compiledStatement, 8, [ProcedureName1 UTF8String], -1, SQLITE_TRANSIENT);


                        sqlite3_bind_text(compiledStatement, 9, [ClientID1 UTF8String], -1, SQLITE_TRANSIENT);


                       sqlite3_bind_text(compiledStatement, 10, [IsActive1 UTF8String], -1, SQLITE_TRANSIENT);



                        sqlite3_bind_text(compiledStatement, 11, [UpdatedDateTime1 UTF8String], -1, SQLITE_TRANSIENT);
                      //  NSLog(@"UpdatedDateTime= %@",UpdatedDateTime1);



                        if (sqlite3_step(compiledStatement) == SQLITE_DONE) {
                            if (i == cntProcedures)
                                sqlite3_finalize(compiledStatement);
                            else
                                sqlite3_reset(compiledStatement);
                        }
                        else {

                        }
                        i++;
                    }


                }
            }

提前致谢。

4

1 回答 1

0

试试这个:我认为不止一个 sql 语句在循环中触发,现在我们使用函数触发并且每次创建新的编译语句时......所以它不会崩溃。

        for (statusNotification in nsNotification)
        {
            NSString *NotificationID1 =[statusNotification objectForKey:@"NotificationID"];

            NotificationID1 =  [NotificationID1 stringByTrimmingCharactersInSet:
                        [NSCharacterSet whitespaceCharacterSet]];

            NSString *Description1 = [statusNotification objectForKey:@"Description"];
            Description1 =  [Description1 stringByTrimmingCharactersInSet:
                     [NSCharacterSet whitespaceCharacterSet]];


            NSString *BeforeHrs1 = [statusNotification objectForKey:@"BeforeHrs"];
            BeforeHrs1 =  [BeforeHrs1 stringByTrimmingCharactersInSet:
                   [NSCharacterSet whitespaceCharacterSet]];

            NSString *AfterHrs1 = [statusNotification objectForKey:@"AfterHrs"];
            AfterHrs1 =  [AfterHrs1 stringByTrimmingCharactersInSet:
                  [NSCharacterSet whitespaceCharacterSet]];
            NSString *PreparationID1 =[statusNotification objectForKey:@"PreparationID"];
            PreparationID1 =  [PreparationID1 stringByTrimmingCharactersInSet:
                       [NSCharacterSet whitespaceCharacterSet]];
            NSString *PreparationName1 =[statusNotification objectForKey:@"PreparationName"];
            PreparationName1 =  [PreparationName1 stringByTrimmingCharactersInSet:
                         [NSCharacterSet whitespaceCharacterSet]];
            NSString *ProcedureID1 =[statusNotification objectForKey:@"ProcedureID"];
            ProcedureID1 =  [ProcedureID1 stringByTrimmingCharactersInSet:
                     [NSCharacterSet whitespaceCharacterSet]];
            NSString *ProcedureName1 =[statusNotification objectForKey:@"ProcedureName"];
            ProcedureName1 =  [ProcedureName1 stringByTrimmingCharactersInSet:
                       [NSCharacterSet whitespaceCharacterSet]];
            NSString *ClientID1 =[statusNotification objectForKey:@"ClientID"];
            ClientID1 =  [ClientID1 stringByTrimmingCharactersInSet:
                  [NSCharacterSet whitespaceCharacterSet]];
            NSString *IsActive1 =[statusNotification objectForKey:@"IsActive"];
            IsActive1 =  [IsActive1 stringByTrimmingCharactersInSet:
                  [NSCharacterSet whitespaceCharacterSet]];


            NSString *UpdatedDateTime1 = [HomeScreen returnDateTime];


            [self insertTblNotification:NotificationID1 :Description1 :BeforeHrs1 :AfterHrs1 :PreparationID1 :PreparationName1 :ProcedureID1 :ProcedureName1 :ClientID1 :IsActive1 :UpdatedDateTime1]
        }



    -(void)insertTblNotification :(NSString *) NotificationID:(NSString *) Description:(NSString *) BeforeHrs:(NSString *) AfterHrs:(NSString *) PreparationID:(NSString *) PreparationName:(NSString *) ProcedureID:(NSString *) ProcedureName:(NSString *) ClientID:(NSString *) IsActive:(NSString *) UpdatedDateTime
    {

        const char *insertSql;
    insertSql = nil;
        sqlite3_stmt *compiledStatement;

        compiledStatement = nil;

        if(compiledStatement == nil)
        {


                    insertSql ="INSERT INTO tblNotifications (NotificationID,Description,BeforeHrs,AfterHrs,PreparationID,PreparationName,ProcedureID,ProcedureName,ClientID,IsActive,UpdatedDateTime) VALUES (?,?,?,?,?,?,?,?,?,?,?)"; // Total 11 ?

                    if(sqlite3_prepare_v2(adhDB, insertSql, -1, &compiledStatement, NULL) != SQLITE_OK){
                        NSAssert1(0, @"Error: failed to prepare insert statement with message '%s'.", sqlite3_errmsg(appDelegate.objDBAdapter.database));
            }

            sqlite3_bind_text(compiledStatement, 1, [NotificationID1 UTF8String], -1, SQLITE_TRANSIENT);

            sqlite3_bind_text(compiledStatement, 2, [Description1 UTF8String], -1, SQLITE_TRANSIENT);

            sqlite3_bind_text(compiledStatement, 3, [BeforeHrs1 UTF8String], -1, SQLITE_TRANSIENT);

            sqlite3_bind_text(compiledStatement, 4, [AfterHrs1 UTF8String], -1, SQLITE_TRANSIENT);


            sqlite3_bind_text(compiledStatement, 5, [PreparationID1 UTF8String], -1, SQLITE_TRANSIENT);


            sqlite3_bind_text(compiledStatement, 6, [PreparationName1 UTF8String], -1, SQLITE_TRANSIENT);


            sqlite3_bind_text(compiledStatement, 7, [ProcedureID1 UTF8String], -1, SQLITE_TRANSIENT);


            sqlite3_bind_text(compiledStatement, 8, [ProcedureName1 UTF8String], -1, SQLITE_TRANSIENT);


            sqlite3_bind_text(compiledStatement, 9, [ClientID1 UTF8String], -1, SQLITE_TRANSIENT);


            sqlite3_bind_text(compiledStatement, 10, [IsActive1 UTF8String], -1, SQLITE_TRANSIENT);



            sqlite3_bind_text(compiledStatement, 11, [UpdatedDateTime1 UTF8String], -1, SQLITE_TRANSIENT);
    //  NSLog(@"UpdatedDateTime= %@",UpdatedDateTime1);

            if(sqlite3_step(compiledStatement) != SQLITE_DONE)
            {
                NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(adhDB));
                return;
            }
            sqlite3_reset(compiledStatement);

        }

    }
于 2013-04-23T10:38:04.447 回答