轉載:在c#中使用sqlite的3種自定義函式
來源 https://blog.csdn.net/lc156845259/article/details/68944742
自定義函式
有三種型別函式可以自定義,分別是:Scalar,Aggregate,Collation。
Scalar:標量(對單條資料進行計算的函式)
Aggregate:聚合(對多條資料進行計算的函式)
Collation:集合(用於排序)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
public class test
{
/// <summary>
/// 求平方根
/// </summary>
[SQLiteFunction(Name = "sqrt", Arguments = 1, FuncType = FunctionType.Scalar)]
public class Sqrt : SQLiteFunction
{
public override object Invoke(object[] args)
{
double d = Convert.ToDouble(args[0]);
return Math.Sqrt(d);
}
}
/// <summary>
/// 求平均
/// </summary>
[SQLiteFunction(Name = "mean", Arguments = -1, FuncType = FunctionType.Aggregate)]
public class Mean : SQLiteFunction
{
int step = 0;
public override void Step(object[] args, int stepNumber, ref object contextData)
{
double sum = Convert.ToDouble(contextData);
sum += Convert.ToDouble(args[0]);
contextData = sum;
step++;
}
public override object Final(object contextData)
{
double sum = Convert.ToDouble(contextData);
double mean = sum / step;
return mean;
}
}
/// <summary>
/// 中文排序
/// </summary>
[SQLiteFunction(FuncType = FunctionType.Collation, Name = "pinyin")]
public class PinYin : SQLiteFunction
{
public override int Compare(string x, string y)
{
return string.Compare(x, y);
}
}
/*
create table student(id int,high int,name varchar(10));
insert into student values(1,100,'張三');
insert into student values(2,10,'李四');
insert into student values(3,50,'王五');
.save d:/test.db
*/
static void Main(string[] args)
{
//註冊自定義函式
SQLiteFunction.RegisterFunction(typeof(Sqrt));
SQLiteFunction.RegisterFunction(typeof(Mean));
SQLiteFunction.RegisterFunction(typeof(PinYin));
using (var connection = new SQLiteConnection(string.Format("Data source={0}", "d:/test.db")))
{
connection.Open();
using (var com = connection.CreateCommand())
{
com.CommandText = "select sqrt(id) as value from student ";
using (SQLiteDataReader dr = com.ExecuteReader())
{
while (dr.Read())
{
double value = Convert.ToDouble(dr["value"]);
Console.WriteLine(value);
}
dr.Close();
}
com.CommandText = "select mean(high) as val from student ";
using (SQLiteDataReader dr = com.ExecuteReader())
{
if (dr.Read())
{
double value = Convert.ToDouble(dr["val"]);
Console.WriteLine(value);
}
dr.Close();
}
com.CommandText = "select name from student ORDER BY name COLLATE pinyin";
using (SQLiteDataReader dr = com.ExecuteReader())
{
while (dr.Read())
{
Console.WriteLine(dr["name"].ToString());
}
dr.Close();
}
}
}
}
}
編譯方法
從http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
下載了http://system.data.sqlite.org/downloads/1.0.112.0/sqlite-netFx40-binary-Win32-2010-1.0.112.0.zip
解壓縮,然後在那個解壓縮目錄執行
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc ..\sqlite_udf.cs -r:System.Data.SQLite.dll
相關文章
- C# 註冊並使用sqlite 自定義函式C#SQLite函式
- 在python中使用sqlite的自定義函式功能PythonSQLite函式
- 轉:對字串的“sum”——在Oracle中自定義聚集函式的例子字串Oracle函式
- stl中各種容器的自定義比較函式函式
- HIVE中的自定義函式Hive函式
- 在資料庫中自定義外部函式資料庫函式
- Hive中自定義函式Hive函式
- Oracle中自定義函式Oracle函式
- Loadrunner 使用者自定義函式使用[轉]函式
- 關於在oracle 的group by中使用自定義函式Oracle函式
- 函式索引使用細節——自定義函式的索引化函式索引
- 自定義跳轉函式的通用unhook方法函式Hook
- MySQL使用之五_自定義函式和自定義過程MySql函式
- C#定時器中呼叫WCF服務+自定義回撥函式C#定時器函式
- sqlite中存放自定義表結構的位置SQLite
- shell自定義函式函式
- Oracle 自定義函式Oracle函式
- perl自定義函式函式
- TDengine 3.0 中如何編譯、建立和使用自定義函式編譯函式
- Clickhouse 使用者自定義外部函式函式
- matlab自定義函式建立與使用Matlab函式
- 7.yii中使用自定義函式函式
- 自定義函式索引使用及其注意點函式索引
- C# 自定義事件和委託(精彩轉載)C#事件
- C#中的函式指標 (轉)C#函式指標
- GRDB自定義的純函式函式
- Hive常用函式及自定義函式Hive函式
- 影片直播系統原始碼,在Laravel中自定義模板函式 並在模板中呼叫原始碼Laravel函式
- hive 3.0.0自定義函式Hive函式
- MySQL建立自定義函式MySql函式
- python 自定義函式Python函式
- oracle 自定義聚合函式Oracle函式
- mysql自定義函式篇MySql函式
- 一個自定義函式函式
- Oracle自定義聚集函式Oracle函式
- ORACLE 自定義函式BUG?Oracle函式
- SQL SERVER 自定義函式SQLServer函式
- SQL 自定義函式FUNCTIONSQL函式Function