SQLite Database Example
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Database"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"/>
<EditText
android:layout_width="160dp"
android:layout_height="wrap_content"
android:id="@+id/txtName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number:"/>
<EditText
android:layout_width="160dp"
android:layout_height="wrap_content"
android:inputType="phone"
android:id="@+id/txtPhonenumber"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"
android:onClick="btnInsert"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Delete"
android:onClick="btnDelete"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
1
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:onClick="btnUpdate"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Search"
android:onClick="btnSearch"/>
</LinearLayout>
</LinearLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText n;
EditText p;
SQLiteDatabase mydatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
n=findViewById([Link]);
p=findViewById([Link]);
mydatabase=openOrCreateDatabase("contact_db",MODE_PRIVATE,null);
[Link]("CREATE TABLE IF NOT EXISTS my_contact (" +
"name varchar,phone varchar);");
}
public void btnInsert(View view){
[Link]("INSERT INTO my_contact VALUES ('"+[Link]()
+"'," +
"'"+[Link]()+"');");
}
public void btnDelete(View v){
[Link]("DELETE FROM my_contact where name=='"+[Link]()
2
+"'");
}
public void btnUpdate(View v){
[Link]("UPDATE my_contact set phone='"+[Link]()+"'" +
"where name=='"+[Link]()+"'");
}
public void btnSearch(View v){
Cursor c=[Link]("SELECT * FROM my_contact where " +
"name='"+[Link]()+"'",null );
if([Link]()){
[Link]([Link](1));
}
}
}