-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMvSQLiteDB.java
344 lines (314 loc) · 9.77 KB
/
MvSQLiteDB.java
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* AndroidWithoutStupid Java Library
* Created by V. Subhash
* http://www.VSubhash.com
* Released as Public Domain Software in 2014
*/
package com.vsubhash.droid.androidwithoutstupid;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDoneException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteStatement;
/**
* This class provides methods for easy SQLite database operations.
*
* @author V. Subhash (<a href="http://www.VSubhash.com/">www.VSubhash.com</a>)
* @version 2016.08.15
*
*/
public class MvSQLiteDB {
String msLocation;
/**
* Identifies whether the SQLite database file exists.
*/
public boolean mbIsAvailable = false;
SQLiteDatabase moDB;
/**
* Creates a new database file with specified tables. The file will be
* created in the application's data directory.
*
* @param aoCallingContext
* application context of the activity or service
* @param asDatabase
* name of the database
* @param aoTableCreationSqlList
* list of table-definition SQL ("CREATE") statements
*/
public MvSQLiteDB(Context aoCallingContext, String asDatabase, ArrayList<String> aoTableCreationSqlList) {
this(aoCallingContext.getApplicationInfo().dataDir + File.separator + asDatabase, aoTableCreationSqlList);
}
/**
* Initializes a new database instance with an already existing file.
*
* @param asDatabase
* pathname of the file
*/
public MvSQLiteDB(String asDatabase) {
msLocation = asDatabase;
this.mbIsAvailable = false;
if (MvFileIO.isPathExists(msLocation)) {
try {
moDB = SQLiteDatabase.openDatabase(msLocation, null, SQLiteDatabase.OPEN_READONLY);
moDB.close();
mbIsAvailable = true;
} catch (SQLiteException se) {
mbIsAvailable = false;
MvMessages.logMessage("SQLite exception");
}
} else {
MvMessages.logMessage("No database file found at " + asDatabase);
}
}
/**
* Creates a new database with specified path and tables.
*
* @param asDatabase
* pathname of the database file
* @param aoTableCreationSqlList
* list of table-definition SQL ("CREATE") statements
*/
public MvSQLiteDB(String asDatabase, ArrayList<String> aoTableCreationSqlList) {
MvException oCreateTables;
msLocation = asDatabase;
if (MvFileIO.isPathExists(msLocation)) {
mbIsAvailable = true;
MvMessages.logMessage("Private database already exits at " + msLocation);
for (String sSQL : aoTableCreationSqlList) {
if (!isOpen()) {
if (!openDB(true).mbSuccess) {
//MvMessages.logMessage("Database at " + msLocation + " cannot be opened.");
break;
}
}
oCreateTables = executeSQL(sSQL);
if (!oCreateTables.mbSuccess) {
MvMessages.logMessage("Unable to create table with SQL");
}
}
this.closeDB();
} else {
try {
moDB = SQLiteDatabase.openOrCreateDatabase(msLocation, null);
mbIsAvailable = true;
for (String sSQL : aoTableCreationSqlList) {
oCreateTables = executeSQL(sSQL);
if (!oCreateTables.mbSuccess) {
MvMessages.logMessage("Unable to create table with SQL");
}
}
closeDB();
} catch (SQLiteException se) {
MvMessages.logMessage("Error: Database at " + asDatabase + " could not be found or created");
mbIsAvailable = false;
}
}
}
/**
* Creates a database database based on a copy of a database in the
* application's assets directory.
*
* @param aoCallingContext
* context of the activity or service
* @param asDatabase
* full path of the database file that needs to be created
* @param asFromAssetsDatabase
* name of the database (from the application's assets directory)
* that needs to be copied
*/
public MvSQLiteDB(Context aoCallingContext, String asDatabase, String asFromAssetsDatabase) {
msLocation = asDatabase;
if (MvFileIO.isPathExists(msLocation)) {
mbIsAvailable = true;
} else {
MvException oCopyDatabase;
try {
InputStream oAssetsDbStream = aoCallingContext.getAssets().open(asFromAssetsDatabase);
oCopyDatabase = MvFileIO.copyFile(oAssetsDbStream, msLocation);
if (oCopyDatabase.mbSuccess) {
//MvMessages.logMessage("Private database copied");
mbIsAvailable = true;
} else {
//MvMessages.logMessage("Private database could not be copied");
mbIsAvailable = false;
}
} catch (IOException e) {
MvMessages.logMessage("Private database could not be found or copied");
mbIsAvailable = false;
}
}
}
/**
* Opens the database for reading and returns a cursor. IMPORTANT: Remember to
* close the cursor and database after use.
*
* @param asSelectSQL
* SQL query for which the cursor needs to be created
* @return the cursor in .moResult if .mbSuccess is true
*/
public MvException getCursorForSelectSQL(String asSelectSQL) {
Cursor oCursor;
MvException oResult = new MvException();
if (openDB(false).mbSuccess) {
oCursor = moDB.rawQuery(asSelectSQL, null);
//MvMessages.logMessage("Executed SQL for cursor: " + asSelectSQL);
oResult.moResult = oCursor;
oResult.mbSuccess = true;
// this.closeDB(); // no need for this as cursor will get closed as well
} else {
oResult.mbSuccess = false;
}
return(oResult);
}
/**
* Returns text result of a 1-row-by-1-column SQL query.
*
* @param asSelectSQL
* SQL query that returns a single row with a single column.
* @return text result of the query
*/
public MvException getSingleResultForSelectSQL(String asSelectSQL) {
return(getSingleResultForSelectSQL(asSelectSQL, false));
}
/**
* Returns result of a 1-row-by-1-column SQL query.
*
* @param asSelectSQL
* SQL query that returns a single row with a single column.
* @param abIsNumberResult whether the result of the query is a number
* @return text or number result of the query in this.moResult
* @see android.database.sqlite.SQLiteStatement#simpleQueryForString() SQLiteStatement.simpleQueryForString()
* @see android.database.sqlite.SQLiteStatement#simpleQueryForLong() SQLiteStatement.simpleQueryForLong()
*/
public MvException getSingleResultForSelectSQL(String asSelectSQL, boolean abIsNumberResult) {
SQLiteStatement oSQL;
MvException oResult = new MvException();
if (openDB(false).mbSuccess) {
try {
oSQL = moDB.compileStatement(asSelectSQL);
if (abIsNumberResult) {
oResult.moResult = oSQL.simpleQueryForLong();
} else {
oResult.moResult = oSQL.simpleQueryForString();
}
oResult.mbSuccess = true;
} catch (SQLiteDoneException sde) {
MvMessages.logMessage("SQL did not return rows");
oResult.mException = sde;
} catch (SQLException se) {
MvMessages.logMessage("SQL error");
oResult.mException = se;
}
closeDB();
} else {
oResult.mbSuccess = false;
}
return(oResult);
}
/**
* Executes the specified SQL statement on the database and automatically
* closes it. Use this method for statements that modify the database.
*
* @param asSQL
* SQL statement that needs to be executed
* @return success or failure of the execution
*/
public MvException executeSQL(String asSQL) {
return(executeSQL(asSQL, true));
}
/**
* Executes the specified SQL statement on the database. Use this method for
* statements that modify the database.
*
* @param asSQL
* SQL statement that needs to be executed
* @param abAutoClose
* whether the database needs to be closed after executing the SQL
* statement
* @return success or failure of the execution
*/
public MvException executeSQL(String asSQL, boolean abAutoClose) {
SQLiteStatement oSQL;
MvException oResult = new MvException();
if (openDB(true).mbSuccess) {
try {
oSQL = moDB.compileStatement(asSQL);
oSQL.execute();
oResult.mbSuccess = true;
} catch (SQLException se) {
oResult.mException = se;
MvMessages.logMessage("SQL error");
}
if (abAutoClose) {
closeDB();
}
}
return(oResult);
}
/**
* Opens the database either in readonly or writable state.
*
* @param abWritable
* true if the database needs to be writable; false if otherwise
* @return result of the operation
*/
public MvException openDB(boolean abWritable) {
MvException oResult = new MvException();
if (mbIsAvailable) {
try {
if (abWritable) {
moDB = SQLiteDatabase.openDatabase(msLocation, null, SQLiteDatabase.OPEN_READWRITE);
} else {
moDB = SQLiteDatabase.openDatabase(msLocation, null, SQLiteDatabase.OPEN_READONLY|SQLiteDatabase.NO_LOCALIZED_COLLATORS);
}
oResult.mbSuccess = true;
} catch (SQLiteException se) {
oResult.mbSuccess = false;
oResult.mException = se;
oResult.msProblem = "Unable to open DB at " + this.msLocation;
}
} else {
oResult.mbSuccess = false;
oResult.msProblem = "DB is not available at location " + this.msLocation;
}
return(oResult);
}
/**
* Returns whether the DB is open.
*
* @return whether the DB is open
*/
public boolean isOpen() {
if (mbIsAvailable) {
if (moDB != null) {
if (moDB.isOpen()) {
return(true);
}
}
} else {
MvMessages.logMessage("isOpen: DB is not available");
}
return(false);
}
/**
* Closes the database.
*/
public void closeDB() {
if (mbIsAvailable) {
if (isOpen()) {
moDB.close();
//MvMessages.logMessage("closeDB: DB closed");
} else {
MvMessages.logMessage("closeDB: DB is not open.");
}
} else {
MvMessages.logMessage("closeDB: DB is not available.");
}
}
}