iOS/SQLite,使用 guid 有效更新大量记录?

标签 ios sql iphone sqlite

我正在尝试找出向新字段添加(不同的)guid 的最快方法,其中可能有数千条记录。

我很确定 SQLite 不提供创建 guid 的函数。所以我需要以某种方式为每一行创建这个。

我提出的想法很有效。

  1. 只需执行字段为空且限制为 1 的更新查询。显然,即使在事务内,这也会很慢。

  2. 将 cte(公共(public)表扩展)与临时表一起使用,我在其中构建包括 guid、更新/连接在内的值。然而,我似乎无法让临时表在 iOS 中工作,我不断收到一个名为乱序错误的库。

  3. 将内联子查询与更新查询结合使用。但是我不知道如何识别需要 guid 的行,除了它们是空白的。我需要使用递归,我相信这又需要 cte?

编辑:这是我的解决方案,归功于@CL。对于想法...

void newguid(sqlite3_context *context, int argc, sqlite3_value **argv)
{
    if (argc != 1 || sqlite3_value_type(argv[0]) != SQLITE_TEXT) {
        sqlite3_result_null(context);
        return;
    }

    @autoreleasepool {
        CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
        NSString *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
        CFRelease(uuid);

        sqlite3_result_text(context, [uuidString UTF8String], -1, SQLITE_TRANSIENT);
    }
}

- (void)createSqlGuidFunction:(sqlite3*)db
{
    if (sqlite3_create_function_v2(db, "newguid", 1, SQLITE_ANY, NULL, &newguid, NULL, NULL, NULL) != SQLITE_OK)
    {
        NSLog(@"%s: sqlite3_create_function_v2 error: %s", __FUNCTION__, sqlite3_errmsg(db));
    }
}

最佳答案

documentation

applications can generate globally unique identifiers using this function together with hex() and/or lower() like this:

hex(randomblob(16))

lower(hex(randomblob(16)))

但是如果你想使用不同的机制来生成GUID,你可以register your own function .

关于iOS/SQLite,使用 guid 有效更新大量记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45342890/

相关文章:

ios - 如何在同一个iOS项目中使用多个Icomoon字体?

ios - 应该显式调用 willMoveToParentViewController :(UIViewController *)parent & didMoveToParentViewController:(UIViewController *)parent?

mysql - 从 EXCEL 连接到 SQL 数据库

java - 使用映射到一个表的实体

iphone - 试图在 UIPanGestureRecognizer 上移动 UIImageView

ios - 选择搜索栏时,导航栏下的容器 View 没有向上移动

ios - CloudKit 订阅和 UNNotificationServiceExtension

mysql - 是否可以使用 SQL 将可执行代码存储到表记录中

iPhone 服务器客户端应用程序

iphone - 将 Int 从 subview 返回到 ViewController