【问题标题】:How to use my own database - android [duplicate]如何使用我自己的数据库 - android [重复]
【发布时间】:2013-01-10 14:28:08
【问题描述】:

可能重复:
How to use an existing database with an Android application

我有一个 sqlite 数据库文件,我想在我的应用程序中使用它。

我该怎么做?

如何将我的 db 文件推送到模拟器,然后在我的代码中使用它?

欢迎任何解决方案。 提前谢谢你,汤姆。

【问题讨论】:

    标签: android sqlite


    【解决方案1】:
    public class AssetDatabaseHelper extends SQLiteOpenHelper {
    
        private String dbName;
        private String db_path;
        private Context context;
    
        /**
         * A helper class to import db files.
         * 
         * @param base
         *            /app context
         * @param dbName
         *            The name of the db in asset folder .
         */
        public AssetDatabaseHelper(Context context, String dbName) {
            super(context, dbName, null, 1);
            this.dbName = dbName;
            this.context = context;
            db_path = "/data/data/" + context.getPackageName() + "/databases/";
        }
    
        /**
         * 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
         */
        public boolean checkExist() {
    
            SQLiteDatabase checkDB = null;
    
            try {
                String myPath = db_path + dbName;
                checkDB = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READONLY);
    
            } catch (SQLiteException e) {
                e.printStackTrace();
                // database does't exist yet.
    
            } catch (Exception ep) {
                ep.printStackTrace();
            }
    
            if (checkDB != null) {
    
                checkDB.close();
    
            }
    
            return checkDB != null ? true : false;
        }
    
        /**
         * Creates a empty database on the system and rewrites it with your own
         * database.
         * */
        public void importIfNotExist() throws IOException {
    
            boolean dbExist = checkExist();
    
            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");
    
                }
            }
    
        }
    
        private void copyDatabase() throws IOException {
            InputStream is = context.getAssets().open(dbName);
    
            OutputStream os = new FileOutputStream(db_path + dbName);
    
            byte[] buffer = new byte[4096];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
            os.flush();
            os.close();
            is.close();
            this.close();
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    
    }
    

    基于此tutorial。你把你的 sqlite 数据库文件放在你的资产文件夹中,当你运行代码时它会被复制。我的版本允许多个数据库文件,因为它选择了路径。 要使用它:

    AssetDatabaseHelper dbHelper = new AssetDatabaseHelper(
                    getBaseContext(), SomeDataBase.SOME_DATABASE_NAME);
            try {
                dbHelper.importIfNotExist();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    【解决方案2】:

    您需要扩展 SQLiteOpenHelper 以匹配您的数据库表。这将允许您的应用程序与数据库交互。检查这个:http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

    要使用数据库,请在模拟器运行时使用 Android->Eclipse 中的文件资源管理器视图将其推送到/data/data/your.package/databases/

    【讨论】:

    • 用户设备呢,他怎么会push the DB进入用户设备?
    • 我没有尝试直接向/从设备推/拉,但链接 here 的“向模拟器/设备实例复制文件或从模拟器/设备实例复制文件”部分提供了一些建议。
    猜你喜欢
    • 2011-01-24
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 2015-03-06
    • 2021-11-26
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多