SQLite is a opensource SQL database that stores data to a text file on a device.Android comes in with built in SQLite database implementation. SQLite supports all the relational database features.
SQLite is used in Android to implement the database concept. When you are building an application there might be a requirement to group and store the data(groups corresponds to tables).
To Create SQLite Database We Need To Start New Androi Studio Project, If you don’t know how please go HERE.
- And Create New “Java Class” named “DatabaseHelper”then “OK”.
- Add “extends SQLiteOpenHelper” to your Class And “Implements Methods” (onCreate,onUpgrade).
- Add 2 String :
- Database : Our Database Name.
- Table : Our Table Name.
public static final String Database = “Uni.db”;
public static final String Table = “students”;
Right-Click And Generate Constructor Method.
- Go to onCreate Method and copy/paste that code :
db.execSQL(“CREATE TABLE “+Table+”(id Integer PRIMARY KEY AUTOINCREMENT,NAME TEXT,ROLL_NO TEXT);”);
This line Will Execute the query to create our Table.
- Go to Constructor Method and copy/paste that code to get permission in order to write in Database:
SQLiteDatabase mdb = this.getWritableDatabase();
- Then add this line if we update our App we don’t need to create new Table we just need to use the same one with the same Data :
db.execSQL(“DROP TABLE IF EXISTS ” + Table+””);
- Now Go to MainActivity and copy/paste those codes :
helper= new DatabaseHelper(this);
- Then Run The Application on the Emulator :
This App is for creating Database so nothing fancy to see 🙂
- But if you go to “Device File Explorer” and look for Package Name>databases you will see our Database :
I hope that’s easy and clear.
Next Time we will Add,Read and Delete Data from our Database, Until then, have a good time.