Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create database using SQLite Helper

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 491
    Comment on it

    Create Database class

    Here is an example to create database using SqliteHelper class in android.
    We are creating a database with the name "detail" and having 4 columns as :-Key, Name, City and Age.

    public class CreateDatabase {
        public DatabaseHelper mDbHelper;
        public SQLiteDatabase mDb;  
        public static final String DATABASE_NAME= "Detail_database";    // Internal Database
    //    public static final String DATABASE_NAME= Environment.getExternalStorageDirectory()+File.separator+"Detail_database.sqlite";      //External Database
        public static final String TAG = "tag";
        public static final int DATABASE_VERSION = 1;
    
        public static final String Id = "id";
    
        // Definition for Detail Table
        public static final String Key = "key";
        public static final String Name = "name";
        public static final String City = "city";
        public static final String Age = "age";
        public static final String DATABASE_TABLE_DETAIL = "DETAIL";
    
        /** * Database creation sql statement */
    
        //Detail Table creation
        private static final String DATABASE_CREATE_DETAIL = "CREATE TABLE IF NOT EXISTS "
                + DATABASE_TABLE_DETAIL + " (Key text PRIMARY KEY, "+" Name text, "+" City text" +
                ", "+" Age text)";
    
    
        private final Context mCtx;
    
        private static class DatabaseHelper extends SQLiteOpenHelper {
            DatabaseHelper(Context context) { 
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }
    
            /**
             * Creating Tables
             */
    
            @Override
            public void onCreate(SQLiteDatabase db) {
                try{
                    db.execSQL(DATABASE_CREATE_DETAIL);
    
                    Log.i(TAG, "DATABASE IS CREATING.............");
                }
                catch(SQLException e){
    
                }
            }
    
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
            }
        }
        public CreateDatabase(Context ctx) {
    
            this.mCtx = ctx;
        }
    
        public void DeleteDatabase(String table)
        {
            mDb.delete(table, null, null);   
        }
    
        public CreateDatabase open() throws SQLException {
    
            //  Log.i(TAG, "OPening DataBase Connection....");
            mDbHelper = new DatabaseHelper(mCtx);
            mDb = mDbHelper.getWritableDatabase();
            return this;
        }
    
        public SQLiteDatabase openDB() throws SQLException{
            //  Log.i(TAG, "OPening DataBase Connection....");
            mDbHelper = new DatabaseHelper(mCtx);
            mDb = mDbHelper.getWritableDatabase();
            return mDb;
        }
        public void close() {
            //  Log.i(TAG, "Closing DataBase Connection....");
            mDbHelper.close();
    
        }
    
        // Inserting record values for Detail
        public long createElementsDetail(String key, String name, String city, String age) {
            //  Log.i(TAG, "Inserting Detail   ...");
    
            ContentValues initialValues = new ContentValues();
            initialValues.put(Key, key);
            initialValues.put(Name, name);
            initialValues.put(City, city);
            initialValues.put(Age, age);
    
            return mDb.insert(DATABASE_TABLE_DETAIL, null, initialValues);
        }
    
        /**
         * Cursor for Detail
         * @return
         * @throws android.database.SQLException
         */
        public Cursor fetchDetail() throws SQLException{
            Cursor mCursor = mDb.query(DATABASE_TABLE_DETAIL, new String[] {Key, Name, City, Age},null,null,null,null,null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }
    }
    

    MainActivity

    Here is the main activity class to call the create database class and to fill data in the columns of the table.

    public class MainActivity extends Activity {
        CreateDatabase db;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            db = new CreateDatabase(MainActivity.this);
            db.open();
            db.createElementsDetail("1", "Ankit", "Dehradun", "12");
            db.close();
        }
    }
    

    Manifest Permissions
    We need external storage permission if we make database on external storage.

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
        </uses-permission>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: