Langsung ke konten utama

How To Create Database SQLite (CRUD SQLite Android)

Today is the tutorial to create inventory application for Android.

This app will be using SQLite database and MVC paradigm, so make sure you have understood them.

Firstly, we need to make an Android project first (Learn to create Android project with Android Studio).

Name your package to any name you like, but for this tutorial I name the package to be id.aft.appinventory. You can change it to the name you like.

As I mentioned in the second paragraph above that this app will be using MVC paradigm, and we will use mode class, which is item.java.

The item class takes the role to define item object along with its functions and attributes. Just like item in general, they have names, ID, brands and etc.

So the following are the codes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package id.aft.appinventory;
public class Item {
    private long id;
    private String item_name;
    private String item_brand;
    private String item_price;
    public Item()
    {
    }
    /**
     * @return the id
     */
    public long getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }
    /**
     * @return the item_name
     */
    public String getItem_name() {
        return item_name;
    }
    /**
     * @param item_name the item_name to set
     */
    public void setItem_name(String item_name) {
        this.item_name = item_name;
    }
    /**
     * @return the item_brand
     */
    public String getItem_brand() {
        return item_brand;
    }
    /**
     * @param item_brand the item_brand to set
     */
    public void setItem_brand(String item_brand) {
        this.item_brand = item_brand;
    }
    /**
     * @return the item_price
     */
    public String getItem_price() {
        return item_price;
    }
    /**
     * @param item_price the item_price to set
     */
    public void setItem_price(String item_price) {
        this.item_price = item_price;
    }
    @Override
    public String toString()
    {
        return "Item "+ item_name +" "+ item_brand + " "+ item_price;
    }
}

We have the item inventory model now. Simple isn't it? However the above class only contains attributes and functionalities getter and setter.

So after we have finished creating the item class, now we continue to create Helper Database class.

Database Helper


As its name refers to, this function is to facilitate us for creating and accessing SQLite database that will be used by the app.

Mostly this class contains methods relate to database management, like onUpgrade() and onCrate().

Actually Database Helper has already own class, which is SQLiteOpenHelper. So we just need to extend this class from SQLiteOpenHelper class.

So the following is the code for DBHelper.java.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package id.twooh.appinventory;
//deklarasi import package
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper{
        /** constantas declaration to be used on database, like table name, column name, and database version **/
    public static final String TABLE_NAME = "data_inventori";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NAME = "item_name";
    public static final String COLUMN_MERK = "item_brand";
    public static final String COLUMN_HARGA = "item_price";
    private static final String db_name ="inventori.db";
    private static final int db_version=1;
    // SQL command to create new database table
      private static final String db_create = "create table "
          + TABLE_NAME + "("
          + COLUMN_ID +" integer primary key autoincrement, "
          + COLUMN_NAME+ " varchar(50) not null, "
          + COLUMN_MERK+ " varchar(50) not null, "
          + COLUMN_HARGA+ " varchar(50) not null);";
    public DBHelper(Context context) {
        super(context, db_name, null, db_version);
        // Auto generated
    }
        //execute SQL command above to create new database table
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(db_create);
    }
        // to be run if want to upgrade the database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(DBHelper.class.getName(),"Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

As we have seen above. DBHelper.java class mostly filled with constanta such as table name, database name, column name and etc.

This will facilitate us when we wish to use that constanta, we don't need to write the table name repetitively, but simply access the DBHelper class.

And there is also constanta in the form: SQL statement, this constanta by database expert is simply a command to be used for creating new database table.

SQL command will be executed on onCreate() method.

Komentar

Postingan populer dari blog ini

How To Create Android App Using Android Studio (Complete Guides)

How to create Android App using Android Studio (Complete Guides). Hi guys today I would like to share a tutorial on how to create Android app using Android Studio.

Surely you have fully aware that currently Android Studio had been ordained to be the most recommended IDE officially by Google to help users creating an Android app.

Tutorial To Create Spinner (DropDown List) On Android

Today we are going to learn on how to create Spinner in Android. Spinner is one of the views that we see a lot almost in every Android application.