SQLite - Insert special symbols (trademark, ...) into table - sqlite

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

Related

I keep getting a syntax error on this code [duplicate]

I'm trying to insert some information to MySQL with Pascal, but when I run the program I get the error
unknown column 'mohsen' in field list
This is my code
procedure TForm1.Button1Click(Sender: TObject);
var
aSQLText: string;
aSQLCommand: string;
namee:string;
family:string;
begin
namee:='mohsen';
family:='dolatshah';
aSQLText:= 'INSERT INTO b_tbl(Name,Family) VALUES (%s,%s)';
aSQLCommand := Format(aSQLText, [namee, family]);
SQLConnector1.ExecuteDirect(aSQLCommand);
SQLTransaction1.Commit;
end;
How can I solve this problem?
It's because your
VALUES (%s,%s)
isn't surrounding the namee and family variable contents by quotes. Therefore, your back-end Sql engine thinks your mohsen is a column name, not a value.
Instead, use, e.g.
VALUES (''%s'',''%s'')
as in
Namee := 'mohsen';
Family := 'dolatshah';
aSQLText:= 'INSERT INTO b_tbl(Name,Family) VALUES (''%s'',''%s'')';
aSQLCommand := Format(aSQLText,[namee,family]);
In the original version of my answer, I explained how to fix your problem by "doubling up" single quotes in the Sql you were trying to build, because it seemed to me that you were having difficulty seeing (literally) what was wrong with what you were doing.
An alternative (and better) way to avoid your problem (and the one I always use in real life) is to use the QuotedStr() function. The same code would then become
aSQLText := 'INSERT INTO b_tbl (Name, Family) VALUES (%s, %s)';
aSQLCommand := Format(aSQLText, [QuotedStr(namee), QuotedStr(family)]);
According to the Online Help:
Use QuotedStr to convert the string S to a quoted string. A single quote character (') >is inserted at the beginning and end of S, and each single quote character in the string is >repeated.
What it means by "repeated" is what I've referred to as "doubling up". Why that's important, and the main reason I use QuotedStr is to avoid the Sql db-engine throwing an error when the value you want to send contains a single quote character as in O'Reilly.
Try adding a row containing that name to your table using MySql Workbench and you'll see what I mean.
So, not only does using QuotedStr make constructing SQL statements as strings in Delphi code less error-prone, but it also avoid problems at the back-end, too.
Just in case this will help anybody else I had the same error when I was parsing a python variable with a sql statement and it had an if statement in i.e.
sql="select bob,steve, if(steve>50,'y','n') from table;"
try as I might it coming up with this "unknown column y" - so I tried everything and then I was about to get rid of it and give it up as a bad job until I thought I would swap the " for ' and ' for "..... Hoooraaahh it works!
This is the statement that worked
sql='select bob,steve, if(steve>50,"y","n") from table;'
Hope it helps...
To avoid this sort of problem and SQL injection you should really look into using SQL parameters for this, not the Pascal format statement.

Get the correct Hexadecimal for strange symbol

I have this strange symbol on my pl/sql developer client (check image it's the symbol between P and B )
In the past, and for a different symbol, i was able to update my DB and remove them making this:
update table set ent_name = replace(ent_name, UTL_RAW.CAST_TO_VARCHAR2(HEXTORAW('C29B')), ' ');
The problem is that i dont remember how I translated the symbol (i had at that time) to the C29B.
Can you help me to understand how can i translate the currenct symbol to the HEX format, to i can use the command to remove it from my database?
Thanks
As long as it's in your table, you can use the DUMP function to find it.
Use DUMP to get the byte representation of the data in code of you wish to inspect for weirdness.
A good overview: Oracle / PLSQL: DUMP Function
Here's some text with plain ASCII:
select dump('Dashes-and "smart quotes"') from dual;
Typ=96 Len=25:
68,97,115,104,101,115,45,97,110,100,32,34,115,109,97,114,116,32,113,117,111,116,101,115,34
Now introduce funny characters:
select dump('Dashes—and “smart quotes”') from dual;
Typ=96 Len=31:
68,97,115,104,101,115,226,128,148,97,110,100,32,226,128,156,115,109,97,114,116,32,113,117,111,116,101,115,226,128,157
In this case, the number of bytes increased because my DB is using UTF8. Numbers outside of the valid range for ASCII stand out and can be inspected further.
The ASCIISTR function provides an even more convenient way to see the special characters:
select asciistr('Dashes—and “smart quotes”') from dual;
Dashes\2014and \201Csmart quotes\201D
This one converts non-ASCII characters into backslashed Unicode hex.
The DUMP function takes an additional argument that can be used to format the output in a nice way:
select DUMP('Thumbs 👍', 1017) from dual;
Typ=96 Len=11 CharacterSet=AL32UTF8: T,h,u,m,b,s, ,f0,9f,91,8d
select DUMP('Smiley 😊 Face', 17) from dual;
Typ=96 Len=16: S,m,i,l,e,y, ,f0,9f,98,8a, ,F,a,c,e

Is there any way to escape dot in path Json1 Sqlite

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

Extract first word in a SQLite3 database

I have a SQLITE3 database wherein I have stored various columns. One column (cmd) in particular contains the full command line and associated parameters. Is there a way to extract just the first word in this column (just before the first space)? I am not interested in seeing the various parameters used, but do want to see the command issued.
Here's an example:
select cmd from log2 limit 3;
user-sync //depot/PATH/interface.h
user-info
user-changes -s submitted //depot/PATH/build/...#2011/12/06:18:31:10,#2012/01/18:00:05:55
From the result above, I'd like to use an inline SQL function (if available in SQLITE3) to parse on the first instance of space, and perhaps use a left function call (I know this is not available in SQLITE3) to return just the "user-sync" string. Same for "user-info" and "user-changes".
Any ideas?
Thanks.
My soluion:
sqlite> CREATE TABLE command (cmd TEXT);
sqlite> INSERT INTO command (cmd) VALUES ('ls'),('cd ~'),(' mpv movie.mkv ');
sqlite> SELECT substr(trim(cmd),1,instr(trim(cmd)||' ',' ')-1) FROM command;
ls
cd
mpv
Pros:
it's not that a dirty hack
it only uses core functions
"Finds the first occurrence" function is one of the SQLite3 Core Functions (http://www.sqlite.org/lang_corefunc.html).
Of course, it is much better to use instr(X,Y).
So you can write:
SELECT substr(cmd,1,instr(cmd,' ')-1) FROM log2
As the position of your first space character is unknown, I don't think there is a corefunction in SQLite that will help.
I think you'll have to create one http://www.sqlite.org/c3ref/create_function.html
Here's a hack
sqlite> create table test (a);
sqlite> insert into test values ("This is a test.");
sqlite> select * from test;
This is a test.
sqlite> select rtrim(substr(replace(a,' ','----------------------------------------------------------------------------------------'),1,80),'-') from test;
This
It works as long as your longest command is less than 80 characters (and you include 80 '-' characters in the substitution string -- I didn't count them!). If your commands can contain '-' just use a different character that is not allowed in the commands.
I don't believe that's something you'll be able to do within the SQL itself. SQLite's support for string handling functions is not as extensive as other RDBMSs (some of which would let you do a SUBSTR with a Reg Exp).
My suggestion is either to write your own SQL function as suggested by #Jon or just do it as a post-processing step in your app code.

sqlite and batch file

I am using a sqlite database.
I want to excute sqlite from dos command line and insert fields into the tables by passing them as parameters from command line.
C:\run.bat name surname
want to create the run.bat file ...
sqlite > INSERT into tab(col1, col2) Values (%1,%2);
I guess you're looking for something like this:
sqlite3 DATABASENAME 'insert into dbo.TABLE values('%1', '%2')'
You'll need the command line program sqlite3.exe. Then you can put into your batch file:
sqlite3 dbname "INSERT INTO TableName (ColName1, ColName2) VALUES (%1, %2);"
Also note that you will have to be aware of the datatypes of the columns and, if the VALUES for those datatypes require quoting, you'll need to add the quotes into the command line (and handle embedded quotes in the values, if that's a possible occurence).

Resources