问题描述
我现在用的是查询
功能,以生成SQL查询我的表。有没有办法看到,运行的实际查询?比如登录它的地方?
目前为止最好的,我所能做的就是看看在使用断点光标的成员mQuery。我喜欢输出的自动查询,虽然。该成员当然是不公开的,没有一个getter。
只是为了记录在案,这里是公认的anwser的实现。
/ **
*为了返回之前记录的查询执行的指针厂
*光标
*
* @author文森特@ MarvinLabs
* /
公共类SQLiteCursorFactory实现CursorFactory {
私人布尔debugQueries = FALSE;
公共SQLiteCursorFactory(){
this.debugQueries = FALSE;
}
公共SQLiteCursorFactory(布尔debugQueries){
this.debugQueries = debugQueries;
}
@覆盖
公共光标newCursor(SQLiteDatabase分贝,SQLiteCursorDriver masterQuery,
字符串editTable,SQLiteQuery查询){
如果(debugQueries){
Log.d(SQL,query.toString());
}
返回新SQLiteCursor(DB,masterQuery,editTable,查询);
}
}
您可以使用自己的 SQLiteDatabase.CursorFactory
到数据库。 (请参阅openDatabase参数)。这将允许你创建自己的光标
,这使查询以方便现场的子类。
修改:其实,你甚至可能不会有子类光标
。只要有你的工厂的 newCursor()
方法返回一个标准的 SQLiteCursor
,但这样做之前登录查询。
I am using the query
functions in order to build the SQL queries for my tables. Is there a way to see the actual query that is run? For instance log it somewhere?
So far the best I could do was to have a look at the cursor's member mQuery using a breakpoint. I'd love to output the queries automatically though. This member is of course not public and does not have a getter.
Just for the record, here is an implementation of the accepted anwser.
/**
* Implement the cursor factory in order to log the queries before returning
* the cursor
*
* @author Vincent @ MarvinLabs
*/
public class SQLiteCursorFactory implements CursorFactory {
private boolean debugQueries = false;
public SQLiteCursorFactory() {
this.debugQueries = false;
}
public SQLiteCursorFactory(boolean debugQueries) {
this.debugQueries = debugQueries;
}
@Override
public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery,
String editTable, SQLiteQuery query) {
if (debugQueries) {
Log.d("SQL", query.toString());
}
return new SQLiteCursor(db, masterQuery, editTable, query);
}
}
You can apply your own SQLiteDatabase.CursorFactory
to the database. (See the openDatabase parameters.) This will allow you to create your own subclass of Cursor
, which keeps the query in an easily accessible field.
edit: In fact, you may not even have to subclass Cursor
. Just have your factory's newCursor()
method return a standard SQLiteCursor
, but log the query before doing so.
这篇关于在Android的日志记录SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!