SQLiteException:无法将只读数据库从版本0升级到1

时间:2016-12-14 06:59:23

标签: android android-sqlite

在某些手机中我得到这个例外,不知道我在这方面做错了什么以及如何解决这个问题。请帮我解决这个问题。

下面的

是堆栈跟踪:

android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 1: 
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:244)
    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)

PFB我用于SQLliteDB的类

public class DatabaseHandler extends SQLiteOpenHelper {
    public DatabaseHandler(Context context) {
        super(context, Environment.getExternalStorageDirectory()+""
                + File.separator + "Mineappdatabase"
                + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
    }

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "content";
    private static final String TABLE = "content";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String CREATE_CONTENT_TABLE = "CREATE TABLE " + TABLE + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT"+ ")";
        sqLiteDatabase.execSQL(CREATE_CONTENT_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE);
        // Create tables again
        onCreate(sqLiteDatabase);
    }

    public void saveContent(String id, String content){

        Cursor cursor =getData(Integer.parseInt(id));
        if(cursor != null && cursor.getCount() >0){
            System.out.println("updateContent done");
            updateContent(id,content);
        }else {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(KEY_ID, id);
            contentValues.put(KEY_NAME, content);
            db.insert(TABLE, null, contentValues);
            System.out.println("saveContent done");
        }
    }

    public void updateContent(String id, String content){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_ID,id);
        contentValues.put(KEY_NAME,content);
        db.update(TABLE, contentValues, "id = ? ", new String[] { id} );
        System.out.println("updateContent done");

    }

    public Integer deleteContent (Integer id) {
        SQLiteDatabase db = this.getWritableDatabase();
        System.out.println("delete content done");
        return db.delete(TABLE,
                "id = ? ",
                new String[] { Integer.toString(id) });

    }

    public Cursor getData(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from content where id="+id+"", null );
        System.out.println("get content done");

        return res;
    }




    @Override
    protected void finalize() throws Throwable {
        this.close();
        super.finalize();
    }
}

0 个答案:

没有答案