VB6.0如何用 libmySQL.dll API访问MYSQL数据库
MYSQL 应用十分广范,很多网站几乎都在用他,因为他的小巧和灵活性等方面让我决定要对他进行研究,到现在已经成功的不用经过ODBC,ADO等方式就可以读取和更新数据。 好了不多罗嗦,以下我来介绍总个程序的细节: (一)API声明 1. '分配或初始化适合mysql_real_connect()的一个MYSQL对像。 2. '关闭一个以前打开了的连接 3. '能用於设置额外连接选项并且影响一个连接的行为。 4. 设置客户端访问的字符集 (这个很重要,访问中文时一定要用这个) Public Declare Function mysql_set_character_set Lib "libmySQL" (ByVal lMysql As Long,ByVal cs_name As String) As Long 5.联接数据库 Public Declare Function mysql_real_connect Lib "libmySQL" _ 6.执行查询操作 Public Declare Function mysql_query Lib "libmySQL" _ 7. 获取查询的结果集 Public Declare Function mysql_affected_rows Lib "libmySQL" (ByVal lMYSQL_RES As Long) As Long Public Declare Function mysql_fetch_field_direct Lib "libmySQL" _ Public Declare Sub mysql_free_result Lib "libmySQL" (ByVal lMysql As Long) 8. 出错信息获取 'status and error-reporting routines 9. MYSQL 字段结构 Public Type typ_MYSQL_FIELD 10. 要用到的WINDOS API Public Const LONG_SIZE = 4
11. 常量 Public Enum MYSQL_OPTION
Public Enum MYSQL_FLAG ' connection state enum (二)打开数据库联接 Private MyOptArr() As MyOption Private m_MYSQL As Long 'pointer to mysql connection ‘打开数据库联接 ’sHostName 主机名称 ‘sUserName 用户名 ’sPassword 密码 ‘sDbName 数据库名称 ’lPortNum 端口号 ’lFlags 联接参数 ‘cs_name 字符集 Function OpenConnection(ByVal sHostName As String,_ gServerName = sHostName If mysql_real_connect(m_MYSQL,gServerName,gUserID,gUserPass,gDbName,gPortNum,m_CESApp.SoftWare,gOptions) = 0 Then OpenConnection = m_State ‘錯誤檢查 Private Sub CheckForError(ByVal vProcName As String) Dim lNumber As Long Dim LPSTR As Long lNumber = mysql_errno(m_MYSQL) If lNumber <> 0 Then LPSTR = mysql_error(m_MYSQL) Err.Raise lNumber,vProcName,CStringPointerToVbString(LPSTR) End If End Sub (三)执行SQL语句并获取数据 Private m_RowCount As Long 'number of records in result set Private m_FieldCount As Long 'number of fields in result set Private m_AffectedRecords As Long 'number of rows affected by query Private m_MySqlString As String 'last sql statement queried on this rs object Private m_MYSQL_RES As Long 'pointer to mysql result set Private m_MYSQL_ROW As Long 'pointer to mysql row Private m_MYSQL_FIELD_LENGTHS As Long 'pointer to array of column lengths Function OpenRecordset(sSQL As String,_ lMysql As Long,_ bGotError As Boolean) As enumRecordSetState Dim lRc As Long CloseRecordset m_AffectedRecords = 0 OpenRecordset = m_State m_MySqlString = sSQL
If gSystemDebugSQL Then WriteToLog sSQL End If
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) If m_MYSQL_RES = 0 Then lRc = mysql_field_count(lMysql) If lRc = 0 Then m_AffectedRecords = mysql_affected_rows(lMysql) Else bGotError = True Exit Function End If Else m_AffectedRecords = mysql_affected_rows(lMysql) m_RowCount = mysql_num_rows(m_MYSQL_RES) m_FieldCount = mysql_num_fields(m_MYSQL_RES) 'start by pointing to row #1 m_CurrentRecord = 1 m_State = MY_RS_OPEN OpenRecordset = m_State 'reposition the record pointer pGetRow End If End Function Private Sub pGetRow() If m_CurrentRecord > 0 And m_CurrentRecord <= m_RowCount Then Dim cSeekRow As Currency 'adjust because currency fields have 4 fixed decimals ' 1.000 gets adjusted to 0.001 cSeekRow = (m_CurrentRecord - 1) / (10 ^ 4) mysql_data_seek m_MYSQL_RES,cSeekRow m_MYSQL_ROW = mysql_fetch_row(m_MYSQL_RES) m_MYSQL_FIELD_LENGTHS = mysql_fetch_lengths(m_MYSQL_RES) Else m_MYSQL_ROW = 0 m_MYSQL_FIELD_LENGTHS = 0 End If End Sub Function SetProperties(ByRef vRequestedField As Variant,_ ByRef lFieldCount As Long,_ ByRef lMYSQL_RES As Long,_ ByRef lMYSQL_ROW As Long,_ ByRef lMYSQL_FIELD_LENGTHS As Long) 'setup the values required for handling MYSQL_FIELD properties ... m_FieldCount = lFieldCount 'set the field count m_MYSQL_RES = lMYSQL_RES 'set the pointer to the result set m_MYSQL_ROW = lMYSQL_ROW 'set the pointer to the current row 'set the pointer to the arrary of column lengths m_MYSQL_FIELD_LENGTHS = lMYSQL_FIELD_LENGTHS If IsNumeric(vRequestedField) Then 'column referenced by index m_RequestedField = vRequestedField Else 'column referenced by name pBuildNameCollection m_RequestedField = pGetFieldFromNameCollection(vRequestedField) End If 'check for any invalid values If m_RequestedField < 0 _ Or m_FieldCount = 0 _ Or m_RequestedField >= m_FieldCount Then m_MYSQL_RES = 0 m_MYSQL_ROW = 0 m_MYSQL_FIELD_LENGTHS = 0 End If End Function ‘獲取字段名集合 Private Sub pBuildNameCollection() 'attempt to build collection of column names Dim lCnt As Long Dim sName As String On Local Error Resume Next 'in case multiple columns in rs have same name If oNames.Count = 0 And m_FieldCount > 0 Then For lCnt = 1 To m_FieldCount sName = pGetFieldName(lCnt - 1) If Len(sName) > 0 Then oNames.Add lCnt,sName Next End If End Sub Private Function pGetFieldFromNameCollection(ByRef vRequestedField As Variant) As Long On Local Error Resume Next 'in case requested field is not in collection ... If oNames.Count > 0 Then pGetFieldFromNameCollection = oNames.Item(vRequestedField) 'collection is 1 based ... fields are 0 based pGetFieldFromNameCollection = pGetFieldFromNameCollection - 1 End Function ‘獲取字段名 Private Function pGetFieldName(ByRef lField As Long) As String Dim mField As typ_MYSQL_FIELD mField = pGetFieldStructure(lField) pGetFieldName = CStringPointerToVbString(mField.FieldName) End Function '获取 MYSQL 字段结构指针 Private Function pGetFieldStructure(ByRef lField As Long) As typ_MYSQL_FIELD Dim lMYSQL_FIELD As Long Dim mField As typ_MYSQL_FIELD
If m_MYSQL_RES = 0 _ Or lField < 0 _ Or lField >= m_FieldCount Then Exit Function End If lMYSQL_FIELD = mysql_fetch_field_direct(m_MYSQL_RES,lField) 'Debug.Print "Pointer to MYSQL_FIELD structure for field " & lField & " = " & pGetFieldStructure
If lMYSQL_FIELD = 0 Then Exit Function CopyMemory mField,ByVal lMYSQL_FIELD,LenB(mField)
Let pGetFieldStructure = mField End Function ‘获取 MYSQL 字段值 Public Property Get Value() As Variant Dim lTmpChar As Long,lCurLen As Long Dim lRowData As Long Dim b() As Byte Dim sVal As String Dim sName As String 'get pointer to requested field CopyMemory lRowData,ByVal (m_MYSQL_ROW + (LONG_SIZE * m_RequestedField)),LONG_SIZE 'get length of requested field CopyMemory lCurLen,ByVal (m_MYSQL_FIELD_LENGTHS + (LONG_SIZE * m_RequestedField)),LONG_SIZE If lRowData = 0 Then Value = Null Else sVal = "" If lCurLen > 0 Then ReDim b(0 To (lCurLen - 1)) 'copy string to byte array CopyMemory b(0),ByVal lRowData,BYTE_SIZE * lCurLen
'convert to unicode sVal = StrConv(b(),vbUnicode) Else sVal = "" End If Value = sVal End If End Property -- 完 -- 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/CHRL/archive/2009/12/24/5070389.aspx (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |