FMDB 未插入数据库

发布于 2024-11-29 04:24:49 字数 180 浏览 1 评论 0原文


我正在尝试使用 FMDB 将一些查询插入到数据库中。
当我检查插入查询时,它说它正在插入。
后来我实际上可以从表中读取数据。
但发生的情况是,如果我尝试从数据库读取信息,它会返回空值。
当我检查 Xcode 项目中的数据库时,似乎没有更新。
这让我有点困惑。有什么建议可能是什么问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漆黑的白昼 2024-12-06 04:24:49

你的代码是错误的。这就是我用来写:

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

和读:

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

还要检查我定位数据库的位置。我认为按照您的方式进行操作可以在模拟器上运行,但由于沙箱等原因而无法在设备上运行。

祝你好运!

编辑:

将其放入您的应用程序委托中:

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

并从 application:didFinishLaunchingWithOptions 运行该方法:

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

确保首先将数据库放入项目树中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文