如何在Xamarin Studio中查看SQLite.NET PCL生成的SQL?

kqlmhetl  于 2023-05-18  发布在  SQLite
关注(0)|答案(3)|浏览(164)

我研究了这个问题,我能找到的就是这样打开.Trace = true的建议:

db1 = DependencyService.Get<ISQLite>().GetConnection();
        db1.Trace = true;

我也试过这个:

db2.Trace = true;
 var categories = db2.Query<Category>("SELECT * FROM Category ORDER BY Name").ToList();
 Debug.WriteLine("xxxx");

我这样做,然后重新启动应用程序。当我查看应用程序输出时,我只看到有关已启动线程和xxxx的信息,但看不到任何SQL跟踪信息。
接下来我可以尝试什么?

7z5jn7bk

7z5jn7bk1#

您需要在SQLiteConnection上设置TraceTracer(action)属性,以将查询打印到输出:

db.Tracer = new Action<string>(q => Debug.WriteLine(q));
db.Trace = true;
hiz5n14c

hiz5n14c2#

Application Output窗口中查找以**Executing**开头的行
Trace设置为true后的输出示例:

Executing: create table if not exists "Valuation"(
"Id" integer primary key autoincrement not null ,
"StockId" integer ,
"Time" datetime ,
"Price" float )
Executing Query: pragma table_info("Valuation")
Executing: create  index if not exists "Valuation_StockId" on "Valuation"("StockId")

Executing: insert  into "Stock"("Symbol") values (?)

Executing Query: select * from "Stock" where ("Symbol" like (? || '%'))
  0: A

参考:https://github.com/praeclarum/sqlite-net/blob/38a5ae07c886d6f62cecd8fdeb8910d9b5a77546/src/SQLite.cs

wfypjpf4

wfypjpf43#

SQLite PCL使用Debug.WriteLine,这意味着日志仅包含在PCL的Debug构建中。
删除对sqlite.net PCL的nuget引用(保留原生引用),并将SQLite.cs作为类添加到项目中,然后执行调试构建,并设置Trace标志,您将看到跟踪。
除了在Xamarin iOS项目中包含SQLite.cs文件外,我不需要做任何特殊的事情就可以做到这一点:

using (var conn = new SQLite.SQLiteConnection("mydb.sqlite") { Trace = true }) {
    var rows = conn.Table<PodcastMetadata>().Where(row => row.DurationMinutes < 10).Select(row => new { row.Title });
    foreach (var row in rows) {
        Debug.WriteLine(row);
    }
}

输出:

Executing Query: select * from "PodcastMetadata" where ("DurationMinutes" < ?)
  0: 10

相关问题