如何在 UWP 的 sqlite-net-pcl 中使用 REGEXP
How to use REGEXP in `sqlite-net-pcl` in UWP
我有一个使用 Sqlite
数据库的 UWP
项目。我在参考文献中添加了 sqlite-net-pcl
。
我想在 select
查询中使用 REGEXP
,但它给了我 no such function: REGEXP
。
我搜索了错误,但结果是关于 SQLiteFunction
未在此处定义的。
我该怎么办?
参考 The LIKE, GLOB, REGEXP, and MATCH operators:
The REGEXP operator is a special syntax for the regexp() user function. No regexp() user function is defined by default and so use of the REGEXP operator will normally result in an error message. If an application-defined SQL function named "regexp" is added at run-time, then the "X REGEXP Y" operator will be implemented as a call to "regexp(Y,X)".
所以要使用 REGEXP,我们需要能够创建用户定义的函数。和 SQLiteFunction is the class that designed to handle user-defined functions easily in System.Data.SQLite。但是 System.Data.SQLite 是 SQLite 的 ADO.NET 提供程序,不能在 UWP 应用程序中使用。
在下面的 SQLite-net PCL, there is no such method. However, SQLite-net PCL uses SQLitePCL.raw 中,它是一个非常薄的 C# 包装器,围绕着 SQLite 的 C API 并提供对 SQLite 的低级(原始)访问。使用 SQLitePCL.raw,我们可以创建用户定义的函数,就像@Maryam 的回答中那样。
如果你可以使用像 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()); }
}
}
}
最后,我从 nuget
安装了 sqlite-net-pcl
,而不是 ReferenceManger 中的 Universal windows extensions
。
sqlite-net-pcl
nuget 中的包有 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);
}
这很好用:)
我有一个使用 Sqlite
数据库的 UWP
项目。我在参考文献中添加了 sqlite-net-pcl
。
我想在 select
查询中使用 REGEXP
,但它给了我 no such function: REGEXP
。
我搜索了错误,但结果是关于 SQLiteFunction
未在此处定义的。
我该怎么办?
参考 The LIKE, GLOB, REGEXP, and MATCH operators:
The REGEXP operator is a special syntax for the regexp() user function. No regexp() user function is defined by default and so use of the REGEXP operator will normally result in an error message. If an application-defined SQL function named "regexp" is added at run-time, then the "X REGEXP Y" operator will be implemented as a call to "regexp(Y,X)".
所以要使用 REGEXP,我们需要能够创建用户定义的函数。和 SQLiteFunction is the class that designed to handle user-defined functions easily in System.Data.SQLite。但是 System.Data.SQLite 是 SQLite 的 ADO.NET 提供程序,不能在 UWP 应用程序中使用。
在下面的 SQLite-net PCL, there is no such method. However, SQLite-net PCL uses SQLitePCL.raw 中,它是一个非常薄的 C# 包装器,围绕着 SQLite 的 C API 并提供对 SQLite 的低级(原始)访问。使用 SQLitePCL.raw,我们可以创建用户定义的函数,就像@Maryam 的回答中那样。
如果你可以使用像 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()); }
}
}
}
最后,我从 nuget
安装了 sqlite-net-pcl
,而不是 ReferenceManger 中的 Universal windows extensions
。
sqlite-net-pcl
nuget 中的包有 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);
}
这很好用:)