package com.myandroidapp.app.util;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 5;
private static final String DATABASE_NAME = "px.db";
private static final String TABLE_IDENTITY = "identity";
private static final String TABLE_DEFAULT_IDENTITY = "default_identity";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.e("TAG", "onCreate");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e("TAG", "upGrade");
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e("TAG", "downGrade");
}
public Cursor getIdentityCard() {
String query = "SELECT * FROM " + TABLE_IDENTITY;
return querryDb(query);
}
public Cursor getDefaultIdentityCard() {
String query = "SELECT * FROM " + TABLE_DEFAULT_IDENTITY;
return querryDb(query);
}
private Cursor querryDb(String query) {
SQLiteDatabase db = this.getReadableDatabase(); /*Error Comes here even if i use this.getWritableDatabase();*/
Cursor cursor = db.rawQuery(query, null);
return cursor;
}
}
04-28 11:13:32.934: E/AndroidRuntime(1325): FATAL EXCEPTION: Thread-10
04-28 11:13:32.934: E/AndroidRuntime(1325): android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 8 to 5: /data/data/com.paytronix.client.android.app.marketplacegrill/databases/px.db
04-28 11:13:32.934: E/AndroidRuntime(1325): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:199)
04-28 11:13:32.934: E/AndroidRuntime(1325): at com.paytronix.client.android.app.marketplacegrill.util.DatabaseHandler.querryDb(DatabaseHandler.java:51)
04-28 11:13:32.934: E/AndroidRuntime(1325): at com.paytronix.client.android.app.marketplacegrill.util.DatabaseHandler.getIdentityCard(DatabaseHandler.java:39)
04-28 11:13:32.934: E/AndroidRuntime(1325): at com.paytronix.client.android.app.activity.Main$1.run(Main.java:26)
04-28 11:13:32.934: E/AndroidRuntime(1325): at java.lang.Thread.run(Thread.java:1019)
我正在获取Android SQLiteException: attempt to write a readonly database: BEGIN EXCLUSIVE when calling this.getWritableDatabase()
和android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 8 to 5 when calling this.getReadableDatabase()
场景:我已根据我的手机版2.3.6 GalaxyS2
获取了现有应用中的DB
文件并将其推送到我应用的数据库文件夹,因此访问{ {1}}文件无论我在pushed DB
中使用DATABASE_VERSION
,我都会收到相同的错误。
答案 0 :(得分:0)
请注意,每次更改数据库版本时,都必须重新创建数据库。
首先,您需要从Database文件夹中删除数据库,然后再次重新创建数据库。
请使用此代码
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + "Your table name");
// Create tables again
onCreate(db);
}
希望这对你有所帮助!! 如果它不起作用请告诉我,我会尽力帮助你。