Skip to content

Commit 816eb82

Browse files
Merge pull request #19 from vinayagasundar/master
Added option to export database
2 parents 55f72f5 + 10626b1 commit 816eb82

5 files changed

Lines changed: 124 additions & 3 deletions

File tree

app/src/main/java/com/sample/database/ContactDBHelper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.database.sqlite.SQLiteOpenHelper;
2828

2929
import java.util.ArrayList;
30+
import java.util.Calendar;
3031

3132
/**
3233
* Created by amitshekhar on 16/11/16.
@@ -41,6 +42,7 @@ public class ContactDBHelper extends SQLiteOpenHelper {
4142
public static final String CONTACTS_COLUMN_STREET = "street";
4243
public static final String CONTACTS_COLUMN_CITY = "place";
4344
public static final String CONTACTS_COLUMN_PHONE = "phone";
45+
public static final String CONTACTS_CREATED_AT = "createdAt";
4446

4547
public ContactDBHelper(Context context) {
4648
super(context, DATABASE_NAME, null, 1);
@@ -51,7 +53,7 @@ public void onCreate(SQLiteDatabase db) {
5153
// TODO Auto-generated method stub
5254
db.execSQL(
5355
"create table contacts " +
54-
"(id integer primary key, name text,phone text,email text, street text,place text)"
56+
"(id integer primary key, name text,phone text,email text, street text,place text, createdAt integer)"
5557
);
5658
}
5759

@@ -70,6 +72,7 @@ public boolean insertContact(String name, String phone, String email, String str
7072
contentValues.put("email", email);
7173
contentValues.put("street", street);
7274
contentValues.put("place", place);
75+
contentValues.put(CONTACTS_CREATED_AT, Calendar.getInstance().getTimeInMillis());
7376
db.insert("contacts", null, contentValues);
7477
return true;
7578
}

debug-db/proguard-rules.pro

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,61 @@
1515
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
1616
# public *;
1717
#}
18+
19+
-keepparameternames
20+
-renamesourcefileattribute SourceFile
21+
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,EnclosingMethod
22+
23+
# Preserve all annotations.
24+
25+
-keepattributes *Annotation*
26+
27+
# Preserve all public classes, and their public and protected fields and
28+
# methods.
29+
30+
-keep public class * {
31+
public protected *;
32+
}
33+
34+
# Preserve all .class method names.
35+
36+
-keepclassmembernames class * {
37+
java.lang.Class class$(java.lang.String);
38+
java.lang.Class class$(java.lang.String, boolean);
39+
}
40+
41+
# Preserve all native method names and the names of their classes.
42+
43+
-keepclasseswithmembernames class * {
44+
native <methods>;
45+
}
46+
47+
# Preserve the special static methods that are required in all enumeration
48+
# classes.
49+
50+
-keepclassmembers class * extends java.lang.Enum {
51+
public static **[] values();
52+
public static ** valueOf(java.lang.String);
53+
}
54+
55+
# Explicitly preserve all serialization members. The Serializable interface
56+
# is only a marker interface, so it wouldn't save them.
57+
# You can comment this out if your library doesn't use serialization.
58+
# If your code contains serializable classes that have to be backward
59+
# compatible, please refer to the manual.
60+
61+
-keepclassmembers class * implements java.io.Serializable {
62+
static final long serialVersionUID;
63+
static final java.io.ObjectStreamField[] serialPersistentFields;
64+
private void writeObject(java.io.ObjectOutputStream);
65+
private void readObject(java.io.ObjectInputStream);
66+
java.lang.Object writeReplace();
67+
java.lang.Object readResolve();
68+
}
69+
70+
# Your library may contain more items that need to be preserved;
71+
# typically classes that are dynamically created using Class.forName:
72+
73+
# -keep public class mypackage.MyClass
74+
# -keep public interface mypackage.MyInterface
75+
# -keep public class * implements mypackage.MyInterface

debug-db/src/main/assets/app.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ $( document ).ready(function() {
77
});
88
});
99

10+
var isDatabaseSelected = true;
11+
1012
function getData(tableName) {
1113

1214
$.ajax({url: "getAllDataFromTheTable?tableName="+tableName, success: function(result){
@@ -31,6 +33,13 @@ function queryFunction() {
3133

3234
}
3335

36+
function downloadDb() {
37+
if (isDatabaseSelected) {
38+
var url = window.location.href;
39+
window.open(url+"downloadDb", "_blank")
40+
}
41+
}
42+
3443

3544
function getDBList() {
3645

@@ -59,9 +68,15 @@ function openDatabaseAndGetTableList(db) {
5968
if("APP_SHARED_PREFERENCES" == db) {
6069
$('#run-query').removeClass('active');
6170
$('#run-query').addClass('disabled');
71+
$('#export-db').removeClass('active');
72+
$('#export-db').addClass('disabled');
73+
isDatabaseSelected = false;
6274
} else {
6375
$('#run-query').removeClass('disabled');
6476
$('#run-query').addClass('active');
77+
$('#export-db').removeClass('disabled');
78+
$('#export-db').addClass('active');
79+
isDatabaseSelected = true;
6580
}
6681

6782
$("#selected-db-info").text("Selected Database : "+db);

debug-db/src/main/assets/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@
109109
<button id="run-query" type="submit" onclick="queryFunction()" class="btn btn-primary pull-right disabled">Run
110110
Query
111111
</button>
112+
<button id="export-db" type="button" onclick="downloadDb()" class="btn btn-primary">
113+
Export DB
114+
</button>
112115
</div>
113116
</div>
114117

debug-db/src/main/java/com/amitshekhar/server/ClientServer.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
4040
import java.io.BufferedReader;
4141
import java.io.ByteArrayOutputStream;
4242
import java.io.File;
43+
import java.io.FileInputStream;
4344
import java.io.FileNotFoundException;
4445
import java.io.IOException;
4546
import java.io.InputStream;
4647
import java.io.InputStreamReader;
48+
import java.io.OutputStream;
4749
import java.io.PrintStream;
4850
import java.net.ServerSocket;
4951
import java.net.Socket;
@@ -84,6 +86,11 @@ public class ClientServer implements Runnable {
8486
private Gson mGson;
8587
private boolean isDbOpenned;
8688

89+
/**
90+
* Hold the selected database name
91+
*/
92+
private String mSelectedDatabase = null;
93+
8794
/**
8895
* WebServer constructor.
8996
*/
@@ -226,13 +233,17 @@ private void handle(Socket socket) throws IOException {
226233
if (Constants.APP_SHARED_PREFERENCES.equals(database)) {
227234
response = getAllPrefTableName();
228235
closeDatabase();
236+
mSelectedDatabase = null;
229237
} else {
230238
openDatabase(database);
231239
response = getAllTableName();
240+
mSelectedDatabase = database;
232241
}
233242

234243
String data = mGson.toJson(response);
235244
bytes = data.getBytes();
245+
} else if (route.startsWith("downloadDb")) {
246+
bytes = getDatabaseFile();
236247
} else {
237248
bytes = loadContent(route);
238249
}
@@ -246,7 +257,12 @@ private void handle(Socket socket) throws IOException {
246257
// Send out the content.
247258
output.println("HTTP/1.0 200 OK");
248259
output.println("Content-Type: " + detectMimeType(route));
249-
output.println("Content-Length: " + bytes.length);
260+
261+
if (route.startsWith("downloadDb")) {
262+
output.println("Content-Disposition: attachment; filename=" + mSelectedDatabase);
263+
} else {
264+
output.println("Content-Length: " + bytes.length);
265+
}
250266
output.println();
251267
output.write(bytes);
252268
output.flush();
@@ -318,6 +334,32 @@ private String detectMimeType(String fileName) {
318334
}
319335
}
320336

