简体   繁体   中英

FMDB not inserting into DB


I am trying to use FMDB to insert a few queries into the DB.
When I check the query for insert it is saying that it is inserting.
Later I can actually read the data from the Tables.
But what happens is that if I try to read the information from the DB, it comes back as null.
When I checked the DB in the Xcode Project, not seems to have updated.
It kind of confusing me. Any suggestion what the problem could be?

Your code is wrong. This is what I use to write:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];

FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
[db open];

[db executeUpdate:@"INSERT INTO foo (bar) VALUES (?)", bar];

[db close];

And to read:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];

    FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
    [db open];

    NSMutableArray *bar = [[[NSMutableArray alloc] init] autorelease];

    FMResultSet *s = [db executeQuery:@"select * from foo"];
    while ([s next]) {

        [bar addObject:[s stringForColumn:@"bar"]];

    }

    NSLog(@"%@", bar);

    [db close];

Also checkout where I located the db. I think doing your way will work on simulator but not on the device because of sandboxing and all.

Good luck!

EDIT:

Put this in your app delegate:

- (void)createEditableCopyOfDatabaseIfNeeded {


    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

And run that method from application:didFinishLaunchingWithOptions with:

[self performSelector:@selector(createEditableCopyOfDatabaseIfNeeded) withObject:nil];

Be sure that you put your database in your projecttree first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM