10000 added very simple example activity for testing · Fiv38/android-database-sqlcipher@07e5f2f · GitHub
[go: up one dir, main page]

Skip to content

Commit 07e5f2f

Browse files
committed
added very simple example activity for testing
1 parent 77bfcbf commit 07e5f2f

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

src/example/EventDataSQLHelper.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package example;
2+
3+
import info.guardianproject.database.sqlcipher.SQLiteDatabase;
4+
import info.guardianproject.database.sqlcipher.SQLiteOpenHelper;
5+
import android.content.Context;
6+
import android.provider.BaseColumns;
7+
import android.util.Log;
8+
9+
/** Helper to the database, manages versions and creation */
10+
public class EventDataSQLHelper extends SQLiteOpenHelper {
11+
private static final String DATABASE_NAME = "events.db";
12+
private static final int DATABASE_VERSION = 1;
13+
14+
// Table name
15+
public static final String TABLE = "events";
16+
17+
// Columns
18+
public static final String TIME = "time";
19+
public static final String TITLE = "title";
20+
21+
public EventDataSQLHelper(Context context) {
22+
super(context, DATABASE_NAME, null, DATABASE_VERSION);
23+
}
24+
25+
@Override
26+
public void onCreate(SQLiteDatabase db) {
27+
String sql = "create table " + TABLE + "( " + BaseColumns._ID
28+
+ " integer primary key autoincrement, " + TIME + " integer, "
29+
+ TITLE + " text not null);";
30+
Log.d("EventsData", "onCreate: " + sql);
31+
db.execSQL(sql);
32+
}
33+
34+
@Override
35+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
36+
if (oldVersion >= newVersion)
37+
return;
38+
39+
String sql = null;
40+
if (oldVersion == 1)
41+
sql = "alter table " + TABLE + " add note text;";
42+
if (oldVersion == 2)
43+
sql = "";
44+
45+
Log.d("EventsData", "onUpgrade : " + sql);
46+
if (sql != null)
47+
db.execSQL(sql);
48+
}
49+
50+
}

src/example/SQLDemoActivity.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)
0