外部生成的SQLite数据库视为只读Android的SQLiteOpenHelper [英] Externally generated SQLite database considered read-only by Android's SQLiteOpenHelper

查看:144
本文介绍了外部生成的SQLite数据库视为只读Android的SQLiteOpenHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用发现这里 SQLite的JDBC连接器已经产生了SQLite数据库。

I have an SQLite database that I have generated using the SQLite JDBC connector found here.

我已经然后复制这个数据库,我的Andr​​oid手机的SD卡,并试图打开它,使用SQLiteOpenHelper。出现以下异常:

I have then copied this database to my Android phone's SD card, and attempted to open it, using an SQLiteOpenHelper. I get the following exceptions:

ERROR/SQLiteOpenHelper(7373): android.database.sqlite.SQLiteException: attempt to write a readonly database: BEGIN EXCLUSIVE;

ERROR/AndroidRuntime(7373): Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 1: /mnt/sdcard/4Trak/4Trak.db

这没有任何意义,我。为什么它是一个只读数据库?我能写它生成它摆在首位,并再次当我意识到我需要一个android_metadata表中,使用一个SQLite浏览器添加它。

This doesn't make any sense to me. Why is it a read-only database? I was able to write to it to generate it in the first place, and again when I realised I would need an android_metadata table, and added it using an SQLite browser.

推荐答案

sqlite的将只开放式数据库从您的应用程序的数据库目录。

Sqlite will only open databases from your applications database directory.

DB_PATH = "/data/data/"+ context.getPackageName() + "/databases/"; 

为了读取和写入数据库,我的理解是,你会需要你的数据库复制到以下位置:

In order to read and write your database, my understanding is that you will need to copy your database to this location:

InputStream myInput = myContext.getAssets().open(DB_NAME);      
String outFileName = DB_PATH + DB_NAME;     
OutputStream myOutput = new FileOutputStream(outFileName);

byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();

为了做到这一点的副本,你必须首先在你的数据库文件的最终目的地打开一个数据库:

In order to do this copy, you will first have to open a database at the eventual destination of your database file:

checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

让我知道,如果你有任何问题

Let me know if you have any questions

这篇关于外部生成的SQLite数据库视为只读Android的SQLiteOpenHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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