ios - SQL 错误 : Not an Error

标签 ios objective-c database sqlite

更多 SQLite 问题。所以我的界面如下(这都是.m里的):

@interface Search()
    @property (strong, nonatomic) NSString *databasePath; //path to sqlite database file
    @property (strong, nonatomic) NSString *databaseName;
    @property (nonatomic) sqlite3 *database;
@end

初始化如下:

- (id)init
{
    if ((self = [super init]))
    {
        self.databaseName = DB_NAME;

        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [documentPaths objectAtIndex:0];
        _databasePath = [documentsDir stringByAppendingPathComponent:self.databaseName];
        [self checkAndCreateDatabase];
        if (sqlite3_open_v2([self.databasePath UTF8String], &_database, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
        {
            [[[UIAlertView alloc]initWithTitle:@"Missing"
                                       message:@"Database file not found"
                                      delegate:nil
                             cancelButtonTitle:@"OK"
                             otherButtonTitles:nil, nil]show];
        }
        else
        {
            NSLog(@"%s: sqlite3_open_v2 error: %s", __FUNCTION__, sqlite3_errmsg(self.database));
        }
    }

init 中的 Log 返回的错误是:sqlite3_open_v2 error: not an error。在我的搜索中,我听说 SQLite 在指向不存在的数据库时不会返回错误。但我不确定为什么数据库不存在。我正在使用的复制功能(我得到的并且之前似乎可以使用)如下:

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

    // 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
    dbExists = [fileManager fileExistsAtPath:_databasePath];

    // If the database already exists then return without doing anything
    if(dbExists)
    {
        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];
    NSError *error = nil;
    if (![fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:&error])
    {
        NSLog(@"%s: copyItemAtPathError: %@", __FUNCTION__, error);
    }
}

最后,我在 iOS Simulator Documents 目录中验证了该数据库存在,并且我尝试对其执行的查询有效。为什么我会收到此错误?

最佳答案

我从来没有像这样使用过 SQLLite,我只想提一下,在上面的代码中,当 sqlite_open_v2 == SQL_OK 时,您的 else 语句被调用。所以也许在那种情况下,没有错误返回并且一切都很好?!

关于ios - SQL 错误 : Not an Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17253021/

相关文章:

database - 如何在 MySQL 5.7 中锁定用户帐户?

ios - 为什么我的模式在非用户换行符处停止?

ios - 是否可以在通话过程中从 iOS 应用程序向当前调用者发送短信?

objective-c - 为什么表格单元格不显示内容

iphone - "Image view"的位置和大小不明确

ios - 未知对象类型的zlib解压方法

MYSQL 多个表中具有最小值的不同值

javascript - RethinkDB - 多边形不接受顶点数组?

ios - 如何通过 iOS 6 中的 RESTkit 2.0 将 JSON 中的 Base64 图像导入核心数据二进制文件?

ios - 如何区分指纹和 TOUCH ID