【问题标题】:Mysql stored procedure returns blob as nullMysql 存储过程将 blob 返回为 null
【发布时间】:2021-06-11 22:10:55
【问题描述】:

我正在尝试从 mysql 数据库中获取 blob 记录(图像)到 vba 用户窗体图片图像。

Mysql 表“images”的结构 = id int, image1 blobmedium。 如果我使用

SELECT id, image1 FROM images 

我明白了:

select statement returns id and also blob column

我编写了 mysql 存储过程 GetImageNow 来从数据库中获取这个 blob(图像):

DELIMITER // 

CREATE PROCEDURE GetImageNow()
BEGIN
SELECT id, image1 FROM images;
END //

DELIMITER ;

当我运行此过程时,它在 image1 列中返回 null。我不明白为什么

stored procedure returns null in blobmedium column

【问题讨论】:

  • 测试确实在第二个选项中返回 NULL SELECT id, image1, LENGTH(image1) len1 FROM images。 `
  • 我试了这条sql命令,结果len1 = 2504,表示有数据,但是看不到。
  • 这是您的打印软件问题...从另一方面来看,这是合乎逻辑的 - BLOB 是二进制数据,而您尝试将其打印为 HTML 文本。

标签: mysql null blob procedure


【解决方案1】:

好的。谢谢你的建议。最后我得到了从数据库中读取图像并将其加载到用户窗体上的 Picturebox 的代码:


Private Sub ReadImageFromDB ()

    Dim cnSqlConn As New ADODB.Connection
    cnSqlConn.Connectionstring = "DSN=XXX"
    cnSqlConn.Open
    

    /* create command for running mysql stored procedure GetImage */

    Dim cmd As New ADODB.Command
    
    With cmd
        .ActiveConnection = cnSqlConn
        .CommandType = adCmdStoredProc
        .CommandText = "GetImage"
        .CommandTimeout = 15
    End With
    

    /* read blob image from database via ADODB.Stream and save it temporarily on disk*/
    
    Dim rsImage as New ADODB.Recordset
    Set rsImage = cmd.Execute

    Dim stm As New ADODB.Stream
    With stm
        .Type = adTypeBinary
        .Open
        .Write rsImage.Fields("image1").Value 
        .SaveToFile "d:\Temp.jpg", adSaveCreateOverWrite
    End With

    /* load the picture into Image on userform */
    Userform1.imgImageFromDatabase.Picture = LoadPicture("d:\temp.jpg")
    Kill ("d:\temp.jpg")
 
    rsImage.Close
    Set rsImage = Nothing
    cnSqlConn.Close
    Set cnSqlConn = Nothing
    Set stm = Nothing

End Sub 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 2012-11-29
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    相关资源
    最近更新 更多