0

データベースで実行する必要がある一連のクエリがあります..ほとんどの場合、正常に動作します..しかし、クエリの挿入に失敗する場合があります。

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {

        for (int i=0;i<[queries count]; i++)
        {

            NSString *query = [queries objectAtIndex:i];
            const char *Insert_query = [query UTF8String];

            sqlite3_prepare(contactDB, Insert_query, -1, &statement, NULL);
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                //NSLog(@" \n\n\n\n %@ done query",query);
            }
            else {

                NSLog(@" \n\n\n\n  %@ not done query",query);
            }
        }
        sqlite3_finalize(statement);
        sqlite3_close(contactDB);
    }

上記は、挿入操作を実行するために実装したコードです...

エラーを処理できるように、データベースへの挿入に失敗した理由が何であるかを見つけるのを手伝ってくれる人はいますか..

4

2 に答える 2

2

このメソッドを使用して、sqlite でクエリを実行します

//-----------------------------------------------------------------------------------------------------//
#pragma mark - Helper methods
//-----------------------------------------------------------------------------------------------------//

-(BOOL)dbOpenedSuccessfully
{
    if(sqlite3_open([[self dbPath] UTF8String], &_database) == SQLITE_OK)
    {
        return YES;
    }
    else
    {
        [[[UIAlertView alloc]initWithTitle:@"Error"
                                   message:@"Error on opening the DB"
                                  delegate:self
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil, nil]show];
        return NO;
    }
}






//-----------------------------------------------------------------------------------------------------//
#pragma mark - Query
//-----------------------------------------------------------------------------------------------------//


- (void) executeQuery:(NSString *)strQuery
{
    char *error = NULL;
    if([self dbOpenedSuccessfully])
    {
        NSLog(@"%@",strQuery);
        sqlite3_exec(_database, [strQuery UTF8String], NULL, NULL,&error);
        if (error!=nil) {
            NSLog(@"%s",error);
        }
        sqlite3_close(_database);


    }

}

また、挿入が正しく機能しない場合、ファイルがドキュメント ディレクトリにない可能性があります。バンドルにある場合は、データをフェッチしますが、db がバンドルにある場合は値を更新または挿入できません。ドキュメント ディレクトリにコピーしてから、使ってみてください

-(void) checkAndCreateDatabase
{
    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    BOOL success;

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:_databasePath];

    // If the database already exists then return without doing anything
    if(success) return;

    // If not then proceed to copy the database from the application to the users filesystem

    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_databaseName];

    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:nil];

}

詳細については、これを参照してください

于 2013-07-26T09:04:24.530 に答える