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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT INTO problem

Status
Not open for further replies.

ceaton

MIS
Apr 26, 1999
27
US
I have an INSERT INTO query with several fields, one of which is a name field. The problem I'm having is if the person has a name like &quot;O'brien&quot;, the query is then rejected due to the single apostrophe. Here is the bit of my query that I'm using.<br>
<br>
String query = &quot;INSERT INTO caseInfo(caseName, evalDate, docketNum)&quot;<br>
+&quot;VALUES(&quot;+getAsSQLString(labelcaseName) <br>
+&quot;,&quot;+getAsSQLString(labelevalDate)<br>
+&quot;,&quot;+getAsSQLString(labelfileNum)<br>
+&quot;)&quot;;<br>
<br>
This the getAsSQLString, this checks my labels for null values.<br>
<br>
public String getAsSQLString(Label tf) {<br>
String s;<br>
<br>
s=tf.getText();<br>
if ((s==null) ¦¦ (s.length()==0)) { s=&quot;null&quot;; }<br>
else { s=&quot; ' &quot;+s+&quot; ' &quot;; }<br>
return s;<br>
}<br>
<br>
<br>
I tried changing the else stmt to else { s=&quot;\&quot; &quot;+s+&quot;\&quot; &quot;; }<br>
and else { s=&quot;\' &quot;+s+&quot;\' &quot;; } none of these seem to work. Here is the error I'm getting when I try to INSERT.<br>
<br>
SQLException: [Microsoft][ODBC Microsoft Access 97 Driver] Syntax error (missing operator) in query expression ' 'O'brien'.<br>
<br>
As you can see by the error I'm using Access97 as my dB.<br>
Any help would be greatly appreciated.<br>
<br>
Thanks, <p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Courtney,<br>
<br>
Did you try and change your application to put &quot;&quot; around the fieldname, such as:<br>
<br>
String query = &quot;INSERT INTO caseInfo(caseName, evalDate, docketNum)&quot;<br>
+&quot;VALUES(&quot; + <b>&quot;&quot;&quot;</b> +getAsSQLString(labelcaseName) <br>
+<b>&quot;&quot;&quot;</b> +&quot;,&quot;+<br>
<br>
I know you can do this in vb.<br>
<br>
C
 
I tried that and it doesn't work out right, thanks for trying!<br>
<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Courtney,<br>
<br>
Your problem is not an SQL one, SQL does not have an issue with strings with single quotes in them. Maybe you need to post a question in the C forum. If you can pass a string out to a database with the o'brien like &quot;o'brein&quot; then the database will have no trouble.<br>
<br>
Sorry I can't be of more help, but at least its a small help!<br>
<br>
C
 
Thanks anyway, I have my question posted in the Java(sun) forum also. I've had Otto help me, but I've run into this little problem and thought someone else may be able to help.<br>
<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
I'm not up to much with Access - but with Oracle you can work with embedded single quotes like this:<br>
<br>
'Mike''s'<br>
<br>
Put another single quote in front of the embedded quote.<br>
<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
This is the snippet of code that goes through each of my strings, maybe there is something missing?<br>
<br>
public String getAsSQLString(Label tf) {<br>
String s;<br>
<br>
s=tf.getText();<br>
if ((s==null) ¦¦ (s.length()==0)) { s=&quot;null&quot;; }<br>
else { s=&quot; ' &quot;+s+&quot; ' &quot;; }<br>
return s;<br>
}<br>
<br>
<br>
I tried adding another single quote to this line...<br>
<br>
else { s=&quot; ' ' &quot;+s+&quot; ' ' &quot;; } <br>
<br>
and that doesn't work either.<br>
<br>
Does anyone have any ideas how to insert the name O'Neil?<br>
<br>
Thanks,<br>
Courtney <p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Just a thought... If the single quotes are giving you a problem, why not write code in your application to replace them with another character (probably an extended character) before you insert, then change that character back to the single quote after you select the data for display?<br>
<br>
I know this may be a clunky solution, but it appears that Access will not accept a single quote as part of a string value ..... <p>Doug Trocino<br><a href=mailto:dtrocino@tecumsehgroup.com>dtrocino@tecumsehgroup.com</a><br><a href= Forums</a><br>Web Application Developer<br>
Tecumseh Group, Inc.<br>
Sponsors of Tek-Tips Forums<br>
 
I'm not trying to display the data, I'm trying to INSERT the data, Access accepts everything except for something like &quot;O'Brian&quot;. Anything with an apostrophe in the word.<br>
<br>
Is there some kind of other way to insert apostrophe words?<br>
<br>
Thanks,<br>
<br>
Courtney <p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Here is the code I'm trying to get to work properly.<br>
<br>
public String getAsSQLString(Label tf) {<br>
String s;<br>
<br>
s=tf.getText();<br>
if (s.charAt(i)=='\ ' ') { s.insert(i, '\ ' '); }<br>
else { s=&quot; ' &quot;+s+&quot; ' &quot;; } <br>
return s;<br>
}<br>
<br>
The error that I'm getting is for the i and it reads:<br>
Undefined variable: i<br>
<br>
I know I must be missing something, I'm just not sure what.<br>
<br>
Any ideas?<br>
<br>
Thanks again,<br>
Courtney<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
I don't program Access, so this may be a meaningless observation. However, since &quot;i&quot; is what's giving you the problem because it's not defined, then we need to define it. Looking at your code, I suspect you are trying to traverse a character string. If that is the case, shouldn't the traversal portion be in a loop?<br>
<br>
Something like <br>
<br>
FOR i IN 1..length(s) LOOP<br>
if (s.charAt(i)=='\ ' ') { s.insert(i, '\ ' '); }<br>
else { s=&quot; ' &quot;+s+&quot; ' &quot;; };<br>
END LOOP;<br>
<br>
I think this would define i and traverse the string on a character-by-character basis - but like I said, I'm just guessing here (as is probably obvious since I doubt if this is valid Access syntax - but you get the idea!).<br>
<br>
When all else fails, fall back on biblical documentation: &quot;If thine i offends thee, pluck it out!&quot;<br>

 
I got it to work, thanks for your help.<br>
<br>
Courtney <p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Courtney -<br>
<br>
I'm glad to hear you got it to work. Could you post the final solution so all of us could learn from your trials? It might save somebody else a lot of trouble in the future.
 
