Automate transfer of csv file to MySQL

前端 未结 3 794
天命终不由人
天命终不由人 2021-01-07 05:27

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

相关标签:
3条回答
  • 2021-01-07 06:02

    You can use Quartz to create a cronjob - for periodically updating your database. with the help of cronmaker (http://www.cronmaker.com/), you get to choose when and how often your database gets updated.

    This is a sample SQL Script to import data into your MySQL database:

    LOAD DATA INFILE 'c:/.../filename.csv' 
    INTO TABLE discounts 
    FIELDS TERMINATED BY ',' 
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    IGNORE 1 ROWS;
    

    run the above script in your cronjob using your preferred language.

    0 讨论(0)
  • 2021-01-07 06:07

    You can use a here document:

    # some bash script stuff
    mysql ... <<EOF
    SQL COMMANDS
    GO HERE
    EOF
    # more bash script stuff
    
    0 讨论(0)
  • 2021-01-07 06:11

    You can execute a MySQL script from the command line by doing something like:

    mysql -uUsername -pPassword database_name < 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 ','
    
    0 讨论(0)
提交回复
热议问题