2013-06-22 49 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