Android Question The MySQL statement cannot find a result when searching for c based on the specified conditions a and b...

bskotu555

Member
I have a code that connects to MYSQL database for querying, but it doesn't work. I don't know where I wrote it wrong. I'm seeking advice!

Private Sub EditText2_EnterPressed'比对结果
Dim zw As String
Dim bt As String
Dim cx As String
Dim nm As String
MysqlJdbc.InitializeAsync("EventMysql", driver, jdbcUrl, Username, Password)'重新连接
Wait For EventMysql_Ready (Success As Boolean)
If Success = False Then
ToastMessageShow("数据库连接失败,请检查",True)
Else'开始进行查找比对
cx=Button3.Text'程序名
zw=EditText1.text'站位
bt=EditText2.text'物料条码
Dim Cursor As JdbcResultSet
Cursor =MysqlJdbc.ExecQuery($"SELECT sys FROM smt WHERE 程序名='${cx}' and 站位='${zw}'"$) '查询结果
Do While Cursor.NextRow
nm=Cursor.GetString("物料编码")
If nm<>"" Then
If nm=bt Then
Label3.text="OK"
Else
Label3.text="NG"
End If
End If
Loop
' ToastMessageShow(zw,True)
MysqlJdbc.Close
End If
End Sub
 

walt61

Well-Known Member
Licensed User
Longtime User
Please use 'code' tags when posting code. Assuming that you mean the query doesn't return results, try this instead:
B4X:
 Cursor = MysqlJdbc.ExecQuery2("SELECT sys FROM smt WHERE `程序名`=? and `站位`=?", Array As String(cx, zw)) '查询结果

That is a 'parametrised' query, which also protects against SQL injections.

EDIT: added backticks (not single quotes) to the column names in the WHERE clause, in case they were causing trouble.
 
Last edited:
Upvote 0

bskotu555

Member
Thank you for your wonderful answer. I have another question, which is how to represent the split function in B4A similar to VB?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
1.As walt61 said. use code tag</>
2.Read the Tutorial Code Smells - common mistakes and other tips first, item5 using parameterized queries
3.Your query statement only retrieves the sys Field, and you would not get the "物料编码" at all.
4. You can join the WeChat group for @aeric help。

 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…