337+
private byte[] getDatabaseFile() {
338+
if (TextUtils.isEmpty(mSelectedDatabase)) {
339+
return null;
340+
}
341+
342+
File file = new File(mDatabaseDir, mSelectedDatabase);
343+
344+
byte[] byteArray = null;
345+
try {
346+
InputStream inputStream = new FileInputStream(file);
347+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
348+
byte[] b = new byte[(int) file.length()];
349+
int bytesRead = 0;
350+
351+
while ((bytesRead = inputStream.read(b)) != -1) {
352+
bos.write(b, 0, bytesRead);
353+
}
354+
355+
byteArray = bos.toByteArray();
356+
} catch (IOException e) {
357+
Log.e(TAG, "getDatabaseFile: ", e);
358+
}
359+
360+
return byteArray;
361+
}
362+
321363
private void getDatabaseDir() {
322364
File root = mContext.getFilesDir().getParentFile();
323365
File dbRoot = new File(root, "/databases");
@@ -383,7 +425,7 @@ private Response query(String sql) {
383425
row.add(cursor.getFloat(i));
384426
break;
385427
case Cursor.FIELD_TYPE_INTEGER:
386-
row.add(cursor.getInt(i));
428+
row.add(cursor.getLong(i));
387429
break;
388430
case Cursor.FIELD_TYPE_STRING:
389431
row.add(cursor.getString(i));

0 commit comments

Comments
 (0)