开发者

How to create custom functions in SQLite

开发者 https://www.devze.com 2022-12-16 22:14 出处:网络
Can you create 开发者_开发问答functions in SQLite like you can in MSSQL? If so, how? What is the syntax?

Can you create 开发者_开发问答functions in SQLite like you can in MSSQL?

If so, how? What is the syntax?

Thanks


SQLite does not have a stored function/stored procedure language. So CREATE FUNCTION does not work. What you can do though is map functions from a c library to SQL functions (user-defined functions). To do that, use SQLite's C API (see: http://www.sqlite.org/c3ref/create_function.html)

If you're not using the C API, your wrapper API may define something that allows you access to this feature, see for example:

  • PHP sqlite_create_function() (http://www.php.net/manual/en/function.sqlite-create-function.php)
  • Python sqlite3.create_function() (http://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_function)
  • Perl $dbh->sqlite_create_function($name,$argc,$code_ref,$flags) (https://metacpan.org/pod/DBD::SQLite#$dbh-%3Esqlite_create_function(-$name,-$argc,-$code_ref,-$flags-))


This could be useful to many: in SQLiteStudio it is possible to define new functions and collations easily from interface through a sql built-in plugin for example.

https://github.com/pawelsalawa/sqlitestudio/wiki/Official_plugins#sql-built-in

Through the function editor.


You can write arbitrary functions in SQL with the define extension:

-- define a function to sum the numbers 1..n
select define('sumn', ':n * (:n + 1) / 2');

-- use it as a regular function
select sumn(3);
6
select sumn(5);
15
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号