-1

请帮忙。在代码中更改数据库版本时,什么都没有发生 - 应用程序中的数据仍然来自旧数据库。

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

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

先感谢您

4

1 回答 1

0

你如何使用数据库助手?

我有一个和你类似的设置 - 没有create_db.

当我在 Helper 中更改版本时,我在调用 helper 时也需要更改版本

dbHandler = new ButtonDBHandler(this, null, null, 2);
于 2019-03-25T10:11:11.260 回答