微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决无法从复制的数据库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 举报,一经查实,本站将立刻删除。