从资产复制的数据库为空

时间:2018-05-01 19:47:09

标签: android sqlite android-sqlite

我有一个预先填充的SQLite数据库,我在安装时从apk复制。数据库成功复制但后来似乎是空的(我在查询时遇到SQLite缺失表异常)。

我正在使用此代码来处理数据库文件操作:

public class ExternalDbOpenHelper extends SQLiteOpenHelper {

    //Path to the device folder with databases
    public static String DB_PATH;

    //Database file name
    public static String DB_NAME;
    public SQLiteDatabase database;
    public final Context context;

    public SQLiteDatabase getDb() {
        return database;
    }

    public ExternalDbOpenHelper(Context context, String databaseName) {
        super(context, databaseName, null, 1);
        this.context = context;
//Write a full path to the databases of your application
        String packageName = context.getPackageName();
        DB_PATH = String.format("//data//data//%s//databases//", packageName);
        DB_NAME = databaseName;
        openDataBase();
    }

    //This piece of code will create a database if it’s not yet created
    public void createDataBase() {
        boolean dbExist = checkDataBase();
        if (!dbExist) {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                Log.e(this.getClass().toString(), "Copying error");
                throw new Error("Error copying database!");
            }
        } else {
            Log.i(this.getClass().toString(), "Database already exists");
        }
    }

    //Performing a database existence check
    private boolean checkDataBase() {
        SQLiteDatabase checkDb = null;
        try {
            String path = DB_PATH + DB_NAME;
            checkDb = SQLiteDatabase.openDatabase(path, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLException e) {
            Log.e(this.getClass().toString(), "Error while checking db");
        }
        //Android doesn’t like resource leaks, everything should
        // be closed
        if (checkDb != null) {
            checkDb.close();
        }
        return checkDb != null;
    }

    //Method for copying the database
    private void copyDataBase() throws IOException {
        //Open a stream for reading from our ready-made database
        //The stream source is located in the assets
        InputStream externalDbStream = context.getAssets().open(DB_NAME);

        //Path to the created empty database on your Android device
        String outFileName = DB_PATH + DB_NAME;

        //Now create a stream for writing the database byte by byte
        OutputStream localDbStream = new FileOutputStream(outFileName);

        //Copying the database
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = externalDbStream.read(buffer)) > 0) {
            localDbStream.write(buffer, 0, bytesRead);
        }
        //Don’t forget to close the streams
        localDbStream.close();
        externalDbStream.close();
    }

    public SQLiteDatabase openDataBase() throws SQLException {
        String path = DB_PATH + DB_NAME;
        if (database == null) {
            createDataBase();
            database = SQLiteDatabase.openDatabase(path, null,
                    SQLiteDatabase.OPEN_READWRITE);
        }
        return database;
    }

    @Override
    public synchronized void close() {
        if (database != null) {
            database.close();
        }
        super.close();
    }
    @Override
    public void onCreate(SQLiteDatabase db) {}
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}

我在3个几乎相同的应用中使用了这个确切的代码而没有任何问题。关于为什么我可能在新应用程序中遇到相同代码问题的任何想法?该应用程序以API级别25为目标/构建。

2 个答案:

答案 0 :(得分:1)

如果没有日志的详细信息,只能猜到问题。

第一个问题可能是数据库是在没有表的情况下创建的。因此,第一步是删除App的数据或卸载App,然后重新运行App。

注意如果在没有与所使用的数据库名称完全相同的文件的情况下运行应用程序,应用程序崩溃

,则可能会发生这种情况
  

05-01 20:22:41.988 1309-1309/? E/AndroidRuntime: FATAL EXCEPTION: main java.lang.Error: Error copying database! at .......

然而,数据库已创建但为空(如此)。因此,后续运行将不会尝试数据库副本,因为数据库现在已存在。

如果这不能解决问题,那么您要么从assets文件夹中复制一个不包含所提到的表的文件,要么使用了错误的表名。

通过询问sqlite_master表来确定实际复制的内容。但是,可以复制的here's a class具有logDatabaseInfo方法,可以执行此操作以及更多。只需创建 CommonSQLiteUtilities.java 并使用标题 Addition 1 - logDatabaseInfo 复制答案中的代码,然后在ExternalDbOpenHelper实例化后立即调用logDatabaseInfo方法例如: -

    ExternalDbOpenHelper extdbhlpr = new ExternalDbOpenHelper(this,"your_database_name");
    CommonSQLiteUtilities.logDatabaseInfo(extdbhlpr.getWritableDatabase());

如果您有一个空数据库logDatabaseInfo,则会记录以下内容: -

