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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:07:52+00:00 2026-05-25T19:07:52+00:00

I have a problem in sqlite database.First i created database externally and then put

  • 0

I have a problem in sqlite database.First i created database externally and then put it in the assets folder.The following code copy the database.

public class DbH extends SQLiteOpenHelper{
    private Context mycontext;

    private String DB_PATH = "/data/data/com.android.quotes/databases/";
    private static String ROW_ID="_id";
    private static String DB_NAME = "mdb1.db";
    public SQLiteDatabase myDataBase;
    /*private String DB_PATH = "/data/data/"
        + mycontext.getApplicationContext().getPackageName()
        + "/databases/";
    */

    public DbH(Context context) throws IOException  {
        super(context,DB_NAME,null,1);
        this.mycontext=context;
        boolean dbexist = checkdatabase();
        if(dbexist){
            //System.out.println("Database exists");
            opendatabase(); 
        } else {
            System.out.println("Database doesn't exist");
            createdatabase();
        }
    }

    public void createdatabase() throws IOException{
        boolean dbexist = checkdatabase();
        if(dbexist){
            System.out.println(" Database exists.");
        } else {
            this.getReadableDatabase();
            try{
                copydatabase();
            } catch(IOException e){
            throw new Error("Error copying database");
            }
        }
    }

    private boolean checkdatabase() {
        //SQLiteDatabase checkdb = null;
        boolean checkdb = false;
        try{
            String myPath = DB_PATH + DB_NAME;
            File dbfile = new File(myPath);
            SQLiteDatabase.openDatabase
               (myPath,null,SQLiteDatabase.OPEN_READWRITE);
            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
       String outfilename = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myoutput = new FileOutputStream(outfilename);

        // 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 opendatabase() throws SQLException{
        //Open the database
        String mypath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(
            mypath, null, SQLiteDatabase.OPEN_READWRITE
        );
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Empty
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub
    }

}

In main activity i first call createdatabase and then opendatabase.
Now i craeted only two table in sqlite database one is android_metadata and second is mTable.when i run the program the following errors occur.

  1. Although database is created and i can see it using file explorer in eclipse but only android_metadata table is there and mTable is not there.
  2. So it says no such table exists while selecting * from mTable.

Any suggestion where i m going wrong. I m pulling my hair from one week me. Please help.
Thanks in advance.

  • 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-25T19:07:53+00:00Added an answer on May 25, 2026 at 7:07 pm

    First Of all check database database exists in internal storage if not than copy

    private void checkDatabase() {
    
            StringBuffer dbPath = new StringBuffer();
            File databaseFile;
            // Location of the database file, where it will be stored to access
            // throughout the program.
            dbPath.append("/data/data/");
            dbPath.append(getBaseContext().getPackageName());
            dbPath.append("/databases/");
    
            // Location of the database file stored in assets folder.
            String storedDatabase = Constants.DATABASE_FILE_NAME;
    
            // copy the database
            try {
    
                databaseFile = new File(dbPath.toString(), Constants.DATABASE_FILE_NAME);
    
                if (databaseFile.exists()) {
                    Log.i("database", "database already Exists");
    
                } else {
                    SQLiteDatabase database = openOrCreateDatabase(Constants.DATABASE_FILE_NAME,
                            SQLiteDatabase.OPEN_READONLY, null);
                    database.close();
                    copyDatasbase(getBaseContext().getAssets(), storedDatabase,
                            dbPath + Constants.DATABASE_FILE_NAME);
                }
            } catch (IOException ioException) {
    
                ioException.printStackTrace();
            } catch (Exception exception) {
    
                exception.printStackTrace();
            }
    
        }
    

    if database not exists then copy from asset to internal storage

    private void copyDatasbase(AssetManager manager, String sourceFileName,
                String destinationFileName) throws IOException {
    
            // Read file from AccessManager
            InputStream inputStream = manager.open(sourceFileName);
            OutputStream outputStream = new FileOutputStream(destinationFileName);
            Log.d("-->", "src: " + sourceFileName);
            Log.d("-->", "Des: " + destinationFileName);
            byte[] buffer = new byte[3072];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                // Write the database file to the folder "databases"
                outputStream.write(buffer, 0, length);
    
            }
    
            outputStream.flush();
            outputStream.close();
            inputStream.close();
    
            outputStream = null;
            inputStream = null;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made the following code to retrive data from SQLite database. public Cursor
guys, I have the problem when copying database from local assets folder to /data/data/package_name/databases
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a problem with relations while using sqlite3 on rails. First i build
i have a huge problem with my sqlite3 database on the iphone sdk 4.
I've created a custom view in an SQLite database for an Android application. I'm
I have four tabs which the first one inserts data on the database, the
The problem : populate a sqlite database when user starts the app at the
I want to insert some data to mydatabase.sqlite but I have a problem. There
i have a problem that i can't solve ! (sqlite3, but i think it

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.