是否有可能获得只读到SQLite数据库访问在APK? [英] Is it possible to obtain read-only access to an sqlite database in an apk?

查看:126
本文介绍了是否有可能获得只读到SQLite数据库访问在APK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  如何将现有的数据库中的.apk文件?

时有可能获得只读到位于同一个APK SQLite数据库的访问?我不希望复制的数据库,如果有可能从内的apk访问它为只读目的

Is it possible to obtain read-only access to an sqlite database located with an apk? I don't want to duplicate the database if it is possible to access it for read-only purposes from within the apk.

推荐答案

我刚开始为Android开发,并惊讶地发现,捆绑静态数据库的不容易做到的。所以我做了唯一合理的事情:创建这不只是一个图书馆。

I've just started developing for Android, and was surprised to discover that bundling a static database is not easy to do. So I did the only reasonable thing: created a library which does just that.

Android的staticdb 是一个Android库允许只读包含在应用程序的<$ C SQLite数据库的访问$ C> .apk文件文件。这是适合于那些需要附带大量的静态数据(如离线维基百科阅读器)的应用程序。

android-staticdb is an Android library to allow read-only access to SQLite databases contained in an application's .apk file. This is ideal for apps which need to ship with a large quantity of static data (e.g. an offline Wikipedia reader).

它可以通过注册一个新的虚拟文件系统层与原生C code,它能够拦截的SQLite所做的文件系统调用的SQLite库。由于每个虚拟机的过程SQLite库中只有一个拷贝,我们实际上最终拦截提出的任何SQLite的调用的任意的在同一过程中的应用为我们!

It works by registering a new "virtual filesystem" layer with the SQLite library from native C code, which is able to intercept the filesystem calls made by SQLite. Because there is only one copy of the SQLite library per VM process, we actually end up intercepting any SQLite calls made by any application in the same process as us!

在正常操作中,我们的VFS层只是代理了所有调用的默认 VFS,它只是使用的open()和read()来访问常规的数据库文件。但 当一个特殊的文件名匹配的 *。APK!文件名模式遇到, 我们的VFS抓住控制。使用zlib的和minizip,它打开了 .apk文件文件 看起来里面数据库文件;然后,它会读取这个文件的组块来 满足任何阅读()从SQLite的请求。

In normal operation, our VFS layer simply proxies all calls to the "default" VFS, which just uses open() and read() to access regular database files. But when a special filename matching the *.apk!filename pattern is encountered, our VFS grabs control. Using zlib and minizip, it opens the .apk file and looks inside for the database file; it will then read chunks of this file to satisfy any read() requests from SQLite.

通过以这种方式这样,应用程序可以继续使用标准的 Android的数据库API。

By doing this in this manner, applications can continue to use the standard Android database APIs.

实例:

import android.database.sqlite.SQLiteDatabase;
import kiwidrew.staticdb.StaticDatabase;

public class BlahBlah extends Activity {
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SQLiteDatabase db = StaticDatabase.openDatabase(this, "assets/foobar.db");
    // Do normal database stuff with 'db'...
  }
}

您得到一个标准的SQLiteDatabase对象,唯一的限制是它不支持写入。 (很明显!)

You get back a standard SQLiteDatabase object, with the only restriction being that it doesn't support writing. (Obviously!)

请注意,这将失败,除非数据库存储在你的 .apk文件无COM pression。添加使用 AAPT -0 命令SQLite数据库或修改的build.xml 打发&LT; nocom preSS延长=DB/&GT; 标记的&LT; AAPT&GT; 标记

Note that this will fail unless the database is stored in your .apk without compression. Add the SQLite database using the aapt -0 command or modify your build.xml to pass the <nocompress extension="db" /> flag to the <aapt> tag...

我从字面上刚写完这一点,只是做了非常基本的测试为止。错误报告将AP preciated。

I've literally just finished writing this, and have only done very basic testing so far. Bug reports would be appreciated.

这篇关于是否有可能获得只读到SQLite数据库访问在APK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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