Creating stored procedure and SQLite?

Viewed 203847

Is it somehow possible to create a stored procedure, when using SQLite?

6 Answers

No, but you can :

  • Write long multi-statement scripts
  • Create temporary one-row tables called e.g. Vars to hold variables
  • Create a View over a Recursive CTE to program arbitrary functions in pure SQL queries.

So you can do most things you would normally do with stored procs.

For how to program functions in a SQL View see https://www.cafe-encounter.net/p3300/pretending-that-sqlite-has-stored-procedures-and-functions.

Alternatively you can:

  • Compile short single-page C programs to program arbitrary functions

This is easier and less work than you might think!

A step-by-step guide is at https://www.cafe-encounter.net/p3244/installing-and-using-sqlite-extensions-on-macos-and-maybe-windows-linux-too . This does add some deployment work: you will have to deploy the additional dll/so/dylib files with your application.

I've come across this question myself. I think stored procedures are supported in php pdo, but that module is handling it and building normal sql queries to send to sqlite. So, in php, possible to write stored procedures in your code, but no performance gain from using them.

correct me if I'm wrong, please.

Related