如何在Objective-c中使用sqlite3 PRAGMA user_version? [英] How do I use sqlite3 PRAGMA user_version in Objective-c?

查看:615
本文介绍了如何在Objective-c中使用sqlite3 PRAGMA user_version?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查sqlite DB的user_version。我有一个管理工具来破坏版本,但我不理解pragma语句的语法。我期望在if语句中测试该值。有人可以提供代码示例吗?当我在我的objective-c代码中嵌入pragma语句时,编译器会抛出一个错误。

I am trying to check the user_version of the sqlite DB. I have an admin tool to bump the version, but I don't understand the syntax of the pragma statement. I am expecting to test the value in an if-statement. Can someone provide a code sample? When I embed the pragma statement in my objective-c code, the compiler throws an error.

推荐答案

我把它想出来了来自newtover的灵感,深入研究FMDB并重新阅读 sqlite3 文档(在我看来,它仍然非常模糊)。当我对需要迁移的模式进行显着更改时,此代码返回我在管理工具中遇到的值。

I figured it out with the inspiration from newtover, digging into FMDB and re-reading the sqlite3 documentation (it is still very vague in my opinion). This code returns the value that I bumped in the admin tool when I make notable changes to the schema that require migration.

-(int)queryUserVersion: (sqlite3*) db {
    // get current database version of schema
    static sqlite3_stmt *stmt_version;
    int databaseVersion;

    if(sqlite3_prepare_v2(db, "PRAGMA user_version;", -1, &stmt_version, NULL) == SQLITE_OK) {
        while(sqlite3_step(stmt_version) == SQLITE_ROW) {
            databaseVersion = sqlite3_column_int(stmt_version, 0);
            NSLog(@"%s: version %d", __FUNCTION__, databaseVersion);
        }
        NSLog(@"%s: the databaseVersion is: %d", __FUNCTION__, databaseVersion);
    } else {
        NSLog(@"%s: ERROR Preparing: , %s", __FUNCTION__, sqlite3_errmsg(db) );
    }
    sqlite3_finalize(stmt_version);

    return databaseVersion;
}

我有一个类似的方法,用于修改sql语句的模式版本到PRAGMA schema_version;

I have a similar method for the schema version where the sql-statement is changed to "PRAGMA schema_version;"

这篇关于如何在Objective-c中使用sqlite3 PRAGMA user_version?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