I'm trying to include this function in a websql query, something like
"select * from products where filter like '%pname%' order by soundex(filter);"
But according to this: https://www.sqlite.org/lang_corefunc.html
Soundex function is only available if sqlite was compiled with SQLITE_SOUNDEX argument, which I don't think is the case for chrome as is throwing
could not prepare statement (1 no such function: soundex)
So, my question is, is there a way to use soundex function, or at least some other similar function?
Edit 1:
For now I'm just using
order by coalesce(like('pname', filter), 0)
which is not the final solution, but better than a simple order by a specific column.
WebSQL does not specify which SQL functions are available, and has no mechanism to install user-defined functions.
Related
My R workflow now involves dealing with a lot of queries (RPostgreSQL library). I really want to make code easy to maintain and manage in the future.
I started loading large queries from separate .SQL files (this helped) and it worked great.
Then I started using interpolated values (that helped) which means that I can write
SELECT * FROM table WHERE value = ?my_value;
and (after loading it into R) interpolate it using sqlInterpolate(ANSI(), query, value = "stackoverflow").
What happens now is I want to use something like this
SELECT count(*) FROM ?my_table;
but how can I make it work? sqlInterpolate() only interpolates safely by default. Is there a workaround?
Thanks
In ?DBI::SQL, you can read:
By default, any user supplied input to a query should be escaped using
either dbQuoteIdentifier() or dbQuoteString() depending on whether it
refers to a table or variable name, or is a literal string.
Also, on this page:
You may also need dbQuoteIdentifier() if you are creating tables or
relying on user input to choose which column to filter on.
So you can use:
sqlInterpolate(ANSI(),
"SELECT count(*) FROM ?my_table",
my_table = dbQuoteIdentifier(ANSI(), "table_name"))
# <SQL> SELECT count(*) FROM "table_name"
sqlInterpolate() is for substituting values only, not other components like table names. You could use other templating frameworks such as brew or whisker.
Is it possible to find index of an character in SQLite without using extension functions?
I need to substring texts like below from the beginning until ( character in a SELECT statement.
TT 15 (Something...)
TT 5 (blabla...)
I cannot use instr in our version of SQLite (i think it is 3.6) and it's not possible to update SQLite either.
If it were possible to do something like instr() without actually calling instr(), the authors of SQLite would not have felt the need to add instr().
If you cannot update SQLite or create an extension function, you have to read the entire string from the database and search it in your own code.
I have written a custom SQLite function that transforms a string as it is copied from one table to another. It's very basic and has does its job well for quite a while. The query looks like this:
INSERT INTO Table2 (field1) SELECT MYTRANSFORM(field2) FROM Table2;
Now I need to modify the behavior of that function so that it does a lookup on another table and factors the result into how it transforms the value. Is that possible? Has anyone done it successfully?
This is possible; you can execute other SQL queries from within your function, as long as you do not call your function recursively.
SQLite has a core strftime() function. Instead of writing strftime('%Y-%m-%dT%H:%M:%S', x, ...) every time I want the datetime in ISO format, I'd like to have a function isotime(x, ...) which will simply return the value of the parameterized strftime().
Is it possible? I'd rather not change sqlite3.c directly and/or copy&paste the code.
You can create new functions:
http://www.sqlite.org/c3ref/create_function.html
Keep in mind that using them will bind your SQL statements to your particular modified implementation.
EDIT:
This tutorial contains an example of creating and registering a new SQLite function.
I need to apply two successive aggregate functions to a dataset (the sum of a series of averages), something that is easily and routinely done with common table expressions in SQL Server or another DBMS that supports CTEs. Unfortunately, I am currently stuck with SQLite which does not support CTEs. Is there an alternative or workaround for achieving the same result in SQLite without performing two queries and rolling up the results in code?
To add a few more details, I don't think it could be easily done with views because the first set of aggregate values need to be retrieved based on a WHERE clause with several parameters. E.g.,
SELECT avg(elapsedTime)
FROM statisticsTable
WHERE connectionId in ([lots of values]) AND
updateTime > [startTime] AND
updateTime < [endTime]
GROUP BY connectionId
And then I need the sum of those averages.
Now that we are in THE FUTURE, let me note here that SQLite now does support Common Table Expressions, as of version 3.8.3 of 2014-02-03.
http://www.sqlite.org/lang_with.html
Would this work?
SELECT SUM(t.time) as sum_of_series_of_averages
FROM
(
SELECT avg(elapsedTime) as time
FROM statisticsTable
WHERE connectionId in ([lots of values]) AND
updateTime > [startTime] AND
updateTime < [endTime]
GROUP BY connectionId
) as t
By converting your averages into an inline view, you can SUM() the averages.
Is this what you are looking for?
As you've mentioned, SQLite doesn't support CTEs, window functions, or any of the like.
You can, however, write your own user functions that you can call inside SQLite by registering them to the database with the SQLite API using sqlite_create_function(). You register them with the database, and then you can use them in your own application code. You can make an aggregate function that would perform the sum of a series of averages based on the individual column values. For each value, a step-type callback function is called that allows you to perform some calculation on the data, and a pointer for holding state data is also available.
In your SQL, then, you could register a custom function called sum_of_series_of_averages and have:
SELECT sum_of_series_of_averages(columnA,columnB)
FROM table
WHERE ...
For some good examples on how those work, you should check out the SQLite source code, and also check out this tutorial (search for Defining SQLite User Functions).