2010-11-30 8 views
1

私はiPhone開発者としてのスキルを磨こうとしていますが、現在はSQLiteデータベースを使用しています。SQLiteデータベースから結果を取得して表示することができません

- (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()メソッドは次のようになります。

- (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呼び出しを行う:私のGroceryListAppDelegate.mクラスで次のように私はSQLiteのテーブルを作成しました:

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

} 

をこの場合、私は結果を得るに終わります。どうしてこれなの?私は間違って何をしていますか?なぜ私は1つのSQLコールから結果を得ているのですか?

答えて

0

ご協力いただきありがとうございますが、解決策が見つかりました。私は数字のバインド文を持っていた

- (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私に必要な問題は、私のbindArgumentsに()関数でした。この変数は、ステートメント内でどの変数を使用するかをSQLに指示します。したがって、この場合、1つの変数は? 5で置換する必要があります。

0

シミュレータまたはデバイスから.sqliteファイルを抽出し、コマンドラインでsqlite3を使用して開くと、クエリは機能しますか?これにより、データベース作成コードとクエリコードのテストを分離することができます。

+0

ご返信ありがとうございます。しかし、どのように私はシミュレータから.sqliteファイルを抽出し、sqlite3を使用してコマンドラインでそれを開きますか?あなたの助けをもう一度ありがとう。 – syedfa

関連する問題