Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6866639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:12:38+00:00 2026-05-27T03:12:38+00:00

While installing my Application into the Android device, the Database file is storing in

  • 0

While installing my Application into the Android device, the Database file is storing in the SD card root folder. But it should be placed in the path <SD Card_Root>\<Project_Name>\<dbfile>. After, uninstalling an App from the device the database file including with the project folder should also be deleted. So, Please help me to solve the two Problems as mentioned below:

1. After installation of apk file into the device, the database file should be placed in the path <SD Card_Root>\<Project_Name>\<dbfile>.
2. After uninstallation of apk file, the database file including with the root folder should be deleted automatically.

Please help me with your ideas/links.

Here is my code:

    import java.io.BufferedInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;

    import android.os.Environment;

    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteException;
    import android.database.sqlite.SQLiteOpenHelper;

    public class DataBaseHelper extends SQLiteOpenHelper{
 private static String DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() ;
 private static String DB_NAME = "Database.db";
 private SQLiteDatabase myDataBase; 
 private final Context myContext;  



public DataBaseHelper(Context context) {         
    super(context, DB_NAME, null, 2);
    this.myContext = context;
    }   
 public void createDataBase() throws IOException 
 {
    // boolean dbExist = checkDataBase();        
     DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
     if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 
     {
         File db = new File(DB_PATH, DB_NAME);
         if(!db.exists())
         {
             db.createNewFile();
             try { 
                  copyFromZipFile(); 
              } catch (IOException e) { 
              throw new Error("Error copying database",e); 
              }
         }


        }        
 }

 private void copyFromZipFile() throws IOException{ 
     InputStream is = myContext.getResources().openRawResource(R.raw.database); 
         // Path to the just created empty db 
         File outFile = new File(DB_PATH ,DB_NAME); 
          //Open the empty db as the output stream 
         OutputStream myOutput = new FileOutputStream(outFile.getAbsolutePath()); 
         ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); 
          try { 
              ZipEntry ze =  null; 
              while ((ze = zis.getNextEntry()) != null) { 
                  ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
                  byte[] buffer = new byte[24 * 1024]; 
                  int count; 
                  while ((count = zis.read(buffer)) != -1) { 
                      baos.write(buffer, 0, count); 
                      //Log.d("", buffer.toString()); 
                      baos.writeTo(myOutput); 
                      baos.reset();
                  } 
                  baos.writeTo(myOutput); 


              } 
          } 
          catch (IOException e) {}
              zis.close(); 
              myOutput.flush(); 
              myOutput.close(); 
              is.close(); 


          /*finally { 
              zis.close(); 
              myOutput.flush(); 
              myOutput.close(); 
              is.close(); 
          } */
        }


 private boolean checkDataBase(){        
        SQLiteDatabase checkDB = null;   
        try{
            String myPath = DB_PATH +""+ "/" + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);   
        }catch(SQLiteException e){

            //database does't exist yet.

        }    
        if(checkDB != null){     
            checkDB.close();     
        }    
        return checkDB != null ? true : false;
    } 
 public void openDataBase() throws SQLException{

        //Open the database
        String myPath = DB_PATH + "/" + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

    }
 @Override
    public synchronized void close() {   
            if(myDataBase != null)
                myDataBase.close();  
            super.close();   
    }

 @Override
    public void onCreate(SQLiteDatabase db) {      
     String sql = "create table if not exists info(_id integer primary key autoincrement, Name text not null, firmName text not null);";
     db.execSQL(sql);
     sql = "create table if not exists Client(_id integer primary key autoincrement, Name text not null, clientName text not null);";
     db.execSQL(sql);

    }

  @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {   
     db.execSQL("DROP TABLE IF EXISTS employee");
     db.execSQL("DROP TABLE IF EXISTS info");
     db.execSQL("DROP TABLE IF EXISTS Client");
     onCreate(db);
    }
 public void onDestroy()
 { 
     myDataBase.close(); 
     }
    public Cursor getView(String sql)
       {
 if(myDataBase != null) {
     myDataBase.close();

   }
   openDataBase();
    Cursor xcur =  myDataBase.rawQuery(sql,null);
        return xcur; 

 }
 private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);   
        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;  
        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);   
        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:12:39+00:00Added an answer on May 27, 2026 at 3:12 am

    If you want DB file (or any other file) to be deleted on app uninstall, it should be placed below path:

    “sd_card/Android/data/your.package.name/“

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have packaged my application into an RPM package, say, myapp.rpm . While installing
Do i need to install sqlite for android or its already installed while installing
My Application while installing will create and run a wcf service hosted on windows
I am getting the following warning while installing any plugin in my rails application.
I am getting the following message while installing my application: You cannot start application
Is there any noticable performance gain, after installing the application vs while you are
I need to create a local windows user while installing my application. The Setup
Some additional permissions, not coded in Manifest, show up while installing my application on
While installing an application onto a client's server, I would like to make sure
While cross-site scripting is generally regarded as negative, I've run into several situations where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.