Iphone 无法从SQLite数据库检索和查看结果

Iphone 无法从SQLite数据库检索和查看结果,iphone,objective-c,database,sqlite,Iphone,Objective C,Database,Sqlite,我正在努力培养作为iPhone开发人员的技能,目前我正在使用SQLite数据库。我在GroceryListAppDelegate.m类中创建了一个SQLite表,如下所示: - (void)applicationDidFinishLaunching:(UIApplication *)application { self.database = [[[ISDatabase alloc] initWithFileName:@"TestDB.sqlite"] autorelease];

我正在努力培养作为iPhone开发人员的技能,目前我正在使用SQLite数据库。我在GroceryListAppDelegate.m类中创建了一个SQLite表,如下所示:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    self.database = [[[ISDatabase alloc] initWithFileName:@"TestDB.sqlite"] autorelease];

if(![[database tableNames] containsObject:@"GroceryItem"]) {

    [database executeSql:@"create table GroceryItem(primaryKey integer primary key autoincrement, name text NOT NULL, number integer NOT NULL)"];
    [database executeSql:@"insert into GroceryItem (name, number) values ('apples', 5)"];
    [database executeSql:@"insert into GroceryItem (name, number) values ('oranges', 3)"];

}

[window addSubview:navigationController.view];
[window makeKeyAndVisible];

}
我在我的RootViewController.m类中进行sql调用:

- (void)viewDidLoad {
[super viewDidLoad];

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

self.results = [appDelegate.database executeSqlWithParameters:@"SELECT * from GroceryItem where number < ?", [NSNumber numberWithInt:6], nil];

}
当我构建和运行我的代码时,我没有得到任何结果,而事实上,如果给定我的SQL调用,我的列表中应该有苹果和桔子。但是,当我将sql代码修改为以下内容时:

self.results = [appDelegate.database executeSql:@"SELECT * from GroceryItem"];
它调用不同的方法:executeSql:

- (NSArray *) executeSql: (NSString *)sql {

return [self executeSql:sql withParameters: nil];

}

在这种情况下,我最终得到的结果是:苹果和桔子。为什么会这样?我做错了什么?为什么我从一个SQL调用中得到结果,而不是从另一个SQL调用中得到结果?

如果您从模拟器或设备中提取.sqlite文件,并使用sqlite3在命令行中打开它,查询是否有效?这将使您能够单独测试数据库创建代码和查询代码。

非常感谢您的帮助,但我找到了解决方案。问题出在我的bindArguments函数中:

- (void) bindArguments: (NSArray *) arguments toStatement: (sqlite3_stmt *) statement queryInfo: (NSDictionary *) queryInfo {
就在我有数字绑定语句的地方:

else if ([argument isKindOfClass:[NSNumber class]])
        {
            sqlite3_bind_double(statement, -1, [argument doubleValue]);
        }

我需要把-1改成I。这个变量告诉SQL我将在语句中替换哪个变量。所以在这种情况下,我们只有一个变量,表示为?而且应该用5代替。

非常感谢您的回复。但是如何从模拟器中提取.sqlite文件,并使用sqlite3在命令行上打开它呢?再次感谢你的帮助。
else if ([argument isKindOfClass:[NSNumber class]])
        {
            sqlite3_bind_double(statement, -1, [argument doubleValue]);
        }