Java Code Examples for com.j256.ormlite.stmt.QueryBuilder#queryForFirst()

The following examples show how to use com.j256.ormlite.stmt.QueryBuilder#queryForFirst() . 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: GameRankingBiz.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
/**
 * 返回最高得分
 * 
 * @return
 */
public GameRanking getTopScore()
{
	QueryBuilder<GameRanking, ?> qb = getDao().queryBuilder();
	try
	{
		// qb.where().eq("user_name", userName);
		qb.orderBy("score", false);
		return qb.queryForFirst();
	}
	catch (SQLException e)
	{
		e.printStackTrace();
	}

	return null;
}
 
Example 2
Source File: UserDao.java    From AndroidQuick with MIT License 5 votes vote down vote up
public User getUserById(int userId) {
    try {
        QueryBuilder<User, Integer> qb = dao.queryBuilder();
        qb.where().eq(User.FIELD_NAME_ID, userId);
        return qb.queryForFirst();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 3
Source File: OrmLiteDao.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
/**
 * 通过表列名查询第一条记录
 *
 * @param map
 * @return
 */
public T queryForFirst(Map<String, Object> map) throws SQLException {
    QueryBuilder<T, Integer> queryBuilder = ormLiteDao.queryBuilder();
    Where where = queryBuilder.where();
    where.isNotNull("id");
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        where.and().eq(entry.getKey(), entry.getValue());
    }
    return queryBuilder.queryForFirst();
}
 
Example 4
Source File: OrmLiteDao.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
/**
 * 排序查询
 *
 * @param map         查询条件键值组合
 * @param orderColumn 排序的列
 * @param ascending   是否升序
 * @return
 */
public T queryForFirstByOrder(Map<String, Object> map, String orderColumn, boolean ascending) throws SQLException {
    QueryBuilder<T, Integer> queryBuilder = ormLiteDao.queryBuilder();
    Where where = queryBuilder.where();
    queryBuilder.orderBy(orderColumn, ascending);
    where.isNotNull("id");
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        where.and().eq(entry.getKey(), entry.getValue());
    }
    return queryBuilder.queryForFirst();
}
 
Example 5
Source File: MessageListFragment.java    From zulip-android with Apache License 2.0 5 votes vote down vote up
private void selectPointer() {
    if (filter != null) {
        Where<Message, Object> filteredWhere;
        try {
            filteredWhere = filter.modWhere(app.getDao(Message.class)
                    .queryBuilder().where());

            filteredWhere.and().le(Message.ID_FIELD, app.getPointer());

            QueryBuilder<Message, Object> closestQuery = app.getDao(
                    Message.class).queryBuilder();

            closestQuery.orderBy(Message.TIMESTAMP_FIELD, false).setWhere(
                    filteredWhere);
            Message closestMessage = closestQuery.queryForFirst();

            // use anchor message id if message was narrowed
            if (anchorId != -1) {
                selectMessage(getMessageById(anchorId));
            } else {
                recyclerView.scrollToPosition(adapter.getItemIndex(closestMessage));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    } else {
        int anc = app.getPointer();
        selectMessage(getMessageById(anc));
    }
}
 
Example 6
Source File: DatabaseHelper.java    From android-viewer-for-khan-academy with GNU General Public License v3.0 5 votes vote down vote up
public Video getVideoForFilename(String path) {
	if (path == null) return null;
	
	String youtubeId = OfflineVideoManager.youtubeIdFromFilename(path);
	QueryBuilder<Video, String> q = videoDao.queryBuilder();
	try {
		q.where().eq("youtube_id", youtubeId);
		return q.queryForFirst();
	} catch (SQLException e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 7
Source File: BaseMappedQueryTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCacheWithSelectColumns() throws Exception {
	Dao<CacheWithSelectColumns, Object> dao = createDao(CacheWithSelectColumns.class, true);
	// Add object to database
	CacheWithSelectColumns foo = new CacheWithSelectColumns();
	foo.text = "some text";
	dao.create(foo);
	// enable object cache
	dao.setObjectCache(true);

	// fetch the object, but only with its id field
	QueryBuilder<CacheWithSelectColumns, Object> qb =
			dao.queryBuilder().selectColumns(CacheWithSelectColumns.COLUMN_NAME_ID);
	CacheWithSelectColumns result = qb.queryForFirst();
	assertNotNull(result);
	assertNull(result.text);

	// fetch the same object again, this time asking for the text column as well
	qb = dao.queryBuilder().selectColumns(CacheWithSelectColumns.COLUMN_NAME_ID,
			CacheWithSelectColumns.COLUMN_NAME_TEXT);
	result = qb.queryForFirst();
	assertNotNull(result);
	assertEquals(foo.text, result.text);

	// fetch the same object again, this time asking for everything
	qb = dao.queryBuilder();
	result = qb.queryForFirst();
	assertNotNull(result);
	assertEquals(foo.text, result.text);
}
 
Example 8
Source File: OrmLiteDao.java    From AndroidBase with Apache License 2.0 3 votes vote down vote up
/**
 * 排序查询
 *
 * @param columnName  查询条件列名
 * @param value       查询条件值
 * @param orderColumn 排序的列
 * @param ascending   是否升序
 * @return
 */
public T queryForFirstByOrder(String columnName, Object value, String orderColumn, boolean ascending) throws SQLException {
    QueryBuilder<T, Integer> queryBuilder = ormLiteDao.queryBuilder();
    queryBuilder.orderBy(orderColumn, ascending);
    queryBuilder.where().eq(columnName, value);
    return queryBuilder.queryForFirst();
}
 
Example 9
Source File: OrmLiteDao.java    From AndroidBase with Apache License 2.0 2 votes vote down vote up
/**
 * 按条件查询,返回第一条符号要求的记录
 *
 * @param columnName 查询条件列名
 * @param value      查询条件值
 * @return
 */
public T queryForFirst(String columnName, Object value) throws SQLException {
    QueryBuilder<T, Integer> queryBuilder = ormLiteDao.queryBuilder();
    queryBuilder.where().eq(columnName, value);
    return queryBuilder.queryForFirst();
}