gpt4 book ai didi

mysql - 使用带有 OUT 参数的 VB6 调用 MySQL 存储过程

转载 作者:可可西里 更新时间:2023-11-01 07:08:43 25 4
gpt4 key购买 nike

我已经在 MySQL (Server 5.5) 中编写了这个程序

DELIMITER $$

DROP PROCEDURE IF EXISTS `InsertList` $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertList`(IN fName VARCHAR(20), IN fType VARCHAR(3), IN fFood varchar(20), Out fResult int)

BEGIN

insert into tblguest (firstname, confirm, food) values (fName, fType, fFood);

select count(id) from tblguest into fResult;

END $$

DELIMITER ;

当我从 MySQL 查询浏览器调用此过程时,它按预期返回

Call InsertList ('V1', 'No', 'F1', @result);

Select @result;

--> 成功返回表中id的个数

我用VB6写了下面的代码

Dim res As Integer
On Error GoTo chkErr

Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "InsertList"

cmd.Parameters.Append cmd.CreateParameter("fName", adVarChar, adParamInput, 20, Text3.Text)
cmd.Parameters.Append cmd.CreateParameter("fType", adVarChar, adParamInput, 3, Text2.Text)
cmd.Parameters.Append cmd.CreateParameter("fFood", adVarChar, adParamInput, 20, Text1.Text)
cmd.Parameters.Append cmd.CreateParameter("fResult", adInteger,adParamOutput)

cmd.Execute

res = cmd("fResult")
MsgBox res
Exit Sub

chkErr:

Select Case Err.Number
Case Else
Text4.Text = Err.Number & " - " & Err.Description
End Select

但当它尝试运行语句 cmd.execute 时,它会抛出以下错误:

-2147467259 - [MySQL][ODBC 5.1 Driver][mysqld-5.5.34]OUT or INOUT argument 4 for routine dbtest.InsertList is not a variable or NEW pseudo-variable in BEFORE trigger

我看到过适用于 SQL 的代码,那么是不是 MySQL 本身在使用带有 OUT 参数的存储过程时存在问题?

最佳答案

看来是 MySQL ODBC 和 C/API 的一个 Unresolved bug

一种解决方案是使用带有准备好的变量的 SQL 命令来执行:

Dim rs As ADODB.Recordset 

Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandType = adCmdText
cmd.CommandText = "call InsertList(?,?,?,@fResult)"

cmd.Parameters.Append cmd.CreateParameter("fName", adVarChar, adParamInput, 20, Text3.Text)
cmd.Parameters.Append cmd.CreateParameter("fType", adVarChar, adParamInput, 3, Text2.Text)
cmd.Parameters.Append cmd.CreateParameter("fFood", adVarChar, adParamInput, 20, Text1.Text)

cmd.Execute

'And after that, using the same connection, get the value of
'@fResult from a single query:

Set rs = cn.Execute("select @fResult as fResult")
MsgBox rs!fResult

您将获得预期的值(value)。

关于mysql - 使用带有 OUT 参数的 VB6 调用 MySQL 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43535704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com