This video shows the complete steps to create a simple Business App using the SQLite Database in Android Studio. It shows how to create the App interface and then write the SQL Query and Insert commands to play with the database. It takes a very simple database table example to show it.
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
Source code:
package com.example.mybusinessappyt.mybusinessappyoutube;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {
private EditText editTextStockName;
private EditText editTextQuantity;
private TextView textViewDisplay;private myDatabaseHandle dbHandle;
private SQLiteDatabase myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);editTextStockName = findViewById(R.id.editTextStockName);
editTextQuantity = findViewById(R.id.editTextQuantity);textViewDisplay = findViewById(R.id.textViewDisplay);
dbHandle = new myDatabaseHandle(this, “FirstDatabase”, null, 1);
myDB = dbHandle.getWritableDatabase();
try{
String stringCreateTable = “CREATE TABLE firstTable(StockName TEXT, Quantity INTEGER)”;
myDB.execSQL(stringCreateTable);
}catch (Exception e){
e.printStackTrace();
}
}public void writeButton(View view){
ContentValues contentValues = new ContentValues();contentValues.put(“StockName”, editTextStockName.getText().toString());
contentValues.put(“Quantity”, Integer.parseInt(editTextQuantity.getText().toString()));
myDB.insert(“firstTable”, null, contentValues);
}public void readButton(View view){
String query = “Select * FROM firstTable WHERE StockName = ‘”+ editTextStockName.getText().toString()+”‘”;
Cursor myCursor = myDB.rawQuery(query, null);
if (myCursor.moveToFirst()){
myCursor.moveToFirst();
textViewDisplay.setText(myCursor.getString(0) + ” — ” + myCursor.getString(1));
}else{
textViewDisplay.setText(“Data Not Found … “);
}}
}
package com.example.mybusinessappyt.mybusinessappyoutube;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;public class myDatabaseHandle extends SQLiteOpenHelper {
public myDatabaseHandle(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.mybusinessappyt.mybusinessappyoutube”><application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” /><category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application></manifest>
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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”><EditText
android:id=”@+id/editTextStockName”
android:layout_width=”144dp”
android:layout_height=”wrap_content”
android:layout_alignParentStart=”true”
android:layout_alignParentTop=”true”
android:layout_marginStart=”13dp”
android:layout_marginTop=”57dp”
android:ems=”10″
android:hint=”@string/stockname”
android:inputType=”textPersonName” /><EditText
android:id=”@+id/editTextQuantity”
android:layout_width=”149dp”
android:layout_height=”wrap_content”
android:layout_alignParentEnd=”true”
android:layout_alignTop=”@+id/editTextStockName”
android:layout_marginEnd=”46dp”
android:ems=”10″
android:hint=”@string/quantity”
android:inputType=”textPersonName” /><TextView
android:id=”@+id/textViewDisplay”
android:layout_width=”284dp”
android:layout_height=”224dp”
android:layout_alignParentBottom=”true”
android:layout_alignParentStart=”true”
android:layout_marginBottom=”44dp”
android:layout_marginStart=”39dp”
android:text=”@string/output_will_be_displayed_here”
android:textAlignment=”center” /><Button
android:id=”@+id/buttonRead”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_alignStart=”@+id/textViewDisplay”
android:layout_marginTop=”134dp”
android:onClick=”readButton”
android:text=”@string/read” /><Button
android:id=”@+id/buttonWrite”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentEnd=”true”
android:layout_alignTop=”@+id/buttonRead”
android:layout_marginEnd=”89dp”
android:onClick=”writeButton”
android:text=”@string/write” />
</RelativeLayout>
This source code is too helpful for me I applied here: http://amazeinvent.com/
We are glad that it helped. Thanks.
Cheers
Programmer World
. –