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 回答
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(@"sqlite3_step error: '%s'", sqlite3_errcode(contactDB));
            NSLog(@" \n\n\n\n  %@ not done query",query);
        }
    }
    sqlite3_finalize(statement);
    sqlite3_close(contactDB);
}

此外,

SQLITE_DONE 表示该语句已成功执行完毕。如果没有先调用 sqlite3_reset() 将虚拟机重置回其初始状态,则不应在此虚拟机上再次调用 sqlite3_step()。

您可以使用 SQLITE_OK 代替 SQLITE_DONE。

于 2013-07-26T09:23:42.690 回答