-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDBAdapter.java
More file actions
207 lines (181 loc) · 5.88 KB
/
DBAdapter.java
File metadata and controls
207 lines (181 loc) · 5.88 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
package com.panoskrt.dbadapter;
/**
########################################################################
# Copyright (C) 2012 Panagiotis Kritikakos <panoskrt@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
########################################################################
**/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Build;
public class DBAdapter {
private SQLiteDatabase database = null;
private String dbPath;
private String dbName;
private Context context;
public DBAdapter(Context context, String dbName) {
this.context = context;
this.dbName = dbName;
dbPath = "/data/data/" + context.getPackageName() + "/databases/";
}
public void copyDB() {
try {
try {
database = SQLiteDatabase.openDatabase(dbPath + dbName, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// Checking if required dirs exist as sometimes they do not and
// they need to be created.
File dbDir = new File(dbPath);
if (!dbDir.exists()) {
dbDir.mkdirs();
}
// Copying db from assets to db directory.
InputStream in = context.getAssets().open(dbName);
OutputStream out = new FileOutputStream(dbPath + dbName);
bufCopy(in, out);
}
} catch (IOException ex) {
ex.printStackTrace();
}
if (database != null) {
database.close();
}
}
public SQLiteDatabase openDB() {
database = SQLiteDatabase.openDatabase(dbPath + dbName, null,
SQLiteDatabase.OPEN_READONLY);
return database;
}
public Cursor runQuery(String query) {
Cursor c = database.rawQuery(query, null);
return c;
}
public String getStringEntry(String column, String table, String match) {
String text = null;
Cursor c = database.rawQuery("SELECT " + column + " FROM " + table
+ " WHERE " + column + "=" + "'" + match + "'", null);
if (c != null) {
if (c.moveToFirst()) {
text = c.getString(c.getColumnIndex(column));
}
}
return text;
}
public int getIntEntry(String column, String table, String match) {
int number = 0;
Cursor c = database.rawQuery("SELECT " + column + " FROM " + table
+ " WHERE " + column + "=" + match, null);
if (c != null) {
if (c.moveToFirst()) {
number = c.getInt(c.getColumnIndex(column));
}
}
return number;
}
public long getLongEntry(String column, String table, String match) {
long number = 0;
Cursor c = database.rawQuery("SELECT " + column + " FROM " + table
+ " WHERE " + column + "=" + match, null);
if (c != null) {
if (c.moveToFirst()) {
number = c.getInt(c.getColumnIndex(column));
}
}
return number;
}
public double getDoubleEntry(String column, String table, String match) {
double number = 0;
Cursor c = database.rawQuery("SELECT " + column + " FROM " + table
+ " WHERE " + column + "=" + match, null);
if (c != null) {
if (c.moveToFirst()) {
number = c.getInt(c.getColumnIndex(column));
}
}
return number;
}
public void dropTable(String tableName) {
database.rawQuery("DROP TABLE " + tableName, null);
}
public long tableCount(String tableName) {
long count = DatabaseUtils.queryNumEntries(database, tableName);
return count;
}
// The .deleteDatabase method is supported on API 16 and higher.
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void deleteDB() {
File oldDB = new File(dbPath + dbName);
SQLiteDatabase.releaseMemory();
SQLiteDatabase.deleteDatabase(oldDB);
}
public SQLiteDatabase getDB() {
return database;
}
public void closeDB() {
database.close();
}
public void downloadDB(Context context, String dbName, String urlLink) {
try {
URL url = new URL(urlLink);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = context.openFileOutput(dbName,
Context.MODE_PRIVATE);
fos.write(baf.toByteArray());
fos.close();
File dbFile = new File(context.getFilesDir() + "/" + dbName);
InputStream in = new FileInputStream(dbFile);
OutputStream out = new FileOutputStream(dbPath + dbName);
bufCopy(in, out);
dbFile.delete();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void bufCopy(InputStream in, OutputStream out) {
try {
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}