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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:14:36+00:00 2026-06-16T00:14:36+00:00

The official Android developer website first mentions that we have to first ‘define a

  • 0

The official Android developer website first mentions that we have to first ‘define a schema and contract’, which is something like this:

public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
...

}

The second step mentioned is to ‘Create a Database Using a SQL Helper’.

But all the tutorials I have seen on the web directly create a class using ‘helper’; For example. Which is the correct way? Both?

Also, should I define and create my database in the main Activity or create a separate Activity for the database?

  • 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-06-16T00:14:37+00:00Added an answer on June 16, 2026 at 12:14 am

    Create database in SQLite browser (like SQLite Maestro), copy your database in assets folder in your project. Call createDatabase() method in your MainActivity (which is first in your app, launcher activity).

    private void createDataBase(){
        DataBaseHelper db = new DataBaseHelper(this);
        try {
            db.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            db.close();
        }
    }
    

    And helper class:

    public class DataBaseHelper extends SQLiteOpenHelper{   
    
        private static String DB_PATH = "/data/data/your.package/databases/";   
        private static String DB_NAME = "YourDataBaseName.sqlite";
        private static int DB_VERSION = 1;
    
        private static short WRITE_BUFFER_SIZE = 8192;
    
        private Context context;
    
        /**
         * Constructor
         * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
         * @param context Current application context
         */
        public DataBaseHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
    
            this.context = context;
        }   
    
      /**
         * Creates a empty database on the system and rewrites it with your own database.
         * */
        public synchronized void createDataBase() throws IOException{
    
            boolean dbExist = checkDataBase();
    
            if(dbExist){
                //do nothing - database already exist
            }
            else
            {
                //By calling this method and empty database will be created into the default system path
                   //of your application so we are gonna be able to overwrite that database with our database.
                this.getWritableDatabase();
    
                try {
                    copyDataBase();
                } catch (IOException e) {
                    throw new Error("Error copying database");
                }
    
            }
        }
    
        /**
         * Check if the database already exist to avoid re-copying the file each time you open the application.
         * @return true if it exists, false if it doesn't
         */
        private boolean checkDataBase(){
            SQLiteDatabase checkDB = null;
            try{
                String myPath = DB_PATH + DB_NAME;
    
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            }catch(SQLException e){
                e.printStackTrace();
                //database does't exist yet.
            }catch(Exception e){
                e.printStackTrace();
            }
    
            if(checkDB != null){
                if(checkDB.getVersion() < DB_VERSION){
                    checkDB.close();
                    return false;
                }else{ 
                    checkDB.close();
                    return true;
                }
            }
    
            return false;
        }
    
        /**
         * Copies your database from your local assets-folder to the just created empty database in the
         * system folder, from where it can be accessed and handled.
         * This is done by transfering bytestream.
         */
        private void copyDataBase() throws IOException{     
            //Open your local db as the input stream    
            InputStream myInput = context.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[WRITE_BUFFER_SIZE];
            int length;
            while ((length = myInput.read(buffer)) > 0){
                myOutput.write(buffer, 0, length);
            }
            //Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
            this.getWritableDatabase().close();
        }
    
        @Override
        public synchronized void close() {
                super.close();
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {   
        }
    
        /**
         * Delete database and create new one or copy from assets if exists.
         */
        public void clearDatabase(){      
            context.deleteDatabase(DB_NAME);
            try {
                createDataBase();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            this.getWritableDatabase().close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really like the diagrams and flowcharts from the official developer.android.com website and I
official android dev website says that:「You can implement your tab content in one of
I have read it on the official website that there are several ways in
I have noticed on the official Android blog that WiFi Direct APIs are supported
I've installed the Android Developer tools as per the official instructions . Why doesn't
Right now, you have to use fakeTagsActivity ( http://developer.android.com/resources/samples/NFCDemo/src/com/example/android/nfc/simulator/FakeTagsActivity.html ) if you want to
I have checked the official Android documentation/guide for Looper , Handler and MessageQueue .
I was following the ListView official tutorial - [url]http://developer.android.com/resources/tutorials/views/hello-listview.html[/url], because I get the entries
There is a specific and official analytics SDK for native Android apps (note that
I read the official document about supporting multiple screens http://developer.android.com/guide/practices/screens_support.html According to that document,

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.