2010-11-13 49 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