Re: Maybe more of a VB question, but here goes

From: Cedar Cox <cedarc(at)visionforisrael(dot)com>
To: "Henshall, Stuart - WCP" <SHenshall(at)westcountrypublications(dot)co(dot)uk>
Cc: "'cgibbs(at)westmarkproducts(dot)com'" <cgibbs(at)westmarkproducts(dot)com>, pgsql-odbc(at)postgresql(dot)org
Subject: Re: Maybe more of a VB question, but here goes
Date: 2002-03-14 21:09:45
Message-ID: Pine.LNX.4.21.0203142250480.20817-100000@nanu.visionforisrael.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-odbc


For those that like ready made function type of things, here's something
for you. This functions converts a string to be suitable for handing over
to the back end, with appropriate characters escaped and single quotes
surrounding if necessary. It also handles NULL. I escape using \ so:

input output
----------------
hello 'hello'
bla'h 'bla\'h'
c:\linux 'c:\\linux'
<null> NULL

Here's the listing:

Function escapeString(text As Variant) As String
'Converts a string to be suitable for handing over to the back end, with all appropriate characters escaped
' and single quotes surrounding if necessary
Dim pos As Integer, ret As String
If IsNull(text) Then
escapeString = "NULL"
Else
ret = text
pos = 0
Do
pos = InStr(pos + 1, ret, "\") 'we must do \ first
If pos > 0 Then
ret = Left(ret, pos - 1) & "\" & Mid(ret, pos)
pos = pos + 1
End If
Loop Until pos = 0
pos = 0
Do
pos = InStr(pos + 1, ret, "'")
If pos > 0 Then
ret = Left(ret, pos - 1) & "\" & Mid(ret, pos)
pos = pos + 1
End If
Loop Until pos = 0
escapeString = "'" & ret & "'"
End If
End Function

For Corey's case, it would probably be useful to make a wrapper function to
add a comma on the end, eg..

Function wrapper(text As Variant) As String
escapeString = escapeString(text) & ", "
End Function

And then just do this:

> Here's the insert statement:
pg_cn.Execute "INSERT into itemlist(jobno,area,floor,building,room,elevation,phase,cycle,model,w,h,d,itemno," & _
"finish,mtostatus) values(" & _
wrapper(rs!jobno) & wrapper(rs!area) & _
wrapper(rs!floor) & wrapper(rs!building) & _
wrapper(rs!room) & wrapper(rs!elevation) & _
wrapper(rs!phase) & wrapper(rs!cycle) & _
wrapper(rs!model) & wrapper(rs!w) & _
wrapper(rs!h) & wrapper(rs!d) & _
wrapper(rs!itemno) & wrapper(rs!finish) & _
escapeString(rs!mtostatus) & ")"

Remember to call escapeString instead of wrapper for the last one..
Someone may want to add this to a howto or something (hint, hint)..
-Cedar

On Thu, 14 Mar 2002, Henshall, Stuart - WCP wrote:

> Try double quoteing (ie '' for ').
> Also you can escape it using \'.
> Of course bveign able to use \ as an escape character means that when they
> really have one you have to give \\ :)
> Cheers,
> - Stuart

In response to

Browse pgsql-odbc by date

  From Date Subject
Next Message Jean-Christophe FABRE 2002-03-15 10:00:47 backup database under cygwin
Previous Message Dave Page 2002-03-14 20:58:45 Re: Maybe more of a VB question, but here goes