带有单引号错误的VB.Net SQLite Select语句

时间:2018-10-15 15:42:29

标签: vb.net sqlite select escaping

我有一个带有列的SQLite表,该表包含的条目已经将任何单引号字符加倍以转义它们。一个例子是:

MD Prince George''s

我想查询该表以查看是否可以找到上面的条目。这段代码显示了一个尝试执行此操作的小型测试程序:

Private Sub TEST2_Click(sender As Object, e As EventArgs) Handles TEST2.Click
    Dim sql() As String = {"LIKE 'MD Prince George%';",
                           "= 'MD Prince George''s';",
                           "= 'MD Prince George's';"}
    Dim ThisTable As New List(Of DataRow)

    For a As Integer = 0 To sql.Count - 1
        Try
            Connect(2)  ' Getting my connection
            Using tr As SQLiteTransaction = conn.BeginTransaction
                Using cmd As New SQLiteCommand
                    cmd.Connection = conn
                    cmd.CommandType = CommandType.Text
                    cmd.CommandText = "SELECT * FROM `counties` WHERE `STATE_COUNTY` " & sql(a)
                    Using da As New SQLiteDataAdapter(cmd)
                        Dim dt As New DataTable
                        da.Fill(dt)

                        If dt.Rows.Count > 0 Then
                            ThisTable.Add(dt.Rows(0))
                        End If
                    End Using
                End Using
                tr.Commit()
            End Using
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        Finally
            conn.Close()
        End Try

        Debug.WriteLine("Result(" & a & ": " & sql(a) & ") gives ThisTable.Count=" & ThisTable.Count)
    Next
End Sub

结果是:

Result(0: LIKE 'MD Prince George%';) gives ThisTable.Count=1
Result(1: = 'MD Prince George''s';) gives ThisTable.Count=1
SQLite error (1): near "s": syntax error
Exception thrown: 'System.Data.SQLite.SQLiteException' in System.Data.SQLite.dll
SQL logic error
near "s": syntax error
Result(2: = 'MD Prince George's';) gives ThisTable.Count=1

这表明第一条语句是匹配的:我包括它来证明表中有这样的条目。第二条语句试图将2个单引号与2个单引号匹配,但不返回结果。第三个语句尝试将2个单引号与1个单引号匹配,将返回错误。

我的问题:我的select语句应该是什么才能查找该单个条目的整体,或者查找其中可能带有2个单引号的其他条目?不,我不想只找到%''%。

0 个答案:

没有答案