我正在尝试建立我作为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];
}
我的executeSqlWithParameters()方法如下所示:
- (NSArray *) executeSql:(NSString *)sql withParameters: (NSArray *) parameters {
NSMutableDictionary *queryInfo = [NSMutableDictionary dictionary];
[queryInfo setObject:sql forKey:@"sql"];
if (parameters == nil) {
parameters = [NSArray array];
}
//we now add the parameters to queryInfo
[queryInfo setObject:parameters forKey:@"parameters"];
NSMutableArray *rows = [NSMutableArray array];
//log the parameters
if (logging) {
NSLog(@"SQL: %@ \n parameters: %@", sql, parameters);
}
sqlite3_stmt *statement = nil;
if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
[self bindArguments: parameters toStatement: statement queryInfo: queryInfo];
BOOL needsToFetchColumnTypesAndNames = YES;
NSArray *columnTypes = nil;
NSArray *columnNames = nil;
while (sqlite3_step(statement) == SQLITE_ROW) {
if (needsToFetchColumnTypesAndNames) {
columnTypes = [self columnTypesForStatement:statement];
columnNames = [self columnNamesForStatement:statement];
needsToFetchColumnTypesAndNames = NO;
}
id row = [[NSMutableDictionary alloc] init];
[self copyValuesFromStatement:statement toRow:row queryInfo:queryInfo columnTypes:columnTypes columnNames:columnNames];
[rows addObject:row];
[row release];
}
}
else {
sqlite3_finalize(statement);
[self raiseSqliteException:[[NSString stringWithFormat:@"failed to execute statement: '%@', parameters: '%@' with message: ", sql, parameters] stringByAppendingString:@"%S"]];
}
sqlite3_finalize(statement);
return rows;
}
当我构建并运行我的代码时,我没有得到任何结果,实际上,在我的SQL调用中,我应该在我的列表中得到苹果和橙子。但是,当我修改我的SQL代码时:
self.results = [appDelegate.database executeSql:@"SELECT * from GroceryItem"];
调用不同的方法:executeSql():
- (NSArray *) executeSql: (NSString *)sql {
return [self executeSql:sql withParameters: nil];
}
在这种情况下,我最终得到了结果:苹果和橘子。为什么是这样?我究竟做错了什么?为什么我从一个SQL调用获得结果,而不是从另一个SQL调用获得结果?
答案 0 :(得分:0)
如果从模拟器或设备中提取.sqlite文件并使用sqlite3
在命令行中打开它,查询是否有效?这将使您能够分别测试数据库创建代码和查询代码。
答案 1 :(得分:0)
非常感谢您的所有帮助,但我找到了解决方案。问题出在我的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代替。