2013-06-22 51 views
0

更多的SQLite问题。所以我的界面如下(这是所有在.M):SQL错误:不是错误

@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收益为: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模拟器Documents目录已经验证,以及查询我试图执行它的作品。为什么我会得到这个错误?

+0

只是一个想法,如果你正在使用大量的SQLite交互,然后去[FMDB框架](https://github.com/ccgus/fmdb)。它非常轻巧,简单而干净。 – icodebuster

+0

在我阅读更多内容之前,它是否允许SQL查询?我正在从Android移植项目,并且查询已写入。 – muttley91

+0

100%它将工作..和它的只是Objective-C SQLite包装。所以没有问题。 – icodebuster

回答

0

事实证明这个问题非常简单。我试图将值分配给(然后将它们拉出)的对象未初始化。这是我的一个非常愚蠢的疏忽,但我仍然是新的Objective C.

此外,我建议icodebuster的建议在iOS中使用FMDB for SQLite。它清理了我的一些SQLite混乱,使它更好用。

3

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

+0

那么检查什么会更好?我问这个问题的原因是因为我不会注意到这个错误,除了从我的查询得到不正确的结果导致我检查我的日志输出,这是我发现这个错误的地方。 – muttley91

+0

@rar你对'sqlite2_open_v2'的'if'检查很好。问题是'else'。数据库正确打开时,就会到达'else'。所以没有错误。日志语句正在显示它应该是什么 - 没有错误。你一直认为在没有问题的情况下会陷入困境。 – rmaddy

+0

不见了,我完全同意@rmaddy--似乎没有错误,如果没问题 - 你只是把自己搞糊涂了 – Mario