Java Code Examples for android.database.sqlite.SQLiteStatement.close()

The following are Jave code examples for showing how to use close() of the android.database.sqlite.SQLiteStatement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: ucar-weex-core   File: DefaultWXStorage.java   Source Code and License Vote up 6 votes
private long performGetLength() {
    SQLiteDatabase database = mDatabaseSupplier.getDatabase();
    if (database == null) {
        return 0;
    }

    String sql = "SELECT count(" + WXSQLiteOpenHelper.COLUMN_KEY + ") FROM " + WXSQLiteOpenHelper.TABLE_STORAGE;
    SQLiteStatement statement = null;
    try {
        statement = database.compileStatement(sql);
        return statement.simpleQueryForLong();
    } catch (Exception e) {
        WXLogUtils.e(WXSQLiteOpenHelper.TAG_STORAGE, "DefaultWXStorage occurred an exception when execute getLength:" + e.getMessage());
        return 0;
    } finally {
        if(statement != null) {
            statement.close();
        }
    }
}
 
Example 2
Project: buildAPKsApps   File: DataManager.java   Source Code and License Vote up 6 votes
public void updateItemActiveness(int itemId, boolean active)
{
    Log.d(L.TAG, "Setting active property of " + itemId + " to " + active);

    //long t = System.currentTimeMillis();
    SQLiteDatabase db = helper.getWritableDatabase();
    //long t2 = System.currentTimeMillis();
    //Log.d(TAG, "Open DB took " + ((t2-t)/1000.0));
    //t = t2;
    SQLiteStatement stmt = db.compileStatement("update items set active=? where id=?");        
    stmt.bindLong(1, active?1:0);
    stmt.bindLong(2, itemId);
    //t2 = System.currentTimeMillis();
    //Log.d(TAG, "stmt creation took " + ((t2-t)/1000.0));
    //t = t2;
    stmt.execute();
    //t2 = System.currentTimeMillis();
    //Log.d(TAG, "execute() took " + ((t2-t)/1000.0));
    //t = t2;
    stmt.close();
    db.close();
    //t2 = System.currentTimeMillis();
    //Log.d(TAG, "close() took " + ((t2-t)/1000.0));
    //t = t2;
}
 
Example 3
Project: weex-uikit   File: DefaultWXStorage.java   Source Code and License Vote up 6 votes
private long performGetLength() {
    SQLiteDatabase database = mDatabaseSupplier.getDatabase();
    if (database == null) {
        return 0;
    }

    String sql = "SELECT count(" + WXSQLiteOpenHelper.COLUMN_KEY + ") FROM " + WXSQLiteOpenHelper.TABLE_STORAGE;
    SQLiteStatement statement = null;
    try {
        statement = database.compileStatement(sql);
        return statement.simpleQueryForLong();
    } catch (Exception e) {
        WXLogUtils.e(WXSQLiteOpenHelper.TAG_STORAGE, "DefaultWXStorage occurred an exception when execute getLength:" + e.getMessage());
        return 0;
    } finally {
        if(statement != null) {
            statement.close();
        }
    }
}
 
Example 4
Project: buildAPKsApps   File: DataManager.java   Source Code and License Vote up 6 votes
public void deleteList(int list_id)
{
    Log.d(L.TAG, "Deleting list " + list_id);

    SQLiteDatabase db = helper.getWritableDatabase();
    
    SQLiteStatement stmt = db.compileStatement("delete from items where list_id=?");        
    stmt.bindLong(1, list_id);
    stmt.execute();
    stmt.close();
    
    SQLiteStatement stmt2 = db.compileStatement("delete from lists where id=?");        
    stmt2.bindLong(1, list_id);
    stmt2.execute();
    stmt2.close();
    db.close();
}
 
