0% found this document useful (0 votes)
5 views3 pages

SQLite DatabaseConnectivity Example

The document provides an example of an SQLite database implementation in an Android application. It includes an XML layout file for the user interface with fields for name and phone number, as well as buttons for inserting, deleting, updating, and searching contacts. The Java code initializes the database, creates a contacts table, and defines the functionality for each button's action to manipulate the contact data.

Uploaded by

eliasendale130
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

SQLite DatabaseConnectivity Example

The document provides an example of an SQLite database implementation in an Android application. It includes an XML layout file for the user interface with fields for name and phone number, as well as buttons for inserting, deleting, updating, and searching contacts. The Java code initializes the database, creates a contacts table, and defines the functionality for each button's action to manipulate the contact data.

Uploaded by

eliasendale130
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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));
}
}
}

You might also like