 |
| Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 1st, 2003, 11:47 AM
|
|
Authorized User
|
|
Join Date: Jul 2003
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Getting single and double quotes into DB
I am trying to create a textarea that can be inserted into an Access 2000 DB memo field. I am hoping to set the textarea up with no restrictions on the entries, but am having trouble dealing with single and double quotes ( ' " ).
The following is the information typed into the text area between the []'s:
[
"Double"
'Single'
]
I need to pick up the quotes and the CrLf characters...haven't gotten to the crlf yet, but am having trouble formatting the INSERT command.
I can get the double quotes into the DB with the following format(JavaScript):
DevotionalTextNew = "\"Double\" "
SQL = "INSERT INTO DevotionalData "
+ "(EffDate, Devotion) "
+ "VALUES ('" + EffDate + "', '" + DevotionalTextNew + "')";
but when I try to add the single quotes, I'm not able to get the syntax setup correctly.
DevotionalTextNew = "\"Double\" and \'Single\' "
SQL = "INSERT INTO DevotionalData "
+ "(EffDate, Devotion) "
+ "VALUES ('" + EffDate + "', '" + DevotionalTextNew + "')";
gives error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression ''"Double" and 'Single' ''.
/Devotional/PostDevotionalToDBSQL.asp, line 93
How do I format a text string with single quotes for insertion into the DB?
Thanks for the help.
__________________
Ego is a faithful friend; He stays with us all the way to the crater.
|
|

December 1st, 2003, 12:00 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
The usual way is to replace one single quote with two (that's '' not ")
|
|

December 1st, 2003, 02:54 PM
|
|
Authorized User
|
|
Join Date: Jul 2003
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The SQL statement requires the single quote ' to denote text. When it encounters the ' that is part of the string I want to store, it interprets it as the end of the string to include. What is the operator for SQL / Access that takes the place of the \ operator in Javascript that causes whatever follows to be treated as part of the string rather than the end of the string?
|
|

December 1st, 2003, 10:38 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
To repesent a literal single quote mark, you escape it by prepending another single quote mark:
INSERT INTO myTable (myField) VALUES ('This is text with a quote '' in it')
and the text:
This is test with a quote ' in it
will be inserted into your field.
Cheers
Ken
Microsoft MVP - Windows Server (IIS)
www.adOpenStatic.com
|
|

December 1st, 2003, 11:28 PM
|
|
Authorized User
|
|
Join Date: Jul 2003
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Finally understand it.
Thanks for the help
|
|

December 2nd, 2003, 01:36 AM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
you can use the method 'replace' to replace all instances of ' in the text area eg
replace (stringtoclean, "character(s) to replace", "replacement character(s)")
get the data from the textare assign it to a variable(dirtytxt) and then
cleantxt = replace(dirtytxt, "'", "''")
this will replace all instances of a single ' with a '' so they can be entered in the database
if you need to clean multiple text strings you could write this into a function so you dont need to keep rewriting it
Hope this helps
|
|

December 2nd, 2003, 08:40 AM
|
|
Authorized User
|
|
Join Date: Jul 2003
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I tried that first, [ MyString.Replace("'", "''") ] but was only replacing the first incidence of the matching text. I know it SHOULD get all of them, but that was not the experience I was having.
Rich
|
|

December 2nd, 2003, 10:14 PM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Try this
<%
txtval = "a'b'c'd"
txtval = replace(txtval, "'", "''")
response.write txtval
%>
the output should be
a''b''c''d
this should work, not too sure about MyString.Replace("'", "''") i seem to get an error about using parentheses when calling a sub
Hope this helps
|
|

December 3rd, 2003, 09:53 PM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Try this
if you want store (') to database: replace(str,"'","''")
if you want store (") to database: replace(str,""","""")
Hope this helps
|
|

December 4th, 2003, 12:24 AM
|
|
Authorized User
|
|
Join Date: Jul 2003
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I've gotten it.
Thanks,
Rich
|
|
 |