安卓:从资产问题复制SQLite数据库(我有一个数据库,没有任何田部毕竟)数据库、没有任何、我有一个、资产

2023-09-07 10:05:42 作者:灬諟刂抿酒zυιㄣ

我试图使一个DataAdapter的扩展SQLiteOpenHelper。贝娄是code:

I'm trying to make an dataAdapter extend SQLiteOpenHelper. Bellow is the code:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import com.muoiot.enlistening.idefined.*;
import com.muoiot.enlistening.utilities.MsgUtils;

public class DbAdapter extends SQLiteOpenHelper implements DB_INFO, DB_USER, DB_QUESTION {

    public static String defaultDbPath = "";
    protected SQLiteDatabase databaseControl;
    private Context context = null;

    public DbAdapter(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.context = context;
        defaultDbPath = "/data/data/" + context.getApplicationContext().getPackageName() + "/databases/";
    }

    /***********************************************************************************/
    // This function auto call at the first time when the database was not found.
    /***********************************************************************************/   
    @Override
    public void onCreate(SQLiteDatabase db) {

        MsgUtils.DB("Don't have database, need to copy from asset");
        try {
            copyDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    /***********************************************************************************/
    // This function auto call when the database has been opened.
    /***********************************************************************************/   
    @Override
    public void onOpen(SQLiteDatabase db){
        MsgUtils.DB(" AAAAAAAAAAAAAAAAAAAAAAAAA has been opened!!!");
    }

    /***********************************************************************************/
    // Copy database
    /***********************************************************************************/       
    private void copyDataBase() throws IOException {

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

        // Path to the just created empty db
        String outFileName = defaultDbPath + DB_NAME;

        // 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();

    }

    /***********************************************************************************/
    // Open and close database 
    /***********************************************************************************/   
    public void openDatabase(){
        databaseControl = this.getWritableDatabase();
    }

    public void closeDatabase(){
        this.getWritableDatabase().close();
    }

}

而且,现在我有copyDataBase功能的问题。我总是与任何表得到一个数据库。 (我的手机扎根,所以我复制回从数据/数据​​//数据库的数据库审核)。

And, now I have an issue with copyDataBase function. I always get an database with out any table. (my phone was rooted, so I copy back the database from data/data//databases to review).

我是什么错在这里?谁能给我个忙吗?

What am I wrong here? Anyone can give me a hand?

推荐答案

据我所知,函数的onCreate()仅在我们第一次访问数据库,该数据库是不存在的调用。所以我认为把一些code这里复制的数据库就可以了。但是看起来像有东西......我不是清楚在这个时候。

AFAIK, the function onCreate() only call at the first time we access to database and the database isn't exist. So i think that put some code here to copy database is ok. But look like there are something... that i'm not clearly at this time.

当我试图把我的code这个功能(从数据资产复制数据库),创建数据库与雷相同的大小,但没有表。

When i try to put my code to this function (copy database from data asset), the database was created have the same size with mine but there are no table.

我只是按照在数据库解决方案。现在它的工作性能!我刚才只看到一个不同的事情,他在调用拷贝构造数据库,而不是在OnCreate并对他一叫getReadableDatabase()。

I just follow the solution that mentioned at Database not copying from assets. And it working properties now! I just see only one different thing that he call copy database in constructor instead OnCreate and he call getReadableDatabase() at first.

无论如何,我要完成我已经开始然后再回到这个话题,因为我不是的回答满足。如果有人有相同的问题,尝试使用在数据库解决方案不能从资产首先解决。如果你能找出我错了,请帮帮我!

Anyway, i have to complete what i have started then come back to this topic since i'm not satisfy with the answer. If some one have the same issues, try to use the solution at Database not copying from assets to solve first. If you can figure out what am i wrong, please help me!

非常感谢大家!