· 8 years ago · Jul 30, 2018, 03:36 AM
1Updating SQLite database android
2import android.content.ContentValues;
3import android.content.Context;
4import android.database.sqlite.SQLiteDatabase;
5import android.database.sqlite.SQLiteOpenHelper;
6
7public class Cook_tab_snacks_data extends SQLiteOpenHelper {
8
9public static final String DATABASE_NAME = "Snacks";
10
11public Cook_tab_snacks_data(Context context) {
12 super(context, DATABASE_NAME, null, 1);
13}
14
15@Override
16public void onCreate(SQLiteDatabase db) {
17
18 String sql = "CREATE TABLE IF NOT EXISTS snacks (" +
19 "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
20 "name TEXT, " +
21 "disc TEXT, " +
22 "photo TEXT, " +
23 "prep TEXT, " +
24 "thumb TEXT, " +
25 "ingre TEXT, " +
26 "howto TEXT, " +
27 "info TEXT, " +
28 "snackId INTEGER)";
29 db.execSQL(sql);
30
31 ContentValues values = new ContentValues();
32
33 values.put("name", "Name 1");
34 values.put("disc", "here is the description");
35 values.put("photo", "stub.png");
36 values.put("thumb", "stub.png");
37 values.put("prep", "takes 30 mins");
38 values.put("ingre", "the ingredients of the snack");
39 values.put("howto", "how to make this thing");
40 values.put("info", "basically its this much calorie and such and such");
41 db.insert("snacks", "name", values);
42
43 values.put("name", "Name 2");
44 values.put("disc", "here is the description");
45 values.put("photo", "stub.png");
46 values.put("thumb", "ic_launcher.png");
47 values.put("prep", "takes 500 mins");
48 values.put("ingre", "the ingredients of the snack");
49 values.put("howto", "how to make this thing");
50 values.put("info", "basically its this much calorie and such and such");
51 db.insert("snacks", "name", values);
52
53 values.put("name", "Name 3");
54 values.put("disc", "here is the description");
55 values.put("photo", "stub.png");
56 values.put("thumb", "stub.png");
57 values.put("ingre", "the ingredients of the snack");
58 values.put("howto", "how to make this thing");
59 values.put("info", "basically its this much calorie and such and such");
60 db.insert("snacks", "name", values);
61
62 values.put("name", "Name 4");
63 values.put("disc", "here is the description");
64 values.put("photo", "stub.png");
65 values.put("ingre", "the ingredients of the snack");
66 values.put("howto", "how to make this thing");
67 values.put("info", "basically its this much calorie and such and such");
68 db.insert("snacks", "name", values);
69
70 values.put("name", "Name 5");
71 values.put("disc", "here is the description");
72 values.put("photo", "stub.png");
73 values.put("ingre", "the ingredients of the snack");
74 values.put("howto", "how to make this thing");
75 values.put("info", "basically its this much calorie and such and such");
76 db.insert("snacks", "name", values);
77
78 values.put("name", "Name 6");
79 values.put("disc", "here is the description");
80 values.put("photo", "stub.png");
81 values.put("ingre", "the ingredients of the snack");
82 values.put("howto", "how to make this thing");
83 values.put("info", "basically its this much calorie and such and such");
84 db.insert("snacks", "name", values);
85
86 values.put("name", "Name 7");
87 values.put("disc", "here is the description");
88 values.put("photo", "stub.png");
89 values.put("ingre", "the ingredients of the snack");
90 values.put("howto", "how to make this thing");
91 values.put("info", "basically its this much calorie and such and such");
92 db.insert("snacks", "name", values);
93
94}
95
96@Override
97public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
98 db.execSQL("DROP TABLE IF EXISTS snacks");
99 onCreate(db);
100}}
101
102public Cook_tab_snacks_data(Context context) {
103 super(context, DATABASE_NAME, null, 2); //previously 1, now 2
104}
105
106public Cook_tab_snacks_data(Context context) {
107 super(context, DATABASE_NAME, null, 1);
108}