簡體   English   中英

無法從SQLite數據庫檢索和查看結果

[英]Unable to retrieve and view results from a SQLite database

我正在嘗試提高自己作為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調用獲得結果?

如果您從Simulator或設備中提取.sqlite文件,並使用sqlite3在命令行中將其打開,該查詢有效嗎? 這將使您能夠分別測試數據庫創建代碼和查詢代碼。

非常感謝您的所有幫助,但是我找到了解決方案。 問題出在我的bindArguments()函數中:

- (void) bindArguments: (NSArray *) arguments toStatement: (sqlite3_stmt *) statement queryInfo: (NSDictionary *) queryInfo {

在我有數字的bind語句的地方:

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

我需要將-1更改為i。 該變量告訴SQL我將在語句中使用哪個變量。 因此,在這種情況下,我們只有一個由?表示的變量。 並應以5代替。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM