【问题标题】:How can I see the SQL generated by SQLite.NET PCL in Xamarin Studio?如何在 Xamarin Studio 中查看 SQLite.NET PCL 生成的 SQL?
【发布时间】:2017-03-05 02:23:39
【问题描述】:

我对此进行了研究,我能找到的只是一个打开 .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 跟踪信息。

谁能给我这方面的建议。谢谢

【问题讨论】:

    标签: sqlite xamarin xamarin.forms xamarin-studio sqlite-net


    【解决方案1】:

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

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

    【讨论】:

    • 虽然这行得通,但如果我发现错误,它并没有真正提供太多信息。例如,我必须添加 try...catch 来捕获仅显示“约束”的 SQLIteException。虽然我知道约束是什么,但我不知道其他任何事情,例如它发生在哪里以及为什么以及在哪个约束上。我希望启用 Tracer 来跟踪会提供更多信息,但它提供的只是它正在执行一些查询
    【解决方案2】:

    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

    【讨论】:

    • 谢谢,但我已经在那里找了 3-4 个小时 :-( 说真的,我需要一些帮助和建议。我所做的是将 Krueger 的 nuget 包 1.2.0 安装到我的应用程序 IOS 中和 Android。但是我看到你引用了一个 SQLite.cs 文件。你能告诉我你的应用程序吗?你使用什么 nuget 包,它还需要我把那个 SQLite.cs 文件放在一些地方吗?非常感谢提前.
    【解决方案3】:

    SQLite PCL 使用 Debug.WriteLine,这意味着日志仅包含在 PCL 的调试版本中。

    删除您对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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      相关资源
      最近更新 更多