0% found this document useful (0 votes)
55 views4 pages

SQLite Database App Development Guide

The document outlines the steps to design an Android application using an SQLite database named 'mydb'. It includes creating a Java class 'MyHelper' for database management, defining a table 'PRODUCTS', and inserting sample data. The application displays product information (name, price, description) in a TextView within the main activity layout when run on an Android Virtual Device (AVD).

Uploaded by

a78134449
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)
55 views4 pages

SQLite Database App Development Guide

The document outlines the steps to design an Android application using an SQLite database named 'mydb'. It includes creating a Java class 'MyHelper' for database management, defining a table 'PRODUCTS', and inserting sample data. The application displays product information (name, price, description) in a TextView within the main activity layout when run on an Android Virtual Device (AVD).

Uploaded by

a78134449
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

Design an application using SQLite database and show its content.

Step 1: Open Android Studio and click on “New Project”.

Step 2: Name the project as ‘Database’.

Step 3: Right click in the app>java>[Link] and select “New” then select “Java”. Make a
java class named “MyHelper”.

Step 4: Add the following script in MyHelper Class.

Script:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

public abstract class MyHelper extends SQLiteOpenHelper {


private static final String dbname= "mydb";
private static final int version = 1;

public MyHelper(Context context){


super(context,dbname,null,version);
}

public void onCreate(SQLiteDatabase db){


String sql = "CREATE TABLE PRODUCTS (_id INTEGER PRIMARY KEY
AUTOINCREMENT, NAME TEXT, DESCRIPTION TEXT, PRICE REAL)";
[Link](sql);

insertData("Jam", "Fruit Jam", 300.50, db);


insertData("Bread","Brown Bread", 25.00, db);
insertData("Butter", "Salted Butter", 85.75, db);
}

private void insertData(String name, String description, double price,


SQLiteDatabase database){
ContentValues values = new ContentValues();
[Link]("NAME", name);
[Link]("DESCRIPTION", description);
[Link]("PRICE", price);
[Link]("PRODUCTS",null,values);
}
}

Step 5: Now in the activity_main.xml write the following code.

Script:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#23B9FD"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</[Link]>

Step 6: In the [Link] write the following code.

Script:
package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

MyHelper helper = new MyHelper(this){


public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion){

}
};
SQLiteDatabase database = [Link]();

Cursor cursor = [Link]("Select NAME, PRICE, DESCRIPTION


FROM PRODUCTS", new String[]{});

if (cursor!=null){
[Link]();
}

StringBuilder builder = new StringBuilder();

do{
String name = [Link](0);
double price = [Link](1);
String description = [Link](2);

[Link](name+" "+price+"
"+description+"\n\n");
} while ([Link]());
TextView textView = (TextView) findViewById([Link]);
[Link]("NAME PRICE DESCRIPTION\
n"+[Link]());
}
}

Step 7: Run the application on AVD.

You might also like