自动将csv文件传输到MySQL [英] Automate transfer of csv file to MySQL
问题描述
我有一个csv文件,我会定期更新通过调用cygwin + bash脚本的批处理脚本。 我想自动将csv文件上传到MySQL数据库,以便我的数据库中的表将以固定间隔以csv文件进行更新。数据库当前正在Windows Server 2003计算机上运行,并使用phpMyAdmin管理。
I have a csv file that I will be regularly updating through a batch script that calls cygwin+ bash script. I would like to automate the upload of the csv file into a MySQL database such that a table in my database would be updated with the csv file at regular intervals. The database is currently running on a Windows Server 2003 machine and administered with phpMyAdmin.
我已经在线查找了一些方法,我可以实现其中的一部分,但我混淆了在这些来源中提供的代码应该放在哪里,以及如何调用它们。例如,将CSV文件直接导入MySQL 似乎会显示如何上传
I have looked online and found some ways that I could achieve part of that, but I am confused as to where the code presented in those sources should be placed and how they would be called. For instance, Import CSV file directly into MySQL seems to show how to upload a csv file to a MySQL database from the SQL command line once, but not repeatedly, the latter being what I need.
我更愿意使用涉及到的bash脚本的解决方案(如反对批处理和php)如果可能(即我更喜欢一个解决方案,我可以与bash脚本更新csv文件)。
I would prefer the solution to involved bash scripting (as opposed to batch and php) if possible (i.e. I would prefer a solution that I could integrate with the bash scripts that update the csv file).
推荐答案
您可以通过执行以下命令从命令行执行MySQL脚本:
You can execute a MySQL script from the command line by doing something like:
mysql -uUsername -pPassword database_name < infile.sql
您可以从命令行调用,在infile.sql中,例如:
You could invoke that from the command line and in the infile.sql you could have code like:
LOAD DATA INFILE 'filename.csv' TO table_name
FIELDS TERMINATED BY ','
这篇关于自动将csv文件传输到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!