如何过滤包含列数据库中指定的所有单词的记录
How to filter records that contains all the words specified in a Column database
我知道如何使用 LIKE
运算符和 WHERE
子句在数据库的列中搜索指定的模式。例如"SELECT * From customer WHERE nam like '%" & txts & "%'"
。
如果 txts
包含“The boy”。显示“这孩子很好”、“这孩子怎么了”等记录。
但是,我想要的是,如果 txts
包含“男孩”,它还应该显示“女孩和两个男孩在那里”,“经理带着男孩来了”之类的记录。
我可以使用什么 syntax/code 来实现这一目标?
However, what I want is that if the txts contains "the boy", it should also display records like "the girl and two boys are there", "the manager came with a boy". What syntax/code can I used to achieve this?
类似于:
Dim search = "the boy"
search = "%" & search.Replace(" ", "%") & "%"
Dim sqlDa = New SQLiteDataAdapter("SELECT * From customer WHERE nam like :n", "your connection string here")
sqlDa.SelectCommand.Parameters.Add("n", SQLiteType.Text, search.Length, search)
Dim dt as New DataTable
sqlDa.Fill(dt)
关键部分是将搜索词从 "the boy"
修改为 "%the%boy%"
,但我还提供了一个如何使用参数化查询的示例。总是(总是总是)使用参数化查询。永远(永远)没有理由不使用它们。
既然我们已经澄清了问题,您可以继续修改此技术:
Dim search = "the boy"
Dim sqlDa = New SQLiteDataAdapter("SELECT * From customer WHERE 1=1 ", "your connection string here")
Dim terms = search.Split()
For i = 0 to UBound(terms)
Dim term = terms(i)
sqlDA.SelectCommand.CommandText &= $" AND name LIKE :n{i}"
sqlDa.SelectCommand.Parameters.Add("n" & i, SQLiteType.Text, term.Length + 2, "%" & term & "%")
Next i
Dim dt as New DataTable
sqlDa.Fill(dt)
它只是建立一个 SQL 像:
SELECT * From customer WHERE 1=1 AND nam LIKE :n0 AND nam LIKE :n1
'n0 = %the%
'n1 = %boy%
或者我们可以使用全文搜索;设置和保持最新有点麻烦(看看 https://hackernoon.com/sqlite-the-unknown-feature-edfa73a6f022 之类的东西)——你基本上创建了一个 FTS5 虚拟 table 来跟踪你的真实 table nam/id 并使用触发器使其保持最新,然后您可以像这样查询它:
Dim search = "the boy"
Dim sqlDa = New SQLiteDataAdapter("SELECT * From customer JOIN fts_customer ON customer.id = fts_customer.id WHERE fts_customer.nam MATCH :s"
sqlDa.SelectCommand.Parameters.Add("s", SQLiteType.Text, search.Length, search)
Dim dt as New DataTable
sqlDa.Fill(dt)
请注意 System.Data.Sqlite 默认情况下似乎没有启用 FTS(您可能会收到“没有这样的模块:fts5”错误)- Microsoft.Data.Sqlite 有,但它没有一个 DataAdapter(因此您可能需要切换到 DataTable.Load(sqliteCommand.ExecuteReader())
工作方式
我就是这样解决的
Dim search = "the boy"
Using sqlconn As New SQLiteConnection(connectionString)
Dim readN As String = "SELECT id From customer WHERE 1=1 "
Dim cmd As New SQLiteCommand(readN, sqlconn)
Dim terms = search.Split()
For i = 0 To UBound(terms)
Dim term = terms(i)
cmd.CommandText &= $" AND name LIKE :n{i}"
cmd.Parameters.AddWithValue(":n" & i, "%" & term & "%")
Next i
sqlconn.Open()
Dim reader As SQLiteDataReader = cmd.ExecuteReader
While reader.Read
For i = 0 To reader.FieldCount - 1
MsgBox(reader.GetValue(i))
Next
End While
sqlconn.Close()
End Using
我知道如何使用 LIKE
运算符和 WHERE
子句在数据库的列中搜索指定的模式。例如"SELECT * From customer WHERE nam like '%" & txts & "%'"
。
如果 txts
包含“The boy”。显示“这孩子很好”、“这孩子怎么了”等记录。
但是,我想要的是,如果 txts
包含“男孩”,它还应该显示“女孩和两个男孩在那里”,“经理带着男孩来了”之类的记录。
我可以使用什么 syntax/code 来实现这一目标?
However, what I want is that if the txts contains "the boy", it should also display records like "the girl and two boys are there", "the manager came with a boy". What syntax/code can I used to achieve this?
类似于:
Dim search = "the boy"
search = "%" & search.Replace(" ", "%") & "%"
Dim sqlDa = New SQLiteDataAdapter("SELECT * From customer WHERE nam like :n", "your connection string here")
sqlDa.SelectCommand.Parameters.Add("n", SQLiteType.Text, search.Length, search)
Dim dt as New DataTable
sqlDa.Fill(dt)
关键部分是将搜索词从 "the boy"
修改为 "%the%boy%"
,但我还提供了一个如何使用参数化查询的示例。总是(总是总是)使用参数化查询。永远(永远)没有理由不使用它们。
既然我们已经澄清了问题,您可以继续修改此技术:
Dim search = "the boy"
Dim sqlDa = New SQLiteDataAdapter("SELECT * From customer WHERE 1=1 ", "your connection string here")
Dim terms = search.Split()
For i = 0 to UBound(terms)
Dim term = terms(i)
sqlDA.SelectCommand.CommandText &= $" AND name LIKE :n{i}"
sqlDa.SelectCommand.Parameters.Add("n" & i, SQLiteType.Text, term.Length + 2, "%" & term & "%")
Next i
Dim dt as New DataTable
sqlDa.Fill(dt)
它只是建立一个 SQL 像:
SELECT * From customer WHERE 1=1 AND nam LIKE :n0 AND nam LIKE :n1
'n0 = %the%
'n1 = %boy%
或者我们可以使用全文搜索;设置和保持最新有点麻烦(看看 https://hackernoon.com/sqlite-the-unknown-feature-edfa73a6f022 之类的东西)——你基本上创建了一个 FTS5 虚拟 table 来跟踪你的真实 table nam/id 并使用触发器使其保持最新,然后您可以像这样查询它:
Dim search = "the boy"
Dim sqlDa = New SQLiteDataAdapter("SELECT * From customer JOIN fts_customer ON customer.id = fts_customer.id WHERE fts_customer.nam MATCH :s"
sqlDa.SelectCommand.Parameters.Add("s", SQLiteType.Text, search.Length, search)
Dim dt as New DataTable
sqlDa.Fill(dt)
请注意 System.Data.Sqlite 默认情况下似乎没有启用 FTS(您可能会收到“没有这样的模块:fts5”错误)- Microsoft.Data.Sqlite 有,但它没有一个 DataAdapter(因此您可能需要切换到 DataTable.Load(sqliteCommand.ExecuteReader())
工作方式
我就是这样解决的
Dim search = "the boy"
Using sqlconn As New SQLiteConnection(connectionString)
Dim readN As String = "SELECT id From customer WHERE 1=1 "
Dim cmd As New SQLiteCommand(readN, sqlconn)
Dim terms = search.Split()
For i = 0 To UBound(terms)
Dim term = terms(i)
cmd.CommandText &= $" AND name LIKE :n{i}"
cmd.Parameters.AddWithValue(":n" & i, "%" & term & "%")
Next i
sqlconn.Open()
Dim reader As SQLiteDataReader = cmd.ExecuteReader
While reader.Read
For i = 0 To reader.FieldCount - 1
MsgBox(reader.GetValue(i))
Next
End While
sqlconn.Close()
End Using