@MediumTest public void testSimpleQuery() throws Exception { mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL, str TEXT NOT NULL);"); mDatabase.execSQL("INSERT INTO test VALUES (1234, 'hello');"); SQLiteStatement statement1 = mDatabase.compileStatement("SELECT num FROM test WHERE str = ?"); SQLiteStatement statement2 = mDatabase.compileStatement("SELECT str FROM test WHERE num = ?"); try { statement1.bindString(1, "hello"); long value = statement1.simpleQueryForLong(); assertEquals(1234, value); statement1.bindString(1, "world"); statement1.simpleQueryForLong(); fail("shouldn't get here"); } catch (SQLiteDoneException e) { // expected } try { statement2.bindLong(1, 1234); String value = statement1.simpleQueryForString(); assertEquals("hello", value); statement2.bindLong(1, 5678); statement1.simpleQueryForString(); fail("shouldn't get here"); } catch (SQLiteDoneException e) { // expected } statement1.close(); statement2.close(); }
/** * Pulled partially from code, it runs a "PRAGMA quick_check(1)" to see if the database is ok. * This method will {@link #restoreBackUp()} if they are enabled on the database if this check * fails. So use with caution and ensure that you backup the database often! * * @return true if the database is ok, false if the consistency has been compromised. */ public boolean isDatabaseIntegrityOk() { boolean integrityOk = true; SQLiteStatement prog = null; try { prog = getWritableDatabase().compileStatement("PRAGMA quick_check(1)"); String rslt = prog.simpleQueryForString(); if (!rslt.equalsIgnoreCase("ok")) { // integrity_checker failed on main or attached databases FlowLog.log( FlowLog.Level.E, "PRAGMA integrity_check on " + databaseDefinition.getDatabaseName() + " returned: " + rslt); integrityOk = false; if (databaseDefinition.backupEnabled()) { integrityOk = restoreBackUp(); } } } finally { if (prog != null) { prog.close(); } } return integrityOk; }