簡體   English   中英

如何在Android設備中存儲數據庫?

[英]how can i store database in android device?

我創建了一個包含表的sqlite應用程序。 在那個應用程序中,我已經從表中檢索了數據。 問題是如何將數據庫中的應用程序加載到android設備中。 我已經嘗試過,但是沒有找到答案。 在我的應用程序中,我使用了以下代碼。

try{
            String destpath = "/data/data/"+getPackageName()+"/database/mmts1.db";
            File f = new File(destpath);
            if(!f.exists())
            {
                  copyDb(getResources().getAssets().open("mmts1.db"),new FileOutputStream(destpath));
            }
        }
        catch(FileNotFoundException e)
        {
            Log.e("FileNotFoundException",e.getMessage());
        }
        catch(IOException e)
        {
            Log.e("IoException",e.getMessage());
        }

private void copyDb(InputStream open, FileOutputStream fileOutputStream) throws IOException{
            // TODO Auto-generated method stub
            byte [] buffer = new byte[1024];
            int length;
            while((length=open.read(buffer))>0)
            {
                  fileOutputStream.write(buffer,0,length);
            }
            open.close();
            fileOutputStream.close();

      }

使用SQLiteAssetHelper將數據庫與您的應用程序打包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM