自动将 csv 文件传输到 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 似乎显示了如何上传从 SQL 命令行将 .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)的解决方案(即我更喜欢可以与更新 csv 文件的 bash 脚本集成的解决方案).
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 ','
相关文章