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

本文是小编为大家收集整理的关于外部生成的SQLite数据库被Android'的SQLiteOpenHelper视为只读的。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个sqlite数据库,我使用sqlite jdbc连接器生成 .

然后我已将此数据库复制到我的Android手机的SD卡,并尝试使用Sqliteopenhelper打开它.我收到以下例外:

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浏览器添加它.

推荐答案

sqlite只会从应用程序数据库目录中打开数据库.

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

为了读写您的数据库,我的理解是您需要将数据库复制到此位置:

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();

为了执行此副本,您将首先在数据库文件的最终目标中打开一个数据库:

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

让我知道如果您有任何疑问

其他推荐答案

这刚刚发生在我身上.我写了一个在"生产"和设备上的程序,并在SQLite DB中具有数据.

我以SQL语句的形式备份此数据(通过ASQLiteManager).在仿真器上,我重新创建了这个db.

然后我推出了我的程序,它期待DB版本5.我的第一次访问程序是通过SQLiteopenhelper的子类中的GetReadableableAzease.

在那里,我收到了这个例外:无法从版本0到5升级只读数据库.

我通过替换getwritabledatabase替换打开呼叫来修复它.

现在,它变得奇怪,因为称为OnCreate的SQLite帮助程序,它包含了Create表格语句,而表格已经存在,因此它导致另一个错误.我不得不发表整个oncreate.

本文地址:https://www.itbaoku.cn/post/102726.html

问题描述

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

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

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 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

其他推荐答案

This just occurred to me. I wrote a program that is in 'production' and on a device and has data in a SQLite DB.

I backed up this data in the form of SQL statements (through aSQLiteManager). On the emulator, I recreated this db.

Then I launched my program, which was expecting a DB of version 5. My first access of the program was through getReadableDatabase in a subclass of SQLiteOpenHelper.

And there, I got this exception : can't upgrade a read-only database from version 0 to 5.

I fixed it by replacing the open call by getWritableDatabase.

Now, it gets weird, because the SQLite helper called onCreate, which contained CREATE TABLE statements, while the tables were already there, so it resulted in another error. I had to comment out the whole onCreate.