05-01 20:43:59.424 1706-1706/? I/class mjt.adapters.ExternalDbOpenHelper: Database already exists
05-01 20:43:59.424 1706-1706/? D/SQLITE_CSU: DatabaseList Row 1 Name=main File=/data/data/mjt.adapters/databases/my_db
    Database Version = 1
    Table Name = android_metadata Created Using = CREATE TABLE android_metadata (locale TEXT)
05-01 20:43:59.428 1706-1706/? D/SQLITE_CSU: Table = android_metadata ColumnName = locale ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
  • 注意第一行是由ExternalDbOpenhelper
  • 的实例化发出的

以下是第一次运行的示例(从资产文件夹复制数据库时)并填充数据库: -

05-01 20:50:01.396 1784-1784/mjt.adapters E/SQLiteLog: (14) cannot open file at line 30174 of [00bb9c9ce4]
    (14) os_unix.c:30174: (2) open(//data//data//mjt.adapters//databases//my_db) - 
05-01 20:50:01.404 1784-1786/mjt.adapters D/dalvikvm: GC_CONCURRENT freed 211K, 9% free 6187K/6791K, paused 11ms+1ms, total 27ms
05-01 20:50:01.404 1784-1784/mjt.adapters E/SQLiteDatabase: Failed to open database '//data//data//mjt.adapters//databases//my_db'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
        at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
        at mjt.adapters.ExternalDbOpenHelper.checkDataBase(ExternalDbOpenHelper.java:59)
        at mjt.adapters.ExternalDbOpenHelper.createDataBase(ExternalDbOpenHelper.java:40)
        at mjt.adapters.ExternalDbOpenHelper.openDataBase(ExternalDbOpenHelper.java:98)
        at mjt.adapters.ExternalDbOpenHelper.<init>(ExternalDbOpenHelper.java:35)
        at mjt.adapters.MainActivity.onCreate(MainActivity.java:39)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
05-01 20:50:01.404 1784-1784/mjt.adapters E/class mjt.adapters.ExternalDbOpenHelper: Error while checking db
05-01 20:50:01.444 1784-1784/mjt.adapters D/SQLITE_CSU: DatabaseList Row 1 Name=main File=/data/data/mjt.adapters/databases/my_db
    Database Version = 0
    Table Name = user_table Created Using = CREATE TABLE user_table (ID TEXT, OCC INTEGER)
    Table = user_table ColumnName = ID ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
    Table = user_table ColumnName = OCC ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
    Table Name = sqlite_sequence Created Using = CREATE TABLE sqlite_sequence(name,seq)
    Table = sqlite_sequence ColumnName = name ColumnType =  Default Value = null PRIMARY KEY SEQUENCE = 0
    Table = sqlite_sequence ColumnName = seq ColumnType =  Default Value = null PRIMARY KEY SEQUENCE = 0
    Table Name = tasks Created Using = CREATE TABLE tasks (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT date DATE, new_colum INTEGER DEFAULT 0)
    Table = tasks ColumnName = _id ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 1
    Table = tasks ColumnName = title ColumnType = TEXT date DATE Default Value = null PRIMARY KEY SEQUENCE = 0
    Table = tasks ColumnName = new_colum ColumnType = INTEGER Default Value = 0 PRIMARY KEY SEQUENCE = 0
    Table Name = Words Created Using = CREATE TABLE Words (Word TEXT, LIKEWORD TEXT)
05-01 20:50:01.448 1784-1784/mjt.adapters D/SQLITE_CSU: Table = Words ColumnName = Word ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
    Table = Words ColumnName = LIKEWORD ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
    Table Name = android_metadata Created Using = CREATE TABLE android_metadata (locale TEXT)
    Table = android_metadata ColumnName = locale ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
  • 注意堆栈跟踪是由于检查数据库存在的方式(我个人检查文件是否存在,因此您没有获得堆栈跟踪)。因此,当从资产文件夹中实际复制数据库时,您会期望这个堆栈跟踪。
  • 如您所见,表格与列一起列出。
  • 注意 android_metadata和任何以sqlite_开头的表都是内部表(android_metadata由SQLiteOpenHelper创建,sqlite_表是SQLite表)。

答案 1 :(得分:0)

下面的功能将帮助您将数据库复制到SD卡或任何其他地方。现在它被复制到SD卡上了。

public static void copyDbToSdCard(Context context) {
        try {
            File sd = Environment.getExternalStorageDirectory();
            //File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String currentDBPath = "/data/data/" + context.getPackageName() + "/databases/JSandeshEmpTracker";
                String backupDBPath = "db_backup.db";
                File currentDB = new File(currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }