2010-11-13 52 views
0

我用下面的代碼從file.sql中讀取數據沒有發生什麼問題?SQL讀取數據?

+ (void) getInitialDataToDisplay:(NSString *)dbPath { 

SQLAppDelegate *appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate]; 

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { 

    const char *sql = "select coffeeID, coffeeName from coffee"; 
    sqlite3_stmt *selectstmt; 
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) { 

     while(sqlite3_step(selectstmt) == SQLITE_ROW) { 

      NSInteger primaryKey = sqlite3_column_int(selectstmt, 0); 
      Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey]; 
      coffeeObj.CoffeeName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; 

      coffeeObj.isDirty = NO; 

      [appDelegate.coffeeArray addObject:coffeeObj]; 
      [coffeeObj release]; 
     } 
    } 
} 
    else 
     sqlite3_close(database); 
} 

appDelegate.m - (空)的applicationDidFinishLaunching:(UIApplication的*)應用程序{

[self copyDatabaseIfNeeded]; 
NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
self.coffeeArray = tempArray; 
[tempArray release]; 

[Coffee getInitialDataToDisplay:[self getDBPath]]; 
[window addSubview:[navigationController view]]; 
[window makeKeyAndVisible]; 

}

+1

你確定appDelegate.coffeeArray不是零嗎?另外,是sqlite3_open報告錯誤? sqlite3_prepare_v2是否報告錯誤? – 2010-11-13 19:29:36

+0

請參閱編輯的問題 – 2010-11-13 19:35:45

+1

使用[FMDB](http://github.com/ccgus/fmdb)。 – 2010-11-13 20:32:30

回答

0

執行是否曾經進入,而條款?

除了那些已經被提出,這裏有一些更多的東西嘗試:

1)啓動在模擬器上你的應用程序。使用SQLite數據庫瀏覽器(http://sqlitebrowser.sourceforge.net)打開數據庫文件並驗證該表是否存在。檢查表格和列名稱的確切拼寫。

2)檢查數據庫是否正在打開文件,並且dbPath是應用程序沙箱中數據庫文件的正確路徑。應該用NSDocumentsDirectory生成路徑:

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDirectory = [paths objectAtIndex:0]; 

3)確保你正確地清理所有舊的語句與sqlite_finalize並妥善關閉數據庫。 sqlite_close不應該在「else」子句中。

+0

我遵循所有你的步驟,我也看不到任何從我的數據庫中提取的數據我會發送我的項目到任何一個來解決這個問題我真的很困惑這個問題 – 2010-11-14 14:08:35

+0

btw如果我把一個斷點上while循環沒有發生 – 2010-11-14 14:59:23