In this tutorial we will learn about how to create table in sqlite database in android studio and we will add contacts in this table
Step 1: Create three package by right click on your package named as data , params and Models
Step 2: now we create a class in params
in params we have declare all parameters that we will use in the table
now right click on the params folder and create class and give name Params . class name should start with the capital letter it is the professional approach
Params.java
package guru.happylife.mydemodb.Params;
import android.os.Build;
public class Params {
public static final int DB_VERSION =1;
public static final String DB_NAME = "Contacts";
public static final String DB_TABLE ="contact_table";
public static final String KEY_ID ="id";
public static final String KEY_NAME="name";
public static final String KEY_PHONE="phone";
}
Step 3: now we will create contact class
Contact.java
package guru.happylife.mydemodb.Models;
public class Contact {
private int id ;
private String name;
private String PhoneNumber;
public Contact(String name, String phoneNumber) {
this.name = name;
PhoneNumber = phoneNumber;
}
public Contact(int id, String name, String phoneNumber) {
this.id = id;
this.name = name;
PhoneNumber = phoneNumber;
}
public Contact() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return PhoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
PhoneNumber = phoneNumber;
}
}
Step 4: now we will create a class for database
create a class named as Mydbhandler
package guru.happylife.mydemodb.data;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
import guru.happylife.mydemodb.Models.Contact;
import guru.happylife.mydemodb.Params.Params;
public class Mydbhandler extends SQLiteOpenHelper {
public Mydbhandler(Context context) {
super(context, Params.DB_NAME,null, Params.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String create = "CREATE TABLE " + Params.DB_TABLE + "("
+ Params.KEY_ID +" INTEGER PRIMARY KEY ," + Params.KEY_NAME +" TEXT," + Params.KEY_PHONE
+" TEXT " + ")";
Log.d("ashish","The id is being run: "+ create);
db.execSQL(create);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void addContact(Contact contact){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Params.KEY_NAME,contact.getName());
values.put(Params.KEY_PHONE,contact.getPhoneNumber());
db.insert(Params.DB_TABLE,null,values);
Log.d("ashish","Successfully inserted");
db.close();
}
}
Step:5
MainActivity.java
package guru.happylife.mydemodb;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import guru.happylife.mydemodb.Models.Contact;
import guru.happylife.mydemodb.data.Mydbhandler;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Mydbhandler db = new Mydbhandler(MainActivity.this);
Contact ashish = new Contact();
ashish.setName("Ashish");
ashish.setPhoneNumber("9598652485");
db.addContact(ashish);
Contact amit = new Contact();
amit.setName("amit");
amit.setPhoneNumber("9898682485");
db.addContact(amit);
Contact arpit = new Contact();
arpit.setName("arpit");
arpit.setPhoneNumber("9598652485");
db.addContact(arpit);
Log.d("dbshow","ashish ,amit and arpit contacts are inserted successfully");
}
}
now run your app in your device or emulator
and in write the tag name dbshow in your search history bar and click on logcat
Android Development | Tags: how to create class in android studio