Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create/Use User-defined functions in System.Data.SQLite?

User-Defined Functions & Collating Sequences Full support for user-defined functions and collating sequences means that in many cases if SQLite doesn't have a feature, you can write it yourself in your favorite .NET language. Writing UDF's and collating sequences has never been easier

I spotted this bit on the C# SQLite ADO.NET provider l found here, and was having problems understanding the documentation on how to implement/use user-defined functions.

Could anyone explain how to, or provide any working examples for this lost newbie?

like image 795
Ashlocke Avatar asked Oct 05 '08 22:10

Ashlocke


People also ask

Can you create functions in SQLite?

1. Executive Summary. Applications that use SQLite can define custom SQL functions that call back into application code to compute their results. The custom SQL function implementations can be embedded in the application code itself, or can be loadable extensions.

Can we write SQL inside the UDF?

User-defined functions can't make use of dynamic SQL or temp tables. Table variables are allowed. SET statements aren't allowed in a user-defined function.


1 Answers

Robert Simpson has a great example of a REGEX function you can use in your sqlite queries:

// taken from http://sqlite.phxsoftware.com/forums/p/348/1457.aspx#1457 [SQLiteFunction(Name = "REGEXP", Arguments = 2, FuncType = FunctionType.Scalar)] class MyRegEx : SQLiteFunction {    public override object Invoke(object[] args)    {       return System.Text.RegularExpressions.Regex.IsMatch(Convert.ToString(args[1]),Convert.ToString(args[0]));    } }  // example SQL:  SELECT * FROM Foo WHERE Foo.Name REGEXP '$bar' 
like image 87
Jarrod Dixon Avatar answered Oct 14 '22 03:10

Jarrod Dixon