This is the entire SQL that I needed to perform an INSERT. I had 96 total fields to INSERT so they needed to be checked for words like &quot;O'Brian&quot; and if they where<br>
null, that it is what the function getAsSQLString does. <br>
I shortened this code for posting purposes. I gathered my information from label fields which was used to create a display for the end user, otherwise it would still work for textfields, just change the funtions first line from Label to TextField. Otto helped me out tremendously in getting this done so he deserves a lot of credit.<br>
<br>
Courtney<br>
<br>
<br>
<br>
String url = &quot;jdbc:eek:dbc:JVR&quot;;<br>
Connection con;<br>
Statement stmt;<br>
<br>
String query = &quot;INSERT INTO caseInfo(caseName, evalDate, docketNum, &quot;<br>
+&quot;state, county, geoType, caseSum, plainName, plainAge, plainSex, plainRace)&quot; <br>
+&quot;VALUES(&quot;+getAsSQLString(labelcaseName) <br>
+&quot;,&quot;+getAsSQLString(labelevalDate)<br>
+&quot;,&quot;+getAsSQLString(labelfileNum)<br>
+&quot;,&quot;+getAsSQLString(labelstate)<br>
+&quot;,&quot;+getAsSQLString(labelcounty)<br>
+&quot;,&quot;+getAsSQLString(labeltypeofCounty)<br>
+&quot;,&quot;+getAsSQLString(labelcaseS)<br>
+&quot;,&quot;+getAsSQLString(labelpname)<br>
+&quot;,&quot;+getAsSQLString(labelpage)<br>
+&quot;,&quot;+getAsSQLString(labelpsex)<br>
+&quot;,&quot;+getAsSQLString(labelprace) <br>
+&quot;)&quot;; <br>
<br>
try<br>
{<br>
Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);<br>
}<br>
catch ( ClassNotFoundException e)<br>
{<br>
System.err.println(&quot;ClassNotFoundException: &quot;+<br>
e.getMessage());<br>
}<br>
<br>
try<br>
{<br>
con = DriverManager.getConnection (url,&quot;&quot;,&quot;&quot;);<br>
stmt = con.createStatement();<br>
ResultSet rs = stmt.executeQuery(query);<br>
<br>
stmt.close();<br>
con.close();<br>
<br>
}catch (SQLException ex){<br>
System.err.println(&quot;SQLException: &quot;+ <br>
ex.getMessage());<br>
}<br>
<br>
<br>
public String getAsSQLString(Label tf)<br>
{<br>
String s;<br>
StringBuffer sb;<br>
int i, l;<br>
<br>
s=tf.getText();<br>
if ((s==null) ¦¦ (s.length()==0)) { s=&quot;null&quot;; }<br>
else {<br>
sb=new StringBuffer(s);<br>
l=s.length();<br>
for (i=l-1; i&gt;=0; i--) {<br>
if (sb.charAt(i)==' \ ' ') { sb.insert(i, ' \ ' '); }<br>
}<br>
s=&quot; ' &quot;+sb.toString()+&quot; ' &quot;;<br>
}<br>
return s;<br>
}<br>
<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top