Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom functions in SQLite

Tags:

sqlite

Can you create functions in SQLite like you can in MSSQL?

If so, how? What is the syntax?

Thanks

like image 488
Andrew Bullock Avatar asked Jan 21 '10 11:01

Andrew Bullock


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.

What is virtual table in SQLite?

A virtual table is an object that presents an SQL table interface but which is not stored in the database file, at least not directly. The virtual table mechanism is a feature of SQLite that allows SQLite to access and manipulate resources other than bits in the database file using the powerful SQL query language.

What does sqlite3_exec return?

If the callback function to sqlite3_exec() returns non-zero, then sqlite3_exec() will return SQLITE_ABORT. If a ROLLBACK operation occurs on the same database connection as a pending read or write, then the pending read or write may fail with an SQLITE_ABORT or SQLITE_ABORT_ROLLBACK error.


2 Answers

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-))
like image 124
Roland Bouman Avatar answered Sep 20 '22 16:09

Roland Bouman


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.

like image 45
donnadulcinea Avatar answered Sep 17 '22 16:09

donnadulcinea