NareshRavva
NareshRavva

Reputation: 863

how can i store database in android device?

i have created an sqlite application that contain tables. in that application i have retrieved data from tables. the problem is how can i load that application in android device with my database. i have tried but i didn't found answer. in my application i have used the below code.

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

      }

Upvotes: 0

Views: 955

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

Use SQLiteAssetHelper to package a database with your application.

Upvotes: 1

Related Questions