-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBHelper.java
More file actions
253 lines (218 loc) · 8 KB
/
DBHelper.java
File metadata and controls
253 lines (218 loc) · 8 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package com.foodapp.food.recipe.helper;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import com.foodapp.food.recipe.model.CategoryItem;
import com.foodapp.food.recipe.model.RecipeItem;
import com.foodapp.food.recipe.model.SearchResultItem;
import java.util.ArrayList;
public class DBHelper extends SQLiteOpenHelper
{
ImageHelper imageHelper = new ImageHelper();
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context, name, factory, 3);
}
@Override
public void onCreate(SQLiteDatabase db)
{
//create table recipename,howto,description,pic,image
db.execSQL("CREATE TABLE IF NOT EXISTS RECIPES( _id INTEGER PRIMARY KEY AUTOINCREMENT, category TEXT,"
+ "recipeName TEXT, blank1 TEXT, blank2 TEXT, howTo TEXT, description TEXT,"
+ "thumbnail BLOB, mainImg BLOB, blank3 INTEGER);");
// TABLE INGREDIENTS
db.execSQL("CREATE TABLE IF NOT EXISTS INGREDIENTS( _id INTEGER PRIMARY KEY AUTOINCREMENT, recipeID INTEGER, ingreName TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
public ArrayList<String> ingredients_SelectByRecipeId(int id)
{
// Open available reading database
SQLiteDatabase db = getReadableDatabase();
ArrayList<String> ingredients = new ArrayList<>();
// Get all recipes data
Cursor cursor = db.rawQuery("SELECT ingreName FROM INGREDIENTS WHERE recipeID = " + id, null);
if (cursor != null)
{
while (cursor.moveToNext()) {
ingredients.add(cursor.getString(0));
}
}
cursor.close();
db.close();
return ingredients;
}
//called when ingredients are searched using the floating search button
public ArrayList<SearchResultItem> ingredients_selectRecipeByIngredientName(ArrayList<String> ingredientsName)
{
// database is made readable
SQLiteDatabase db = getReadableDatabase();
ArrayList<SearchResultItem> idRecipes = new ArrayList<>();
String strNames = "";
for (int i=0; i < ingredientsName.size(); i++)
{
strNames += "ingreName = '" + ingredientsName.get(i) + "'";
if (i != ingredientsName.size()-1)
{
strNames += " OR ";
}
}
Cursor cursor = db.rawQuery("SELECT recipeID, count(*) FROM INGREDIENTS WHERE "+
strNames + "GROUP BY recipeID ", null);
if (cursor != null)
{
while (cursor.moveToNext())
{
idRecipes.add(new SearchResultItem(cursor.getInt(0), cursor.getInt(1)));
}
}
cursor.close();
db.close();
return idRecipes;
}
//called from catergoryFragment
public ArrayList<CategoryItem> recipes_SelectCategory()
{
// Open available reading database
SQLiteDatabase db = getReadableDatabase();
ArrayList<CategoryItem> categoryList = new ArrayList<>();
// Get all recipes data
Cursor cursor = db.rawQuery("SELECT category, mainImg FROM RECIPES GROUP BY category HAVING max(_id)", null);
if (cursor != null)
{
while (cursor.moveToNext())
{
categoryList.add(new CategoryItem(
cursor.getString(0),
cursor.getBlob(1)
));
}
}
cursor.close();
db.close();
return categoryList;
}
//called when NO search ingredients are matched and suggestions should be given
public ArrayList<RecipeItem> recipes_SelectBest()
{
// Open available reading database
SQLiteDatabase db = getReadableDatabase();
ArrayList<RecipeItem> allRecipes = new ArrayList<>();
// Get all recipes data
Cursor cursor = db.rawQuery("SELECT * FROM RECIPES ORDER BY blank3 DESC LIMIT 3", null);
if (cursor != null)
{
while (cursor.moveToNext())
{
allRecipes.add(new RecipeItem(
cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5),
cursor.getString(6),
cursor.getBlob(7),
cursor.getBlob(8),
cursor.getInt(9)
));
}
}
cursor.close();
db.close();
return allRecipes;
}
//called from RecipeListActivity
public ArrayList<RecipeItem> recipes_SelectByCategory(String category)
{
// Open available reading database
SQLiteDatabase db = getReadableDatabase();
ArrayList<RecipeItem> allRecipes = new ArrayList<>();
// Get all recipes data
Cursor cursor = db.rawQuery("SELECT * FROM RECIPES WHERE category = '" + category + "'", null);
if (cursor != null)
{
while (cursor.moveToNext())
{
allRecipes.add(new RecipeItem(
cursor.getInt( 0 ),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5),
cursor.getString(6),
cursor.getBlob(7),
cursor.getBlob(8),
cursor.getInt(9)
));
}
}
cursor.close();
db.close();
return allRecipes;
}
//called from RecipeActivity
public RecipeItem recipes_SelectByName(String name)
{
// Open available reading database
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM RECIPES WHERE recipeName = '" + name + "'", null);
if (cursor != null)
{
while (cursor.moveToNext()) {
RecipeItem recipe = new RecipeItem(
cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5),
cursor.getString(6),
cursor.getBlob(7),
cursor.getBlob(8),
cursor.getInt(9)
);
cursor.close();
db.close();
return recipe;
}
}
return null;
}
//called from RecipeListactivity
public RecipeItem recipes_SelectById(int id)
{
// Open available reading database
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM RECIPES WHERE _id = " + id, null);
if (cursor != null)
{
while (cursor.moveToNext()) {
RecipeItem recipe = new RecipeItem(
cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5),
cursor.getString(6),
cursor.getBlob(7),
cursor.getBlob(8),
cursor.getInt(9)
);
cursor.close();
db.close();
return recipe;
}
}
return null;
}
}
/*
count(*) from INGREDIENTS where - basically counts number of records in table ingredients where ---
*/