Getting closer, but I’m still just missing something. So I am working off of the example project PreparedStatements in the examples folder. I also modified the .txt file in the examples folder to include this in the FirstName column since I am trying to search with an apostrophe:
Paul’s house is actually smaller than Ryan’s summer house
Here is the code in the Search method in the example. I added an extra “%” before searchString so the search can be any part of the field:
// perform the search
Var rs As RowSet
Try
rs = db.SelectSQL("SELECT * FROM Customers WHERE FirstName LIKE ?;", "%" + searchString + "%")
Catch err As DatabaseException
MessageDialog.Show("Error: " + err.Message)
Return
End Try
If rs <> Nil Then
ResultsList.RemoveAllRows
ResultsList.ColumnCount = rs.ColumnCount
ResultsList.ColumnWidths = "100,100,100,100,100,100,100,100,100,100,100"
ResultsList.HasHeader = True
Var hasHeadings As Boolean
While rs.AfterLastRow <> True
ResultsList.AddRow("")
For i As Integer = 0 To rs.ColumnCount - 1
If Not hasheadings Then ResultsList.HeaderAt(i) = rs.ColumnAt(i).Name
ResultsList.CellValueAt(ResultsList.LastAddedRowIndex, i) = rs.ColumnAt(i).StringValue
Next
rs.MoveToNextRow
hasHeadings = True
Wend
rs.Close
End If
Search “paul”, it works. Search “paul’s”, it works. Search “ryan” and “ryan’s”, and these work too.
Ok, so I took this whole method and dropped it into my project but replaced with the below so it fits my project. The only pieces I omitted were the ColumnCount, ColumsWidths, and HasHeader pieces:
// perform the search
Var rs As RowSet
Try
rs = dbTests.SelectSQL("SELECT * FROM RDexam WHERE Question LIKE ?;", "%" + searchString + "%")
Catch err As DatabaseException
MessageDialog.Show("Error: " + err.Message)
Return
End Try
If rs <> Nil Then
lbxID.RemoveAllRows
While rs.AfterLastRow <> True
lbxID.AddRow(rs.Column("ID").StringValue)
lbxID.CellValueAt(lbxID.LastAddedRowIndex, 1) = rs.Column("DateRev").StringValue
rs.MoveToNextRow
Wend
rs.Close
End If
This is one of the example questions I am practicing on since I know it contains an apostrophe:
At 12:50pm, Matt’s blood sugar level was 55 mg/dL. He drank 4 oz apple juice. At 1:05pm, he checked his levels again, which is now 65 mg/dL. What should Matt do next?
Search “matt”, get the result. However, search “matt’s”, and the listbox is empty. I am using basically the exact same code as in the examples folder but cannot get this to get the search when an apostrophe is included. What am I missing?
Side question, is it possible to search multiple columns in the table with the LIKE statement? Ultimately, this is what I want my sql statement to look like, but this did not produce any results even with the search of “matt”:
"SELECT * FROM RDexam WHERE TestSet LIKE ? OR Question LIKE ? OR ID LIKE ? OR Solution LIKE ? ORDER BY ID"