Android Open Source - android-database-sqlcipher S Q L Demo Activity






From Project

Back to project page android-database-sqlcipher.

License

The source code is released under:

Apache License

If you think the Android project android-database-sqlcipher listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package example;
/*from ww  w . ja  va2 s .c  o  m*/
import android.database.Cursor;
import info.guardianproject.database.sqlcipher.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());
  }
}




Java Source Code List

example.EventDataSQLHelper.java
example.EventDataSQLHelper.java
example.SQLDemoActivity.java
example.SQLDemoActivity.java
net.sqlcipher.AbstractCursor.java
net.sqlcipher.AbstractWindowedCursor.java
net.sqlcipher.BulkCursorNative.java
net.sqlcipher.BulkCursorToCursorAdaptor.java
net.sqlcipher.CrossProcessCursorWrapper.java
net.sqlcipher.CursorIndexOutOfBoundsException.java
net.sqlcipher.CursorWindow.java
net.sqlcipher.CursorWrapper.java
net.sqlcipher.Cursor.java
net.sqlcipher.DatabaseUtils.java
net.sqlcipher.IBulkCursor.java
net.sqlcipher.MatrixCursor.java
net.sqlcipher.SQLException.java
net.sqlcipher.StaleDataException.java
net.sqlcipher.database.DatabaseObjectNotClosedException.java
net.sqlcipher.database.SQLiteAbortException.java
net.sqlcipher.database.SQLiteClosable.java
net.sqlcipher.database.SQLiteCompiledSql.java
net.sqlcipher.database.SQLiteConstraintException.java
net.sqlcipher.database.SQLiteContentHelper.java
net.sqlcipher.database.SQLiteCursorDriver.java
net.sqlcipher.database.SQLiteCursor.java
net.sqlcipher.database.SQLiteDatabaseCorruptException.java
net.sqlcipher.database.SQLiteDatabaseHook.java
net.sqlcipher.database.SQLiteDatabase.java
net.sqlcipher.database.SQLiteDebug.java
net.sqlcipher.database.SQLiteDirectCursorDriver.java
net.sqlcipher.database.SQLiteDiskIOException.java
net.sqlcipher.database.SQLiteDoneException.java
net.sqlcipher.database.SQLiteException.java
net.sqlcipher.database.SQLiteFullException.java
net.sqlcipher.database.SQLiteMisuseException.java
net.sqlcipher.database.SQLiteOpenHelper.java
net.sqlcipher.database.SQLiteProgram.java
net.sqlcipher.database.SQLiteQueryBuilder.java
net.sqlcipher.database.SQLiteQuery.java
net.sqlcipher.database.SQLiteStatement.java
net.sqlcipher.database.SQLiteTransactionListener.java
net.sqlcipher.database.SqliteWrapper.java