Code example for SQLiteDatabase

Methods: compileStatementexecSQLquery

0
        } 
    } 
 
    @MediumTest 
    public void testStatementConstraint() throws Exception { 
        mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL);");
        SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
 
        // Try to insert NULL, which violates the constraint 
        try { 
            statement.clearBindings();
            statement.execute();
            fail("expected exception not thrown");
        } catch (SQLiteConstraintException e) {
            // expected 
        } 
 
        // Make sure the statement can still be used 
        statement.bindLong(1, 1);
        statement.execute();
        statement.close();
 
        Cursor c = mDatabase.query("test", null, null, null, null, null, null);
        int numCol = c.getColumnIndexOrThrow("num");
        c.moveToFirst();
        long num = c.getLong(numCol);
        assertEquals(1, num);
        c.close();