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>com.example.database and select “New” then select “Java”. Make a
java class named “MyHelper”.
Step 4: Add the following script in MyHelper Class.
Script:
package com.example.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
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)";
db.execSQL(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();
values.put("NAME", name);
values.put("DESCRIPTION", description);
values.put("PRICE", price);
database.insert("PRODUCTS",null,values);
}
}
Step 5: Now in the activity_main.xml write the following code.
Script:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 6: In the MainActivity.java write the following code.
Script:
package com.example.database;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyHelper helper = new MyHelper(this){
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion){
}
};
SQLiteDatabase database = helper.getReadableDatabase();
Cursor cursor = database.rawQuery("Select NAME, PRICE, DESCRIPTION
FROM PRODUCTS", new String[]{});
if (cursor!=null){
cursor.moveToFirst();
}
StringBuilder builder = new StringBuilder();
do{
String name = cursor.getString(0);
double price = cursor.getDouble(1);
String description = cursor.getString(2);
builder.append(name+" "+price+"
"+description+"\n\n");
} while (cursor.moveToNext());
TextView textView = (TextView) findViewById(R.id.txtData);
textView.setText("NAME PRICE DESCRIPTION\
n"+builder.toString());
}
}
Step 7: Run the application on AVD.