无法从复制的数据库Android查询表

问题描述

我有一个用于字典应用程序的大型数据库。 我将该数据库从原始文件夹复制到了应用程序包的数据库路径。

public class DbHelper extends sqliteOpenHelper {


       private sqliteDatabase mDataBase;
      private static int DB_VERSION = 1;

      private Context mContext;

    private static String DB_PATH;

    private static final String DATABASE_NAME = "offlinedictionary.db";

    private static final String LOG_TAG = "DBHelper";


    public DbHelper(@Nullable Context context) {
        super(context,DATABASE_NAME,null,DB_VERSION);
        Log.d(LOG_TAG,"DbHelper constructor");
        mContext = context;

        if (android.os.Build.VERSION.SDK_INT >= 4.2) {
            DB_PATH = context.getApplicationInfo().dataDir + "/databases/";


        } else {
            DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
        }
        Log.d(LOG_TAG,"database path: "+DB_PATH);
    }

    public void createDataBase() throws IOException {

        // If database not exists copy it from the assets

        boolean mDataBaseExist = checkDataBase();
        Log.d(LOG_TAG,"createDataBase: mDataBaseExist "+mDataBaseExist);

        if (mDataBaseExist) {
            //  this.getReadableDatabase();
            // this.close();
            try {
                // copy the database from assests
                copyDataBase();
                Log.e("DataBaseHelper","createDatabase database created");
            } catch (IOException mIOException) {
                throw new Error("Error copying DataBase");
            }
        }

    }

    private boolean checkDataBase() {
        File dbFile = new File(DB_PATH + DATABASE_NAME);
        //Log.v("dbFile",dbFile + "   "+ dbFile.exists());

        Log.d(LOG_TAG,"checkDataBase: dbFile "+dbFile.getName());
        Log.d(LOG_TAG,"checkDataBase: dbFile.exists() "+dbFile.exists());
        return dbFile.exists();
    }

    private void copyDataBase() throws IOException {


        InputStream mInput = mContext.getResources().openRawResource(R.raw.offlinedictionary);
        String outFileName = DB_PATH + DATABASE_NAME;
        OutputStream mOutput = new FileOutputStream(outFileName);
        Log.d(LOG_TAG,"copyDataBase: mInput "+mInput);
        Log.d(LOG_TAG,"copyDataBase: outFileName "+outFileName);
        Log.d(LOG_TAG,"copyDataBase: mOutput "+mOutput);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer)) > 0) {
            mOutput.write(mBuffer,mLength);
            Log.d(LOG_TAG,"copyDataBase: mLength "+mLength);
        }
        Log.d(LOG_TAG,"copyDataBase: done ");
        mOutput.flush();
        mOutput.close();
        mInput.close();

    }

    @Override
    public void onCreate(sqliteDatabase db) {


        try {
            createDataBase();
        } catch (IOException e) {
            e.printstacktrace();
        }
     }
  }

上面的代码将我的数据库从原始文件夹成功复制到数据库路径。

数据库包含表“ worddictionary”。

但是当我查询该表时,出现错误提示不存在该表。但是我在复制的数据库中看到该表及其内容存在。

public class LoadActivity extends AppCompatActivity {

    private static final String LOG_TAG = "LoadActivity";

    private DbHelper dbHelper;
    private sqliteDatabase mDatabase;

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load);

        Log.d(LOG_TAG,"Starting Load Activity");

        mContext = getApplicationContext();

        dbHelper = new DbHelper(this);
        mDatabase = dbHelper.getWritableDatabase();

   

        Log.d(LOG_TAG,"Call insertData");
        insertData();


    }


    public void insertData(){
        Log.d(LOG_TAG,"insertData");

        InsertData fetchWordDetails = new InsertData();
        fetchWordDetails.execute();

    }
    
    private class InsertData extends AsyncTask<Void,Void,Void> {
    
    
    }
    
     @Override
     protected Void doInBackground(Void... voids) {
        
        
            String wordQuery = "SELECT * FROM worddictionary;";
            Cursor wordCursor = mDatabase.rawQuery(wordQuery,null);
            int wordCount = wordCursor.getCount();
            Log.d(LOG_TAG,"wod wordCount: "+wordCount);
        
    }
    
}

谁能解释出什么问题了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)