java - 数据库版本 SQLite Android

标签 java android database sqlite android-sqlite

我正在学习Android。我有一个带有 SQLite 数据库的 Quote 应用程序。我需要为该应用程序提供数据库版本,以便每当我更新应用程序时,用户数据库也可以得到更新。我需要对此进行哪些更改?

我的数据库助手/处理程序类如下所示。

public class DataBaseHandler extends SQLiteOpenHelper {
    private static String DB_PATH;
    private static String DB_NAME = "SuccessQuotesNew";
    private SQLiteDatabase myDataBase;
    private final Context myContext;
    public DataBaseHandler(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
        DB_PATH = context.getDatabasePath(DB_NAME).toString();
        Log.e("path", DB_PATH);
    }

    public void createDataBase() throws IOException {

        boolean dbExist = checkDataBase();

        if (dbExist) {

        } else {
            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

    // ==============================================================================

    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;

        try {
            String myPath = DB_PATH;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {

        }

        if (checkDB != null) {

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException {

        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH;
        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();

    }

    // ==============================================================================

    public void openDataBase() throws SQLException {

        // Open the database
        String myPath = DB_PATH;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    // ==============================================================================

    @Override
    public synchronized void close() {

        if (myDataBase != null)
            myDataBase.close();

        super.close();

    }

    // ==============================================================================

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    // ==============================================================================

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


}

最佳答案

构造函数的最后一个参数是数据库的版本号。 super(context, DB_NAME, null, 1); 更改该数字并检查更新方法是否会被调用,因此当您想要更新数据库时更改它,一旦您创建了该数字的对象,应用程序就会调用更新方法。

请在此处查看引用 - Android SQLite database and app update

关于java - 数据库版本 SQLite Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35532689/

相关文章:

java - 使用大量数据创建android应用程序数据库

java - android java如何从右侧在新行中追加文本

java - solr.extraction.ExtractingRequestHandler ClassNotFoundException

java - Java switch 语句中需要的常量表达式

java - 按下主页按钮后如何在Android中播放mediaPlayer并在收到电话时恢复播放视频?

android - 如何在播放(说出)来电者姓名然后电话再次响铃时停止电话响铃

database - 由于这种模式,在同一个结构中使用事务和简单的数据库连接我该怎么办?

android - 为什么我的 mysql 数据库不接受重音符号?

database - 无法使子查询工作

java - 在位图上绘制固定位置的文本