SQL读取数据? [英] SQL read data?

查看:19
本文介绍了SQL读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码从文件中读取数据.sql 没有发生任何问题是不是有问题?

i used the code below to read the data from a file.sql nothing happen is there an issue ?

+ (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{

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];

}

推荐答案

执行是否曾经进入 while 子句?

Does execution ever enter the while clause?

除了已经提出的建议之外,还有一些值得尝试的事情:

In addition to what has already been suggested, here are a few more things to try:

1) 在模拟器上启动您的应用.使用 SQLite 数据库浏览器 ( http://sqlitebrowser.sourceforge.net ) 打开数据库文件并验证该表是否存在.检查表名和列名的准确拼写.

1) Launch your app on the simulator. Open the database file using SQLite Database Browser ( http://sqlitebrowser.sourceforge.net ) and verify the table exists. Check the exact spelling of the table and column names.

2) 检查数据库是否正在打开文件,以及 dbPath 是否是应用程序沙箱中数据库文件的正确路径.应该使用 NSDocumentsDirectory 生成路径:

2) Check that the database is opening the file and that dbPath is the correct path to the database file in the application's sandbox. The path should be generated using the NSDocumentsDirectory:

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

3) 确保使用 sqlite_finalize 正确清理所有旧语句并正确关闭数据库.sqlite_close 不应出现在else"子句中.

3) Make sure you clean up all old statements properly with sqlite_finalize and close the database properly. sqlite_close should not be in the "else" clause.

这篇关于SQL读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