且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何访问SQLite数据库在Android项目的资源文件夹?

更新时间:2023-08-24 10:23:40

不知道,但这个可以帮你

 无效CHECKDB()抛出异常{
    尝试 {
        SQLiteDatabase DBE = SQLiteDatabase
                .openDatabase(
                        /data/data/yourpackagename/databases/yourfilename.sqlite
                        空,0);
        Log.d(opendb,存在);
        dbe.close();
    }赶上(例外五){

        AssetManager AM = getApplicationContext()getAssets()。
        的OutputStream OS =新的FileOutputStream(
                /data/data/yourpackagename/databases/yourfilename.sqlite);
        byte []的B =新的字节[100];

        INT R;
        InputStream的是= am.open(yourfilename.sqlite);
        而((R = is.​​read(二))!=  -  1){
            os.write(B,O,R);
        }
        Log.i(DATABASE_HELPER,复制数据库);
        is.close();
        os.close();
    }

}
 

I need help to use use my existing sqlite database in android.

i am placing my sqlite database in "/assets/(mydatabase)" but unable to use it.

i am using following code. some times it show error "unable to open database" and some times shows error like "can not find table". when i pull out database file from ddms and open it using sqlite browser it dropes my table means my table do not exist any more.

so please help me to get the way how i can use my existing sqlite databse in android just for read and rite operations.

private SQLiteDatabase MyDataBase;
private String path="/data/data/mypackagename/mydatabase";
try{
      MyDataBase=SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READWRITE);
      Cursor myDbCursor;
      myDbCursor=MyDataBase.rawQuery("select user,password from login_master", null);
      String username=myDbCursor.getString(0);
      String password=myDbCursor.getString(01);
      Toast.makeText(getBaseContext(), username, 10).show();
      Toast.makeText(getBaseContext(), password, 10).show();
      MyDataBase.close();
  }
  catch(SQLiteException e){
      Toast.makeText(getBaseContext(), e.getMessage(), 10).show();
  }

not sure but this can help you

void checkDB() throws Exception {
    try {
        SQLiteDatabase dbe = SQLiteDatabase
                .openDatabase(
                        "/data/data/yourpackagename/databases/yourfilename.sqlite",
                        null, 0);
        Log.d("opendb", "EXIST");
        dbe.close();
    } catch (Exception e) {

        AssetManager am = getApplicationContext().getAssets();
        OutputStream os = new FileOutputStream(
                "/data/data/yourpackagename/databases/yourfilename.sqlite");
        byte[] b = new byte[100];

        int r;
        InputStream is = am.open("yourfilename.sqlite");
        while ((r = is.read(b)) != -1) {
            os.write(b, 0, r);
        }
        Log.i("DATABASE_HELPER", "Copying the database ");
        is.close();
        os.close();
    }

}