Android SQLite Database Create | Android SQLite | Database Create | How to create database in android app

Share:

 How to create database in android app By SQLite

 Learning Part :

  1. How to create SQLite Database in Android 

  2. Insert Data In Database

  3. Delete Data To Database 

    Lest Go Started....

    1. At first Open Your Android Studio. Then Create New Project .  Then Create New Java Class "MyDb" in your main package and extend SQLiteOpenHelper . Fallow below Code.

     
    
      public class MyDb extends SQLiteOpenHelper {
        private static final int VERSION = 1;
        private static final String DATA_BASE_NAME = "mydb.db";
    
        public static final String TABLE_NAME = "friends";
        public static final String FIRST_NAME = "firstname";
        public static final String LAST_NAME = "lastname";
        public static final String _DATE = "date";
    
        private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME
                + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                + FIRST_NAME + " TEXT, "
                + LAST_NAME + " TEXT, "
                + _DATE + " DATE)";
    
        private static final String DELETE_CREATE = "DROP TABLE IF EXISTS " + TABLE_NAME;
        private static final String TAG = "MyDb";
    
    
        public MyDb(@Nullable Context context) {
            super(context, DATA_BASE_NAME, null, VERSION);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(TABLE_CREATE);
            Log.v(TAG, "onCreate: Data Base Create.");
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int i, int i1) {
            db.execSQL(DELETE_CREATE);
            Log.v(TAG, "onUpgrade: Data Base Upgrade.");
            onCreate(db);
        }
    
        public void insertData(SQLiteDatabase db,String fname,String lname) {
    
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
            String date = simpleDateFormat.format(new Date());
            ContentValues values = new ContentValues(3);
            values.put(FIRST_NAME, fname);
            values.put(LAST_NAME, lname);
            values.put(_DATE, simpleDateFormat.format(new Date()));
    
            long newData =db.insert(TABLE_NAME, null, values);
            Log.v(TAG, ""+newData);
        }
    }
    
    
2. Then Copy below Code and Paste Your MainActivity.

 
  public class MainActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener {

    private MyDb myDb;
    private SimpleCursorAdapter adapter;
    private SQLiteDatabase database;
    private Cursor mCursor;

    private ListView listView;
    private ImageView mAdd;
    private EditText fistname,lastname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fistname = (EditText)findViewById(R.id.fname);
        lastname = (EditText)findViewById(R.id.lname);
        mAdd = (ImageView)findViewById(R.id.imageView);
        mAdd.setOnClickListener(this);

        listView = (ListView)findViewById(R.id.listview);
        listView.setOnItemClickListener(this);



        myDb = new MyDb(this);




    }

    @Override
    protected void onResume() {
        super.onResume();
        database = myDb.getWritableDatabase();

        String[] projection = new String[]{"_id",MyDb.FIRST_NAME,MyDb.LAST_NAME,MyDb._DATE};
        mCursor = database.query(MyDb.TABLE_NAME,projection,null,null,null,null,null);

        String[] headePprojection = new String[]{MyDb.FIRST_NAME,MyDb.LAST_NAME,MyDb._DATE};

        adapter = new SimpleCursorAdapter(this,R.layout.item_list,mCursor,headePprojection,
                new int[]{R.id.textView1,R.id.textView2,R.id.textView3});

        listView.setAdapter(adapter);

    }

    @Override
    protected void onPause() {
        super.onPause();
        database.close();
        mCursor.close();
    }

    @Override
    public void onClick(View view) {

        if (!TextUtils.isEmpty(fistname.getText().toString()) && !TextUtils.isEmpty(lastname.getText().toString())) {
            myDb.insertData(database, fistname.getText().toString(), lastname.getText().toString());
            mCursor.requery();
            adapter.notifyDataSetChanged();

            fistname.setText(null);
            lastname.setText(null);
        }

    }

    @Override
    public void onItemClick(AdapterView adapterView, View view, int position, long id) {

        mCursor.moveToPosition(position);

        String rowId = mCursor.getString(0);

        database.delete(MyDb.TABLE_NAME,"_id=?",new String[]{rowId});
        mCursor.requery();
        adapter.notifyDataSetChanged();
    }
}


3. Run this app and enjoy...........



9 comments:

  1. hello can I get your gmail contact

    ReplyDelete
  2. Thanks for this great post, i find it very interesting and very well thought out and put together. I look forward to reading your work in the future. descargar app betcris

    ReplyDelete
  3. Thanks for this great post, i find it very interesting and very well thought out and put together. I look forward to reading your work in the future. codere app

    ReplyDelete
  4. Can we code apps with by learning such coding tutorials?

    ReplyDelete
  5. I can set up my new idea from this post. It gives in depth information. Thanks for this valuable information for all,.. casas de apuestas online mexico

    ReplyDelete
  6. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. calientesports.mx descargar

    ReplyDelete
  7. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. https://cults3d.com/en/users/calientesportsmxappandroid

    ReplyDelete
  8. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! mejores casas de apuestas méxico

    ReplyDelete
  9. imToken download wallet imtoken for Android is an easy and secure digital trusted wallet by millions. It supports BTC, ETH, EOS, TRX, CKB, BCH, LTC, DOT, KSM, FIL and many other crypto assets. imtoken下载

    ReplyDelete

Wellcome to Bengalitutorial..