forked from sqlcipher/android-database-sqlcipher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLDemoActivity.java
More file actions
76 lines (53 loc) · 1.85 KB
/
SQLDemoActivity.java
File metadata and controls
76 lines (53 loc) · 1.85 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
package example;
import android.database.Cursor;
import net.sqlcipher.database.SQLiteDatabase;
import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
public class SQLDemoActivity extends Activity {
EventDataSQLHelper eventsData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//you must set Context on SQLiteDatabase first
SQLiteDatabase.loadLibs(this);
String password = "foo123";
eventsData = new EventDataSQLHelper(this);
//then you can open the database using a password
SQLiteDatabase db = eventsData.getWritableDatabase(password);
for (int i = 1; i < 100; i++)
addEvent("Hello Android Event: " + i, db);
db.close();
db = eventsData.getReadableDatabase(password);
Cursor cursor = getEvents(db);
showEvents(cursor);
db.close();
}
@Override
public void onDestroy() {
eventsData.close();
}
private void addEvent(String title, SQLiteDatabase db) {
ContentValues values = new ContentValues();
values.put(EventDataSQLHelper.TIME, System.currentTimeMillis());
values.put(EventDataSQLHelper.TITLE, title);
db.insert(EventDataSQLHelper.TABLE, null, values);
}
private Cursor getEvents(SQLiteDatabase db) {
Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null,
null, null);
startManagingCursor(cursor);
return cursor;
}
private void showEvents(Cursor cursor) {
StringBuilder ret = new StringBuilder("Saved Events:\n\n");
while (cursor.moveToNext()) {
long id = cursor.getLong(0);
long time = cursor.getLong(1);
String title = cursor.getString(2);
ret.append(id + ": " + time + ": " + title + "\n");
}
Log.i("sqldemo",ret.toString());
}
}