android - 在 Android 中更新预填充的数据库

标签 android sqlite android-assets

我为此浪费了最后 3 个小时。我正在制作一个应用程序,该应用程序将附带一个数据库,其中填充了不会在客户端设备上更改的填充表。 我设法通过将它放在 Assets 文件夹中并将其作为字节流复制到手机上适当的数据文件夹中来做到这一点。问题是当我想更新数据库时,我无法让它工作。我删除了手机数据文件夹中的数据库(从代码),但复制新数据库总是失败,或者复制的数据库具有适当的大小但其中没有表。 这个怎么做?或者有更简单的方法吗?我可以直接从 Assets 中打开数据库吗(如果可以的话,这将是最简单的方法,但我找不到如何从代码访问 Assets 的路径)?

最佳答案

这是我使用的代码:

public class DataBaseHelper extends SQLiteOpenHelper {

    private static final String DB_PATH = "/data/data/com.project.mydb/databases/";
    private static final String DB_NAME = "mydb.db";
    private static final String DB_TABLE = "words";
    private static final int DB_VERSION = 6;
    private static final String TAG = "DataBaseHelper";
    int id = 0;
    Random random = new Random();
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    public DataBaseHelper(Context context){
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        createDB();
    }

    @Override
    public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
        Log.w(TAG, "Upgrading DB from version " + oldVersion + " to " +
                newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
        onCreate(db);
    }

    public void createDataBase(){
        createDB();
    }

    private void createDB(){
        boolean dbExist = dbExists();
        if(!dbExist){
            copyDataBase();
        }
        else if(dbExist){
            copyDataBase();
        }
    }

    private boolean dbExists(){
        SQLiteDatabase db = null;
        try{
            String dbPath = DB_PATH + DB_NAME;
            db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
            db.setLocale(Locale.getDefault());
            db.setLockingEnabled(true);
            db.setVersion(DB_VERSION);
        }
        catch(SQLiteException e){
            Log.e("SQL Helper", "database not found");
        }
        if(db != null){
            db.close();
        }
        return db != null ? true : false;
    }

    private void copyDataBase(){
        InputStream iStream = null;
        OutputStream oStream = null;
        String outFilePath = DB_PATH + DB_NAME;
        try{
            iStream = myContext.getAssets().open(DB_NAME);
            oStream = new FileOutputStream(outFilePath);
            byte[] buffer = new byte[1024];
            int length;
            while((length = iStream.read(buffer))>0){
                oStream.write(buffer,0,length);
            }
            oStream.flush();
            oStream.close();
            iStream.close();
        }
        catch(IOException ioe){
            throw new Error("Problem copying database from resource file.");
        }
    }

    public void openDataBase() throws SQLException {
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

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

关于android - 在 Android 中更新预填充的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4189956/

相关文章:

android - Android/Xamarin 项目中的自定义主题 - Action Bar 的中心标题文本

python - web2py 中跨 session 的全局对象访问?

performance - 大型表上的 SQLite 性能

java - NullPointerException 试图访问 String 资源

android - MediaPlayer 'prepare();' 问题

android - 正在运行的应用程序 : No such file or directory

java - 从 Sql 中的另一个表插入部分值,然后手动插入其他表

android - android应用程序中的 Assets 文件夹(eclipse)

java - 安卓 : ArrayList with Bitmap from assets

android - Google map - 无法使用 animateCamera 同时缩放和移动,但可以使用 moveCamera