1

在我的应用程序中,我必须从资产文件夹中检索 sqlite 数据库。但是当我这样做时,我得到了错误。

这是我用来从资产中复制数据库的编码

 public class DataBaseHelper extends SQLiteOpenHelper 
{ 
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window 
//destination path (location) of our database on device 
private static String DB_PATH = "";  
private static String DB_NAME ="(student).sqlite";// Database name 
private SQLiteDatabase mDataBase;  
private final Context mContext; 

public DataBaseHelper(Context context)  
{ 
    super(context, DB_NAME, null, 1);// 1? its Database Version 
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
    Log.i(TAG, DB_PATH);
    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"); 
        } 
    } 
} 
    //Check that the database exists here: /data/data/your package/databases/Da Name 
    private boolean checkDataBase() 
    { 
        File dbFile = new File(DB_PATH + DB_NAME); 
        //Log.v("dbFile", dbFile + "   "+ dbFile.exists()); 
        return dbFile.exists(); 
    } 

    //Copy the database from assets 
    private void copyDataBase() throws IOException 
    { 
        InputStream mInput = mContext.getAssets().open(DB_NAME); 
        String outFileName = DB_PATH + DB_NAME; 
        OutputStream mOutput = new FileOutputStream(outFileName); 
        byte[] mBuffer = new byte[1024]; 
        int mLength; 
        while ((mLength = mInput.read(mBuffer))>0) 
        { 
            mOutput.write(mBuffer, 0, mLength); 
        } 
        mOutput.flush(); 
        mOutput.close(); 
        mInput.close(); 
    } 

    //Open the database, so we can query it 
    public boolean openDataBase() throws SQLException 
    { 
        String mPath = DB_PATH + DB_NAME; 
        //Log.v("mPath", mPath); 
        mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); 
        //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
        return mDataBase != null; 
    } 

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

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    } 

} 

调用数据库编码

public class TestAdapter {
protected static final String TAG = "DataAdapter";

private Context mContext;
public static SQLiteDatabase mDb;
private DataBaseHelper mDbHelper;
public static TestAdapter instance;

public TestAdapter(Context context) {
    this.mContext = context;
    mDbHelper = new DataBaseHelper(mContext);
    createDatabase();
    open();
}

public static TestAdapter getTestAdaptor(Context context) {
    if (instance == null) {
        instance = new TestAdapter(context);
        // createDatabase();
        // open();
    }
    return instance;
}

private void createDatabase() throws SQLException {
    try {
        mDbHelper.createDataBase();
    } catch (IOException mIOException) {
        Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
        throw new Error("UnableToCreateDatabase");
    }

}

private void open() throws SQLException {
    try {
        mDbHelper.openDataBase();
        mDbHelper.close();
        mDb = mDbHelper.getReadableDatabase();
    } catch (SQLException mSQLException) {
        Log.e(TAG, "open >>" + mSQLException.toString());
        throw mSQLException;
    }
}

public void close() {
    mDbHelper.close();
}

这是我得到的错误

    03-14 10:18:03.731: E/AndroidRuntime(788): FATAL EXCEPTION: main
03-14 10:18:03.731: E/AndroidRuntime(788): java.lang.Error: ErrorCopyingDataBase
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.example.voicereg.DataBaseHelper.createDataBase(DataBaseHelper.java:49)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.example.voicereg.TestAdapter.createDatabase(TestAdapter.java:38)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.example.voicereg.TestAdapter.<init>(TestAdapter.java:23)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.example.voicereg.TestAdapter.getTestAdaptor(TestAdapter.java:29)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.example.voicereg.VoiceActivity.onOptionsItemSelected(VoiceActivity.java:186)
03-14 10:18:03.731: E/AndroidRuntime(788):  at android.app.Activity.onMenuItemSelected(Activity.java:2502)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
03-14 10:18:03.731: E/AndroidRuntime(788):  at android.widget.AdapterView.performItemClick(AdapterView.java:292)
03-14 10:18:03.731: E/AndroidRuntime(788):  at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
03-14 10:18:03.731: E/AndroidRuntime(788):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
03-14 10:18:03.731: E/AndroidRuntime(788):  at android.widget.AbsListView$1.run(AbsListView.java:3168)
03-14 10:18:03.731: E/AndroidRuntime(788):  at android.os.Handler.handleCallback(Handler.java:605)
03-14 10:18:03.731: E/AndroidRuntime(788):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 10:18:03.731: E/AndroidRuntime(788):  at android.os.Looper.loop(Looper.java:137)
03-14 10:18:03.731: E/AndroidRuntime(788):  at android.app.ActivityThread.main(ActivityThread.java:4424)
03-14 10:18:03.731: E/AndroidRuntime(788):  at java.lang.reflect.Method.invokeNative(Native Method)
03-14 10:18:03.731: E/AndroidRuntime(788):  at java.lang.reflect.Method.invoke(Method.java:511)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-14 10:18:03.731: E/AndroidRuntime(788):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-14 10:18:03.731: E/AndroidRuntime(788):  at dalvik.system.NativeStart.main(Native Method)

谁能帮我在编码部分做错了什么

4

1 回答 1

0

在 createDatabase 中,您关闭数据库,因此您无法复制

this.getReadableDatabase(); 
    this.close();   

应该只是

this.getReadableDatabase(); 
于 2013-03-14T05:17:12.877 回答