ios - 向sqlite数据库中插入数据

标签 ios objective-c database sqlite

我将数据插入到 sqlite 数据库中,我在控制台结果中看到了 NSLog(@"DONE"),但我没有在我的 sqlite 数据库文件中看到数据更新。请帮助我!!!

这里是我保存数据的代码:

- (void)saveData:(NSString *)_Id
{
    sqlite3_stmt    *statement;
    NSString *_databasePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"data.db"];

    const char *dbpath = [_databasePath UTF8String];

    if (sqlite3_open(dbpath, &db) == SQLITE_OK)
    {

        NSString *insertSQL = [NSString stringWithFormat:
                               @"INSERT INTO MyTable (id) VALUES (\"%@\")", _Id];

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(db, insert_stmt,
                           -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"DONE");
        } else {
            NSLog(@"Failed to add contact");
        }
        sqlite3_finalize(statement);
        sqlite3_close(db);
    }
}

最佳答案

您正在打开包中的数据库。该 bundle 在设备上是只读的。您必须将数据库复制到您的 Documents 文件夹(如果它不存在)并从那里打开它。

另请注意,您正在处理数据库的多个副本(项目中的一个, bundle 中的一个,现在是设备/模拟器的 Documents 文件夹中的一个)。确保您在正确的数据库中检查插入的记录(Documents 中的那个)

顺便说一句,您还应该检查 sqlite3_prepare_v2 是否返回了 SQLITE_OK,如果没有,则记录 sqlite3_errmsg

您还应该在 SQL 中使用 ? 占位符并使用 sqlite3_bind_text 绑定(bind)值(或者,如果它可能是 nilsqlite_bind_null ):

- (void)saveData:(NSString *)_Id
{
    sqlite3_stmt    *statement;
    NSString *bundlePath = [[[NSBundle mainBundle] resourcePath ] stringByAppendingPathComponent:@"data.db"];
    NSString *documentsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *documentsPath = [documentsFolder stringByAppendingPathComponent:@"data.db"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:documentsPath isDirectory:NO]) {
        NSError *error;
        if (![fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error]) {
            NSLog(@"database copy failed: %@", error);
        }
    }

    const char *dbpath = [documentsPath UTF8String];

    if (sqlite3_open(dbpath, &db) == SQLITE_OK) {
        const char *insertSql = "INSERT INTO MyTable (id) VALUES (?)";
        if (sqlite3_prepare_v2(db, insertSql, -1, &statement, NULL) != SQLITE_OK) {
            NSLog(@"prepare error: %s", sqlite3_errmsg(db));
        }

        // bind a value to the ? placeholder in the SQL

        if (_Id) {
            if (sqlite3_bind_text(statement, 1, [_Id UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK) {
                NSLog(@"bind text error: %s", sqlite3_errmsg(db));
            }
        } else {
            if (sqlite3_bind_null(statement, 1) != SQLITE_OK) {
                NSLog(@"bind null error: %s", sqlite3_errmsg(db));
            }
        }

        if (sqlite3_step(statement) == SQLITE_DONE) {
            NSLog(@"DONE");
        } else {
            NSLog(@"Failed to add contact: %s", sqlite3_errmsg(db));
        }

        sqlite3_finalize(statement);
        sqlite3_close(db);
    }
}

大多数人在专用的 openDatabase 方法中移动“如果文档中不存在数据库,则从包中复制它并从那里打开它”逻辑,但希望这能说明这个想法。

关于ios - 向sqlite数据库中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24497519/

相关文章:

ios - 有没有办法知道 UIViewController 何时呈现失败?

ios - 如何将百分比值精确舍入到小数点后两位双倍?

ios - 从子级调用父 View 上的 PerformSegueWithIdentifier 不会更改 View

objective-c - BDD 测试框架 - RSpec 和 Cucumber

iphone - 如何通过单击按钮更改数字时钟的时间格式?

android - 当我启动我的应用程序时,如何强制我的 SQLiteOpenHelper 中的 onCreate 运行?

sql-server - 将 SQL Server 列限制为可能值的列表

sql-server - 有没有办法在运行另一个数据库引擎时模拟特定的数据库引擎?

ios - Swift - UIButton 没有固定在屏幕底部

ios - 动画 UIButton 状态淡入淡出点击