1
+ package example ;
2
+
3
+ import info .guardianproject .database .Cursor ;
4
+ import info .guardianproject .database .sqlcipher .SQLiteDatabase ;
5
+ import android .app .Activity ;
6
+ import android .content .ContentValues ;
7
+ import android .os .Bundle ;
8
+ import android .util .Log ;
9
+
10
+ public class SQLDemoActivity extends Activity {
11
+ EventDataSQLHelper eventsData ;
12
+
13
+ @ Override
14
+ public void onCreate (Bundle savedInstanceState ) {
15
+ super .onCreate (savedInstanceState );
16
+
17
+ eventsData = new EventDataSQLHelper (this );
18
+ addEvent ("Hello Android Event" );
19
+ Cursor cursor = getEvents ();
20
+ showEvents (cursor );
21
+ }
22
+
23
+ @ Override
24
+ public void onDestroy () {
25
+ eventsData .close ();
26
+ }
27
+
28
+ private void addEvent (String title ) {
29
+ SQLiteDatabase db = eventsData .getWritableDatabase ();
30
+ ContentValues values = new ContentValues ();
31
+ values .put (EventDataSQLHelper .TIME , System .currentTimeMillis ());
32
+ values .put (EventDataSQLHelper .TITLE , title );
33
+ db .insert (EventDataSQLHelper .TABLE , null , values );
34
+
35
+ }
36
+
37
+ private Cursor getEvents () {
38
+ SQLiteDatabase db = eventsData .getReadableDatabase ();
39
+ Cursor cursor = db .query (EventDataSQLHelper .TABLE , null , null , null , null ,
40
+ null , null );
41
+
42
+ startManagingCursor (cursor );
43
+ return cursor ;
44
+ }
45
+
46
+ private void showEvents (Cursor cursor ) {
47
+ StringBuilder ret = new StringBuilder ("Saved Events:\n \n " );
48
+ while (cursor .moveToNext ()) {
49
+ long id = cursor .getLong (0 );
50
+ long time = cursor .getLong (1 );
51
+ String title = cursor .getString (2 );
52
+ ret .append (id + ": " + time + ": " + title + "\n " );
53
+ }
54
+
55
+ Log .i ("sqldemo" ,ret .toString ());
56
+ }
57
+ }
0 commit comments