Example 5
Project: weex-uikit   File: DefaultWXStorage.java   Source Code and License Vote up 5 votes
private boolean performSetItem(String key, String value, boolean isPersistent, boolean allowRetryWhenFull) {
    SQLiteDatabase database = mDatabaseSupplier.getDatabase();
    if (database == null) {
        return false;
    }

    WXLogUtils.d(WXSQLiteOpenHelper.TAG_STORAGE, "set k-v to storage(key:" + key + ",value:" + value + ",isPersistent:" + isPersistent + ",allowRetry:" + allowRetryWhenFull + ")");
    String sql = "INSERT OR REPLACE INTO " + WXSQLiteOpenHelper.TABLE_STORAGE + " VALUES (?,?,?,?);";
    SQLiteStatement statement = null;
    String timeStamp = WXSQLiteOpenHelper.sDateFormatter.format(new Date());
    try {
        statement = database.compileStatement(sql);
        statement.clearBindings();
        statement.bindString(1, key);
        statement.bindString(2, value);
        statement.bindString(3, timeStamp);
        statement.bindLong(4, isPersistent ? 1 : 0);
        statement.execute();
        return true;
    } catch (Exception e) {
        WXLogUtils.e(WXSQLiteOpenHelper.TAG_STORAGE, "DefaultWXStorage occurred an exception when execute setItem :" + e.getMessage());
        if (e instanceof SQLiteFullException) {
            if (allowRetryWhenFull && trimToSize()) {
                //try again
                //setItem/setItemPersistent method only allow try once when occurred a sqliteFullException.
                WXLogUtils.d(WXSQLiteOpenHelper.TAG_STORAGE, "retry set k-v to storage(key:" + key + ",value:" + value + ")");
                return performSetItem(key, value, isPersistent, false);
            }
        }

        return false;
    } finally {
        if(statement != null) {
            statement.close();
        }
    }
}
 
Example 6
Project: LaunchEnr   File: LauncherProvider.java   Source Code and License Vote up 5 votes
/**
 * Replaces all shortcuts of type {@link Favorites#ITEM_TYPE_SHORTCUT} which have a valid
 * launcher activity target with {@link Favorites#ITEM_TYPE_APPLICATION}.
 */
@Thunk void convertShortcutsToLauncherActivities(SQLiteDatabase db) {
    db.beginTransaction();
    Cursor c = null;
    SQLiteStatement updateStmt = null;

    try {
        // Only consider the primary user as other users can't have a shortcut.
        long userSerial = getDefaultUserSerial();
        c = db.query(Favorites.TABLE_NAME, new String[] {
                Favorites._ID,
                Favorites.INTENT,
            }, "itemType=" + Favorites.ITEM_TYPE_SHORTCUT + " AND profileId=" + userSerial,
            null, null, null, null);

        updateStmt = db.compileStatement("UPDATE favorites SET itemType="
                + Favorites.ITEM_TYPE_APPLICATION + " WHERE _id=?");

        final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
        final int intentIndex = c.getColumnIndexOrThrow(Favorites.INTENT);

        while (c.moveToNext()) {
            String intentDescription = c.getString(intentIndex);
            Intent intent;
            try {
                intent = Intent.parseUri(intentDescription, 0);
            } catch (URISyntaxException e) {
                e.printStackTrace();
                continue;
            }

            if (!Utilities.isLauncherAppTarget(intent)) {
                continue;
            }

            long id = c.getLong(idIndex);
            updateStmt.bindLong(1, id);
            updateStmt.executeUpdateDelete();
        }
        db.setTransactionSuccessful();
    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        db.endTransaction();
        if (c != null) {
            c.close();
        }
        if (updateStmt != null) {
            updateStmt.close();
        }
    }
}
 
