SQLite Android Bindings

Check-in Differences
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Difference From 0b5fd0b0d3a7dccf To 60c548bff9d6c50b

2013-12-25
17:50
Add some documentation files. (check-in: 67c837ca6c user: dan tags: trunk)
2013-12-24
19:16
Add a test to check that SQLiteOpenHelper works with SEE. (check-in: 60c548bff9 user: dan tags: trunk)
18:51
Add extra SEE tests. And fix problems revealed by the same. (check-in: 0b5fd0b0d3 user: dan tags: trunk)
12:07
In SQLITE_HAS_CODEC builds, do not initialize the LOCALIZED collation automatically (as if the SQLiteDatabase.NO_LOCALIZED_COLLATORS flag was set). Require apps to call the enableLocalizedCollators() method to explicitly initialize it. This gives the app an opportunity to execute a PRAGMA statement to configure an encryption key before the database is first accessed. (check-in: 9c4a073c3b user: dan tags: trunk)

Changes to src/org/sqlite/app/customsqlite/CustomSqlite.java.
12
13
14
15
16
17
18

19
20

21
22
23
24
25
26
27
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29







+


+







import java.util.Arrays;

import java.lang.InterruptedException;

import org.sqlite.database.sqlite.SQLiteDatabase;
import org.sqlite.database.sqlite.SQLiteStatement;
import org.sqlite.database.sqlite.SQLiteDatabaseCorruptException;
import org.sqlite.database.sqlite.SQLiteOpenHelper;

import android.database.Cursor;
import android.content.Context;

/*
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
*/

import org.sqlite.database.DatabaseErrorHandler;
163
164
165
166
167
168
169
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

191
192
193
194
195
196
197
165
166
167
168
169
170
171

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

192
193
194
195
196
197
198
199







-
+



















-
+







      res = res + "." + x;
    }

    return res;
  }

  /*
  ** Check that using openSeeDatabase() creates encrypted databases. 
  ** If this is a SEE build, check that encrypted databases work.
  */
  public void see_test_1() throws Exception {
    if( !SQLiteDatabase.hasCodec() ) return;

    SQLiteDatabase.deleteDatabase(DB_PATH);
    String res = "";
    
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
    db.execSQL("PRAGMA key = 'secretkey'");

    db.execSQL("CREATE TABLE t1(x)");
    db.execSQL("INSERT INTO t1 VALUES ('one'), ('two'), ('three')");
    
    res = string_from_t1_x(db);
    test_result("see_test_1.1", res, ".one.two.three");
    db.close();

    test_result("see_test_1.2", db_is_encrypted(), "encrypted");

    db = SQLiteDatabase.openOrCreateDatabase(DB_PATH.getPath(), null, new DoNotDeleteErrorHandler());
    db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
    db.execSQL("PRAGMA key = 'secretkey'");
    res = string_from_t1_x(db);
    test_result("see_test_1.3", res, ".one.two.three");
    db.close();

    res = "unencrypted";
    try {
216
217
218
219
220
221
222









































223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
245
246
247
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+















+










    } catch ( SQLiteDatabaseCorruptException e ){
      res = "encrypted";
    } finally {
      db.close();
    }
    test_result("see_test_1.5", res, "encrypted");
  }

  class MyHelper extends SQLiteOpenHelper {
    public MyHelper(Context ctx){
      super(ctx, DB_PATH.getPath(), null, 1);
    }
    public void onConfigure(SQLiteDatabase db){
      db.execSQL("PRAGMA key = 'secret'");
    }
    public void onCreate(SQLiteDatabase db){
      db.execSQL("CREATE TABLE t1(x)");
    }
    public void onUpgrade(SQLiteDatabase db, int iOld, int iNew){
    }
  } 

  /*
  ** If this is a SEE build, check that SQLiteOpenHelper still works.
  */
  public void see_test_2() throws Exception {
    if( !SQLiteDatabase.hasCodec() ) return;
    SQLiteDatabase.deleteDatabase(DB_PATH);

    MyHelper helper = new MyHelper(this);
    SQLiteDatabase db = helper.getWritableDatabase();
    db.execSQL("INSERT INTO t1 VALUES ('x'), ('y'), ('z')");

    String res = string_from_t1_x(db);
    test_result("see_test_2.1", res, ".x.y.z");
    test_result("see_test_2.2", db_is_encrypted(), "encrypted");

    helper.close();
    helper = new MyHelper(this);
    db = helper.getReadableDatabase();
    test_result("see_test_2.3", res, ".x.y.z");

    db = helper.getWritableDatabase();
    test_result("see_test_2.4", res, ".x.y.z");

    test_result("see_test_2.5", db_is_encrypted(), "encrypted");
  }


  public void run_the_tests(View view){
    System.loadLibrary("sqliteX");
    DB_PATH = getApplicationContext().getDatabasePath("test.db");
    DB_PATH.mkdirs();

    myTV.setText("");
    myNErr = 0;
    myNTest = 0;

    try {
      report_version();
      csr_test_1();
      thread_test_1();
      see_test_1();
      see_test_2();

      myTV.append("\n" + myNErr + " errors from " + myNTest + " tests\n");
    } catch(Exception e) {
      myTV.append("Exception: " + e.toString() + "\n");
      myTV.append(android.util.Log.getStackTraceString(e) + "\n");
    }
  }
}