DataBase Connectivity with SQLite

DataBase Connectivity with SQLite

DataBase Connectivity with SQLite

  1. First create project and create add no Activity

          This activity is created because I have to put various packages in that.

  1. In Activity, create and empty activity and select that as launcher activity as well (for splashScreen).
  2. Then create activity name it AddStudentActivity

Now define its user interface

First start with splash screen

  1. Change layout to relative, write student database, also set size, color and style of text.

Then we will go to AddStudentActivity and design that according the need.

Now write code of splashscreen, AddStudentActivity. We have also design a DBHelper class for executing the query and connectivity of DataBase. The code file of all required file is given .

 

 

Splash activity XML

<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=”.activity.SplashActivity”>

<TextView
android:id=”@+id/textView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentEnd=”true”
android:layout_alignParentBottom=”true”
android:layout_marginEnd=”54dp”
android:layout_marginBottom=”324dp”
android:text=”Student Database”
android:textColor=”@color/colorPrimary”
android:textSize=”40sp”
android:textStyle=”bold” />
</RelativeLayout>

 

Splash activity java file

public class SplashActivity extends AppCompatActivity {
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// getSupportActionBar().hide();
handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent=new Intent(SplashActivity.this, AddStudentActivity.class);
startActivity(intent);
finish();
}
},3000);
}
}

 

 

 

AddStudentActivity.XML <LinearLayout 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”
android:orientation=”vertical”
android:padding=”16dp”
tools:context=”.activity.AddStudentActivity”>

<EditText
android:id=”@+id/edName”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:inputType=”textPersonName”
android:hint=”Name” />

<Spinner
android:id=”@+id/spinnerCourse”
android:layout_width=”match_parent”
android:layout_height=”wrap_content” />

<EditText
android:id=”@+id/edContact”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:inputType=”number”
android:hint=”Contact” />

<EditText
android:id=”@+id/edTotalFee”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:inputType=”number”
android:hint=”Total Fee” />

<EditText
android:id=”@+id/edFeePaid”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:inputType=”number”
android:hint=”Fee Paid” />

<Button
android:id=”@+id/btnSave”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”SAVE”
android:onClick=”process”
/>

<Button
android:id=”@+id/btnShowAll”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”SHOW ALL”
android:onClick=”process”
/>
</LinearLayout>

 

 

AddStudentActivity.javapublic class AddStudentActivity extends AppCompatActivity {
Spinner spinnerCourse;

String[] courses={“C++”, “Java”, “Python”,“IOS”};
ArrayAdapter<String> courseAdapter;
EditText edName, edContact, edTotalFee, edFeePaid;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_student);
spinnerCourse=findViewById(R.id.spinnerCourse);
courseAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,courses);
spinnerCourse.setAdapter(courseAdapter);

edName=findViewById(R.id.edName);
edContact=findViewById(R.id.edContact);
edTotalFee=findViewById(R.id.edTotalFee);
edFeePaid=findViewById(R.id.edFeePaid);
}

public void process(View view) {
switch (view.getId())
{
case R.id.btnSave:

String name=edName.getText().toString();
String course=spinnerCourse.getSelectedItem().toString();
String contact=edContact.getText().toString();
String strTotalFee=edTotalFee.getText().toString();
String strFeePaid=edFeePaid.getText().toString();

int totalFee = Integer.parseInt(strTotalFee);
int feePaid = Integer.parseInt(strFeePaid);

DBHelper dbHelper = new DBHelper(this);
Student student=new Student(name,course,contact,totalFee,feePaid);

long result = dbHelper.addStudent(name,course,contact,totalFee,feePaid);
if(result != -1)
{
Toast.makeText(this, “Student saved”, Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(this, “Not Saved Student”, Toast.LENGTH_SHORT).show();
}
break;

case R.id.btnShowAll:
startActivity(new Intent( AddStudentActivity.this, StudentListActivity.class));
break;
}
}

}

 

DBHelper.java public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME=“intell.db”;
private static final int DATABASE_VERSION=1;

private static final String KEY_ID=“id”;
private static final String KEY_NAME=“name”;
private static final String KEY_COURSE=“course”;
private static final String KEY_CONTACT=“contact”;
private static final String KEY_TOTAL_FEE=“total_fee”;
private static final String KEY_FEE_PAID=“fee_paid”;
//HERE WE WILL WRITE QUERY
private static final String TABLE_STUDENT=“tbl_student”;

private static final String CREATE_TABLE_STUDENT= “CREATE TABLE ” + TABLE_STUDENT+“(”
+ KEY_ID + ” INTEGER PRIMARY KEY AUTOINCREMENT,”
+ KEY_NAME + ” TEXT NOT NULL,”
+ KEY_COURSE + ” TEXT NOT NULL,”
+ KEY_CONTACT + ” TEXT NOT NULL,”
+ KEY_TOTAL_FEE + ” INTEGER NOT NULL,”
+ KEY_FEE_PAID + ” INTEGER NOT NULL)”;

public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_TABLE_STUDENT);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(“Drop table if EXISTS ” + TABLE_STUDENT );
onCreate(db);
}
public long addStudent(String name, String course, String contact, int totalFee, int feePaid) {
SQLiteDatabase db=getWritableDatabase();

ContentValues values= new ContentValues();

values.put(KEY_NAME,name);
values.put(KEY_COURSE,course);
values.put(KEY_CONTACT,contact);
values.put(KEY_TOTAL_FEE,totalFee);
values.put(KEY_FEE_PAID,feePaid);

return db.insert(TABLE_STUDENT, null, values);
}
}

Please follow and like us:

Author: Author

Leave a Reply

Your email address will not be published. Required fields are marked *