· 8 years ago · Jan 27, 2018, 03:52 PM
1package mad.s20018367.shoppinglist;
2
3import android.content.Context;
4import android.database.sqlite.SQLiteDatabase;
5import android.database.sqlite.SQLiteOpenHelper;
6
7public class CoffeeSQLiteHelper extends SQLiteOpenHelper {
8
9 //define DB name & version#
10 private static final String DATABASE_NAME = "coffee.db";
11 private static final int DATABASE_VERSION = 1;
12
13 //define DB table & column name
14 public static final String TABLE_NAME = "coffeetable";
15 public static final String COLUMN_ID = "_id";
16 public static final String COLUMN_NAME = "name";
17 public static final String COLUMN_ORIGIN = "origin";
18 public static final String COLUMN_VARIETY = "variety";
19 public static final String COLUMN_ROAST = "roast";
20 public static final String COLUMN_PRICE = "price";
21 public static final String COLUMN_QUANTITY = "quantity";
22 public static final String COLUMN_RETAILER = "retailer";
23
24 //SQL statement to create table
25 private static final String DATABASE_CREATE =
26 "CREATE TABLE " + TABLE_NAME
27 + "("
28 + COLUMN_ID + " integer primary key autoincrement, "
29 + COLUMN_NAME + " text not null, "
30 + COLUMN_ORIGIN + " text not null, "
31 + COLUMN_VARIETY + " text not null, "
32 + COLUMN_ROAST + " text not null, "
33 + COLUMN_PRICE + " text not null, "
34 + COLUMN_QUANTITY + " text not null, "
35 + COLUMN_RETAILER + " text not null, "
36 + ");";
37
38
39 public CoffeeSQLiteHelper(Context context){
40 super(context, DATABASE_NAME, null, DATABASE_VERSION);
41 }
42
43 @Override
44 public void onCreate(SQLiteDatabase db) {
45 //create table(s) & insert dummy data
46
47 db.execSQL(DATABASE_CREATE);
48 db.execSQL("INSERT INTO " + TABLE_NAME
49 + " VALUES("
50 + "1, "
51 + "'Jamaica High Mountain', "
52 + "'Jamaica', "
53 + "'Typica', "
54 + "'City', "
55 + "'$98', "
56 + "'8', "
57 + "'Hong Kong Coffee' "
58 + " )"
59 );
60
61 db.execSQL("INSERT INTO " + TABLE_NAME
62 + " VALUES("
63 + "2, "
64 + "'Geisha Special Panama La Esmeralda Geisha Special ', "
65 + "'Panama', "
66 + "'Geisha', "
67 + "'City', "
68 + "'$388', "
69 + "'3', "
70 + "'Hong Kong Coffee' "
71 + " )"
72 );
73
74
75 db.execSQL("INSERT INTO " + TABLE_NAME
76 + " VALUES("
77 + "3, "
78 + "'Ethiopia Operation Red Cherry Kebado G1', "
79 + "'Ethiopia', "
80 + "'Typica & Heirloom Mix', "
81 + "'Full City', "
82 + "'$128', "
83 + "'6', "
84 + "'Hong Kong Coffee' "
85 + " )"
86 );
87
88 db.execSQL("INSERT INTO " + TABLE_NAME
89 + " VALUES("
90 + "4, "
91 + "'Ninety Plus Perci Red N2 ', "
92 + "'Panama', "
93 + "'Geisha', "
94 + "'Full City', "
95 + "'$228', "
96 + "'4', "
97 + "'Hong Kong Coffee' "
98 + " )"
99 );
100
101 db.execSQL("INSERT INTO " + TABLE_NAME
102 + " VALUES("
103 + "5, "
104 + "'Ethiopia Aricha G1 ', "
105 + "'Ethiopia', "
106 + "'Heirloom', "
107 + "'Full City Plus', "
108 + "'$128', "
109 + "'10', "
110 + "'Hong Kong Coffee' "
111 + " )"
112 );
113
114 db.execSQL("INSERT INTO " + TABLE_NAME
115 + " VALUES("
116 + "6, "
117 + "'Guatemala Finca Chacaya ', "
118 + "'Guatemala ', "
119 + "'Caturra', "
120 + "'City', "
121 + "'$108', "
122 + "'12', "
123 + "'Hong Kong Coffee' "
124 + " )"
125 );
126
127 }
128
129 @Override
130 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
131 // delete table & recreate table
132 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
133 onCreate(db);
134 }
135
136
137}