Handling single quotes in the SELECT statement

xiaoxiao2021-03-06  44

In database applications, you often need to dynamically generate SELECT statements, typical situations: sqlstring = "Select * from mybas where name = '" & text1 & "" "Ok, the problem appears, if there is a single in the entry Text1 Quotation marks, the result is that when SQLString is sent to the database, it will be wrong!

In fact, the prevention to do is very simple, add a function:

Function checkstring (s) as stringpos = INSTR (s, "') While Pos> 0s = MID (S, 1, POS) &"' "& MID (S, POS 1) POS = INSTR (POS 2, s, "'") Wend

Checkstring = "'" & s & "'" End Function

In the future, generate a SELECT statement in the future, use: sqlstring = "SELECT * from mybas where name =" & checkstring (Text1)

转载请注明原文地址:https://www.9cbs.com/read-79629.html

New Post(0)