Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Text to Mysql

Status
Not open for further replies.

Khanjan

Programmer
Feb 24, 2004
41
NL
Hi,

When i insert an article into my database table it works perfectly, but when i have a quote in mine article it can not be inserted.

What should i do to prevent this?

Here is an example of it.

insert into `afghan`.`nieuws`
(NIEUWSID, TITEL, CONTENT, AUTEUR, DATUM, NIEUWSTYPE)
values
(NIEUWSID, TITEL, CONTENT, AUTEUR, DATUM, NIEUWSTYPE)


If Content exist from an article like this:


Although this is a sad poem, I liked it because of the beauty in yearning for one's home where you grow up. The part that talks about the "gard alood" city also hit home.

it wont work, because a quote exist in the article "gard alood" , How should i solve this probleem.
 
If the text is enclosed by double-quotes, then any double-quotes in the text must be changed to either two double-quotes, or a backslash and a double-quote. The same applies to single-quotes in a string enclosed by single-quotes.

For example:
"about the \"gard alood\" city"
and
"about the ""gard alood"" city"
and
'about the "gard alood" city'
are OK.
 
I am using Insert in JSP code.

statement.executeUpdate("INSERT INTO ADMIN VALUES ('"+lastParam+"','"+databaseinvoer+"','"+phoneParam+"')");

Where datbaseinvoer contains the article.

When i use "" or '' in mine article, it does not works. If i put \ before each ", it does works.

But i dont want to do that each time, How can i do this the easiest way?
 
I'm not familiar with JSP, but when I am assembling an SQL statement in C++, I always convert any string which _could_ contain special characters like quotes or backslashes before adding it to the SQL string. For example:
sql="INSERT mytable VALUES('"+string1+"','"+sqltext(string2)+"')";
where:
string1 definitely does not contain special characters,
string2 could contain special characters,
sqltext is a function which converts a string appropriately.
 
I use perl DBI - so it looks like -

$string = $dbh->quote("O'mally");

then do an INSERT of $string ....


Rab
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top