User Defined Functions
Given that C/C++ is not in my skills set, I need some help/guidance on two issues:
A. When should I use sqlite3_create_function as opposed to building an SQLite extension and loading it?
As far as I can see, both options are connection bound and evaporate on the connection being closed and I realise that building an extension is a neater way when creating many functions
B. Anyone has a worked example (of something simple, like adding a number to an existing column in a table in the current connection) of using sqlite3_create_function? I can adapt the template for my purposes.
When should I use sqlite3_create_function as opposed to building an SQLite extension and loading it?
The sqlite3_create_function is how you "hook up" a function so it can be executed by the SQLite3 engine. An "SQLite3 extension" is merely a wrapping paper packaging for functions which are added to SQLite3 via sqlite3_create_function.
That is, if you want to make toast, you need to make a toaster (write the function). Then you need to hook it up (plug it into the electrical outlet -- as in call sqlite3_create_function to "hook up" the toaster").
The "packaging" the toaster arrives in makes no difference. It does not matter whether you build it from scratch yourself, or order it in an Amazon box from Amazon (a loadable extension). In both cases you have to take it out of the box (if any) and plug it in (call sqlite3_create_function) before you can make toast.
Anyone has a worked example (of something simple, like adding a number to an existing column in a table in the current connection) of using sqlite3_create_function? I can adapt the template for my purposes.
This is not normally what a function does. A function is like a toaster. You put something in (raw bread) and something else comes out (toasted bread). While a toaster may have "side-effects" (such as burning down your house) these are generally not the primary function of the toaster.
In an SQL database the way to update a value in a table is with the UPDATE statement. While you may write a function that takes raw bread and make toast and at the same time tallies up the number of slices of bread toasted in some table as a side-effect, but that would greatly limit the utility of the function.
Thanks Keith; as I've mentioned, lack of C/C++ experience is holding me back and I'd appreciate some help.
I am looking for a way to achieve what follows below using the API sqlite3_create_function: is that possible?
connection.CreateFunction( "volume", (double radius, double height) => Math.PI * Math.Pow(radius, 2) * height); var command = connection.CreateCommand(); command.CommandText = @" SELECT name, volume(radius, height) AS volume FROM cylinder ORDER BY volume DESC ";
Why not just do it directly?
select name, pi() * pow(radius, 2) * height as volume from cylinder order by 2 desc ;
You will, of course, need to be using a version of SQLite3 that has the math functions available and has been compiled with those functions turned on.
The function example was simply to provide an illustration: my objective is to be able to find what it takes to have sqlite3_create_function working.
Your example is using a .net interface presumably provided by Microsoft, the underlying sqlite3_create_function() is a C interface and requires the user to process the argument list built by the SQLite VDBE at a somewhat low level.
The tragedy is the lengths people go to avoid answering a question.