How to solve the problem of single quotes in SQL statements?

classic Classic list List threaded Threaded
2 messages Options
38797715 38797715
Reply | Threaded
Open this post in threaded view
|

How to solve the problem of single quotes in SQL statements?

Hi,

for example:

CREATE TABLE City (   ID INT(11),   Name CHAR(35),   CountryCode
CHAR(3),   District CHAR(20),   Population INT(11),   PRIMARY KEY (ID,
CountryCode) ) WITH "template=partitioned, backups=1,
affinityKey=CountryCode, CACHE_NAME=City, KEY_TYPE=demo.model.CityKey,
VALUE_TYPE=demo.model.City";

INSERT INTO City(ID, Name, CountryCode, District, Population) VALUES
(1,'Ka'bul','AFG','Kabol',1780000);

name field's value contains single quotes.

The following writing will throw exceptions:

INSERT INTO City(ID, Name, CountryCode, District, Population) VALUES
(1,'Ka\'bul','AFG','Kabol',1780000);

INSERT INTO City(ID, Name, CountryCode, District, Population) VALUES
(1,'Ka\\'bul','AFG','Kabol',1780000);

INSERT INTO City(ID, Name, CountryCode, District, Population) VALUES
(1,"Ka'bul",'AFG','Kabol',1780000);

I wonder if there are any other solutions besides the PreparedStatement?


38797715 38797715
Reply | Threaded
Open this post in threaded view
|

Re: How to solve the problem of single quotes in SQL statements?

ok,solved.
INSERT INTO City(ID, Name, CountryCode, District, Population) VALUES
(1,'Ka''bul','AFG','Kabol',1780000);

在 2020/8/26 下午7:10, 38797715 写道:

> Hi,
>
> for example:
>
> CREATE TABLE City (   ID INT(11),   Name CHAR(35),   CountryCode
> CHAR(3),   District CHAR(20),   Population INT(11),   PRIMARY KEY (ID,
> CountryCode) ) WITH "template=partitioned, backups=1,
> affinityKey=CountryCode, CACHE_NAME=City, KEY_TYPE=demo.model.CityKey,
> VALUE_TYPE=demo.model.City";
>
> INSERT INTO City(ID, Name, CountryCode, District, Population) VALUES
> (1,'Ka'bul','AFG','Kabol',1780000);
>
> name field's value contains single quotes.
>
> The following writing will throw exceptions:
>
> INSERT INTO City(ID, Name, CountryCode, District, Population) VALUES
> (1,'Ka\'bul','AFG','Kabol',1780000);
>
> INSERT INTO City(ID, Name, CountryCode, District, Population) VALUES
> (1,'Ka\\'bul','AFG','Kabol',1780000);
>
> INSERT INTO City(ID, Name, CountryCode, District, Population) VALUES
> (1,"Ka'bul",'AFG','Kabol',1780000);
>
> I wonder if there are any other solutions besides the PreparedStatement?
>