Search in sources :

Example 51 with SQLiteStatement

use of android.database.sqlite.SQLiteStatement in project WordPress-Android by wordpress-mobile.

the class ReaderUserTable method addOrUpdateUsers.

public static void addOrUpdateUsers(ReaderUserList users) {
    if (users == null || users.size() == 0)
        return;
    SQLiteDatabase db = ReaderDatabase.getWritableDb();
    db.beginTransaction();
    SQLiteStatement stmt = db.compileStatement("INSERT OR REPLACE INTO tbl_users (" + COLUMN_NAMES + ") VALUES (?1,?2,?3,?4,?5,?6,?7)");
    try {
        for (ReaderUser user : users) {
            stmt.bindLong(1, user.userId);
            stmt.bindLong(2, user.blogId);
            stmt.bindString(3, user.getUserName());
            stmt.bindString(4, user.getDisplayName());
            stmt.bindString(5, user.getUrl());
            stmt.bindString(6, user.getProfileUrl());
            stmt.bindString(7, user.getAvatarUrl());
            stmt.execute();
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        SqlUtils.closeStatement(stmt);
    }
}
Also used : ReaderUser(org.wordpress.android.models.ReaderUser) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteStatement(android.database.sqlite.SQLiteStatement)

Example 52 with SQLiteStatement

use of android.database.sqlite.SQLiteStatement in project sugar by satyan.

the class SugarRecord method count.

public static <T> long count(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {
    long result = -1;
    String filter = (!TextUtils.isEmpty(whereClause)) ? " where " + whereClause : "";
    SQLiteStatement sqliteStatement;
    try {
        sqliteStatement = getSugarDataBase().compileStatement("SELECT count(*) FROM " + NamingHelper.toTableName(type) + filter);
    } catch (SQLiteException e) {
        e.printStackTrace();
        return result;
    }
    if (whereArgs != null) {
        for (int i = whereArgs.length; i != 0; i--) {
            sqliteStatement.bindString(i, whereArgs[i - 1]);
        }
    }
    try {
        result = sqliteStatement.simpleQueryForLong();
    } finally {
        sqliteStatement.close();
    }
    return result;
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLiteException(android.database.sqlite.SQLiteException)

Example 53 with SQLiteStatement

use of android.database.sqlite.SQLiteStatement in project requery by requery.

the class SqliteStatement method execute.

@Override
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
    SQLiteStatement statement = null;
    try {
        statement = connection.getDatabase().compileStatement(sql);
        if (autoGeneratedKeys == RETURN_GENERATED_KEYS) {
            long rowId = statement.executeInsert();
            insertResult = new SingleResultSet(this, rowId);
            return true;
        } else {
            statement.execute();
        }
    } catch (android.database.SQLException e) {
        SqliteConnection.throwSQLException(e);
    } finally {
        if (statement != null) {
            statement.close();
        }
    }
    return false;
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement)

Example 54 with SQLiteStatement

use of android.database.sqlite.SQLiteStatement in project requery by requery.

the class SqliteStatement method executeUpdate.

@Override
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
    SQLiteStatement statement = null;
    try {
        statement = connection.getDatabase().compileStatement(sql);
        if (autoGeneratedKeys == RETURN_GENERATED_KEYS) {
            long rowId = statement.executeInsert();
            insertResult = new SingleResultSet(this, rowId);
            updateCount = 1;
        } else {
            updateCount = statement.executeUpdateDelete();
        }
    } catch (android.database.SQLException e) {
        SqliteConnection.throwSQLException(e);
    } finally {
        if (statement != null) {
            statement.close();
        }
    }
    return updateCount;
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement)

Example 55 with SQLiteStatement

use of android.database.sqlite.SQLiteStatement in project Talon-for-Twitter by klinker24.

the class FavoriteTweetsDataSource method getCursor.

public synchronized Cursor getCursor(int account) {
    String where = FavoriteTweetsSQLiteHelper.COLUMN_ACCOUNT + " = " + account;
    Cursor cursor;
    String sql = "SELECT COUNT(*) FROM " + FavoriteTweetsSQLiteHelper.TABLE_FAVORITE_TWEETS + " WHERE " + where;
    SQLiteStatement statement;
    try {
        statement = database.compileStatement(sql);
    } catch (Exception e) {
        open();
        statement = database.compileStatement(sql);
    }
    long count;
    try {
        count = statement.simpleQueryForLong();
    } catch (Exception e) {
        open();
        try {
            count = statement.simpleQueryForLong();
        } catch (Exception x) {
            return null;
        }
    }
    Log.v("talon_database", "FavoriteTweets database has " + count + " entries");
    if (count > timelineSize) {
        try {
            cursor = database.query(FavoriteTweetsSQLiteHelper.TABLE_FAVORITE_TWEETS, allColumns, where, null, null, null, FavoriteTweetsSQLiteHelper.COLUMN_TWEET_ID + " ASC", (count - timelineSize) + "," + timelineSize);
        } catch (Exception e) {
            open();
            cursor = database.query(FavoriteTweetsSQLiteHelper.TABLE_FAVORITE_TWEETS, allColumns, where, null, null, null, FavoriteTweetsSQLiteHelper.COLUMN_TWEET_ID + " ASC", (count - timelineSize) + "," + timelineSize);
        }
    } else {
        try {
            cursor = database.query(FavoriteTweetsSQLiteHelper.TABLE_FAVORITE_TWEETS, allColumns, where, null, null, null, FavoriteTweetsSQLiteHelper.COLUMN_TWEET_ID + " ASC");
        } catch (Exception e) {
            open();
            cursor = database.query(FavoriteTweetsSQLiteHelper.TABLE_FAVORITE_TWEETS, allColumns, where, null, null, null, FavoriteTweetsSQLiteHelper.COLUMN_TWEET_ID + " ASC");
        }
    }
    return cursor;
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) Cursor(android.database.Cursor) SQLException(android.database.SQLException)

Aggregations

SQLiteStatement (android.database.sqlite.SQLiteStatement)252 Cursor (android.database.Cursor)62 MediumTest (android.test.suitebuilder.annotation.MediumTest)49 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)24 Test (org.junit.Test)22 SQLException (android.database.SQLException)21 Date (java.util.Date)12 TargetApi (android.annotation.TargetApi)8 SQLiteDoneException (android.database.sqlite.SQLiteDoneException)8 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)7 SdkSuppress (android.support.test.filters.SdkSuppress)7 ArrayList (java.util.ArrayList)5 SQLiteException (android.database.sqlite.SQLiteException)4 SQLiteFullException (android.database.sqlite.SQLiteFullException)4 SQLException (java.sql.SQLException)4 Timing (com.newsrob.util.Timing)3 ContactIdInfo (com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo)3 IOException (java.io.IOException)3 Savepoint (java.sql.Savepoint)3 ContentValues (android.content.ContentValues)2