Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Android中放置在资产文件夹中的数据库文件中读取版本号_Android_Database_Sqlite_Kotlin_Android Sqlite - Fatal编程技术网

如何从Android中放置在资产文件夹中的数据库文件中读取版本号

如何从Android中放置在资产文件夹中的数据库文件中读取版本号,android,database,sqlite,kotlin,android-sqlite,Android,Database,Sqlite,Kotlin,Android Sqlite,我在asset文件夹中的一个包databases下有一个SQLite文件,我手动将其放置如下:src->main->asset->databases->q-db q-db是我的SQLite文件 现在,我想读取它的版本号,并想匹配这个版本是否放在我自己的应用程序中,如果没有,则需要复制到我的数据库中,否则将跳过。当然,这是它第一次从文件中复制,下一次每当应用打开时,它都会检查版本号 我在谷歌上搜索了很多,但无法获得文件的版本号,我得到的是我的SQLite版本号,在我的案例中我不想要。我们将非常感谢

我在asset文件夹中的一个包databases下有一个SQLite文件,我手动将其放置如下:src->main->asset->databases->q-db q-db是我的SQLite文件

现在,我想读取它的版本号,并想匹配这个版本是否放在我自己的应用程序中,如果没有,则需要复制到我的数据库中,否则将跳过。当然,这是它第一次从文件中复制,下一次每当应用打开时,它都会检查版本号

我在谷歌上搜索了很多,但无法获得文件的版本号,我得到的是我的SQLite版本号,在我的案例中我不想要。我们将非常感谢您的支持

供参考:我的以下内容与此类似:


没有单个版本号,而版本号可以是多个值

据猜测,您谈论的是Android SDK SQLiteOpenHelper使用的用户_版本

还有application_id,它与user_版本一样可以用作用户变量

您已经遇到了SQLite_版本,因此可以打折

还有data_版本,这不太可能是版本号,因为它用于指示是否实时修改了数据库文件

还有schema_版本,您可能不想将其用作警告:误用此pragma可能会导致数据库损坏

用户版本 如前所述,您可能正在谈论用户版本。首先要注意的是,它是一个用户控制的变量/字段,可供自定义使用。SQlite不使用或更改用户_版本,但允许对其进行更改和使用

此外,诸如DB Browser、Navicat等SQLite管理器不会自动更改版本号。因此,在将数据库文件复制到assets文件夹之前,您必须有意更改user_版本以使其可用,注意如果这样做,并且您使用的是SQLiteOpenHelper的子类,则可能会调用onUpgrade和onDowngrade方法

如果未专门更改用户_版本,并且数据库仅由SQLite管理器工具访问,则其用户_版本将为0。如果数据库文件是通过从使用SQLiteOpenHelper子类的Android应用程序复制数据库文件打开的,则根据用作SQLiteOpenHelper构造函数的第四个参数的最后一个值,它将具有1或更多的用户_版本。当然,如果用户版本是通过编程方式更改的,那么如果文件被复制到SQlite管理器工具中,这种更改也会反映出来

在复制文件之前,通常会在SQlite管理器工具中将用户版本更改为适当的值

您可以使用SQL PRAGMA user_version=5更改用户_版本; 您可以使用PRAGMA user\u version或从PRAGMA\u user\u version中选择*来检索用户\u版本

如果需要在打开数据库之前检查版本,则可以读取偏移量60处的4个字节,并将4个字节转换为整数,以对照另一个值检查用户的版本。否则,您可能必须从assets文件夹中复制文件(可能使用不同的名称),将其作为SQLiteDatabase打开,并使用上面的SQL检索user_版本,然后对照其他值检查它,关闭数据库文件。如果不需要,则删除该文件,否则删除先前的数据库文件,然后重命名复制的文件

实例 下面是一个工作示例,注意到我很少使用Kotlin,这是从java使用AS studio转换而来的

这使用了一个类,即SQLAssetVersionCheck,它从文件中提取版本号,而不是将文件作为SQLiteDatabase打开

SQLAssetVersionCheck.kt:- 结果日志:- 然后在Navicat中关闭连接后将文件复制到assets文件夹,这样我就有两个testdb文件,结果是:-

