Postgresql how to escape comma - sql

I want to insert a string like
"keyId"=>"5cfd1f5bd6d0c2e6aae4421553a8b3ea","server"=>"http://10.10.27.10/keys/key_file.key","keyRotationPeriod"=>"10"
to the database, my current SQL is
insert abr.drm(id, name, type, info, ksid, iv)
0, "test", 1, ""keyId"=>"5cfd1f5bd6d0c2e6aae4421553a8b3ea","server"=>"http://10.10.27.10/keys/key_file.key","keyRotationPeriod"=>"10"", NULL, NULL
But I got error as
INSERT has more expressions than target columns,
LINE 1: ....10/keys/key_file.key','keyRotationPeriod"=>"10"',NULL,NULL)...
^
`
how to escape comma in postgresql?

Related

how to save value in oracle using nq'[]' operator

I need to save string with single quote in column with nvarchar2 data type. I find here
https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm
that I can save it using quote operator. I try to do it in my SQL query
insert into table (question_pattern_id, text, event_selection_id, context_theme_id, placeholder_body)
values ('65', nq'[👽]', '', '', nq'[👽]')
but it inserts ?? instead of 👽
Before data was added by query
insert into table (question_pattern_id, text, event_selection_id, context_theme_id, placeholder_body)
values ('65', n'👽', '', '', n'👽')
and everything was saved correctly

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

Missing select keyword in Oracle SQL

I am getting this error
SQL Error: ORA-00928: missing SELECT keyword
00928. 00000 - "missing SELECT keyword"`
when I am trying to insert like this
create table certf
(
certificate_id integer primary key,
certificate_name varchar(100) not null,
certificate_content varchar(300) not null
);
insert into certf (&certificate_id, &certificate_name, &certificate_content);
You are missing values actually:
insert into certf (certificate_id, certificate_name, certificate_content)
values (&certificate_id, &certificate_name, &certificate_content);
Notice that I also added the column list to the insert. This is a best practice.
If those are supposed to be values you're providing as substitution values, then you're missing the values keyword:
insert into certf values (&certificate_id,&certificate_name,&certificate_content);
But you need the string values to be in quotes:
insert into certf values (&certificate_id,'&certificate_name','&certificate_content');
and you should supply the column names too:
insert into certf (certificate_id,certificate_name,certificate_content)
values (&certificate_id,'&certificate_name','&certificate_content');
With you current code the parser is seeing that first list of - possible, but actually invalid in this case - identifiers, i.e column names; because it hasn't seen that values keyword yet. It's treated as something like:
insert into certf (42,some_name,some_content);
And having done that, and when it still doesn't see a values keyword or values list, it's expecting this to be an insert ... select construct instead. You could do it that way:
insert into certf (certificate_id,certificate_name,certificate_content)
select &certificate_id,'&certificate_name','&certificate_content' from dual;
But you aren't doing that. So it doesn't see the select either, and it throws the error you see.

Following regex is not working

I have this constraint in the SQL Server statement.
([VehNo] like '[a-zA-Z][a-zA-Z],[a-zA-Z][0-9][0-9][0-9][0-9][a-zA-Z]')
But when I try to insert data this is giving me error.
INSERT INTO [dbo].Car
VALUES ('SGD1234F','Ferrari','F30','2014-09-24','500000.00','500.00','excellent');
The error is as follows
Msg 547, Level 16, State 0, Line 2 The INSERT statement conflicted
with the CHECK constraint "vehNo_ck_validity". The conflict occurred
in database "t322", table "dbo.Car", column 'VehNo'. The
statement has been terminated.
What is the change that I have to make?
Your regexp contains an unwanted comma. I have added spaces to highlight comma in your regexp.
[a-zA-Z][a-zA-Z] , [a-zA-Z][0-9][0-9][0-9][0-9][a-zA-Z]
For such regexp, VehNo should be SG,D1234F
Remove extra comma and your insert statement will work.
This is another version of your regexp: [a-zA-Z]{3}\d{4}[a-zA-Z]

How to parse colon within string in Oracle

I wish to insert a record to an Oracle database where one field contains the path to a file, i.e 'C:\\ProjectCodes\\ProjectZ\\ZCode.txt'. But when I attempt an INSERT statement I am asked about binding the value \\ProjectCodes\\ProjectZ\\ZCode.txt.
This is because of the colon I think. Is it possible to parse the colon to insert this value? The target field in the table is declared as a varchar2.
Here is the INSERT statement. I am using SQL Developer. Had to hide the IP address and password etc or I would be in trouble
INSERT INTO SFTP_Creds (SFTP_HOST, SFTP_PORT, SFTP_USERNAME, SFTP_PASSWORD,
SFTP_FINGERPRINT, SFTP_HOSTKEY, SFTP_FILE)
VALUES (‘xxxx.xxxx.xxxx.xxxx’, 22, ‘CORP\\username’, ‘password’, NULL,
NULL, 'C:\\ProjectCodes\\ProjectZ\\ZCode.txt');