【问题标题】:How can I create a user-defined function in SQLite in xamarin?如何在 xamarin 的 SQLite 中创建用户定义的函数?
【发布时间】:2016-08-15 12:08:26
【问题描述】:

我尝试在 xamarin 中执行此查询

SELECT * FROM GPSPoint WHERE SQRT( POW({0}-lat,2) + POW({1}-lng,2) ) < 5*5

但它给了我错误信息

SQLite.SQLiteException: no such function: SQRT

经过搜索发现Android开发者可以使用sqlite3_create_function方法在SQLite中创建新函数。

我怎样才能在 xamarin 中做同样的事情?

【问题讨论】:

  • AFAIK 就像存储过程一样,不能在 SQLite 中创建函数。如果您发现我错了,请 LMK 对我有益。
  • 我什至不确定那是一个有效的查询。 SQL 查询将是 WHERE col = value
  • @Takarii 在 c# 语法中是一个有效的查询。原文为var query = conn.Query&lt;GPSPoint&gt;(string.Format("SELECT * FROM GPSPoint WHERE SQRT( POW({0}-lat,2) + POW({1}-lng,2) ) &lt; 5*5", lat, lng));
  • @jj- 但安卓开发者可以创建新功能。请参考this SO answer

标签: c# xamarin sqlite.net


【解决方案1】:

在以下示例中,我将自定义 sqlite 函数称为 MySqliteAdd ,它有两个参数作为输入,它们的总和作为结果

[SqliteFunction(FuncType = FunctionType.Scalar, Arguments = 2, Name = "MySqliteAdd")]
public class MySqliteAdd : SqliteFunction
{
   public override object Invoke(object[] args){
        return System.Convert.ToInt32(args[0]) + System.Convert.ToInt32(args[1]);
   }
}

您可以在下面的 sqlite 查询中使用它

Select MySqliteAdd(25,35)

结果为 60

最后你必须在初始化连接之前注册函数

SqliteFunction.RegisterFunction(typeof(MySqliteAdd));

【讨论】:

    猜你喜欢
    • 2011-12-13
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 2019-08-16
    • 2015-02-06
    相关资源
    最近更新 更多