【问题标题】:SQL read data?SQL读取数据?
【发布时间】:2010-11-14 15:17:44
【问题描述】:

我使用下面的代码从 file.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 - (void)applicationDidFinishLaunching:(UIApplication *)application{

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

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

}

【问题讨论】:

  • 你确定 appDelegate.coffeeArray 不是 nil 吗?还有,sqlite3_open 报错了吗? sqlite3_prepare_v2 报错了吗?
  • 请查看已编辑的问题
  • 使用FMDB

标签: iphone sql objective-c sqlite


【解决方案1】:

执行是否会进入 while 子句?

除了已经提出的建议之外,还有一些可以尝试的方法:

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”子句中。

【讨论】:

  • 我按照你的所有步骤进行操作,而且我无法从我的数据库中看到任何提取的数据,我会将我的项目发送给任何人来解决这个问题,我真的对这个问题感到困惑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-24
  • 2013-05-18
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多