开发者

How to view generated SQL Statements for Android/SQLite

I wonder if there is an easy way t开发者_StackOverflow中文版o see the SQL generated by the SQLiteQueryBuilder class in Android? I want to use this for debugging purposes.

I looked around and searched but couldn't find anything. Either it's not easy or it's super obvious.


What it basically does is calling the SQLiteQueryBuilder#buildQueryString static method, so you can try to call that method passing the correct parameters. This is how the method looks like:

public static String buildQueryString(
        boolean distinct, String tables, String[] columns, String where,
        String groupBy, String having, String orderBy, String limit) {
    if (TextUtils.isEmpty(groupBy) && !TextUtils.isEmpty(having)) {
        throw new IllegalArgumentException(
                "HAVING clauses are only permitted when using a groupBy clause");
    }
    if (!TextUtils.isEmpty(limit) && !sLimitPattern.matcher(limit).matches()) {
        throw new IllegalArgumentException("invalid LIMIT clauses:" + limit);
    }

    StringBuilder query = new StringBuilder(120);

    query.append("SELECT ");
    if (distinct) {
        query.append("DISTINCT ");
    }
    if (columns != null && columns.length != 0) {
        appendColumns(query, columns);
    } else {
        query.append("* ");
    }
    query.append("FROM ");
    query.append(tables);
    appendClause(query, " WHERE ", where);
    appendClause(query, " GROUP BY ", groupBy);
    appendClause(query, " HAVING ", having);
    appendClause(query, " ORDER BY ", orderBy);
    appendClause(query, " LIMIT ", limit);

    return query.toString();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