How to escape semicolon in Sql ? I used playframework i tried to insert html code inside "values" but when i try using semicolon it's not working ?
CREATE TABLE "R_EMAIL_TEMPLATE"
(
"ID" uuid NOT NULL,
"WP_ID" uuid NOT NULL,
"CODE" text NOT NULL,
"SUBJECT" text NOT NULL,
"CONTENT" text NOT NULL,
"CREATED_AT" timestamp without time zone,
"CREATED_BY" text,
"UPDATED_AT" timestamp without time zone,
"UPDATED_BY" text,
CONSTRAINT "R_EMAIL_TEMPLATE_pkey" PRIMARY KEY ("ID"),
CONSTRAINT "R_EMAIL_TEMPLATE_WP_pkey" FOREIGN KEY ("WP_ID") REFERENCES "C_WP" ("ID")
);
INSERT INTO "R_EMAIL_TEMPLATE"
VALUES (
'30abd6ec-3496-45ff-be54-7f6f9290ebc4',
'30abd6ec-3496-45ff-be54-7f6f9290ebcf',
'user-activation',
'User Registration',
';',
'2018-05-17 19:02:39.643',
'LOGIN',
null,
null
);
Semicolon does not need to be escaped.
It looks like problem is not about semicolon, but about double quotes you used in second INSERT
String values in SQL must be in single quotes. Double quote is reserved for object names such as schema/tables/columns names etc.
so try
INSERT INTO "R_EMAIL_TEMPLATE"
VALUES (
'30abd6ec-3496-45ff-be54-7f6f9290ebc4',
'30abd6ec-3496-45ff-be54-7f6f9290ebcf',
'user-activation',
'User Registration',
';',
'2018-05-17 19:02:39.643',
'LOGIN',
null,
null
);
instead...
Use \ to set escape character and try following:
INSERT INTO "R_EMAIL_TEMPLATE"
VALUES (
'30abd6ec-3496-45ff-be54-7f6f9290ebc4',
'30abd6ec-3496-45ff-be54-7f6f9290ebcf',
'user-activation',
'User Registration',
'\;',
'2018-05-17 19:02:39.643',
'LOGIN',
null,
null
);
Related
Table Structure:
CREATE TABLE `cs_menuitem` (
`menuitemid` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) DEFAULT NULL,
`itemname` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`menuitemid`),
KEY `fi0` (`catid`)
) ENGINE=InnoDB AUTO_INCREMENT=651879 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='InnoDB free: 9216 kB; (catid) REFER cruzstar_v01/cs_menucate';
Inserted UTF-8 chars using PHP
INSERT INTO `cs_menuitem` (`catid`, `itemname`)
VALUES
(97260, 'as “Sautéed Pastrami” , “Piña Colada Virgin” or “Piña Colada”');
Select query:
SELECT itemname FROM cs_menuitem where menuitemid = 651841;
with the command line, and PHP : (correct result)
as “Sautéed Pastrami” , “Piña Colada Virgin” or “Piña Colada”
But with others (MysqlWorkbench, SequelPro, PhpMyAdmin, PhpStorm, Java), getting: (incorrect result)
as “Sautéed Pastrami†, “Piña Colada Virgin†or “Piña Coladaâ€
I am trying to implement post request using spring data jpa. I am trying to add new room in room table with some values. Some of the column from room table are not null I set that with default. But when I am inserting I am getting error as violates not-null constraint.
RoomService class
// Add new Room Details
public String addNewRoom(#RequestBody RoomInformation roomInfo) {
Room roomRecord = new Room();
if(roomInfo.nCampusId != 0)
roomRecord.nCampusId = roomInfo.nCampusId;
if(roomInfo.nBuildId != 0)
roomRecord.nBuildId=roomInfo.nBuildId;
if(roomInfo.nCRTCodeId !=0)
roomRecord.nCRTCodeId=roomInfo.nCRTCodeId;
roomRecord.nInstId=roomInfo.nInstId;
roomRecord.sRoomNumber=roomInfo.sRoomNumber;
roomRecord.sRoomDesc=roomInfo.sRoomDesc;
roomRecord.nArea=roomInfo.nArea;
roomRecord.sFloor=roomInfo.sFloor;
roomRecord.bIsActive= true;
roomRepository.save(roomRecord);
return "New room added sucessfully";
}
Room Table
CREATE TABLE public.room
(
nroom_id numeric(18,0) NOT NULL DEFAULT nextval('room_seq'::regclass),
ncampus_id numeric(18,0),
nbuild_id numeric(18,0),
ninst_id numeric(18,0) NOT NULL DEFAULT 0,
sfloor character varying(10) COLLATE pg_catalog."default",
sroom_number character varying(10) COLLATE pg_catalog."default",
sroom_desc character varying(255) COLLATE pg_catalog."default",
scomments text COLLATE pg_catalog."default",
daccepted_date timestamp(3) without time zone,
ssurveyor character varying(255) COLLATE pg_catalog."default",
narea integer,
ncrt_code_id numeric(18,0),
ncmn_room_bln smallint,
nunvalidated_bln smallint,
sbfr_field character varying(50) COLLATE pg_catalog."default",
ntemp_room_id numeric(18,0),
bis_active boolean NOT NULL DEFAULT false,
bis_jointuse boolean NOT NULL DEFAULT false,
sstations_desc character varying(25) COLLATE pg_catalog."default",
ddeleted_on timestamp(3) without time zone,
ndeleted_by numeric(18,0),
bis_incluster boolean NOT NULL DEFAULT false,
bis_service_center_activity boolean NOT NULL DEFAULT false,
service_center_comments text COLLATE pg_catalog."default",
CONSTRAINT pk_roomdeails PRIMARY KEY (nroom_id),
)
Error at console
Null value in column "bis_jointuse" violates not-null constraint
Detail: Failing row contains (1203521, 270, 11135, 106, 0, 10, abc, null, null, null, 22, 2122, null, null, null, null, t, null, null, null, null, null, null, null).
You need to skip column or provide DEFAULT keyword instead of explicit NULL:
INSERT INTO tab(col1,...,bis_jointuse)
VALUES (22, ..., DEFAULT);
-- skipping column
INSERT INTO tab(col1,...)
VALUES (22,...);
Those values are not null in table that I set with default value. But default value is not taking when I am trying to insert.
As far I as know only Oracle supports such construct: DEFAULT Values On Explicit NULLs
In the previous section we saw default values are only used when a column is not referenced in an insert statement. If the column is referenced, even when supplying the value NULL, the default value is not used. Oracle 12c allows you to modify this behaviour using the ON NULL clause in the default definition.
CREATE TABLE t2 (
col1 NUMBER DEFAULT 1,
col2 NUMBER DEFAULT ON NULL 2,
description VARCHAR2(30)
);
This is how I tried to create table:
CREATE TABLE namevalues
(
seqid integer NOT NULL,
name text(50) NOT NULL,
value text(50),
CONSTRAINT namevalues_pkey PRIMARY KEY (seqid, name)
);
I tried doing this but this doesn't work. could anyone please tell how should I specify the length?
The TEXT type in PostgreSQL does not have a size. You can fix it like this:
CREATE TABLE namevalues
(
seqid integer NOT NULL,
name text NOT NULL,
value text,
CONSTRAINT namevalues_pkey PRIMARY KEY (seqid, name)
);
or use VARCHAR(50) instead of TEXT.
Don't use text, you want varchar...
CREATE TABLE namevalues
(
seqid integer NOT NULL,
name varchar(50) NOT NULL,
value varchar(50),
CONSTRAINT namevalues_pkey PRIMARY KEY (seqid, name)
);
From the PostgreSQL docs...
text: variable unlimited length
Use varchar(50) instead of text(50).
Because,
PostgreSQL API says text data type does't have size, it allow variable with unlimited length
CREATE TABLE namevalues
(
seqid integer NOT NULL,
name varchar(50) NOT NULL,
value varchar(50),
CONSTRAINT namevalues_pkey PRIMARY KEY (seqid, name)
);
Reference
I'm trying to import a CSV table via Netbeans for my Java application.
However, when I try to import values from a CSV file, I get a file type error. It works if I make all the values VARCHARs, but this is inefficient, especially for the short strings of characters that I use.
Here's the CSV file:
GameID,GameName,Developer,Engine,PublishYear,Rating
1,Bioshock Infinite,Irrational Games,Unreal Engine 3,2013,10
2,Hitman: Absolution,IO Interactive,Glacier 2,2012,9
3,Killing Floor,Tripwire Interactive,Unreal Engine 2,2009,7
4,Half-Life 2,Valve,Source,2004,7
5,ESV Skyrim,Bethesda Softworks,Creation Engine,2011,8
6,Batman: Arkham City,Rocksteady Studios,Unreal Engine 3,2011,8
7,Portal 2,Valve,Source,2011,8
8,The Witcher 2,CD Projekt RED,REDengine,2011,9
9,Assassin's Creed 2,Ubisoft,Anvil,2010,8
10,Garry's Mod,Facepunch Studios,Source,2004,6
11,GRID 2,Codemasters,EGO 3.0,2013,7
12,Hotline Miami,Dennaton Games,Game Maker,2012,8
13,Just Cause 2,Avalanche Studios,Avalanche Engine 2.0,2010,7
14,Watch Dogs,Ubisoft,Disrupt,2014,8
15,Deus Ex,Ion Storm,Unreal Engine 1,2000,7
16,Trine 2,Frozenbyte,Proprietary,2011,6
And here's the table creation statement:
CREATE TABLE tblGames (
GameID INTEGER NOT NULL,
GameName VARCHAR(30) NOT NULL,
Developer VARCHAR(30) NOT NULL,
Engine VARCHAR(30) NOT NULL,
PublishYear VARCHAR(20) NOT NULL,
Rating SMALLINT NOT NULL,
PRIMARY KEY (GameID)
);
And here's the import statement:
CALL SYSCS_UTIL.SYSCS_IMPORT_TABLE (
null,
'TBLGAMES',
'D:\OneDrive\Documents\Topic 9\CSV\tblGames.csv',
',', null, null, 0
)
And this is a copy of the error I get:
Error code 30000, SQL state XIE0R: Import error on line 1 of file D:\OneDrive\Documents\Topic 9\CSV\tblGames.csv: Invalid character string format for type INTEGER.
Error code 99999, SQL state 22018: Invalid character string format for type INTEGER.
Now, as I see it, all the GameID values are integers and the ratings are all SMALLINT, so how could there be an error like this?
I don't know why the update() method doesn't work with ORACLE database
deleteQuery = "delete from USBRPF where upper(userid) = upper(?)" ;
String s= "ABC " ;
getJdbcTemplate().update(deleteQuery, s.trim());
There's a row with column USERID having data 'ABC ' (there's some spaces character after 'C' character)
It seems to not find out that row.
However, if I change code to below, it works
deleteQuery = "delete from USBRPF where upper(userid) like upper(?)" ;
String s= "ABC " ;
getJdbcTemplate().update(deleteQuery, s.trim() + "%");
or
deleteQuery = "delete from USBRPF where upper(trim(userid)) = upper(?)" ;
String s= "ABC " ;
getJdbcTemplate().update(deleteQuery, s.trim());
Note: all works with MSSQL database, with data migrated from ORACLE.
I guess there's problem with database setting. Could have someone figure it out? Thanks
MODIFIED:
Column information:
ORACLE
BRANCH CHAR(2 CHAR)
COMPANY CHAR(1 CHAR)
DATIME TIMESTAMP(6)
JOBNM CHAR(10 CHAR)
UNIQUE_NUMBER NUMBER(18,0)
USERID CHAR(10 CHAR)
USRPRF CHAR(10 CHAR)
MSSQL
[UNIQUE_NUMBER] [bigint] IDENTITY(1,1) NOT NULL,
[USERID] [nchar](10) NULL,
[COMPANY] [nchar](1) NULL,
[BRANCH] [nchar](2) NULL,
[USRPRF] [nchar](10) NULL,
[JOBNM] [nchar](10) NULL,
[DATIME] [datetime2](6)> NULL,
CHAR is a fixed length type. So even if your data looks like "ABC" in the database, it's stored as "ABC ". CHAR columns will be padded with spaces up their size.
Therefore on the first example you're comparing "ABC " (as stored in the DB) to "ABC" (as passed from Java after the trim() call). On your second and third example you're working around this.
I would recommend that you use VARCHAR2 since it's more natural and more commonly used. If not possible, you could try padding the value that you pass from Java up to the CHAR size as defined in Oracle.