· 8 years ago · Jun 26, 2018, 09:26 PM
1public class InfoDBAdapter {
2 public static final String ROW_ID = "_id";
3 public static final String NAME = "name";
4
5 private static final String TAG = "InfoDbAdapter";
6 private static final String DATABASE_NAME = "myappdb";
7 private static final String DATABASE_TABLE = "usersinfo";
8 private static final int DATABASE_VERSION = 1;
9
10
11 private static final String DATABASE_CREATE = "create table usersinfo (_id integer primary key autoincrement, "
12 + NAME
13 + " TEXT," + ");";
14
15 private DatabaseHelper mDbHelper;
16 private SQLiteDatabase mDb;
17
18 private final Context mCtx;
19
20 private static class DatabaseHelper extends SQLiteOpenHelper {
21
22 DatabaseHelper(Context context) {
23 super(context, DATABASE_NAME, null, DATABASE_VERSION);
24 }
25
26 @Override
27 public void onCreate(SQLiteDatabase db) {
28
29 db.execSQL(DATABASE_CREATE);
30 }
31
32 @Override
33 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
34 Log.w(TAG, "Upgrading database from version " + oldVersion + " to " //$NON-NLS-1$//$NON-NLS-2$
35 + newVersion + ", which will destroy all old data"); //$NON-NLS-1$
36 //db.execSQL("DROP TABLE IF EXISTS usersinfo"); //$NON-NLS-1$
37 onCreate(db);
38 }
39 }
40
41
42 public InfoDBAdapter(Context ctx) {
43 this.mCtx = ctx;
44 }
45
46
47 public InfoDBAdapter open() throws SQLException {
48 this.mDbHelper = new DatabaseHelper(this.mCtx);
49 this.mDb = this.mDbHelper.getWritableDatabase();
50 return this;
51 }
52
53 /**
54 * close return type: void
55 */
56 public void close() {
57 this.mDbHelper.close();
58 }
59
60
61 public long createUser(String name) {
62 ContentValues initialValues = new ContentValues();
63 initialValues.put(NAME, name);
64 return this.mDb.insert(DATABASE_TABLE, null, initialValues);
65 }
66
67
68 public boolean deleteUser(long rowId) {
69
70 return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$
71 }
72
73
74 public Cursor fetchAllUsers() {
75
76 return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID,
77 NAME}, null, null, null, null, null);
78 }
79
80
81 public Cursor fetchUser(long rowId) throws SQLException {
82
83 Cursor mCursor =
84
85 this.mDb.query(true, DATABASE_TABLE, new String[] { ROW_ID, NAME}, ROW_ID + "=" + rowId, null, //$NON-NLS-1$
86 null, null, null, null);
87 if (mCursor != null) {
88 mCursor.moveToFirst();
89 }
90 return mCursor;
91
92 }
93
94
95 public boolean updateUser(long rowId, String name) {
96 ContentValues args = new ContentValues();
97 args.put(NAME, name);
98 return this.mDb
99 .update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$
100 }
101}
102
103public class CarsDBAdapter {
104 public static final String ROW_ID = "_id";
105 public static final String NAME = "name";
106 public static final String MODEL = "model";
107 public static final String YEAR = "year";
108
109 private static final String DATABASE_TABLE = "cars";
110
111 private DatabaseHelper mDbHelper;
112 private SQLiteDatabase mDb;
113
114 private final Context mCtx;
115
116 private static class DatabaseHelper extends SQLiteOpenHelper {
117
118 DatabaseHelper(Context context) {
119 super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION);
120 }
121
122 @Override
123 public void onCreate(SQLiteDatabase db) {
124 }
125
126 @Override
127 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
128 }
129 }
130
131 /**
132 * Constructor - takes the context to allow the database to be
133 * opened/created
134 *
135 * @param ctx
136 * the Context within which to work
137 */
138 public CarsDBAdapter(Context ctx) {
139 this.mCtx = ctx;
140 }
141
142 /**
143 * Open the cars database. If it cannot be opened, try to create a new
144 * instance of the database. If it cannot be created, throw an exception to
145 * signal the failure
146 *
147 * @return this (self reference, allowing this to be chained in an
148 * initialization call)
149 * @throws SQLException
150 * if the database could be neither opened or created
151 */
152 public CarsDBAdapter open() throws SQLException {
153 this.mDbHelper = new DatabaseHelper(this.mCtx);
154 this.mDb = this.mDbHelper.getWritableDatabase();
155 return this;
156 }
157
158 /**
159 * close return type: void
160 */
161 public void close() {
162 this.mDbHelper.close();
163 }
164
165 /**
166 * Create a new car. If the car is successfully created return the new
167 * rowId for that car, otherwise return a -1 to indicate failure.
168 *
169 * @param name
170 * @param model
171 * @param year
172 * @return rowId or -1 if failed
173 */
174 public long createCar(String name, String model, String year){
175 ContentValues initialValues = new ContentValues();
176 initialValues.put(NAME, name);
177 initialValues.put(MODEL, model);
178 initialValues.put(YEAR, year);
179 return this.mDb.insert(DATABASE_TABLE, null, initialValues);
180 }
181
182 /**
183 * Delete the car with the given rowId
184 *
185 * @param rowId
186 * @return true if deleted, false otherwise
187 */
188 public boolean deleteCar(long rowId) {
189
190 return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$
191 }
192
193 /**
194 * Return a Cursor over the list of all cars in the database
195 *
196 * @return Cursor over all cars
197 */
198 public Cursor getAllCars() {
199
200 return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID,
201 NAME, MODEL, YEAR }, null, null, null, null, null);
202 }
203
204 /**
205 * Return a Cursor positioned at the car that matches the given rowId
206 * @param rowId
207 * @return Cursor positioned to matching car, if found
208 * @throws SQLException if car could not be found/retrieved
209 */
210 public Cursor getCar(long rowId) throws SQLException {
211
212 Cursor mCursor =
213
214 this.mDb.query(true, DATABASE_TABLE, new String[] { ROW_ID, NAME,
215 MODEL, YEAR}, ROW_ID + "=" + rowId, null, null, null, null, null);
216 if (mCursor != null) {
217 mCursor.moveToFirst();
218 }
219 return mCursor;
220 }
221
222 /**
223 * Update the car.
224 *
225 * @param rowId
226 * @param name
227 * @param model
228 * @param year
229 * @return true if the note was successfully updated, false otherwise
230 */
231 public boolean updateCar(long rowId, String name, String model,
232 String year){
233 ContentValues args = new ContentValues();
234 args.put(NAME, name);
235 args.put(MODEL, model);
236 args.put(YEAR, year);
237
238 return this.mDb.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) >0;
239 }
240
241}
242
243public class DBAdapter {
244
245 public static final String DATABASE_NAME = "stuffIOwn"; //$NON-NLS-1$
246
247 public static final int DATABASE_VERSION = 1;
248
249 private static final String CREATE_TABLE_CARS =
250 "create table cars (_id integer primary key autoincrement, " //$NON-NLS-1$
251 + CarsDBAdapter.NAME+ " TEXT," //$NON-NLS-1$
252 + CarsDBAdapter.MODEL+ " TEXT," //$NON-NLS-1$
253 + CarsDBAdapter.YEAR+ " TEXT" + ");"; //$NON-NLS-1$ //$NON-NLS-2$
254
255 private static final String CREATE_TABLE_BOATS = "create table boats (_id integer primary key autoincrement, " //$NON-NLS-1$
256 +BoatsDBAdapter.NAME+" TEXT," //$NON-NLS-1$
257 +BoatsDBAdapter.MODEL+" TEXT," //$NON-NLS-1$
258 +BoatsDBAdapter.YEAR+" TEXT"+ ");"; //$NON-NLS-1$ //$NON-NLS-2$
259
260 private static final String CREATE_TABLE_CYCLES = "create table cycles (_id integer primary key autoincrement, " //$NON-NLS-1$
261 +CyclesDBAdapter.NAME+" TEXT," //$NON-NLS-1$
262 +CyclesDBAdapter.MODEL+" TEXT," //$NON-NLS-1$
263 +CyclesDBAdapter.YEAR+" TEXT"+ ");"; //$NON-NLS-1$ //$NON-NLS-2$
264
265
266 private final Context context;
267 private DatabaseHelper DBHelper;
268 private SQLiteDatabase db;
269
270 /**
271 * Constructor
272 * @param ctx
273 */
274 public DBAdapter(Context ctx)
275 {
276 this.context = ctx;
277 this.DBHelper = new DatabaseHelper(this.context);
278 }
279
280 private static class DatabaseHelper extends SQLiteOpenHelper
281 {
282 DatabaseHelper(Context context)
283 {
284 super(context, DATABASE_NAME, null, DATABASE_VERSION);
285 }
286
287 @Override
288 public void onCreate(SQLiteDatabase db)
289 {
290 db.execSQL(CREATE_TABLE_CARS);
291 db.execSQL(CREATE_TABLE_BOATS);
292 db.execSQL(CREATE_TABLE_CYCLES);
293 }
294
295 @Override
296 public void onUpgrade(SQLiteDatabase db, int oldVersion,
297 int newVersion)
298 {
299 // Adding any table mods to this guy here
300 }
301 }
302
303 /**
304 * open the db
305 * @return this
306 * @throws SQLException
307 * return type: DBAdapter
308 */
309 public DBAdapter open() throws SQLException
310 {
311 this.db = this.DBHelper.getWritableDatabase();
312 return this;
313 }
314
315 /**
316 * close the db
317 * return type: void
318 */
319 public void close()
320 {
321 this.DBHelper.close();
322 }
323}