应用程序中未更新数据库数据

时间:2019-03-25 09:37:51

标签: java android sqlite

请帮助。在代码中更改数据库版本时,什么也没做-应用程序中的数据仍保留在旧数据库中。

问题出在哪里? 我对数据库进行了更改。我更改了数据库版本号,但应用程序中没有任何变化。

class DatabaseHelper extends SQLiteOpenHelper {
    private static String DB_PATH; 
    private static String DB_NAME = "bd.db";
    private static final int SCHEMA = 28; //version
    static final String TABLE = "categories_second";
    private Context myContext;

    DatabaseHelper(Context context) {
        super(context, DB_NAME, null, SCHEMA);
        this.myContext = context;
        DB_PATH = context.getFilesDir().getPath() + DB_NAME;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "+TABLE+"(_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,name TEXT NOT NULL,items TEXT,categories INTEGER DEFAULT NULL,pictures TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE);
            onCreate(db);            
    }

    void create_db() {
        InputStream myInput = null;
        OutputStream myOutput = null;
        try {
            File file = new File(DB_PATH);
            if (!file.exists()) {
                this.getReadableDatabase();

                myInput = myContext.getAssets().open(DB_NAME);

                String outFileName = DB_PATH;
                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();
            }
        } catch (IOException ex) {
            Log.d("DatabaseHelper", ex.getMessage());
        }
    }

    public SQLiteDatabase open() throws SQLException {
        return SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
    }
}

提前谢谢

1 个答案:

答案 0 :(得分:0)

您如何使用DatabaseHelper?

我有与您类似的设置-没有create_db

当我在Helper中更改版本时,我也需要在致电Helper时更改版本

dbHandler = new ButtonDBHandler(this, null, null, 2);