0

当我尝试使用该方法打开资产文件夹中的数据库时

  public void loadDatabase(String dbName){
    System.out.println("dbName");
    DataBaseAdapterNew mDbHelper = new DataBaseAdapterNew(
                    this, dbName);
            mDbHelper.createDatabase();
            mDbHelper.open();

    }

我得到一个致命的异常ErrorCopyingDataBase

错误堆栈

12-13 18:21:29.179: E/AndroidRuntime(10230): FATAL EXCEPTION: main
12-13 18:21:29.179: E/AndroidRuntime(10230): java.lang.Error: ErrorCopyingDataBase
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.my.app.db.DataBaseHelper.createDataBase(DataBaseHelper.java:43)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.my.app.db.DataBaseAdapterNew.createDatabase(DataBaseAdapterNew.java:42)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.my.app.MyActivity.loadDatabase(MyActivity.java:133)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.my.app.MyActivity$InvocazioneAsincrona$1.run(MyActivity.java:173)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at android.os.Handler.handleCallback(Handler.java:587)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at android.os.Looper.loop(Looper.java:130)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at android.app.ActivityThread.main(ActivityThread.java:3687)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at java.lang.reflect.Method.invokeNative(Native Method)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at java.lang.reflect.Method.invoke(Method.java:507)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at dalvik.system.NativeStart.main(Native Method)

我的 DatabaseAdapterNew 类是:

public class DataBaseAdapterNew {

    protected static final String TAG = "DataAdapter";

    private final Context mContext;
    private SQLiteDatabase mDb;
    private DataBaseHelper mDbHelper;

    public DataBaseAdapterNew(Context context, String nameDB) {
        this.mContext = context;
        mDbHelper = new DataBaseHelper(mContext, nameDB);
    }

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

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

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

    public Cursor allData() {
        try {
            Cursor c = mDb.rawQuery(
                    "SELECT * FROM myTable order by Quantity desc", null);
            if (c != null) {
                c.moveToNext();
            }
            return c;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "allData >>" + mSQLException.toString());
            throw mSQLException;
        }

    }



    }

我的 DatabaseHelper 类是:

public class DataBaseHelper extends SQLiteOpenHelper {
    private static String TAG = "DataBaseHelper"; 
    private static String DB_PATH = "";
    private static String DB_NAME = "";
    private SQLiteDatabase mDataBase;
    private final Context mContext;

    public DataBaseHelper(Context context, String nomeDB) {
        super(context, nomeDB, null, 1);// 1? its Database Version
        DB_NAME = nomeDB;
        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 {
                // Copia il database dalla cartella assests
                copyDataBase();
                Log.e(TAG, "createDatabase database created");
            } catch (IOException mIOException) {
                throw new Error("ErrorCopyingDataBase");
            }
        }
    }

    private boolean checkDataBase() {
        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
    }

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

    public boolean openDataBase() throws SQLException {
        String mPath = DB_PATH + DB_NAME;
        mDataBase = SQLiteDatabase.openDatabase(mPath, null,
                SQLiteDatabase.CREATE_IF_NECESSARY);
        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

    }

}

传递的 String dbName显示了 println 中assets文件夹中存在的 db 的正确名称,并且数据库不是 null 并且是预编译的,所以我不知道错误的原因是什么。有什么建议吗?

4

1 回答 1

1

我将您的代码复制到我的项目中,它可以正常工作。

在 loadDatabase() 中,

System.out.println("dbName");

将始终打印单词dbName。我不确定这是否是资产文件夹中数据库的名称;因为我看不到您对 loadDatabase(dbName) 的调用,所以我不知道您使用的是什么值。

这就是我直接从我的应用程序调用您的代码的方式:

    String dbName = "itr";
    System.out.println(dbName);
    DataBaseAdapterNew mDbHelper = new DataBaseAdapterNew(
                    this, dbName);
            mDbHelper.createDatabase();
            mDbHelper.open();

在 DatabaseHelper.createDataBase() 中,您隐藏了错误条件。如果您想查看导致问题的异常,我可以建议

public void createDataBase() throws IOException {
    // If database not exists copy it from the assets

    boolean mDataBaseExist = checkDataBase();
    if (!mDataBaseExist) {
        this.getReadableDatabase();
        this.close();
        try {
            // Copia il database dalla cartella assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } catch (IOException mIOException) {
            Log.e(TAG, mIOException.toString());
            throw mIOException;
        }
    }
}

那打印了异常

java.io.FileNotFoundException: itr UnableToCreateDatabase

我将现有数据库复制到 /assets/itr 中,并且我的数据库已正确创建。

于 2012-12-13T18:19:11.033 回答