· 6 years ago · Jan 17, 2020, 01:46 AM
1public class Database extends SQLiteOpenHelper {
2
3 private static final int DATABASE_VERSION = 1;
4
5 private static final String DATABASE_NAME = "dbkamus";
6 private static final String TABLE_NAME = "kamus";
7 private static final String INGGRIS= "inggris";
8 private static final String INDONESIA = "indonesia";
9
10 public Database(Context context) {
11 super(context, DATABASE_NAME, null, DATABASE_VERSION);
12 }
13
14 @Override
15 public void onCreate(SQLiteDatabase sqLiteDatabase) {
16 createTable(sqLiteDatabase);
17 generateData(sqLiteDatabase);
18 }
19
20 public void createTable(SQLiteDatabase db) {
21 db.execSQL("drop table if exists " + TABLE_NAME);
22 db.execSQL("create table if not exists " + TABLE_NAME +
23 " (id integer primary key autoincrement, " +
24 INGGRIS + " text, " +
25 INDONESIA + " text)");
26 Log.i("DataKamus", "Create Table Success");
27 }
28
29 public void generateData(SQLiteDatabase db) {
30 ContentValues cv = new ContentValues();
31 cv.put(INGGRIS, "run");
32 cv.put(INDONESIA, "lari");
33 db.insert(TABLE_NAME, null, cv);
34 cv.put(INGGRIS, "walk");
35 cv.put(INDONESIA, "jalan");
36 db.insert(TABLE_NAME, null, cv);
37 cv.put(INGGRIS, "read");
38 cv.put(INDONESIA, "membaca");
39 db.insert(TABLE_NAME, null, cv);
40 }
41
42 @Override
43 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
44 Log.i("DataKamus", "On Upgrade database from " + i + " to " + i1);
45 }
46
47 public String translate(String input) {
48 String result = "-";
49 SQLiteDatabase db = this.getWritableDatabase();
50 Cursor cursor = db.rawQuery("select * from " + TABLE_NAME
51 + " where " + INGGRIS + " = '" + input + "'", null);
52
53 if (cursor.moveToFirst()) {
54 result = cursor.getString(2);
55 }
56
57 return result;
58 }
59
60}