vb6 调用mysql_real_escape_string函数的问题!请教!
本帖最后由 seawaycao 于 2013-12-29 11:18:51 编辑 最近在研究用VB6操作mysql数据库的项目,基本的操作功能已经完成。就是发现一个问题,若SQL语句中包含了C(C++)中的转义字符(例如:\t\n)或二进制数据时,就会出现字符丢失或乱码的情况,百度了很多资料,问题出在mysql_real_escape_string的上面,但实际使用这个函数时一直出错,还没有错误提示,VB6直接崩溃!
我的引用为:
Public Declare Function mysql_real_escape_string Lib "libmySQL" (ByVal lMysql As Long, _
ByRef lTo As String, _
ByVal lFrom As String, _
ByVal lLength As Long) As Long
/*
Add escape characters to a string (blob?) to make it suitable for a insert
to should at least have place for length*2+1 chars
Returns the length of the to string
*/
ulong STDCALL
mysql_escape_string(char *to,const char *from,ulong length)
{
return (uint) escape_string_for_mysql(default_charset_info, to, 0, from, length);
}
ulong STDCALL
mysql_real_escape_string(MYSQL *mysql, char *to,const char *from,
ulong length)
{
if (mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
return (uint) escape_quotes_for_mysql(mysql->charset, to, 0, from, length);
return (uint) escape_string_for_mysql(mysql->charset, to, 0, from, length);
}
Public Function EscapeString(ByVal pStr As String) As String
Dim lPtr As Long
Dim lRC As Long
Dim tmpStr As String
tmpStr = String(lstrlen(pStr) * 2 + 1, Chr(0))
lRC = StrPtr(tmpStr)
lPtr = mysql_real_escape_string(m_Mysql, ByVal lRC, StrPtr(pStr), Len(tmpStr))
lPtr = SysAllocStringByteLen(lRC, lPtr)
EscapeString = CStringPointerToVbString(lPtr)
End Function
Set gConn = New DBConn
'打開數據庫
' MY_MULTI_STATEMENTS --Enable multi-stmt support(開啟多個結果集支持)
gConn.OpenConnection 服務器IP, UserID, Userpwd, dbname, 3306, MY_MULTI_STATEMENTS, "utf8"
MsgBox gConn.EscapeString("select 'hello \t\r mysql\n!!!' as hello")
set gconn = nothing