asp.net - 解码/解析 UPDATE sql 语句

标签 asp.net mysql sql

我想在 ASP 中创建一个日志功能,为此我需要能够解析 UPDATE sql 语句。 如果我有这样的 SQL 语句:

 UPDATE mytable SET aaa='test',bbb=123 WHERE id=508

我想得到的输出是像这样表示字段和值的数组:

FieldArray = aaa,bbb
ValueArray = test,123

我如何在 ASP 中或直接在 SQL 中执行此操作?

最佳答案

忘记使用 SQL。它的作用是使用、访问和修改数据库服务器上的数据。

在 VB.NET 中,您可以使用 LeftMidRightInStr 函数来解析输出你的 SQL 字符串。

' Using your string : UPDATE mytable SET aaa='test',bbb=123 WHERE id=508
strWithoutFirstPart = Mid(strSQL, InStr(strSQL, "SET")) ' Output : SET aaa='test',bbb=123 WHERE id=508
strWithoutLastPart = Left(strWithoutFirstPart, InStr(strWithoutFirstPart, "WHERE")-1) ' OutPut : SET aaa='test',bbb=123

This has more information on the functions .

这是我为您的问题制定的程序:

'Global variables
Dim parameters, values
parameters = Array()
values = Array()

' Procedure for parsing an Update SQL Statement to get parameters and values.
Sub parseSQLUpdate(ByVal strSQL)
    ' Variables
    Dim strParsed, intEqualPos, intCommaPos, intLastPos, regExStrCommaTest

    ' Clearing the global variables.
    ReDim parameters(0), values(0)

    ' Parsing the string for the right section.
    strParsed = Trim(Mid(strSQL, InStr(UCase(strSQL), "SET") + 4))
    strParsed = Trim(Left(strParsed, InStr(UCase(strParsed), "WHERE") - 2))

    ' Getting positions.
    intEqualPos = InStr(strParsed, "=")
    intCommaPos = InStr(strParsed, ",")
    intLastPos = 0

    ' Preparing the Reg. Ex. variable
    Set regExStrCommaTest = new RegExp
    regExStrCommaTest.Pattern = "('[^']*'$)|(^[^']+$)"

    ' Looping for every parameter/value.
    Do While (intEqualPos > 0)
        ' Check if first loop.
        If (intLastPos = 0) Then
            parameters(UBound(parameters)) = Trim(Left(strParsed, intEqualPos - 1))

            ' Check if at last parameter/value.
            If (intCommaPos > 0) Then
                ' Check for commas in text.
                Do While (regExStrCommaTest.Test(Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))) = False)
                    ' Go to next comma.
                    intCommaPos = InStr(intCommaPos + 1, strParsed, ",")

                    ' Already at end, so take remaining characters.
                    If (intCommaPos <= 0) Then
                        values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
                        Exit Sub
                    End If
                Loop

                values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))
            Else
                values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
            End If
        Else
            ' Change arrays' size.
            ReDim Preserve parameters(UBound(parameters) + 1), values(UBound(values) + 1)

            parameters(UBound(parameters)) = Trim(Mid(strParsed, intLastPos + 1, intEqualPos - intLastPos - 1))

            ' Check if at last parameter/value.
            If (intCommaPos > 0) Then
                ' Check for commas in text.
                Do While (regExStrCommaTest.Test(Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))) = False)
                    ' Go to next comma.
                    intCommaPos = InStr(intCommaPos + 1, strParsed, ",")

                    ' Already at end, so take remaining characters.
                    If (intCommaPos <= 0) Then
                        values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
                        Exit Sub
                    End If
                Loop

                values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))
            Else
                values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
            End If
        End If

        ' Remembering last position
        intLastPos = intCommaPos

        ' Ending Loop if last position is 0
        If (intLastPos = 0) Then
            Exit Sub
        End If

        ' Getting new positions.
        intEqualPos = InStr(intLastPos, strParsed, "=")
        intCommaPos = InStr(intEqualPos, strParsed, ",")
    Loop
End Sub

关于asp.net - 解码/解析 UPDATE sql 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6983656/

相关文章:

php - 具有多个命名参数的 PDO bindParam()

sql - 如何在 Kohana 3 中配置 SQLite?

asp.net - 如何从 URL 中获取 .aspx 页面名称?

asp.net - Ajax 数据更新。扩展程序

mysql - 该数据库表的最佳索引是什么?

sql - 将varchar转换为具有毫秒的sql中的日期时间

mysql - 有条件 mysqli ON DUPLICATE KEY UPDATE 问题

asp.net - ListView 内带边框的表格

c# - 回发后页面客户端验证问题

php - 插入到数据库