Example 7
Project: OftenPorter   File: SqliteHandle.java   Source Code and License Vote up 5 votes
void close(SQLiteStatement sqLiteStatement)
{
    if (sqLiteStatement != null)
    {
        try
        {
            sqLiteStatement.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
 
Example 8
Project: buildAPKsApps   File: DataManager.java   Source Code and License Vote up 5 votes
private int countInactiveItemsInList(SQLiteDatabase db, int listId)
{
    SQLiteStatement stmt = db.compileStatement("select count(*) from items where list_id=? and active=1");        
    stmt.bindLong(1, listId);
    int rv = (int)stmt.simpleQueryForLong();
    stmt.close();
    
    return rv;
}
 
Example 9
Project: weex-3d-map   File: DefaultWXStorage.java   Source Code and License Vote up 5 votes
private boolean performSetItem(String key, String value, boolean isPersistent, boolean allowRetryWhenFull) {
    SQLiteDatabase database = mDatabaseSupplier.getDatabase();
    if (database == null) {
        return false;
    }

    WXLogUtils.d(WXSQLiteOpenHelper.TAG_STORAGE, "set k-v to storage(key:" + key + ",value:" + value + ",isPersistent:" + isPersistent + ",allowRetry:" + allowRetryWhenFull + ")");
    String sql = "INSERT OR REPLACE INTO " + WXSQLiteOpenHelper.TABLE_STORAGE + " VALUES (?,?,?,?);";
    SQLiteStatement statement = null;
    String timeStamp = WXSQLiteOpenHelper.sDateFormatter.format(new Date());
    try {
        statement = database.compileStatement(sql);
        statement.clearBindings();
        statement.bindString(1, key);
        statement.bindString(2, value);
        statement.bindString(3, timeStamp);
        statement.bindLong(4, isPersistent ? 1 : 0);
        statement.execute();
        return true;
    } catch (Exception e) {
        WXLogUtils.e(WXSQLiteOpenHelper.TAG_STORAGE, "DefaultWXStorage occurred an exception when execute setItem :" + e.getMessage());
        if (e instanceof SQLiteFullException) {
            if (allowRetryWhenFull && trimToSize()) {
                //try again
                //setItem/setItemPersistent method only allow try once when occurred a sqliteFullException.
                WXLogUtils.d(WXSQLiteOpenHelper.TAG_STORAGE, "retry set k-v to storage(key:" + key + ",value:" + value + ")");
                return performSetItem(key, value, isPersistent, false);
            }
        }

        return false;
    } finally {
        if(statement != null) {
            statement.close();
        }
    }
}
 
Example 10
Project: Bee-Analyzer   File: MapsActivity.java   Source Code and License Vote up 5 votes
public void InsertToSqlite(String imei, String latitude, String longitude, String out)
{
    SQLiteStatement statement = localdb.compileStatement("INSERT INTO position VALUES(?,?,?,datetime(),?)");
    statement.bindString(1, imei);
    statement.bindString(2, latitude);
    statement.bindString(3, longitude);
    statement.bindString(4, out);
    statement.executeInsert();
    statement.close();
}
 
Example 11
Project: ucar-weex-core   File: DefaultWXStorage.java   Source Code and License Vote up 5 votes
private boolean performSetItem(String key, String value, boolean isPersistent, boolean allowRetryWhenFull) {
    SQLiteDatabase database = mDatabaseSupplier.getDatabase();
    if (database == null) {
        return false;
    }

    WXLogUtils.d(WXSQLiteOpenHelper.TAG_STORAGE, "set k-v to storage(key:" + key + ",value:" + value + ",isPersistent:" + isPersistent + ",allowRetry:" + allowRetryWhenFull + ")");
    String sql = "INSERT OR REPLACE INTO " + WXSQLiteOpenHelper.TABLE_STORAGE + " VALUES (?,?,?,?);";
    SQLiteStatement statement = null;
    String timeStamp = WXSQLiteOpenHelper.sDateFormatter.format(new Date());
    try {
        statement = database.compileStatement(sql);
        statement.clearBindings();
        statement.bindString(1, key);
        statement.bindString(2, value);
        statement.bindString(3, timeStamp);
        statement.bindLong(4, isPersistent ? 1 : 0);
        statement.execute();
        return true;
    } catch (Exception e) {
        WXLogUtils.e(WXSQLiteOpenHelper.TAG_STORAGE, "DefaultWXStorage occurred an exception when execute setItem :" + e.getMessage());
        if (e instanceof SQLiteFullException) {
            if (allowRetryWhenFull && trimToSize()) {
                //try again
                //setItem/setItemPersistent method only allow try once when occurred a sqliteFullException.
                WXLogUtils.d(WXSQLiteOpenHelper.TAG_STORAGE, "retry set k-v to storage(key:" + key + ",value:" + value + ")");
                return performSetItem(key, value, isPersistent, false);
            }
        }

        return false;
    } finally {
        if(statement != null) {
            statement.close();
        }
    }
}
 
Example 12
Project: YuiHatano   File: ShadowDatabaseUtils.java   Source Code and License Vote up 5 votes
/**
 * Utility method to run the query on the db and return the value in the
 * first column of the first row.
 */
public static long longForQuery(ShadowSQLiteDatabase db, String query, String[] selectionArgs) {
    SQLiteStatement prog = db.compileStatement(query);
    try {
        return longForQuery(prog, selectionArgs);
    } finally {
        prog.close();
    }
}
 
Example 13
Project: YuiHatano   File: ShadowDatabaseUtils.java   Source Code and License Vote up 5 votes
/**
 * Utility method to run the query on the db and return the value in the
 * first column of the first row.
 */
public static String stringForQuery(SQLiteDatabase db, String query, String[] selectionArgs) {
    SQLiteStatement prog = db.compileStatement(query);
    try {
        return stringForQuery(prog, selectionArgs);
    } finally {
        prog.close();
    }
}
 
Example 14
Project: YuiHatano   File: ShadowDatabaseUtils.java   Source Code and License Vote up 5 votes
/**
 * Utility method to run the query on the db and return the blob value in the
 * first column of the first row.
 *
 * @return A read-only file descriptor for a copy of the blob value.
 */
public static ParcelFileDescriptor blobFileDescriptorForQuery(SQLiteDatabase db,
                                                              String query, String[] selectionArgs) {
    SQLiteStatement prog = db.compileStatement(query);
    try {
        return blobFileDescriptorForQuery(prog, selectionArgs);
    } finally {
        prog.close();
    }
}
 
Example 15
Project: buildAPKsApps   File: DataManager.java   Source Code and License Vote up 5 votes
private int countItemsInList(SQLiteDatabase db, int listId)
{
    SQLiteStatement stmt = db.compileStatement("select count(*) from items where list_id=?");        
    stmt.bindLong(1, listId);
    int rv = (int)stmt.simpleQueryForLong();
    stmt.close();
    
    return rv;
}
 
Example 16
Project: buildAPKsApps   File: DataManager.java   Source Code and License Vote up 5 votes
public void updateListLabel(int listId, String newLabel)
{
    Log.d(L.TAG, "Updating label of list " + listId + " to " + newLabel);

    SQLiteDatabase db = helper.getWritableDatabase();
    SQLiteStatement stmt = db.compileStatement("update lists set label=? where id=?");        
    stmt.bindString(1, newLabel);
    stmt.bindLong(2, listId);
    stmt.execute();
    stmt.close();
    db.close();
}
 
Example 17
Project: buildAPKsApps   File: DataManager.java   Source Code and License Vote up 5 votes
public void createList(String label)
{
    Log.d(L.TAG, "Insert list " + label);

    SQLiteDatabase db = helper.getWritableDatabase();
    SQLiteStatement stmt = db.compileStatement("insert into lists (label) values (?)");        
    stmt.bindString(1, label);
    stmt.executeInsert();
    stmt.close();
    db.close();
}
 
Example 18
Project: KBUnitTest   File: ShadowDatabaseUtils.java   Source Code and License Vote up 5 votes
/**
 * Utility method to run the query on the db and return the value in the
 * first column of the first row.
 */
public static long longForQuery(ShadowSQLiteDatabase db, String query, String[] selectionArgs) {
    SQLiteStatement prog = db.compileStatement(query);
    try {
        return longForQuery(prog, selectionArgs);
    } finally {
        prog.close();
    }
}
 
Example 19
Project: buildAPKsApps   File: DataManager.java   Source Code and License Vote up 5 votes
public void deleteInactive(int list_id)
{
    Log.d(L.TAG, "Deleting inactive items from list " + list_id);

    SQLiteDatabase db = helper.getWritableDatabase();
    SQLiteStatement stmt = db.compileStatement("delete from items where list_id=? and active=0");        
    stmt.bindLong(1, list_id);
    stmt.execute();
    stmt.close();
    db.close();
}
 
Example 20
Project: SimpleUILauncher   File: LauncherProvider.java   Source Code and License Vote up 4 votes
/**
 * Replaces all shortcuts of type {@link Favorites#ITEM_TYPE_SHORTCUT} which have a valid
 * launcher activity target with {@link Favorites#ITEM_TYPE_APPLICATION}.
 */
@Thunk void convertShortcutsToLauncherActivities(SQLiteDatabase db) {
    db.beginTransaction();
    Cursor c = null;
    SQLiteStatement updateStmt = null;

    try {
        // Only consider the primary user as other users can't have a shortcut.
        long userSerial = getDefaultUserSerial();
        c = db.query(Favorites.TABLE_NAME, new String[] {
                Favorites._ID,
                Favorites.INTENT,
            }, "itemType=" + Favorites.ITEM_TYPE_SHORTCUT + " AND profileId=" + userSerial,
            null, null, null, null);

        updateStmt = db.compileStatement("UPDATE favorites SET itemType="
                + Favorites.ITEM_TYPE_APPLICATION + " WHERE _id=?");

        final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
        final int intentIndex = c.getColumnIndexOrThrow(Favorites.INTENT);

        while (c.moveToNext()) {
            String intentDescription = c.getString(intentIndex);
            Intent intent;
            try {
                intent = Intent.parseUri(intentDescription, 0);
            } catch (URISyntaxException e) {
                Log.e(TAG, "Unable to parse intent", e);
                continue;
            }

            if (!Utilities.isLauncherAppTarget(intent)) {
                continue;
            }

            long id = c.getLong(idIndex);
            updateStmt.bindLong(1, id);
            updateStmt.executeUpdateDelete();
        }
        db.setTransactionSuccessful();
    } catch (SQLException ex) {
        Log.w(TAG, "Error deduping shortcuts", ex);
    } finally {
        db.endTransaction();
        if (c != null) {
            c.close();
        }
        if (updateStmt != null) {
            updateStmt.close();
        }
    }
}