RSS link icon

.

The simple but so annoying apostrophe sql problem


This is a common database problem, I have been having the same problem for a while, but didn't think it was that simple to fix.

approstof image

I will describe the problem as simple as possible. Thing with mysql database and any other databases is that they extract data using simple command words, therefore it needs some reserved symbols, and one of them is a very important one in the english language. The apostrophe and what I did for long time was just to avoid contracting words to avoid using the apostrophe. quite ridiculously when the problem is so simple solved.

 

As I use asp.net as my coding language you will see the code for it. But as much as I know of coding, it wont be that hard to make the same functionality for both asp, php and any other web programming language.

 

Here you will see how to get around the sql apostrophe problem with asp.net

Lets say we have a string we want to add to our database.

dim thetextstring as string = "this is the text we want to add to the database, with an apostrophe ' there it was"

Now the only thing we need to do is replace the apostrophe with two, yes thats right, take a look.

thetextstring = thetextstring..Replace("'", "''")

 

Yes it is that simple, just want to share it with you, I just thought it was a bit more difficult, but now that I know, I will never try to take the easy way before I have made a bit of research as now.

 



C?sar says: 2008-05-07

you could also try to replace that char with their ascii value example Chr(34)=char(39) you have execute the replace function example Replace(string,Chr(34)Chr(34)Chr(34),Chr(34)+char(39)+Chr(34)) or something like that

German Bobadilla says: 2008-02-01

If you want you can either replace the apostrophe with another character which users donChr(34)t usually use like "~", ":" , or any special character that you could only know. Then if you display it to a grid or retrieve it back use an SQL to replace that character again to apostrophe.

noypi says: 2007-11-22

if two consecutive (Chr(34)) where entered, I still experience the same problem