Java Code Examples for android.database.sqlite.SQLiteStatement#simpleQueryForLong()

The following examples show how to use android.database.sqlite.SQLiteStatement#simpleQueryForLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultWXStorage.java    From ucar-weex-core with Apache License 2.0 6 votes vote down vote up
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
Source File: DefaultWXStorage.java    From weex-uikit with MIT License 6 votes vote down vote up
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 3
Source File: SugarRecord.java    From ApkTrack with GNU General Public License v3.0 6 votes vote down vote up
public static <T> long count(Class<?> 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.toSQLName(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;
}
 
Example 4
Source File: DBHelper.java    From EasyVPN-Free with GNU General Public License v3.0 5 votes vote down vote up
public long getCountBasic() {
    SQLiteDatabase db = this.getWritableDatabase();
    SQLiteStatement statement = db.compileStatement("SELECT COUNT(*) FROM "
            + TABLE_SERVERS
            + " WHERE "
            + KEY_TYPE
            + " = 0");
    long count = statement.simpleQueryForLong();
    db.close();
    return count;
}
 
Example 5
Source File: SqliteJobQueue.java    From Mover with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Long getNextJobDelayUntilNs(boolean hasNetwork) {
    SQLiteStatement stmt =
            hasNetwork ? sqlHelper.getNextJobDelayedUntilWithNetworkStatement()
            : sqlHelper.getNextJobDelayedUntilWithoutNetworkStatement();
    synchronized (stmt) {
        try {
            stmt.clearBindings();
            return stmt.simpleQueryForLong();
        } catch (SQLiteDoneException e){
            return null;
        }
    }
}
 
Example 6
Source File: SqliteJobQueue.java    From Mover with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int count() {
    SQLiteStatement stmt = sqlHelper.getCountStatement();
    synchronized (stmt) {
        stmt.clearBindings();
        stmt.bindLong(1, sessionId);
        return (int) stmt.simpleQueryForLong();
    }
}
 
Example 7
Source File: DBUtils.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static boolean isTableEmpty(SQLiteDatabase database, String table) {
    String sql = "SELECT COUNT(*) FROM " + table;
    SQLiteStatement statement = database.compileStatement(sql);
    long records = statement.simpleQueryForLong();
    statement.close();
    return records == 0;
}
 
Example 8
Source File: SQLiteDatabaseAdapter.java    From squidb with Apache License 2.0 5 votes vote down vote up
@Override
public long simpleQueryForLong(String sql, Object[] bindArgs) {
    SQLiteStatement statement = null;
    try {
        statement = db.compileStatement(sql);
        SquidCursorFactory.bindArgumentsToProgram(statement, bindArgs);
        return statement.simpleQueryForLong();
    } finally {
        if (statement != null) {
            statement.close();
        }
    }
}
 
Example 9
Source File: DownloadsDB.java    From Alite with GNU General Public License v3.0 5 votes vote down vote up
public long getIDByIndex(int index) {
    SQLiteStatement downloadByIndex = getDownloadByIndexStatement();
    downloadByIndex.clearBindings();
    downloadByIndex.bindLong(1, index);
    try {
        return downloadByIndex.simpleQueryForLong();
    } catch (SQLiteDoneException e) {
        return -1;
    }
}
 
Example 10
Source File: AndroidDatabaseConnection.java    From ormlite-android with ISC License 5 votes vote down vote up
@Override
public long queryForLong(String statement) throws SQLException {
	SQLiteStatement stmt = null;
	try {
		stmt = db.compileStatement(statement);
		long result = stmt.simpleQueryForLong();
		logger.trace("{}: query for long simple query returned {}: {}", this, result, statement);
		return result;
	} catch (android.database.SQLException e) {
		throw SqlExceptionUtil.create("queryForLong from database failed: " + statement, e);
	} finally {
		closeQuietly(stmt);
	}
}
 
Example 11
Source File: DBHelper.java    From EasyVPN-Free with GNU General Public License v3.0 5 votes vote down vote up
public long getCountAdditional() {
    SQLiteDatabase db = this.getWritableDatabase();
    SQLiteStatement statement = db.compileStatement("SELECT COUNT(*) FROM "
            + TABLE_SERVERS
            + " WHERE "
            + KEY_TYPE
            + " = 1");
    long count = statement.simpleQueryForLong();
    db.close();
    return count;
}
 
Example 12
Source File: DBHelper.java    From EasyVPN-Free with GNU General Public License v3.0 5 votes vote down vote up
public long getCount() {
    SQLiteDatabase db = this.getWritableDatabase();
    SQLiteStatement statement = db.compileStatement("SELECT COUNT(*) FROM " + TABLE_SERVERS);
    long count = statement.simpleQueryForLong();
    db.close();
    return count;
}
 
Example 13
Source File: DownloadsDB.java    From play-apk-expansion with Apache License 2.0 5 votes vote down vote up
public long getIDByIndex(int index) {
    SQLiteStatement downloadByIndex = getDownloadByIndexStatement();
    downloadByIndex.clearBindings();
    downloadByIndex.bindLong(1, index);
    try {
        return downloadByIndex.simpleQueryForLong();
    } catch (SQLiteDoneException e) {
        return -1;
    }
}
 
Example 14
Source File: LaunchableActivityPrefs.java    From HayaiLauncher with Apache License 2.0 5 votes vote down vote up
public void writePreference(String className, long number, int priority, int usageQuantity) {
    Log.d("LaunchablePrefs", "writePreference running");
    final SQLiteDatabase db = getWritableDatabase();
    final SQLiteStatement countStatement = db.compileStatement(String.format(
            "SELECT COUNT(*) FROM %s WHERE %s = ?", TABLE_NAME,
            KEY_CLASSNAME));
    countStatement.bindString(1, className);
    final long count = countStatement.simpleQueryForLong();
    countStatement.close();
    final SQLiteStatement statement;
    if (count == 0) {
        statement = db.compileStatement("INSERT INTO "
                + TABLE_NAME + " (" + KEY_CLASSNAME + ", "
                + KEY_LASTLAUNCHTIMESTAMP + "," + KEY_FAVORITE + "," + KEY_USAGEQUANTIY + ") VALUES(?,?,?,?)");
        statement.bindString(1, className);
        statement.bindLong(2, number);
        statement.bindLong(3, priority);
        statement.bindLong(4, usageQuantity);
    } else {
        statement = db.compileStatement("UPDATE "
                + TABLE_NAME + " SET " + KEY_LASTLAUNCHTIMESTAMP + "=? , " + KEY_FAVORITE + "=? , " + KEY_USAGEQUANTIY + "=? WHERE "
                + KEY_CLASSNAME + "=?");
        statement.bindLong(1, number);
        statement.bindLong(2, priority);
        statement.bindLong(3, usageQuantity);
        statement.bindString(4, className);
    }
    statement.executeInsert();
    statement.close();
    db.close();
}
 
Example 15
Source File: DownloadsDB.java    From QtAndroidTools with MIT License 5 votes vote down vote up
public long getIDByIndex(int index) {
    SQLiteStatement downloadByIndex = getDownloadByIndexStatement();
    downloadByIndex.clearBindings();
    downloadByIndex.bindLong(1, index);
    try {
        return downloadByIndex.simpleQueryForLong();
    } catch (SQLiteDoneException e) {
        return -1;
    }
}
 
Example 16
Source File: DownloadsDB.java    From UnityOBBDownloader with Apache License 2.0 5 votes vote down vote up
public long getIDByIndex(int index) {
    SQLiteStatement downloadByIndex = getDownloadByIndexStatement();
    downloadByIndex.clearBindings();
    downloadByIndex.bindLong(1, index);
    try {
        return downloadByIndex.simpleQueryForLong();
    } catch (SQLiteDoneException e) {
        return -1;
    }
}
 
Example 17
Source File: noteEdit.java    From styT with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.sort_e) {
        String content = et_content.getText().toString();
        // Log.d("LOG1", content);
        // 获取写日志时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNum = sdf.format(date);
        String sql;
        String sql_count = "SELECT COUNT(*) FROM note";
        SQLiteStatement statement = dbread.compileStatement(sql_count);
        long count = statement.simpleQueryForLong();
        // Log.d("COUNT", count + "");
        //Log.d("ENTER_STATE", ENTER_STATE + "");
        // 添加一个新的日志
        if (ENTER_STATE == 0) {
            if (!content.equals("")) {
                sql = "insert into " + NotesDB.TABLE_NAME_NOTES
                        + " values(" + count + "," + "'" + content
                        + "'" + "," + "'" + dateNum + "')";
                // Log.d("LOG", sql);
                Toast.makeText(noteEdit.this, "保存成功", Toast.LENGTH_SHORT).show();
                dbread.execSQL(sql);
            }
        }
        // 查看并修改一个已有的日志
        else {
            Toast.makeText(noteEdit.this, "更新成功", Toast.LENGTH_SHORT).show();
            // Log.d("执行命令", "执行了该函数");
            String updatesql = "update note set content='"
                    + content + "' where _id=" + id;
            dbread.execSQL(updatesql);
            // et_content.setText(last_content);
        }
        Intent data = new Intent();
        setResult(2, data);
        finish();
    }
    return super.onOptionsItemSelected(item);
}
 
Example 18
Source File: DatabaseUtils.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Utility method to run the pre-compiled query and return the value in the
 * first column of the first row.
 */
public static long longForQuery(SQLiteStatement prog, String[] selectionArgs) {
    prog.bindAllArgsAsStrings(selectionArgs);
    return prog.simpleQueryForLong();
}
 
Example 19
Source File: SQLiteStoreTestsUtilities.java    From azure-mobile-apps-android-client with Apache License 2.0 3 votes vote down vote up
public long countRows(String tableName) {
    SQLiteDatabase db = this.getReadableDatabase();

    String countQuery = "SELECT COUNT(1) from " + tableName;

    SQLiteStatement s = db.compileStatement(countQuery);

    long count = s.simpleQueryForLong();

    return count;
}
 
Example 20
Source File: UpdateUtil.java    From OpenCircle with GNU General Public License v3.0 3 votes vote down vote up
public static boolean isAdded(SQLiteDatabase db, String remote_id, String TABLE_NAME){
    String select_string =  "select count(*) from "+TABLE_NAME+" where remote_id='" + remote_id + "'; ";

    SQLiteStatement s = db.compileStatement(select_string );

    long count = s.simpleQueryForLong();

    if(count > 0){

        return true;
    }

    return false;
}