Android 如何从资产文件夹就地更新sqlite数据库?

Android 如何从资产文件夹就地更新sqlite数据库?,android,database,sqlite,Android,Database,Sqlite,我使用SQLItemManager创建了一个包含多个表的.sqlite文件。我将这个.sqlite文件放在我的项目的资产文件夹中,该文件夹发布在Google Play Store上 但现在我在表中做了一些更改:在现有表中添加表和修改列 现有用户如何获得更新的数据库?SQLiteOpenHelper类的onUpgrade方法是否有助于升级我的数据库?您可以使用类似的方法 在升级/更改数据库文件时,更改数据库\u版本值 public class Cls_DatabaseHelper extends

我使用SQLItemManager创建了一个包含多个表的.sqlite文件。我将这个.sqlite文件放在我的项目的资产文件夹中,该文件夹发布在Google Play Store上

但现在我在表中做了一些更改:在现有表中添加表和修改列


现有用户如何获得更新的数据库?SQLiteOpenHelper类的
onUpgrade
方法是否有助于升级我的数据库?

您可以使用类似的方法

在升级/更改数据库文件时,更改数据库\u版本值

public class Cls_DatabaseHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "";

    private static final int DATABASE_VERSION = 1;

    private static String DB_NAME = "mydatabase.db";

    public static SQLiteDatabase myDataBase;
    private static Cls_DatabaseHelper myDBHelper = null;

    private final Context myContext;



    /**
     * Constructor Takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     * 
     * @param context
     */
    private Cls_DatabaseHelper(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
        DB_PATH = myContext.getDatabasePath(DB_NAME).getPath();
        try {
            openDataBase();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Cls_DatabaseHelper getInstance(Context context) {
        if (myDBHelper == null) {
            myDBHelper = new Cls_DatabaseHelper(context.getApplicationContext());
        }

        return myDBHelper;
    }

    /**
     * Creates a empty database on the system and rewrites it with your own
     * database.
     * */
    public void createDataBase() throws IOException {

        boolean dbExist = checkDataBase();

        if (dbExist) {
            // do nothing - database already exist
        } else {

            // By calling this method and empty database will be created into
            // the default system path
            // of your application so we are gonna be able to overwrite that
            // database with our database.
            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

    /**
     * Check if the database already exist to avoid re-copying the file each
     * time you open the application.
     * 
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;

        try {
            String myPath = DB_PATH;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY
                            | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            int DB_EXIST_VERSION = PreferenceManager
                    .getDefaultSharedPreferences(myContext).getInt(
                            "DB_VERSION", 0);
            if (DATABASE_VERSION != DB_EXIST_VERSION) {
                checkDB = null;
            }

        } catch (SQLiteException e) {

            // database does't exist yet.

        }

        if (checkDB != null) {

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    /**
     * Copies your database from your local assets-folder to the just created
     * empty database in the system folder, from where it can be accessed and
     * handled. This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException {

        // Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
        PreferenceManager.getDefaultSharedPreferences(myContext).edit()
                .putInt("DB_VERSION", DATABASE_VERSION).commit();
    }

    public void openDataBase() throws SQLException, IOException {
        createDataBase();
        // Open the database
        String myPath = DB_PATH;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE
                        | SQLiteDatabase.NO_LOCALIZED_COLLATORS);

    }

    @Override
    public synchronized void close() {

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

        super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

    /*********************************************
     * Method to execute a SQL statemtn and return a custom adapter. Use only to
     * search Station Codes
     *********************************************/
    public Cursor executeSQLStatement(String SQLStatement) {

        Cursor c = null;
        try {
            if (myDataBase != null) {
                c = myDataBase.rawQuery(SQLStatement, new String[] {});
                if (c != null)
                    c.moveToFirst();

            }
        }

        catch (Exception e) {
            e.printStackTrace();
        }

        return c;
    }
    }

尽量避免段落问题。这样会减少“视图”&因此高质量的回答不必对预填充的数据库使用
SQLiteOpenHelper
。改用
SQLiteAssetHelper
。谢谢您的回复。现在,我如何将现有的数据库表记录复制到较新的数据库,因为复制数据库会将数据库从资产文件夹复制到内部存储?通过代码?你为什么要这样做?您只需替换assets文件夹中的mydatabase.db(或db文件的任何名称),然后更改数据库的版本值。如果你想在数据库文件中执行操作,SQLLITE浏览器是你想要的工具:-)如果我的答案是你想要的,请接受我的答案:-)但是我需要一些旧db版本的有用记录。所以我需要把它们复制到更新的数据库。这是一次性工作吗?然后使用SqLLITE浏览器工具进行复制。我的答案让你满意吗?那么请接受