Discussion Locked

problem in sql insert statement

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

friends,
i'm facing a problem in inserting data in a database where the data contains an apostrophe(').
first i'm extracting some data from a Table(say A)and then after some modification I'm inserting it to another table say(site_table).
the problem arises when i'm trying to insert into site-table a variable whose contents include an apostrophe
after the resultset(rs) of query to extract data from table A i'm giving the following
to insert into site-table
String sname=rs.getString("Name");
String id=rs.getString("School_shortname");
String path="www.abc.com/SCH/"+id;
Statement st1=conn.createStatement();
st1.executeUpdate("Insert into site_table(keyword,web_location)values ('"+sname+"','"+path +"')");
st1.close()

I'm getting the following error [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression ''Children's Valley School'.
it is not inserting the above data as it contains an apostrophe.
how do i handle it.Can anybody help me
thanx

5 replies

by peyison

Since you're using a single quote (apostrophe) around your text values, the text can't contain an apostrophe.

"..values ('"+sname+"','"+path +"')"

This resolved to:

"..values ('Children's Valley School','www.abc.com/SCH/ID')

There are two options - remove the apostrophe from the text, or wrap the text with double quotes.

To wrap with double quotes, it would look something like:

"...values (""" + sName + """,""" + path + """)"

This will resove into

"...values ("Children's Valley School", "www.abc.com/SCH/ID")

Hope this helps.

This discussion is closed to new replies.