I. Sqlite - Define A Schema and Contract Class: "Contact - Info" "Contact - Id" "Name" "Email"
I. Sqlite - Define A Schema and Contract Class: "Contact - Info" "Contact - Id" "Name" "Email"
In this part we are going to see how to create a database schema and the contract class.
We are going to create a table containing three columns: id, name and email.
Now we are going to create the database and table using the SQL open helper class.
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
Log.d("Database Operations", "Table created ....");
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(DROP_TABLE);
onCreate(sqLiteDatabase);
}
}
To insert information into database, we need to add a method that execute the corresponding SQL
query.
database.insert(ContactContract.ContactEntry.TABLE_NAME, null,
contentValues);
Log.d("Database Operations", "One row is inserted ....");
}
Create another fragment called “AddFragmentContact” to add a contact having the following layout.
The AddContactFragment will be launched when we click on the “Add contact” button. Clicking on the
“SAVE” button enables inserting the given row contact into the DB. Modify the code of the
HomeFragment, MainActivity and AddContactFragment classes to implement this.
HomeFragment.java
package com.abc.sqliteexample;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends Fragment implements View.OnClickListener {
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container,
false);
bnSave = view.findViewById(R.id.bn_add_contact);
bnSave.setOnClickListener(this);
return view;
@Override
public void onClick(View view)
{
switch(view.getId())
{
case R.id.bn_add_contact :
dbOpListener.dBOpPerformed(0);
break;
}
MainActivity.java
package com.abc.sqliteexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container)!=null) {
if (savedInstanceState != null) {
return;
}
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
homeFragment).commit();
}
}
@Override
public void dBOpPerformed(int method)
{
switch(method)
{
case 0:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container
, new AddContactFragment()).addToBackStack(null).commit();
break;
}
}
}
AddContactFragment.java
package com.abc.sqliteexample;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class AddContactFragment extends Fragment {
public AddContactFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add_contact, container,
false);
bnSave = view.findViewById(R.id.bn_save);
id = view.findViewById(R.id.txt_contact_id);
name = view.findViewById(R.id.txt_contact_name);
email = view.findViewById(R.id.txt_contact_email);
bnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
contactDbHelper.addContact(Integer.parseInt(sid),sname,semail,
databse);
contactDbHelper.close();
id.setText("");
name.setText("");
email.setText("");
}
});
return view;
}