替换 ' 时堆栈溢出 与'' 在VB 6.0中

发布于 2024-07-24 08:40:51 字数 1215 浏览 4 评论 0原文

我正在研究一些旧版 VB 6.0 代码(Access XP 应用程序)来解决 Access 应用程序的 SQL 语句问题。 对于客户名称中包含撇号的情况,我需要使用用 2 个单引号替换单引号(例如“医生的手术”:

Replace(customerName, "'", "''")

这将转义单引号,因此我得到有效的 SQL:

SELECT blah FROM blah WHERE customer = 'Doctor''s Surgery'

不幸的是,替换函数会导致无限循环和堆栈溢出,大概是因为它替换函数递归地将每个添加的引号转换为另外 2 个引号,例如,一个引号被替换为两个,然后第二个引号也被替换为两个,依此类推...

----- -----------编辑--------------

我注意到(感谢海报)该项目中使用的替换函数是自定义编写的:

Public Function replace(ByVal StringToSearch As String, ByVal ToLookFor As String,
ByVal ToReplaceWith As String) As String
Dim found As Boolean
Dim position As Integer
Dim result As String

position = 0
position = InStr(StringToSearch, ToLookFor)
If position = 0 Then
    found = False
    replace = StringToSearch
    Exit Function
Else
    result = Left(StringToSearch, position - 1)
    result = result & ToReplaceWith
    result = result & Right(StringToSearch, Len(StringToSearch) - position - Len(ToLookFor) + 1)
    result = replace(result, ToLookFor, ToReplaceWith)
End If
replace = result

End Function

显然, VB 并不总是有自己的替换功能。这个实现一定是有缺陷的。我将遵循人们的建议并删除它以支持 VB 6 的实现 - 如果这不起作用,我将编写自己的可行的功能。谢谢各位的意见!

I'm looking into some legacy VB 6.0 code (an Access XP application) to solve a problem with a SQL statement by the Access app. I need to use replace single quotes with 2 single quotes for cases where a customer name has an apostrophe in the name (e.g. "Doctor's Surgery":

Replace(customerName, "'", "''")

Which will escape the single quote, so I get the valid SQL:

SELECT blah FROM blah WHERE customer = 'Doctor''s Surgery'

Unfortunately the Replace function causes an infinite loop and stack overflow, presumably because it replace function recursively converts each added quote with another 2 quotes. E.g. one quote is replaced by two, then that second quote is also replaced by two, and so on...

----------------EDIT---------------

I have noticed (thanks to posters) that the replace function used in this project is custom-written:

Public Function replace(ByVal StringToSearch As String, ByVal ToLookFor As String,
ByVal ToReplaceWith As String) As String
Dim found As Boolean
Dim position As Integer
Dim result As String

position = 0
position = InStr(StringToSearch, ToLookFor)
If position = 0 Then
    found = False
    replace = StringToSearch
    Exit Function
Else
    result = Left(StringToSearch, position - 1)
    result = result & ToReplaceWith
    result = result & Right(StringToSearch, Len(StringToSearch) - position - Len(ToLookFor) + 1)
    result = replace(result, ToLookFor, ToReplaceWith)
End If
replace = result

End Function

Apparently, VB didn't always have a replace function of it's own. This implementation must be flawed. An going to follow folk's advice and remove it in favour of VB 6's implementation - if this doesn't work, I will write my own which works. Thanks everyone for your input!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花海 2024-07-31 08:40:51

您确定它不是替换功能的专有实现吗?

如果是的话用VB6的Replace替换即可。

我不记得它出现在哪个版本中(不是在 Vb3 中,而是在 VB6 中),因此如果原始代码库是 vb3/4,那么它可能是手动编码版本。

编辑

我刚刚看到你的编辑,我是对的!

是的,您应该能够删除该函数,然后它将使用内置 VB6 替换函数。

Are you sure that it's not a proprietary implementation of the Replace function?

If so it can just be replaced by VB6's Replace.

I can't remember which version it appeared in (it wasn't in Vb3, but was in VB6) so if the original code base was vb3/4 it could be a hand coded version.

EDIT

I just saw your edit, I was Right!

Yes, you should be able to just remove that function, it'll then use the in build VB6 replace function.

守不住的情 2024-07-31 08:40:51

我们使用的 VB6 应用程序可以选择将 ' 替换为 ` 或完全删除它们。

您还可以遍历这些字母,构建第二个字符串并将每个“作为”插入。

We use an VB6 application that has the option of replacing ' with ` or removing them completely.

You could also walk through the letters, building a second string and inserting each ' as ''.

安稳善良 2024-07-31 08:40:51

我刚刚在 Access 中尝试过,它工作正常(没有 stackoverflow):

 Public Function ReplaceSingleQuote(tst As String) As String
        ReplaceSingleQuote = Replace(tst, "'", "''")
 End Function


 Public Sub TestReplaceSingleQuote()
        Debug.Print ReplaceSingleQuote("Doctor's Surgery")
 End Sub

I just tried this in Access and it works fine (no stackoverflow):

 Public Function ReplaceSingleQuote(tst As String) As String
        ReplaceSingleQuote = Replace(tst, "'", "''")
 End Function


 Public Sub TestReplaceSingleQuote()
        Debug.Print ReplaceSingleQuote("Doctor's Surgery")
 End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文