更新应用程序,覆盖预先填充在android中的新数据库

时间:2013-09-13 12:23:26

标签: android database sqlite sqliteopenhelper

我有一个以预先填充的数据库开头的应用程序。我想更新应用程序,插入新表并复制预先填充的新数据库。 更新应用程序时,如果我将数据库的版本设置为2,则会创建新表,但不会复制新数据库。但是,如果我继续使用版本1,则应用程序将停止并显示错误。

数据库位于assets文件夹中,并在第一次使用此代码进行复制:

public class DataBaseHelper<E> extends OrmLiteSqliteOpenHelper {
    private static String DB_PATH = "/data/data/com.teste/databases/";
    private static String DB_NAME = "teste.db";
    private static int DB_VERSION = 1;
    private SQLiteDatabase myDataBase;
    Context context;

    public DataBaseHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
            this.context = context;
            try {
                    createDataBase();
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
    }

    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 + DB_NAME;
                    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 = context.getAssets().open(DB_NAME);
            String outFileName = DB_PATH + DB_NAME;
            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 {
            String myPath = DB_PATH + DB_NAME;
            myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                            SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource src) {
            try {
                    TableUtils.createTable(src, Table1.class);
                    TableUtils.createTable(src, Table2.class);
                    TableUtils.createTable(src, Table3.class);
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource src,
                    int oldVersion, int newVersion) {
            try {
                    TableUtils.dropTable(src, Table1.class, true);
                    TableUtils.dropTable(src, Table2.class, true);
                    TableUtils.dropTable(src, Table3.class, true);

                    onCreate(db, src);
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }

    @Override
    public synchronized void close() {
            if (myDataBase != null)
                    myDataBase.close();
            super.close();
    }
}

现在,我还有一个要插入的表,我正在使用此代码:

public class DataBaseHelper<E> extends OrmLiteSqliteOpenHelper {
private static String DB_PATH = "/data/data/com.teste/databases/";
private static String DB_NAME = "teste.db";
private static int DB_VERSION = 2;
private SQLiteDatabase myDataBase;
Context context;

public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.context = context;
        try {
                createDataBase();
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
}

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 + DB_NAME;
                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 = context.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        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 {
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READONLY);

}

@Override
public void onCreate(SQLiteDatabase db, ConnectionSource src) {
        try {
                TableUtils.createTable(src, Table1.class);
                TableUtils.createTable(src, Table2.class);
                TableUtils.createTable(src, Table3.class);
                TableUtils.createTable(src, Table4.class);
        } catch (Exception e) {
                e.printStackTrace();
        }
}

@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource src,
                int oldVersion, int newVersion) {
        try {
                TableUtils.dropTable(src, Table1.class, true);
                TableUtils.dropTable(src, Table2.class, true);
                TableUtils.dropTable(src, Table3.class, true);
                TableUtils.dropTable(src, Table4.class, true);
                onCreate(db, src);
        } catch (Exception e) {
                e.printStackTrace();
        }
}

@Override
public synchronized void close() {
        if (myDataBase != null)
                myDataBase.close();
        super.close();
}
}

编辑2:

我在onUpgrade中调用copyDatabase,并且不会复制数据库。参见:

TableUtils.createTable(src, Linhas.class);
TableUtils.createTable(src, Horarios.class);
TableUtils.createTable(src, Itinerarios.class);
TableUtils.createTable(src, Utils.class);
copyDataBase();

任何帮助?

编辑3:

我注意到,当我们使用该方法更改数据库升级版本并从资产文件夹复制数据库时,数据不会显示,因为此文件夹资产的数据库版本小于当前版本。有没有办法在预先填充新数据库时进行复制,更改它的版本?

2 个答案:

答案 0 :(得分:0)

            TableUtils.dropTable(src, Table1.class, true);
            TableUtils.dropTable(src, Table2.class, true);
            TableUtils.dropTable(src, Table3.class, true);
            TableUtils.dropTable(src, Table4.class, true);
            onCreate(db, src);

在你的onCreate中,你应该再次填充数据库,不过你在之前版本的应用程序中这样做了。

从我可以看到你没有调用copyDatabase方法。

答案 1 :(得分:0)

我无法做我想做的事。否则,使用在初始系统启动时填充数据库的文件。

public void execSqlFromAssets(String path, SQLiteDatabase db) {
    InputStream input;
    String text;
    try {
        input = context.getAssets().open(path + ".sql");
        int size = input.available();
        byte[] buffer = new byte[size];
        input.read(buffer);
        input.close();
        text = new String(buffer);
        String[] inserts = text.split(";");
        for (String insert : inserts) {
            try {
                db.execSQL(insert);
            } catch (Exception e) {
                String err = (e.getMessage() == null) ? "Cant execute sql"
                        + insert : e.getMessage();
            }
        }

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

这样:

@Override
public void onCreate(SQLiteDatabase db, ConnectionSource src) {
    try {
        TableUtils.createTable(src, Linhas.class);
        TableUtils.createTable(src, Horarios.class);
        TableUtils.createTable(src, Itinerarios.class);
        TableUtils.createTable(src, Utils.class);
        execSqlFromAssets("dump_all", db);
    } catch (Exception e) {
        e.printStackTrace();
    }
}