Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long Dim lRC As Long dim sSQL As string sSQL="select 'hello \n\t mysql!!!';" lPtr = mysql_real_escape_string(lMysql, lRC, StrPtr(sSQL), lstrlen(sSQL))
Private Declare Function SysAllocStringByteLen Lib "oleaut32.dll" (ByVal pszStr As Long, ByVal lLenB As Long) As LongPublic Function EscapeString(ByRef lMysql As Long, ByVal pStr As String) As String Dim lPtr As Long Dim lRC As Long Dim tmpStr As String
测试了一下,还是在lPtr = mysql_real_escape_string(lMysql, lRC, StrPtr(sSQL), lstrlen(sSQL))那里直接崩溃掉! Friend Function OpenRecordset(ByRef sSQL As String, _ ByRef lMysql As Long, _ ByRef bGotError As Boolean) As enumRecordSetState Closed Dim lRC As Long,lPtr As Long m_AffectedRecords = 0 OpenRecordset = m_State m_MySqlString = sSQL m_Mysql = lMysql lPtr = mysql_real_escape_string(lMysql, lRc, StrPtr(sSQL), lstrlen(sSQL)) lRC = mysql_real_query(lMysql, sSQL, lstrlen(sSQL)) If lRC <> 0 Then bGotError = True m_State = MY_RS_CLOSED OpenRecordset = MY_RS_CLOSED Exit Function End If m_MYSQL_RES = mysql_store_result(lMysql) 'Debug.Print "Pointer to result = " & m_MYSQL_RES If m_MYSQL_RES = 0 Then lRC = mysql_field_count(lMysql) m_AffectedRecords = 0 m_RowCount = 0 m_AvailRows = 0 m_FieldCount = 0 If lRC = 0 Then m_AffectedRecords = mysql_affected_rows(lMysql) 'Debug.Print "Query affected " & mlAffectedRows & " rows." Else bGotError = True 'Debug.Print "Query should have returned result... have to check for errors." Exit Function End If Else m_AffectedRecords = mysql_affected_rows(lMysql) m_RowCount = mysql_num_rows(m_MYSQL_RES) m_AvailRows = m_RowCount m_FieldCount = mysql_num_fields(m_MYSQL_RES) ReDim m_EditRsIndex(0 To m_RowCount) ReDim m_EditRsPtr(0 To m_RowCount) 'start by pointing to row #1 m_CurrentRecord = 1 m_State = MY_RS_OPEN OpenRecordset = m_State 'reposition the record pointer pGetRow True End If End Function
您的第二段代码里面的sSQL变量没有声明啊,和pStr是一样的? 如果不是一样的,那怎么调用EscapeString函数呢? Private Declare Function SysAllocStringByteLen Lib "oleaut32.dll" (ByVal pszStr As Long, ByVal lLenB As Long) As LongPublic Function EscapeString(ByRef lMysql As Long, ByVal pStr As String) As String Dim lPtr As Long Dim lRC As Long Dim tmpStr As String
呵呵, 是的,sSQL 改為 pStr就行,你可以加入 class DBConn Public Function EscapeString(ByVal pStr As String) As String Dim lPtr As Long Dim lRC As Long Dim tmpStr As String
dim sSQL As string
sSQL="select 'hello \n\t mysql!!!';"
lPtr = mysql_real_escape_string(lMysql, lRC, StrPtr(sSQL), lstrlen(sSQL))
Dim lRC As Long
Dim tmpStr As String
tmpStr = String(lstrlen(sSQL) * 2 + 1, Chr(0))
lRC = StrPtr(tmpStr)
lPtr = mysql_real_escape_string(lMysql, ByVal lRC, StrPtr(pStr), Len(tmpStr))
lPtr = SysAllocStringByteLen(lRC, lPtr)
EscapeString = CStringPointerToVbString(lPtr)
End Function
Friend Function OpenRecordset(ByRef sSQL As String, _
ByRef lMysql As Long, _
ByRef bGotError As Boolean) As enumRecordSetState
Closed Dim lRC As Long,lPtr As Long
m_AffectedRecords = 0
OpenRecordset = m_State m_MySqlString = sSQL m_Mysql = lMysql lPtr = mysql_real_escape_string(lMysql, lRc, StrPtr(sSQL), lstrlen(sSQL))
lRC = mysql_real_query(lMysql, sSQL, lstrlen(sSQL))
If lRC <> 0 Then
bGotError = True
m_State = MY_RS_CLOSED
OpenRecordset = MY_RS_CLOSED
Exit Function
End If m_MYSQL_RES = mysql_store_result(lMysql)
'Debug.Print "Pointer to result = " & m_MYSQL_RES If m_MYSQL_RES = 0 Then
lRC = mysql_field_count(lMysql)
m_AffectedRecords = 0
m_RowCount = 0
m_AvailRows = 0
m_FieldCount = 0
If lRC = 0 Then
m_AffectedRecords = mysql_affected_rows(lMysql)
'Debug.Print "Query affected " & mlAffectedRows & " rows."
Else
bGotError = True
'Debug.Print "Query should have returned result... have to check for errors."
Exit Function
End If
Else
m_AffectedRecords = mysql_affected_rows(lMysql)
m_RowCount = mysql_num_rows(m_MYSQL_RES)
m_AvailRows = m_RowCount
m_FieldCount = mysql_num_fields(m_MYSQL_RES) ReDim m_EditRsIndex(0 To m_RowCount)
ReDim m_EditRsPtr(0 To m_RowCount) 'start by pointing to row #1
m_CurrentRecord = 1
m_State = MY_RS_OPEN
OpenRecordset = m_State 'reposition the record pointer
pGetRow True
End If
End Function
如果不是一样的,那怎么调用EscapeString函数呢?
Private Declare Function SysAllocStringByteLen Lib "oleaut32.dll" (ByVal pszStr As Long, ByVal lLenB As Long) As LongPublic Function EscapeString(ByRef lMysql As Long, ByVal pStr As String) As String Dim lPtr As Long
Dim lRC As Long
Dim tmpStr As String
tmpStr = String(lstrlen(sSQL) * 2 + 1, Chr(0)) '就是这里的sSQL,
lRC = StrPtr(tmpStr)
lPtr = mysql_real_escape_string(lMysql, ByVal lRC, StrPtr(pStr), Len(tmpStr))
lPtr = SysAllocStringByteLen(lRC, lPtr)
EscapeString = CStringPointerToVbString(lPtr)
End Function
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