【问题标题】:How to insert arrays into Sqlite database如何将数组插入 Sqlite 数据库
【发布时间】:2014-02-23 08:31:27
【问题描述】:

请告诉我如何将数组插入到 Sqlite 表中。我将 2 个数组作为参数传递。代码如下:

-(void)saveData:(NSMutableArray *)firstArray second:(NSMutableArray *)secondArray {

[self openDB];

NSString *createSQL = @"CREATE TABLE IF NOT EXISTS Table1"
“(EID INTEGER PRIMARY KEY, Array1 TEXT, Array2 TEXT);";
char *errorMsg;
if (sqlite3_exec (database, [createSQL UTF8String],
                  NULL, NULL, &errorMsg) != SQLITE_OK)
{
    sqlite3_close(database);
    NSAssert(0, @"Error creating table: %s", errorMsg);
}

    char *update = "INSERT OR REPLACE INTO Table1 (ID, Array1, Array2)"
    "VALUES (?, ?, ?);";
    errorMsg=NULL;
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(database, update, -1, &stmt, nil)
        == SQLITE_OK) {

        Please let me know how to insert the data.

        if (sqlite3_step(stmt) != SQLITE_DONE)
            NSAssert(0, @"Error updating table: %s", errorMsg);
        sqlite3_finalize(stmt);

}

}

【问题讨论】:

  • 你面临什么困难?为什么不将数组保存在沙盒文件夹内的 XML 文件中?
  • 我在 firstArray 和 secondArray 中有我的值,因为它作为参数传递。但我不知道如何将这些值插入表中。

标签: ios objective-c arrays sqlite nsmutablearray


【解决方案1】:

您正在做的事情有多个问题,这似乎源于对 SQLite 的不熟悉。首先,sqlite 不将数组理解为原始值,更不用说 NSMutableArrays。其次,您尝试将值作为参数传递,但您没有在任何地方设置参数。我建议this book 可以让您更好地了解 SQLite,或者通过 sqlite.org 上的在线教程

【讨论】:

    【解决方案2】:

    要插入数组,您必须使用一对多关系 所以你需要为 ex id 和 array1 和 array2 创建表 contans 作为复合主键

    【讨论】:

      【解决方案3】:

      JSON1 扩展为管理 (JSON) 数组提供了基本支持。这个需要 SQLite 版本 3.9.0 (2015-10-14)。更多详情,请参阅:

      https://www.sqlite.org/json1.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-22
        • 1970-01-01
        • 2018-11-24
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多