Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA MySQL“;从表格中选择*;信息不全_Mysql_Sql_Vba_Excel - Fatal编程技术网

Excel VBA MySQL“;从表格中选择*;信息不全

Excel VBA MySQL“;从表格中选择*;信息不全,mysql,sql,vba,excel,Mysql,Sql,Vba,Excel,我的Excel VBA代码从MySQL数据库中选择值时遇到了一个非常奇怪的问题。我用以下列构建了一个简单的数据库: 身份证 名字 姓 城市 假设有以下入口: 01;潘;彼得;纽约 02; P彼得;纽约市 但现在的问题是,如果我从表中选择*,它只会显示以下输出: 01;P潘;纽约 02; P潘;纽约市 这意味着我只看到一个条目的最小长度。。。。那里发生了什么事?我在不同的模块中为该任务编写了非常随意的VBA代码: 公共变量: Public cn As ADODB.Connection Public

我的Excel VBA代码从MySQL数据库中选择值时遇到了一个非常奇怪的问题。我用以下列构建了一个简单的数据库:

  • 身份证
  • 名字
  • 城市
  • 假设有以下入口:

    01;潘;彼得;纽约
    02; P彼得;纽约市

    但现在的问题是,如果我从表中选择*,它只会显示以下输出:

    01;P潘;纽约
    02; P潘;纽约市

    这意味着我只看到一个条目的最小长度。。。。那里发生了什么事?我在不同的模块中为该任务编写了非常随意的VBA代码:

    公共变量:

    Public cn As ADODB.Connection
    Public rs As ADODB.Recordset
    Public strSql As String
    
    连接模块:

    Public Function connectDB()
    Dim strServer_Name As String
    Dim strDB_Name As String
    Dim strUser_ID As String
    Dim strPassword As String
    
    With Sheet2
        strServer_Name = .Range("B2").Value
        strDB_Name = .Range("B3").Value
        strUser_ID = .Range("B4").Value
        strPassword = .Range("B5").Value
    End With
    
    Set cn = New ADODB.Connection
    
    cn.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver}" & _
    ";SERVER=" & strServer_Name & _
    ";DATABASE=" & strDB_Name & _
    ";UID=" & strUser_ID & _
    ";PWD=" & strPassword & ""
    End Function
    
    Sub request()
    strSql = "SELECT * FROM test"
    
    Call connectDB
    Set rs = New ADODB.Recordset
    rs.Open strSql, cn, adOpenDynamic
    
    With Sheet1.Range("A1")
        .ClearContents
        .CopyFromRecordset rs
    End With
    
    Call disconnectDB
    End Sub
    
    子模块:

    Public Function connectDB()
    Dim strServer_Name As String
    Dim strDB_Name As String
    Dim strUser_ID As String
    Dim strPassword As String
    
    With Sheet2
        strServer_Name = .Range("B2").Value
        strDB_Name = .Range("B3").Value
        strUser_ID = .Range("B4").Value
        strPassword = .Range("B5").Value
    End With
    
    Set cn = New ADODB.Connection
    
    cn.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver}" & _
    ";SERVER=" & strServer_Name & _
    ";DATABASE=" & strDB_Name & _
    ";UID=" & strUser_ID & _
    ";PWD=" & strPassword & ""
    End Function
    
    Sub request()
    strSql = "SELECT * FROM test"
    
    Call connectDB
    Set rs = New ADODB.Recordset
    rs.Open strSql, cn, adOpenDynamic
    
    With Sheet1.Range("A1")
        .ClearContents
        .CopyFromRecordset rs
    End With
    
    Call disconnectDB
    End Sub
    

    这是VBA问题还是我的MySQL中有任何bug?

    好了,伙计们,我在这篇文章中找到了解决方案:

    和另一个人一样解决了这个问题。在我的代码中,它如下所示:

    Sub request()
    Dim iCols As Integer
    Dim iRows As Integer
    
    strSql = "SELECT * FROM test"
    
    Call connectDB
    Set rs = New ADODB.Recordset
    rs.Open strSql, cn, adOpenDynamic
    
    iRows = 10
    
    While Not rs.EOF
        For iCols = 0 To rs.Fields.Count - 1
            Tabelle1.Cells(iRows, iCols + 1).Value = rs.Fields(iCols).Value
        Next
        rs.MoveNext
        iRows = iRows + 1
    Wend
    
    Call disconnectDB
    End Sub
    

    但是感谢所有试图帮助我的人

    您是否能够通过命令行或phpmyadmin直接查询数据库?如果是这样,你会得到同样的结果吗?您是否能够验证它们是否为适合您插入的数据的字段的char()或varchar()长度?您的代码中没有任何内容可以解释您在输出中看到的内容。您是否确定列足够宽,并且无法看到其中的所有数据?另外,替换CopyFromRecordset,看看这是否是问题所在
    Do:Debug.Print rs.Fields(2)。Value:rs.MoveNext:Loop直到rs.EOF
    是否得到意外的结果?这不会有任何区别,但尝试用以下“从测试中选择ID、名称、姓氏、城市”@jnevil:I能够直接在phpmyadmin中查询并获得正确的结果。字段的数据类型为int(11)表示ID,而文本表示所有其他字段。我将把phpmyadmin查询的屏幕截图添加到问题中。但我又意识到了一些非常奇怪的事情。只有当表的最后一个条目的长度低于其他条目时,才会发生错误。因此,如果最后一个条目的长度与所有其他条目的最大长度相同,它将显示所有内容。每列都有一个单独的字段。我还将在问题中添加屏幕截图。所以解决方案是逐个单元格并检索数据?mysql odbc适配器中一定有一些时髦的调料。看起来是这样,但我想这就是解决方案。一个家伙似乎通过显式强制转换(但他的答案不是公认的)来修复它,这表明MySQL驱动程序有一个bug(可能他仍然使用CopyFromRecordset)。但是这里的OP并没有铸造任何东西,而是避免使用CopyFromRecordset方法,这表明Excel对象模型有一个bug。虽然我已经多次使用CopyFromRecordset(而不是MySQL),但从未见过这个问题。因此,可能错误在于CopyFromRecordset和MySQL驱动程序之间的通信。