我有一个与UWP数据库一起工作的Sqlite项目。我将sqlite-net-pcl添加到我的参考资料中。我想在REGEXP查询中使用select,但它给了我no such function: REGEXP。我搜索了错误,但是结果是关于没有在这里定义的SQLiteFunction的。我该怎么办?
发布于 2017-02-16 07:16:45
最后,我从nuget安装了nuget,而不是ReferenceManger中的Universal windows extensions。
nuget中的sqlite-net-pcl包有sqlite3_create_function方法。
SQLiteConnection con = new SQLiteConnection(@"myDB.db");
SQLitePCL.raw.sqlite3_create_function(con.Handle, "REGEXP", 2, null, MatchRegex);
private void MatchRegex(sqlite3_context ctx, object user_data, sqlite3_value[] args)
    {
        bool isMatched = System.Text.RegularExpressions.Regex.IsMatch(SQLitePCL.raw.sqlite3_value_text(args[1]), SQLitePCL.raw.sqlite3_value_text(args[0]),RegexOptions.IgnoreCase);
        if (isMatched)
            SQLitePCL.raw.sqlite3_result_int(ctx, 1);
        else
            SQLitePCL.raw.sqlite3_result_int(ctx, 0);
    }这件事很好:)
发布于 2016-10-31 10:24:45
参考文献类似的、GLOB、REGEXP和MATCH运算符
REGEXP运算符是regexp()用户函数的特殊语法。默认情况下不定义regexp()用户函数,因此使用REGEXP运算符通常会产生错误消息。如果在运行时添加了名为" REGEXP“的应用程序定义的SQL函数,那么"X”运算符将被实现为对"regexp(Y,X)“的调用。
因此,要使用REGEXP,我们需要能够创建用户定义的函数。SQLiteFunction是设计用来在System.Data.SQLite中轻松处理用户定义函数的类.然而,System.Data.SQLite是SQLite的ADO.NET提供者,不能在UWP应用程序中使用。
在SQLite-net PCL中,没有这样的方法。然而,SQLite-net在底层使用SQLitePCL.raw,这是一个非常薄的C#包装器,围绕着SQLite的C,并提供了对SQLite的低级别(raw)访问。使用SQLitePCL.raw,我们可以在@Maryam的答案中创建用户定义的函数。
发布于 2017-02-14 15:32:16
如果您可以使用类似于SQLite PCL的功能,您可以执行以下操作(可能与其他PCL一起工作,但不确定):
using System;
using SQLitePCL.pretty;
using System.Text.RegularExpressions;
namespace TestSqlite
{ 
    class Program
    {
        static void Main(string[] args)
        {
            Func<ISQLiteValue, ISQLiteValue, ISQLiteValue> regexFunc =
                        (ISQLiteValue val, ISQLiteValue regexStr) =>
                        {
                            if (Regex.IsMatch(Convert.ToString(val), Convert.ToString(regexStr)))
                                return true.ToSQLiteValue();
                            return false.ToSQLiteValue();
                        };
            SQLitePCL.Batteries.Init();
            SQLiteDatabaseConnection _dbcon = SQLiteDatabaseConnectionBuilder
                        .InMemory
                        .WithScalarFunc("REGEXP", regexFunc)
                        .Build();
            string sql = "CREATE TABLE foo (a int, b text);";
            _dbcon.ExecuteAll(sql);
            _dbcon.ExecuteAll(@"INSERT INTO foo VALUES (1, 'this is me');
                                INSERT INTO foo VALUES (2, 'that is me');
                                INSERT INTO foo VALUES (3, 'he is me');");
            sql = "SELECT * FROM foo where '\\w{4} is me' REGEXP b;";
            foreach (var row in _dbcon.Query(sql)) { Console.WriteLine(row[1].ToString()); }
        }
    }
}https://stackoverflow.com/questions/40287330
复制相似问题