Database [ SQLite Database ] [ local database ]
Android SQLite Database
- SQLite is a local database, so it doesn't need any server or any online place to keep the date.
- that's why no need for any ODBC / JDBC queries /connection for its functionality.
- It saves its data into a text file and it saves locally into your mobile or Android device.
- SQLite is an open source database.
- SQLite supports standard relational database features.
- Android comes in with built-in SQLite database implementation.
- so you don't need to install any extra libraries to use SQLite with Android.
- To create and upgrade a database in your Android application :
- you create a subclass of SQLiteOpenHelper class.
- in the constructor of your subclass, you call the super() method of SQLiteOpenHelper.
Start Code:
- we are going to open an Android project.
- then we are going to create a java class [ ex: DatabaseHelper.java ] for handling the SQLite database.
- this class extends SQLiteOpenHelper.
- implements all the @Override methods. [ onCreate(), onUpgrade() ]
- implements Constructor matching super.
- choose 1st of them, when creating it by ( ALT+ENTER ).
- code:
public class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
code: - Now for creating a database, we need a database name.
- Suppose our database looks like below:
- so, let's declare some global variables, which will help us:
code:public static final String Database_Name = "student.db"; //SQLite not case sensitive public static final String Table_Name = "student_table"; /*Now the columns : here we have 4 columns so*/ public static final String Col_1 = "ID"; public static final String Col_2 = "NAME"; public static final String Col_3 = "SURNAME"; public static final String Col_4 = "MARKS"; //
code: - Edit the constructor:
code:public DatabaseHelper(Context context) { //super(context, name, factory, version); super(context, Database_Name, null, 1); }
code:
Note: whenever this constructor is called your database will be created locally into your Android device.
- Now we are going to create a Table in our database by onCreate() method and write some code for upgrade by onUpgrade() method:
code:@Override public void onCreate(SQLiteDatabase db) { //Query is not case sensitive never //String query_for_createTable = "create table student_table (ID integer primary key autoincrement,NAME text,SURNAME text,MARKS integer)"; //String query_for_createTable = "create table "+Database_Name+" ("+Col_1+" integer primary key autoincrement,"+Col_2+" text,"+Col_3+" text,"+Col_4+" integer)"; String query_for_createTable = "create table "+Database_Name+" ("+ Col_1+" integer primary key autoincrement,"+ Col_2+" text,"+ Col_3+" text,"+ Col_4+" integer)"; db.execSQL(query_for_createTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //String query_for_dropTable = "drop table if exists student_table"; String query_for_dropTable = "drop table if exists "+Database_Name+""; db.execSQL(query_for_dropTable); /*for upgrade table we need to drop the table 1st then create the table newly.*/ onCreate(db); }
code: - Now In our mainAcivity.java, we are going to make an instance of DatabaseHelper.java class.
code:public class MainActivity extends AppCompatActivity { DatabaseHelper myDB; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myDB = new DatabaseHelper(this); } }
code:
Student.db

মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন