猿问

复制自定义的 sqlite 数据库到 android 中的错误

我想复制一个自定义的 sqlite 数据库到 android 中

public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
private static String DB_PATH = "/data/data/mypackagename/databases/";//path of our database
private static String DB_NAME ="application-database";// Database name
private SQLiteDatabase mDataBase; 
private final Context mContext;
public DataBaseHelper(Context context) 
{
    super(context, DB_NAME, null, 1);
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    this.mContext = context;
}   
public void createDataBase() throws IOException
{
    //If database not exists copy it from the assets
      boolean mDataBaseExist = checkDataBase();
    if(!mDataBaseExist)
    {
        this.getReadableDatabase();
        this.close();
        try 
        {
            //Copy the database from assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } 
        catch (IOException mIOException) 
        {
            throw new Error("ErrorCopyingDataBase");
        }
    }
}

当运行到 InputStream mInput = mContext.getAssets().open(DB_NAME);
给出一个错误,程序直接跳转到

catch (IOException mIOException) 
        {
            throw new Error("ErrorCopyingDataBase");
        }

数据库是在 assets 文件夹中。
这个问题出在哪里呢?


翻过高山走不出你
浏览 485回答 3
3回答

繁花如伊

你需要先把数据库解压到SD卡上,然后去读取SD卡上的db文件。
随时随地看视频慕课网APP

相关分类

Java
我要回答