iphone - Sqlite 不执行查询 Some time

标签 iphone ios sqlite

我有需要对数据库执行的查询序列.. 大多数时候它工作正常..但有时它无法插入查询。

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);
    }

以上是我为执行插入操作而实现的代码...

任何人都可以帮助我找到它是否失败然后由于什么原因它无法插入数据库以便我可以处理错误..

最佳答案

使用该方法在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);


    }

}

此外,如果插入不能正常工作,原因可能是文件不在文档目录中,如果它在包中,它将获取数据,但如果数据库在包中,则无法更新或插入值,将其复制到文档中目录,然后尝试使用它

-(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];

}

For more info see this

关于iphone - Sqlite 不执行查询 Some time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17876910/

相关文章:

iphone - 从 iPhone 上的应用程序到服务器的已验证 channel

ios - 在 Xcode 中多次自动运行测试用例

python - 绑定(bind)参数 0 时出错 - 可能是不受支持的类型。 SQL/Python

javascript - Sequelizejs--如何将日期时间写入MSSQL

iphone - UICollectionViewCells 与 UICollectionViewFlowLayout - 发生奇怪的布局

iOS——调用 contentsOfDirectoryAtPath 会在循环中泄漏内存

iphone - MPMoviePlayerController 中 prepareToPlay 和 Play 的区别

ios - 下载用于在本地通知上显示的图像不起作用

ios - 如何创建 View 以添加到 View Controller ?

ios - 将 SQL 数据库从一台 iPad 复制到另一台