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.
Related
I wrote a custom SQLite 'sort' over strings that basically replaces each relevant substring by an alphabetic letter in the appropriate place in the alphabet.
The problem is pretty simple - there are too many REPLACE statements running around causing a parser stack overflow.
For example, the statement looks something like: SELECT ... FROM ... WHERE ... ORDER BY REPLACE(REPLACE(...REPLACE('alpha','A'), 'beta','B'), 'gamma','C')...);
The total count of REPLACE calls is 66.
My current work-around is just to use a custom function to apply the replacements (since I am using the sqlite c api), but it would be nice to be able to do this in SQLite itself, rather than having to use a c callback.
Is it possible? Is there a better solution?
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.
I would like to update a text field in an sqlite database created using the peewee python library. Specifically I would like to use peewee's atomic updates something like:
query = Table.update(textfield = Table.textfield + 'string').where(some condition)
query.execute()
This type of update works fine for numeric fields but not for text fields. I'm guessing there may be a way to do this with the sqlite || operator, but as sql in general is somewhat new to me I am unable to figure it out.
You can use the concat operator:
query = Table.update(textfield=Table.textfield.concat('string')).where(whatever)
query.execute()
The concat operator will use || under-the-hood.
I'm using SQLite, and I'm unable to find a way to locate the index of the last occurrence of a character. For example, the records that I need to parse are:
test123.contoso.txt
testABC.contoso.atlanta.docx
another.test.vb
I would appreciate if anybody can point me in the direction how I can parse the file extensions (txt, docx, vb) from these records through a SQLite query. I've tried using the REVERSE function, but unfortunately SQLite doesn't include this in it's toolbox.
You can adapt the solution in How to get the last index of a substring in SQLite? to extract the extension.
select distinct replace(file, rtrim(file, replace(file, '.', '')), '') from files;
If you want to check whether a file name has a specific extension, you can use LIKE:
... WHERE FileName LIKE '%.txt'
However, it is not possible with the built-in functions to extract the file extension.
If you need to handle the file extension separately, you should store it separately in the database, too.
I have a .sqlite db which contains only one table. That table contains three columns and I am interested in indexing one column ONLY.
The problem is, when I perform the indexing, I got an empty index table !
I am using SQLite Manager add-ons for Firefox. This is the syntax that appears before I confirm the indexing:
CREATE INDEX "main"."tableIndex" ON "table" ("column1" ASC)
I don't know what is the problem here. I tried this tool - long time ago - with another database and it works fine.
Any suggestion ?
You cannot "see" the contents of a database index. No table or table-like structure is created that corresponds to the index. So there is nothing to look at that could be empty.
If the CREATE INDEX command ran without error, you can be confident that the index was created and will continue to be maintained by SQLite as you add, remove, and update data.
As per the comments, below, #iturki is actually trying to index for full text search. SQLite supports several extensions for full text search but they are not implemented through the stanard CREATE INDEX command. See this reference.
Try use VACUUM query. It will completely rebuild sqlite database file and will rebuild all indices and reset all ROWID etc.