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 7582457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:26:20+00:00 2026-05-30T18:26:20+00:00

Possible Duplicate: How to access an existing sqlite database in Android? I’ve been searching

  • 0

Possible Duplicate:
How to access an existing sqlite database in Android?

I’ve been searching for a long time but I can’t find the answer. I want to copy my existing Sqlite database test.db with data in it in my application. So when the users download my app from the application the db is delivered with it.

Now I’v seen a lot of references to the tutorials this and this. But none of them work for me.

    public class DatabaseAdapter extends SQLiteOpenHelper {
         private static String dbPath= "data/data/test.test.test/databases/"; 
         private static String dbName = "test"; 
         private SQLiteDatabase applicationDatabase;  
         private final Context applicationContext;


         public DatabaseAdapter(Context context) {    
                 super(context,  dbName , null, 3);
                 this. applicationContext  = context;
         }


         public boolean checkDataBase(){  
                 File dbFile = new File( dbPath +  dbName);  
   return dbFile.exists();
   }


          public void copyDataBase() throws IOException{  
   try {

                    InputStream input =  applicationContext .getAssets().open(dbName);
                           String outPutFileName=  dbPath  +  dbName ;
                      OutputStream output = new FileOutputStream( outPutFileName); 
                       byte[] buffer = new byte[1024];
                    int length;
                    while ((length = input.read(buffer))>0){
                    output.write(buffer, 0, length);
                    }
                    output.flush();
                    output.close();
                    input.close();
         }
                       catch (IOException e) {
                     Log.v("error",e.toString());
                    }
     }


             public void openDataBase() throws SQLException{
                 String fullDbPath= dbPath + dbName;
               applicationDatabase = SQLiteDatabase.openDatabase( fullDbPath,     null,SQLiteDatabase.OPEN_READONLY);
     }

                @Override
  public synchronized void close() {
          if( applicationDatabase != null)
            applicationDatabase .close();
               super.close();
  }
@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub

}


@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

public void getit(){
    this.getReadableDatabase().rawQuery("SELECT * FROM test", null);
}






}

I get the error code = 1 no such table.
If I check the database is created but only with the table android_metadata.

Someone a working example?

  • 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-30T18:26:22+00:00Added an answer on May 30, 2026 at 6:26 pm

    EDIT – Here is my code that works. I added what info you posted, but there may be other variables you didn’t, so you may have to modify it a bit.

    public class DatabaseAdapter extends SQLiteOpenHelper {
    
        private Context mycontext;
    
        private String DB_PATH = "data/data/test.test.test/databases/";
        private static String DB_NAME = "test";
        // the extension may be .sqlite
        // or .db
        public SQLiteDatabase myDataBase;
    
        public DatabaseAdapter(Context context) {
            super(context, DB_NAME, null, 1);
            this.mycontext = context;
            boolean dbexist = checkdatabase();
            if (dbexist) {
            } else {
                System.out.println("Database doesn't exist");
                try {
                    createdatabase();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    
        }
    
        public void createdatabase() throws IOException {
            boolean dbexist = checkdatabase();
            if (dbexist) {
            } else {
                this.getReadableDatabase();
                try {
                    copydatabase();
                } catch (IOException e) {
                    throw new Error("Error copying database");
                }
            }
        }
    
        private boolean checkdatabase() {
            boolean checkdb = false;
            try {
                String myPath = DB_PATH + DB_NAME;
                File dbfile = new File(myPath);
                checkdb = dbfile.exists();
            } catch (SQLiteException e) {
                System.out.println("Database doesn't exist");
            }
    
            return checkdb;
        }
    
        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
            @SuppressWarnings("unused")
            String outfilename = DB_PATH + DB_NAME;
    
            // Open the empty db as the output stream
            OutputStream myoutput = new FileOutputStream(
                    "data/data/test.test.test/databases/test");
    
            // transfer byte to inputfile to 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();
    
        }
    
        public void open() {
            // Open the database
            String mypath = DB_PATH + DB_NAME;
            myDataBase = SQLiteDatabase.openDatabase(mypath, null,
                    SQLiteDatabase.OPEN_READWRITE);
    
        }
    
        public synchronized void close() {
            myDataBase.close();
            super.close();
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ delete - It deletes my objects but I can still access
Possible Duplicate: Remote Database access I'm developing an app for android which shoult connect
Possible Duplicate: C++ delete - It deletes my objects but I can still access
Possible Duplicate: How can I access local scope dynamically in javascript? Hi all. We
Possible Duplicate: Is there a reason you can not define the access modifier on
Possible Duplicate: Access database of another app I made an app which is a
Possible Duplicate: Can I set a breakpoint on 'memory access' in GDB? I have
Possible Duplicate: Default class inheritance access I know I can set the protection level
Possible Duplicate: How to check internet access on Android? InetAddress never timeouts I need
Possible Duplicate: Access the SD card files from Android application I am developing an

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.