简体   繁体   English

将 MySQL 表导出到 .csv 文件

[英]Exporting MySQL table to .csv file

This is my first day with VBA.这是我接触 VBA 的第一天。 I am trying to export a .csv file in MS Access using MySQL query in VBA.我正在尝试使用 VBA 中的 MySQL 查询在 MS Access 中导出 .csv 文件。 I have tried using this code:我曾尝试使用此代码:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sql As String

Set db = CurrentDb

sql = "SELECT wo_id FROM WorkOrder"

Set rs = db.OpenRecordset(sql)
DoCmd.TransferText acExportDelim, , "rs", "C:\export.csv", True

rs.Close
Set rs = Nothing

which gives me:这给了我:

Run time error '3011':运行时错误“3011”:

The Microsoft Office Access database engine could not find the object 'rs'. Microsoft Office Access 数据库引擎找不到对象“rs”。 Make sure the object exists and that you spell its name and path name correctly确保该对象存在并且您正确拼写了它的名称和路径名

MORE INFO更多信息

I have a MySQL table called WorkOrder (I would like to pull data from here).我有一个名为 WorkOrder 的 MySQL 表(我想从这里提取数据)。

What am I missing?我错过了什么?

You can only export MS-Access objects (tables or queries) using DoCmd.TransferText .您只能使用DoCmd.TransferText导出 MS-Access 对象(表或查询)。 If you have a query called qryOutput you can export it:如果您有一个名为qryOutput的查询,您可以将其导出:

DoCmd.TransferText acExportDelim, , "qryOutput", "C:\export.csv", True

You could create the query on runtime (using db.CreateQueryDef ), export it and delete it.您可以在运行时创建查询(使用db.CreateQueryDef ),将其导出并删除。


If you are working with MySQL, maybe it is easier to export the data directly from the command line:如果您正在使用 MySQL,也许直接从命令行导出数据更容易:

c:\> mysql -h YourHost -u YourUser -pYourPassword dbYourDatabase -e"SELECT wo_id FROM WorkOrder" > c:\export.txt

This will create a tab-separated text file with the result of your query (including headers).这将使用您的查询结果(包括标题)创建一个制表符分隔的文本文件。

On a Unix-like command line you could convert this output on-the-fly to a comma separated text file using sed :在类 Unix 的命令行上,您可以使用sed将此输出即时转换为逗号分隔的文本文件:

$ mysql [connectionParameters] -e"select..." | sed 's/\t/,/g' > export.csv

I don't know if there are any pure-windows command line utilities to do this.我不知道是否有任何纯 Windows 命令行实用程序可以执行此操作。 I use Cygwin for this kind of things.我将Cygwin用于此类事情。 If you want to use it, be sure to install sed using Cygwin Setup program.如果要使用它,请务必使用 Cygwin 安装程序安装sed

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM