Ritu Meena
Assistant Professor
Shivaji Collage
Course-B.Sc.(APS)Computer Science
Semester - VI
Paper- Android Programming
SQLITE DATABASE
1. SQLite database is in built into every android device
2. SQLite database is a base class, which provide methods
open(), close(), insert(), update(), delete().
3. execSQL() method used to execute sql statements
4. Data/data/app-name/databases/filename default directory in
which database is saved.
Creating Database and Tables
• To Create and upgrade a database in your Android application
you create a subclass of the SQLiteOpenHelper class. In the
constructor of your subclass you call the super() method of
SQLiteOpenHelper
• Create ->empty activity
• App folder ->java folder-> Right click on Package-> create new
java class -> DatabseHelper.java
SQLite Example
DatbaseHelper.java file
private static class DatabaseHelper extends SQLiteOpenHelper {
Public static final String DATABASE_NAME = “student.db”;
Public static final String TABLE_NAME = “student_table”;
Public static final String COL_1 = “Std_ID”;
Public static final String COL_2 = “Name”;
public static final String COL_3 = “Marks”;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
SQLiteDatabase db = this.getWritableDatbase();
} //after call this method database is created
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(“create table” + TABLE_NAME +”(Std_ID
INTEGER PRIMARY KEY AUTOINCREMENT, NAME
TEXT, MARKS INTEGER)”);
}
@Override
public void onUpgrade(SQLiteDatabase db, int
oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS
“+TABLE_NAME);
onCreate(db);
}
}
MainActivity.java file
DatabaseHelper mydb; //create object of DatabaseHelper class
Protected void onCreate(Bundle savedInstanceState){
Mydb = new DatabseHelper(this);
Insert values to
SQLite Database
table using Android
Continue with DatabaseHelper.java…….(slide no5)
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
db.execSQL("DROP TABLE IF EXISTS
“+TABLE_NAME);
onCreate(db);
}
Public boolean insertData(String Std_ID, String name, String
marks){
SQLiteDatabase db = this.getWritableDatbase(); //we
can remove this line from slide no 4
ContentValues contentvalues = new ContentValues();
contentvalues.put(COL_2, name);
contentvalues.put(COL_3, marks);
long result =
db.insert(TABLE_NAME,null,contentvalues);
If (result == -1)
return false;
else
In MainActivity.java file
Create 3 EditText and a Button name adddata
EditText editName = findViewById(R.id. editName);
EditText editMarks = findViewById(R.id. editMarks);
EditText editID= findViewById(R.id. editID);
Public void gotoActivity(){ \\on Click method
boolean isInserted = mydb.insertData(editName.getText().toString(),
editID.getText().toString(),
editMarks.getText().toString());
}
If (isInserted == true)
Toast.makeText(MainActivity.this,”data
Inserted”,Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,”data not
Inserted”,Toast.LENGTH_LONG).show();
References
• https://developer.android.com/training/data-storage/sqlite