Bash script to insert values in MySQL

后端 未结 5 896
别跟我提以往
别跟我提以往 2020-11-30 07:48

I want to make a bash script that connects to my MySQL server and inserts some valuse from a txt file. I have written this down:

#!/bin/bash
echo \"INSERT I         


        
5条回答
  •  被撕碎了的回忆
    2020-11-30 08:32

    Assuming you have many rows to add, you probably need LOAD DATA INFILE statement, not INSERT. The source file has to be on the server, but it seems to be the case here.

    Something like that:

    #!/bin/bash
    
    mysql -uroot -ptest test << EOF
    
    LOAD DATA INFILE 'test.txt'
        INTO TABLE tbl_name
        FIELDS TERMINATED BY ' ';
    
    EOF
    

    LOAD DATA INFILE has many options as you will discover by reading the doc.

提交回复
热议问题