Assosa University
College of Computing and Informatics
Department of Computer Science
Wireless Communication and Mobile Computing
Mobile Application Development
Practical Lab Sessions
Android SQLite Database Connectivity
Prepared by: Gebreigziabher A.
Email: gebrishab@[Link]
1 Android SQLite CRUD Operations
Android SQLite
Database Connectivity
Prepared by: Gebreigziabher A.
Email: gebrishab@[Link]
2 Android SQLite CRUD Operations
Step 1: UI Design
txtUserName
txtPassword
btnRegister listview1
btnViewAll
btnUpdate
btnDelete
btnBack
btnViewList
lblResult
3 Android SQLite CRUD Operations
…Cont’d
<TextView
android:id="@+id/textView"
android:fontFamily="sans-serif-condensed"
android:text="SQLite CRUD Operations"
android:textColor="@color/colorAccent"
android:textSize="25dp"
android:textStyle="bold"/>
<ListView
android:id="@+id/listview1"/>
4 Android SQLite CRUD Operations
…Cont’d
<EditText
android:id="@+id/txtUserName"
android:hint="User Name"
android:fontFamily="sans-serif-condensed"
android:inputType="textPersonName"/>
<EditText
android:id="@+id/txtPassword"
android:hint="Password"
android:inputType="textPassword"
android:fontFamily="sans-serif-condensed"" />
<TextView
android:id="@+id/txtResult"
android:hint="Result"/>
5 Android SQLite CRUD Operations
…Cont’d
<Button
android:id="@+id/btnRegister"
android:text="REGISTER"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:fontFamily="sans-serif-condensed"/>
<Button
android:id="@+id/btnViewAll"
android:text="VIEW ALL"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:fontFamily="sans-serif-condensed"/>
<Button
android:id="@+id/btnViewAllList"
android:text="VIEW LIST"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:fontFamily="sans-serif-condensed"/>
6 Android SQLite CRUD Operations
…Cont’d
<Button
android:id="@+id/btnUpdate"
android:text="UPDATE"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:fontFamily="sans-serif-condensed"/>
<Button
android:id="@+id/btnDelete"
android:text="DELETE"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:fontFamily="sans-serif-condensed"/>
<Button
android:id="@+id/btnBack"
android:text="BACK"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:fontFamily="sans-serif-condensed"/>
7 Android SQLite CRUD Operations
Step 2: [Link]
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "[Link]";
public static final String TABLE_NAME = "tblStudent";
public static final String COLUMN_1 = "userName";
public static final String COLUMN_2 = "password";
Database Version
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null,1);
}
public void onCreate(SQLiteDatabase db){
[Link]("CREATE TABLE "+TABLE_NAME+
"(userName TEXT PRIMARY KEY, password TEXT)");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVesrion){
8 [Link]("DROP TABLE IF EXISTS "+TABLE_NAME);
Android SQLite CRUD Operations
}
Insert Record
public boolean insertData(String userName, String password){
.
SQLiteDatabase db = [Link]();
ContentValues contentValues = new ContentValues();
[Link](COLUMN_1, userName);
[Link](COLUMN_2, password);
long result = [Link](TABLE_NAME, null, contentValues);
[Link]();
if(result==-1) {
return false;
}else {
return true;
}
}
public Cursor getAllData(){ //Retrieve Record
SQLiteDatabase db =[Link]();
Cursor result = [Link]("SELECT *FROM "+TABLE_NAME, null);
return
9 result; Android SQLite CRUD Operations
}
Update Record
public boolean updateData(String userName, String password) {
.
SQLiteDatabase db = [Link]();
ContentValues contentValues = new ContentValues();
[Link](COLUMN_1, userName);
[Link](COLUMN_2, password);
.
int result = [Link](TABLE_NAME, contentValues,
"userName=?", new String[]{userName});
if (result > 0) {
return true;
}
else {
return false;
}
}
.
public Integer deleteData(String userName){ //Delete Record
SQLiteDatabase db = [Link]();
int result = [Link](TABLE_NAME,"userName=?",
new String[]{userName});
return result;
} 10 Android SQLite CRUD Operations
View All - List View
public Cursor viewAllList() { //Displaying to List View
SQLiteDatabase db = [Link]();
String[] columns = new String[]{COLUMN_1, COLUMN_2};
Cursor cursor = [Link](TABLE_NAME, columns,
null, null, null, null, null);
return cursor;
}
}
11 Android SQLite CRUD Operations
Step 3: [Link]
public class MainActivity extends AppCompatActivity {
EditText txtUserName, txtPassword;
TextView txtResult;
Button btnRegister, btnViewAll, btnUpdate, btnDelete, btnViewList;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
DatabaseHelper db = new DatabaseHelper(this);
txtUserName = (EditText) findViewById([Link]);
txtPassword = (EditText) findViewById([Link]);
txtResult = (TextView) findViewById([Link]);
btnRegister = (Button) findViewById([Link]);
btnViewAll = (Button) findViewById([Link]);
btnUpdate = (Button) findViewById([Link]);
btnDelete = (Button) findViewById([Link]);
btnViewList = (Button) findViewById([Link]);
12 Android SQLite CRUD Operations
Register
[Link](new [Link]() {
@Override
public void onClick(View v) {
String userName = [Link]().toString();
String password = [Link]().toString();
boolean result = [Link](userName, password);
if(result==true){
[Link](getApplicationContext(),
"Registered!",Toast.LENGTH_LONG).show();
}
else {
[Link](getApplicationContext(),
"Failed to Register!",Toast.LENGTH_LONG).show();
}
}
});
13 Android SQLite CRUD Operations
View All – Text View(Label)
[Link](new [Link]() {
@Override
public void onClick(View v) {
Cursor result = [Link]();
StringBuffer stringBuffer = new StringBuffer();
if(result!=null && [Link]()>0) {
while ([Link]()) {
[Link]("UserName: " + [Link](0) + "\n");
[Link]("Password: " + [Link](1) + "\n");
}
[Link]([Link]());
}
else {
[Link](getApplicationContext(), "No Data to
Retrieve!", Toast.LENGTH_LONG).show();
}
}
});
14 Android SQLite CRUD Operations
Update
[Link](new [Link]() {
@Override
public void onClick(View v) {
String userName = [Link]().toString();
String password = [Link]().toString();
boolean result = [Link](userName, password);
if(result==true){
[Link](getApplicationContext(),
"Data Updated!", Toast.LENGTH_LONG).show();
}
else {
[Link](getApplicationContext(),
"No Data to Update!", Toast.LENGTH_LONG).show();
}
}
});
15 Android SQLite CRUD Operations
Delete
[Link](new [Link]() {
@Override
public void onClick(View v) {
String userName = [Link]().toString();
int result = [Link](userName);
if(result==1){
[Link](getApplicationContext(),
"Data Deleted!", Toast.LENGTH_LONG).show();
}
else {
[Link](getApplicationContext(),
"Data Not Deleted!", Toast.LENGTH_LONG).show();
}
}
});
16 Android SQLite CRUD Operations
View List
[Link](new
[Link]() { //Displaying to List View
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
[Link]);
startActivity(i);
}
});
}
}
17 Android SQLite CRUD Operations
View All - List View
public class ViewAllList extends Activity {
//Displaying to List View ([Link])
ArrayList<String> users = new ArrayList<String>();
ArrayAdapter<String> adapter;
ListView listView;
Button btnBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_view);
DatabaseHelper db = new DatabaseHelper(this);
adapter = new ArrayAdapter<String>(this,
[Link].simple_list_item_1, users);
btnBack = (Button) findViewById([Link]);
listView = (ListView)findViewById([Link].listview1);
18 Android SQLite CRUD Operations
…Cont’d
Cursor cursor = [Link]();
if(cursor!=null && [Link]()>0) {
while ([Link]()) {
String userName = [Link](0);
[Link](userName);
}
[Link]();
[Link](adapter);
} else {
[Link](getApplicationContext(),"No Data Found!",
Toast.LENGTH_LONG).show();
}
[Link](new [Link]() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
[Link](getApplicationContext(), [Link](position),
Toast.LENGTH_SHORT).show();
}
});
19 Android SQLite CRUD Operations
Back
[Link](new [Link]() {
@Override
public void onClick(View v) {
Intent in = new Intent(getApplicationContext(),
[Link]);
startActivity(in);
}
});
}
}
20 Android SQLite CRUD Operations