2019-02-19 13:50:09.874 19253-19253/com.example.so54741423assetdbversioning D/SQLAVC: Looking for Asset testdb
2019-02-19 13:50:09.874 19253-19253/com.example.so54741423assetdbversioning D/ASSETVERSIONCHECK: The result of the version check was - The Asset was located and the version number being 101 was higher than the version to be checked which was 100
2019-02-19 13:50:09.874 19253-19253/com.example.so54741423assetdbversioning D/SQLAVC: Looking for Asset databases/testdb
2019-02-19 13:50:09.874 19253-19253/com.example.so54741423assetdbversioning D/ASSETVERSIONCHECK: The result of the version check was - The Asset was located and the version number being 5 was lower than the version to be checked which was 100
i、 e.新文件的用户_版本为101,因此第一个文件会找到该文件,第二个文件会像以前一样找到文件用户_版本5。
class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val db_version_to_check_against = 100

        var mAVC1 = SQLAssetVersionCheck(this, "testdb", 100)

        var result = ""
        when (mAVC1.result) {
            SQLAssetVersionCheck.ASSETIOERROR -> result = "IO ERROR detected - check the Log"
            SQLAssetVersionCheck.ASSETNOTFOUND -> result = "The Asset, for Database " + mAVC1.databaseName + " was not located at  " + mAVC1.assetPath
            SQLAssetVersionCheck.ASSETVERSIONHIGH -> result = "The Asset was located and the version number being " +
                    mAVC1.databaseVersion.toString() +
                    " was higher than the version to be checked which was " +
                    db_version_to_check_against.toString()
            SQLAssetVersionCheck.ASSETVERSIONLOW -> result = "The Asset was located and the version number being " +
                    mAVC1.databaseVersion.toString() +
                    " was lower than the version to be checked which was " +
                    db_version_to_check_against.toString()
            SQLAssetVersionCheck.ASSETVERSIONMATCH -> result = "The Asset version and the version to be check ed are the same."
        }
        Log.d("ASSETVERSIONCHECK", "The result of the version check was - $result")

        var mAVC2 = SQLAssetVersionCheck(this, "testdb", arrayOf("databases"), db_version_to_check_against)
        result = ""
        when (mAVC2.result) {
            SQLAssetVersionCheck.ASSETIOERROR -> result = "IO ERROR detected - check the Log"
            SQLAssetVersionCheck.ASSETNOTFOUND -> result = "The Asset, for Database " + mAVC2.databaseName + " was not located at  " + mAVC2.assetPath
            SQLAssetVersionCheck.ASSETVERSIONHIGH -> result = "The Asset was located and the version number being " +
                    mAVC2.databaseVersion.toString() +
                    " was higher than the version to be checked which was " +
                    db_version_to_check_against.toString()
            SQLAssetVersionCheck.ASSETVERSIONLOW -> result = "The Asset was located and the version number being " +
                    mAVC2.databaseVersion.toString() +
                    " was lower than the version to be checked which was " +
                    db_version_to_check_against.toString()
            SQLAssetVersionCheck.ASSETVERSIONMATCH -> result = "The Asset version and the version to be check ed are the same."
        }
        Log.d("ASSETVERSIONCHECK", "The result of the version check was - $result")
    }
}
2019-02-19 13:11:34.473 19058-19058/com.example.so54741423assetdbversioning D/SQLAVC: Looking for Asset testdb
2019-02-19 13:11:34.473 19058-19058/com.example.so54741423assetdbversioning W/System.err: java.io.FileNotFoundException: testdb
2019-02-19 13:11:34.474 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.content.res.AssetManager.nativeOpenAsset(Native Method)
2019-02-19 13:11:34.474 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:744)
2019-02-19 13:11:34.474 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:721)
2019-02-19 13:11:34.474 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at com.example.so54741423assetdbversioning.SQLAssetVersionCheck.<init>(SQLAssetVersionCheck.kt:31)
2019-02-19 13:11:34.474 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at com.example.so54741423assetdbversioning.SQLAssetVersionCheck.<init>(SQLAssetVersionCheck.kt:67)
2019-02-19 13:11:34.474 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at com.example.so54741423assetdbversioning.MainActivity.onCreate(MainActivity.kt:17)
2019-02-19 13:11:34.474 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.app.Activity.performCreate(Activity.java:7136)
2019-02-19 13:11:34.475 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.app.Activity.performCreate(Activity.java:7127)
2019-02-19 13:11:34.475 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
2019-02-19 13:11:34.475 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
2019-02-19 13:11:34.475 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
2019-02-19 13:11:34.475 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
2019-02-19 13:11:34.475 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
2019-02-19 13:11:34.475 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
2019-02-19 13:11:34.475 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
2019-02-19 13:11:34.475 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2019-02-19 13:11:34.475 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.os.Looper.loop(Looper.java:193)
2019-02-19 13:11:34.476 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6669)
2019-02-19 13:11:34.476 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-02-19 13:11:34.476 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2019-02-19 13:11:34.476 19058-19058/com.example.so54741423assetdbversioning W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-02-19 13:11:34.476 19058-19058/com.example.so54741423assetdbversioning D/ASSETVERSIONCHECK: The result of the version check was - The Asset, for Database testdb was not located at  testdb




2019-02-19 13:11:34.476 19058-19058/com.example.so54741423assetdbversioning D/SQLAVC: Looking for Asset databases/testdb
2019-02-19 13:11:34.477 19058-19058/com.example.so54741423assetdbversioning D/ASSETVERSIONCHECK: The result of the version check was - The Asset was located and the version number being 5 was lower than the version to be checked which was 100
PRAGMA user_version = 101;
2019-02-19 13:50:09.874 19253-19253/com.example.so54741423assetdbversioning D/SQLAVC: Looking for Asset testdb
2019-02-19 13:50:09.874 19253-19253/com.example.so54741423assetdbversioning D/ASSETVERSIONCHECK: The result of the version check was - The Asset was located and the version number being 101 was higher than the version to be checked which was 100
2019-02-19 13:50:09.874 19253-19253/com.example.so54741423assetdbversioning D/SQLAVC: Looking for Asset databases/testdb
2019-02-19 13:50:09.874 19253-19253/com.example.so54741423assetdbversioning D/ASSETVERSIONCHECK: The result of the version check was - The Asset was located and the version number being 5 was lower than the version to be checked which was 100