0

このチュートリアルを使用して、プロジェクトに FMDB を実装しています。しかし、私のコードは値を返しません。ここに値を取得するためのコードがあります。

-(NSMutableArray *) getCustomers
{
    NSMutableArray *customers = [[NSMutableArray alloc] init];
    FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
    [db open];
    FMResultSet *results = [db executeQuery:@"SELECT * FROM customers"];
    NSLog(@"results %i,%@,%@",[results intForColumn:@"id"],[results stringForColumn:@"firstname"],[results stringForColumn:@"lastname"]);
    while([results next]) 
    {
        Customer *customer = [[Customer alloc] init];
        customer.customerId = [results intForColumn:@"id"];
        customer.firstName = [results stringForColumn:@"firstname"];
        customer.lastName = [results stringForColumn:@"lastname"];
        [customers addObject:customer];
    }
    [db close];
    return customers; 
}

ドキュメントディレクトリに存在するデータベースよりもデータベースをチェックすると、ここにログがあります

CRUD[47363:f803] db /Users/pingdanehog/Library/Application Support/iPhone Simulator/5.1/Applications/D9F19E7D-A232-4C4A-8218-58BC136053A7/Documents/Customers.db

これが私のデータベースです ここに画像の説明を入力

そしてそれはデータを含んでいます

ここに画像の説明を入力

しかし、私のresulset値は常にnullであり、このコードが以前に実行されたのを助けてください。しかし、このプロジェクトに同じ名前で新しいデータベースを作成すると、停止しました。

ここに結果の値があります。

results 0,(null),(null)

appdelegate ファイルで DB を初期化しました

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    databaseName = @"Customers.db";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    databasePath = [documentDir stringByAppendingPathComponent:databaseName];

    [self createAndCheckDatabase];


    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

-(void) createAndCheckDatabase
{
    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:databasePath];

    if(success) return;

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
4

2 に答える 2

1

線を引くとどうなるか

NSLog(@"results %i,%@,%@",[results intForColumn:@"id"], ....

[results next] の後ろ、たとえば while ループの中?

FMDB のドキュメントには、「クエリで返された値にアクセスしようとする前に、常に -[FMResultSet next] を呼び出す必要があります。

于 2013-04-08T21:39:52.987 に答える
0

データベースをAppDelegateまたはFMDBを使用しているファイルのいずれかに初期化したことを確認してください。

はい、どこかで初期化した場合は、この SQL "SELECT * FROM customers" を実行してみてください。データベース自体の [SQL] タブ。データベースに値が含まれているかどうかを確認します。

Before that make sure you are running SQL in databse which resides into **Application Support**

プログラミングを楽しもう!

于 2013-04-04T13:02:12.597 に答える