· 6 years ago · May 08, 2019, 02:56 PM
1package com.example.damar.charlearning2;
2
3import android.content.Context;
4import android.database.sqlite.SQLiteDatabase;
5import android.database.sqlite.SQLiteOpenHelper;
6
7
8public class SQLiteDB extends SQLiteOpenHelper {
9 public static final int MYDATABASE_VERSION = 1;
10 public static final String MYDATABASE_NAME = "womancalendar1";
11 public static final String MYDATABASE_TABLE = "tabel_period1";
12 public static final String KEY_ID = "_id";
13 public static final String KEY_NOTES = "notes";
14 public static final String KEY_MEDICINE = "medicine";
15 public static final String KEY_MOODS = "moods";
16
17 //-------------DEKLARASI UNTUK MEMBUAT TABEL-------------//
18 private static final String SCRIPT_CREATE_TABLE =
19 "create table " + MYDATABASE_TABLE + " ("
20 + KEY_ID + " integer primary key autoincrement, "
21 + KEY_NOTES + " text not null, "
22 + KEY_MEDICINE + " text not null, "
23 + KEY_MOODS + " text not null);";
24
25 //-------------DEKLARASI UNTUK MENGHAPUS TABEL-------------//
26 private static final String SCRIPT_DELETE_TABLE="DROP TABLE IF EXISTS " + MYDATABASE_TABLE;
27
28
29 public SQLiteDB(Context context){
30 //BUAT DATABASE JIKA TIDAK ADA
31 super(context,MYDATABASE_NAME,null,MYDATABASE_VERSION);
32 }
33
34
35 public void onCreate(SQLiteDatabase db){
36 //BUAT TABEL
37 db.execSQL(SCRIPT_CREATE_TABLE);
38
39 }
40
41 public void onUpgrade(SQLiteDatabase db,int olv,int newv){
42 db.execSQL(SCRIPT_DELETE_TABLE);
43 onCreate(db);
44 }
45}