gpt4 book ai didi

mysql - 将日期传递给 MySQL 查询时出现 VBA 运行时错误

转载 作者:行者123 更新时间:2023-11-29 11:54:10 24 4
gpt4 key购买 nike

我从 Excel VBA 连接到 MySQL 以获取 my_stored_procedure。工作正常,除了当我使用日期过滤器作为参数传递给 MySQL SP 时,我收到运行时错误。

日期过滤器由用户在两个单元格中输入,即开始日期和结束日期。单元格被定义为范围 dateFrom 和 dateTo。

所以我使用这些范围传递给 mySql rs,但 VBA 会返回:

Run-time error '-2147217900 (80040e14)'

You have an error in MySQL syntax; check the manual that corresponds to your MYSQL server version... 'my_stored_procedure '2015-10-01', '2015-10-30'' at line 1.

下面是我的 VBA 代码:

        Const ConnStr As String = "Driver={MySQL ODBC 5.3 ANSI Driver};Server=0.0.0.0;Database=db;User=user;Password=pw;Option=3;PORT=3306;Connect Timeout=20;"

Function MySqlCommand(strSql As String) As Variant
Dim conMySQL
Set conMySQL = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
conMySQL.ConnectionString = ConnStr
conMySQL.Open
Set cmd.ActiveConnection = conMySQL
cmd.CommandText = strSql
ReturnValue = cmd.Execute
conMySQL.Close
End Function

Function MySqlRS(strSql As String) As ADODB.Recordset
Dim conMySQL
Set conMySQL = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
conMySQL.ConnectionString = ConnStr
conMySQL.Open
Set rs.ActiveConnection = conMySQL
rs.Open strSql
Set MySqlRS = rs
End Function
_____
Public Sub fetchReport()

Dim rs As ADODB.Recordset
Set rs = MySqlRS("my_stored_procedure '" & Format(Range("dateFrom"), "yyyy-mm-dd") & "' ,'" & Format(Range("dateTo"), "yyyy-mm-dd") & "' ")
Dim hd As Integer


If Not rs.EOF Then
For hd = 0 To rs.Fields.Count - 1
Worksheets("data").Cells(1, hd + 1).Value = rs.Fields(hd).Name 'insert column headers
Next

Sheets("data").Range("A2").CopyFromRecordset rs 'insert data
Sheets("data").Range("A1").CurrentRegion.Name = "rsRange" 'set named range for pivot
End If

ActiveWorkbook.RefreshAll

End Sub

这是我的 SP 语法:

CREATE DEFINER=`user`@`%` PROCEDURE `my_stored_procedure`
(
in fromDate date,
in toDate date
)
BEGIN
SELECT ...
WHERE dateColumn >= fromDate AND dateColumn <= toDate
END

感谢任何帮助。

乔尔

最佳答案

我希望这对您有所帮助。自从我这样做以来已经很长时间了。

架构

create table myTable
( id int auto_increment primary key,
dateColumn date not null,
stuff varchar(100) not null
);
insert myTable(dateColumn,stuff) values
('2014-12-21','dogs'),('2015-02-21','frogs'),('2015-07-21','snakes'),('2015-12-21','cats');

存储过程

drop procedure if exists `my_stored_procedure`;
DELIMITER $$
CREATE PROCEDURE `my_stored_procedure`
(
in fromDate date,
in toDate date
)
BEGIN
SELECT *
from myTable
WHERE dateColumn >= fromDate AND dateColumn <= toDate;
END
$$
DELIMITER ;

VBA

Function MySqlStoredProcRS(strSql As String) As ADODB.Recordset
Dim conMySQL
Set conMySQL = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
conMySQL.ConnectionString = ConnStr ' not shown but like yours in Op Question
conMySQL.Open
Set rs.ActiveConnection = conMySQL
rs.CursorLocation = adUseClient
rs.Open strSql, conMySQL, , , adCmdText
Set MySqlStoredProcRS = rs
End Function
Public Sub fetchReport()

Dim rs As ADODB.Recordset
Dim sql As String
sql = "call my_stored_procedure ('" & Format(Range("dateFrom"), "yyyy-mm-dd") & "' ,'" & Format(Range("dateTo"), "yyyy-mm-dd") & "')"
Set rs = MySqlStoredProcRS(sql)
Dim hd As Integer


If Not rs.EOF Then
For hd = 0 To rs.Fields.Count - 1
Worksheets("data").Cells(1, hd + 1).Value = rs.Fields(hd).Name 'insert column headers
Next

Sheets("data").Range("A2").CopyFromRecordset rs 'insert data
Sheets("data").Range("A1").CurrentRegion.Name = "rsRange" 'set named range for pivot
End If

ActiveWorkbook.RefreshAll

End Sub

输出图片(点击蓝色的东西后)

enter image description here

要点

我想探索一下新函数 MySqlStoredProcRS 的更改、ADODB.RecordSet 的处理、使用 call 以及将参数括在调用字符串中的括号。

祝你好运。

关于mysql - 将日期传递给 MySQL 查询时出现 VBA 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33486913/

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