It seems that if I have key test1.test2 there is no way to escape this dot to use this key as is.
Expression:
sqlite> update collections set json_nodes = (select json_set(json(collections.js
on_nodes), '$.test.test1', json('123')) from collections);
will lead to
{"test":{"test1":123}}
For lookup, you can enclose the key name that contains a dot in double quotes. However, if your key name contains a double quote, I think you are out-of-luck unless you modify the source code to the JSON1 extension.
Escaping by double quote seems to be not documented, but you can see the way it works from the source code to the JSON1 extension, in the function jsonLookupStep.
Example:
SELECT json_extract(json_data, '$."Issuer.LongCompanyName"')
FROM instruments_table
Related
Assuming that I have a database with names Main, AA and A'A. Both have a table named MyTable.
The Main database was opened originally and AA and A'A are attached afterwards.
I can use select * from 'AA'.'MyTable', but I found no way to address the A'A database similarly.
The 'A'A' or 'A''A' did not work. I do not insist escaping the ' character but I need to be able to address all possible valid database names.
Never use single quotes around database/table/column names.
When needed use double quotes or square brackets or backticks.
When attaching the database A'A you use an alias like:
ATTACH "c:\...path..\A'A.db" AS anything;
Use that alias in the SELECT statement:
SELECT * FROM anything.MyTable;
I need to replace single quotes in a string of numbers and use in a WHERE IN clause. for example, I have
WHERE Group_ID IN (''4532','3422','1289'')
The criteria within parenthesis is being passed as a parameter, so I have no control over that. I tried using :
WHERE Group_ID IN (REGEXP_REPLACE(''4532','3422','1289'', '[']', ' ',1,0,i))
also tried using OReplace
WHERE Group_ID IN (OReplace(''4532','3422','1289'', '[']', ' '))
but get the same error:
[Teradata Database] [3707] Syntax error, expected something like ','
between a string or a Unicode character literal and the integer '4532'.
Please suggest how to remove the single enclosing quotes or even removing all single quotes should work as well.
The string ''4532','3422','1289'' you are using is incorrect because it contains non-escaped single quotes. This is a syntax error in SQL. In this particular form, no matter what function you use to fix it or which RDBMS you use, it will result in error with standard SQL.
Functions in the SQL cannot fix syntax errors. REGEXP_REPLACE and OReplace never get executed because the query never enters the execution state. It never goes past the SQL syntax parser.
To see the error from perspective of the SQL parser, you may break the string in to multiple parts
'' -- SQL Parser sees this as a starting and ending quote and hence an empty string
4532 -- Now comes what appears to SQL parser as an integer value
',' -- Now this is a pair of quotes containing a single comma
3422 -- Again an integer
',' -- Again a comma
1289 -- Again integer
'' -- Again emtpy string
This amalgam of strings and numbers will not mean anything to the SQL parser and will result in an error.
Fix
The fix is to properly escape the data. Single quotes must be escaped using another preceding single quote. So correct string in this scenario becomes '''4532'',''3422'',''1289'''
Another thing is that the OReplace usage (once syntax is fixed) is like OReplace(yourStringValueHere, '''', ' ')) Observe the usage of escaped single quote here. Two outer quotes are for the string start and end. First inner quote is the escape character and second inner quote is the actual data passed to the function.
I'm wondering what constraints SQLite puts on table and column names when creating a table. The documentation for creating a table says that a table name can't begin with "sqlite_" but what other restrictions are there? Is there a formal definition anywhere of what is valid?
SQLite seems surprisingly accepting as long as the name is quoted. For example...
sqlite> create table 'name with spaces, punctuation & $pecial characters?'(x int);
sqlite> .tables
name with spaces, punctuation & $pecial characters?
If you use brackets or quotes you can use any name and there is no restriction :
create table [--This is a_valid.table+name!?] (x int);
But table names that don't have brackets around them should be any alphanumeric combination that doesn't start with a digit and does not contain any spaces.
You can use underline and $ but you can not use symbols like: + - ? ! * # % ^ & # = / \ : " '
From the sqlite doc,
If you want to use a keyword as a name, you need to quote it. There are four ways of quoting keywords in SQLite:
'keyword' A keyword in single quotes is a string literal.
"keyword" A keyword in double-quotes is an identifier.
[keyword] A keyword enclosed in square brackets is an identifier. This is not standard SQL. This quoting mechanism is used by MS Access and SQL Server and is included in SQLite for compatibility.
`keyword` A keyword enclosed in grave accents (ASCII code 96) is an identifier. This is not standard SQL. This quoting mechanism is used by MySQL and is included in SQLite for compatibility.
So, double quoting the table name and you can use any chars. [tablename] can be used but not a standard SQL.
There is table column containing file names: image1.jpg, image12.png, script.php, .htaccess,...
I need to select the file extentions only. I would prefer to do that way:
SELECT DISTINCT SUBSTR(column,INSTR('.',column)+1) FROM table
but INSTR isn't supported in my version of SQLite.
Is there way to realize it without using INSTR function?
below is the query (Tested and verified)
for selecting the file extentions only. Your filename can contain any number of . charenters - still it will work
select distinct replace(column_name, rtrim(column_name,
replace(column_name, '.', '' ) ), '') from table_name;
column_name is the name of column where you have the file names(filenames can have multiple .'s
table_name is the name of your table
Try the ltrim(X, Y) function, thats what the doc says:
The ltrim(X,Y) function returns a string formed by removing any and all characters that appear in Y from the left side of X.
List all the alphabet as the second argument, something like
SELECT ltrim(column, "abcd...xyz1234567890") From T
that should remove all the characters from left up until .. If you need the extension without the dot then use SUBSTR on it. Of course this means that filenames may not contain more that one dot.
But I think it is way easier and safer to extract the extension in the code which executes the query.
How can I insert special symbols like trademark into SQLite table? I have tried to use PRAGMA encoding = "UTF-16" with no effect :(
Typically if you surround an SQL entry with ''Single quotes, it goes in as a literal.
i.e.
'™'
problem solved. it is necessary to open DB file with sqlite3_open16, then execute command PRAGMA encoding = \"UTF-16\"; (I am not sure, if it is necessary). Now the insert will be done with UTF-16.
To select from db (to get column value) is necessary to use sqlite3_column_text16 function