2014-06-20 30 views
0

好吧,所以我有我的iPhone模擬器文件中的數據庫。我現在知道它在應用程序沙箱中。我的代碼中有些東西很時髦。所以我第一次拿到DB路徑:Sqlite數據庫沒有這樣的表存在

-(NSString *)getsynDbPath 
{ 
    NSString* dataBAse = [[NSBundle mainBundle] pathForResource:@"ddd"ofType:@"sqlite"]; 

    return dataBAse; 
} 

然後,我測試的路徑:

NSString *testData; 
    testData = [self getsynDbPath]; 

    NSFileManager * fileManager = [NSFileManager defaultManager]; 

    BOOL success = [fileManager fileExistsAtPath:testData]; 

    if (success) { 
     NSLog(@"Oh no! There was a big problem!"); 
    } else { 
     //Successfully opened 
     if(sqlite3_open([testData UTF8String], &db)==SQLITE_OK){ 

      NSLog(@"Raise the roof!"); 

      //Calling method to loop through columns 
      [self listOfCols]; 

     } 


    } 

然後我去一個自定義的方法,我遍歷數據庫中的列:

-(NSArray *)listOfCols{ 

    NSMutableArray *retval = [[[NSMutableArray alloc]init]autorelease]; 

    NSString *query = @"SELECT KEY_ID FROM CON_DETAIL"; 

    sqlite3_stmt *statement; 
     //Does not execute 
    if (sqlite3_prepare_v2(db, [query UTF8String], -1, &statement, nil)==SQLITE_OK) { 

     while (sqlite3_step(statement)==SQLITE_ROW) { 

      int key_id = sqlite3_column_int(statement, 0); 

      NSLog(@"Key ID: %d", key_id); 

      char *nameChars = (char *) sqlite3_column_text(statement, 1); 

      NSLog(@"chars %s", nameChars); 

      char *cityChars = (char *) sqlite3_column_text(statement, 2); 

      NSLog(@"chars %s", cityChars); 
     } 
    } 

    NSLog(@"%s Why '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(db), sqlite3_errcode(db)); 

    return retval; 

} 

所以這是我的問題。在我成功打開數據庫之後,爲什麼會出現一條日誌錯誤消息:no such table: CON_DETAIL?任何幫助表示讚賞。

+1

你有沒有在數據庫中命名爲「CON_DETAIL」的故事? – Impossible

+0

爲什麼不使用像FMDB這樣的包裝?它節省了時間... :) – bikram990

+0

@不可能OP正在資源包中查找文件,而不是Documents文件夾。 – rmaddy

回答

1

我認爲你必須複製你的數據庫在你的文檔目錄,然後嘗試獲取。用以下功能複製它。

-(void) dbconnect{ 

    self.databaseName = @」yourdbname.sqlite」; 

    // Get the path to the documents directory and append the databaseName 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    self.databasePath = [documentsDir stringByAppendingPathComponent:self.databaseName]; 

    // Execute the 「checkAndCreateDatabase」 function 

    [self checkAndCreateDatabase]; 
} 

-(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:self.databasePath error:nil]; 

    [fileManager release]; 

} 

注意:如果您未在應用的文檔目錄中獲取數據庫,請執行以下操作。 轉到:目標 - >「構建階段」 - >「複製束資源」然後在這裏添加該特定文件。

之後,調用你的「listOfCols」方法。

+0

self.databaseName名稱將是一個字符串的權利? – user3683815

+0

是的,它是nsstring對象。 –

相關問題