Insert a String Value with Semicolon Causes Error - sql

How can I escape a semicolon in a string for insert into an SQL database table?
Example:
Insert Into items(Description)
Values('Anti Surge T; LBC Slow Blow 6.3 x 32mm 7A / 250V');

semicolon in a string should not cause any problem, still enclose the string in single quotes in insert statement.

Related

What is wrong with my SQL INSERT syntax? quotes or square brackets or...?

Here is my SQL INSERT statement
sSqlInsert = "Insert into GruppoColl Values (12343009,TRUE,'30/06/2022 11:57:35',RD,Descrizione,TRONCHETTI_RAME_12.IPT,20050082,'Tipo B','Bombato',0,oransen,'In')"
Here is the error message:
HResult=0x80040E10
Message=No value given for one or more required parameters.
I know 'In' is a reserved word and that 'Tipo B' has spaces so I've put them in single quotes.
Here is the definition of the table in Access:

Insert ARRAY in PostgreSQL table with apostrophes in values

I have a Python script with SQL insert statements that insert parsed data from file to PostgreSQL table. In case data has apostrophes, execution fails. In detail:
<name>RYAZAN'</name>
or
<name>CHYUL'BYU</name>
I found the solution when it is a string -> adding extra ' to apostrophe so it transforms to 'RYAZAN''' or 'CHYUL''BYU' in INSERT statement.
But in case my values are in list (from python script) like city = ["RYAZAN''", "CHYUL''BYU"] Python automatically puts double quotes instead of single quotes. As a result when trying to insert
INSERT INTO City (uuid, name) VALUES (uuid_generate_v4(), unnest(array{city}))
SQL fails with error
ERROR: column "RYAZAN''.." does not exist
because sql reads the double quotes as a column name or whatever. Is there a way to insert ARRAY with values that contain apostrophes?
Try using $$ dollar-quoted strings
CREATE TEMP TABLE t (f TEXT);
INSERT INTO t VALUES ($$<name>CHYUL'BYU</name>$$),
($$<name>RYAZAN'</name>$$);
SELECT * FROM t;
f
------------------------
<name>CHYUL'BYU</name>
<name>RYAZAN'</name>
(2 Zeilen)
There are also many other ways to escape quotes
INSERT INTO t
VALUES (E'\'foo'),('''bar'''),
('"the answer is" -> ''42'''),($$It's "only"" $1.99$$);
SELECT * FROM t;
f
-------------------------
'foo
'bar'
"the answer is" -> '42'
It's "only"" $1.99
(4 Zeilen)
Demo: db<>fiddle

SQLITE escape comment "--"

I'm using DB Browser for SQLite, doing multiple inserts. However I need to escape -- but it just keeps giving me errors.
INSERT INTO table(name) VALUES ("S6--sfI6H6E"),("ASGts7sa6jw") -- Error at S6
I have tried the following:
INSERT INTO table(name) VALUES ("S6\-\-sfI6H6E"),("ASGts7sa6jw") ESCAPE '\' - Error at ESCAPE
INSERT INTO table(name) VALUES ("S6\-\-sfI6H6E"),("ASGts7sa6jw") - Inserts Literal String
INSERT INTO table(name) VALUES ("S6'-'-sfI6H6E"),("ASGts7sa6jw") - Inserts Literal String
INSERT INTO table(name) VALUES ("S6----sfI6H6E"),("ASGts7sa6jw") - Error at S6
How can I escape --?

Single and double quotes in SQL Server

I want to know exact reason why this error occur playing with quotes.
INSERT INTO table_check(name) VALUES('hello'hi') -- ERROR
INSERT INTO table_check(name) VALUES('hello''hi') -- RESULT:- hello'hi
INSERT INTO table_check(name) VALUES('hello'''hi') --ERROR
INSERT INTO table_check(name) VALUES('hello''''hi') --RESULT:- hello''hi
INSERT INTO table_check(name) VALUES('hello'''''hi') --ERROR
INSERT INTO table_check(name) VALUES('hello''''''hi') --RESULT:- hello'''hi
Single Quotes are Escaped by Doubling Them up.So whenever even number of Quotes are present, then we get the Result.
To Know The Behavior of Single Quotes Try to Run This Below Code:
Select '','''','''''','''''''',''''''''''
So,Single Quotes Should be Even Number Else We get error like:Unclosed quotation mark after the character string ') -- ERROR

How to ignore the splitting character in strings with Liquibase SQL syntax

I have this SQL script:
Insert into DB_TABLE1 (N, B) values ('30','66');
Insert into DB_EXECUTION (COMMAND)
VALUES ('/* Script generated at 20.04.2006 12:38:44 */
/* error_permissible = 955*/
Create Table map_encodekey
( privatekey VARCHAR2(30) NOT NULL
);
/* error_permissible = 2260*/
Alter Table map_encodekey
Add Constraint map_encodekey_pk
Primary Key
(privatekey);
commit;
');
This script is executed by Liquibase, but it raises this error
...
Caused by: java.sql.SQLSyntaxErrorException: ORA-01756: quoted
string not properly terminated
...
The problem is that Liquibase splits the second insert statement when it finds the ';' in the quoted string of the DB_EXECUTION.COMMAND field; in fact Liquibase doesn't interpret the SQL statements as SQL, but treats them as normal strings.
This is a problem, because I have to split the statements somehow because I have multiple insert statements in the same script (so the splitStatements option is not an option for me) meanwhile I have to tell Liquibase to treat SQL strings as strings which start and end with single quotes.
Is there any way to solve this problem?
Thank you
Giulio