1

I am trying to write a bash script to drop all tables in a database then import all csv files in a given directory. I am having a couple of issues though:

  1. Importing 1 csv with the script gives the following error:

    ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BULK
    INSERT paf_addresses
    FROM '../sql/paf-sample.csv'
    WITH
    (
    FIELDTERMINATOR = ' at line 1
    
  2. How to tell the mysql part to run for every file in a set directory


My script is below.

#!/bin/bash

detail="propertystork"
AWK=$(which awk)
GREP=$(which grep)

TABLES=$(mysql -u $detail -p$detail $detail -e 'show tables' | $AWK '{ print $1}' | $GREP -v '^Tables' )

for t in $TABLES
do
  mysql -u $detail -p$detail $detail -e "drop table $t"
done

mysql -u $detail -p$detail -h localhost $detail <<-_END-OF-SCRIPT_

BULK
INSERT paf_addresses
FROM '../sql/paf-sample.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
_END-OF-SCRIPT_

1 Answer 1

1

The error is caused by the fact that there is no BULK INSERT statement in MySQL.

Instead there is LOAD DATA INFILE

LOAD DATA INFILE '../sql/paf-sample.csv'
INTO table_name
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
Sign up to request clarification or add additional context in comments.

4 Comments

I was working off this blog post from 2008 which said there was, or have I misinterpreted it?
You're confused BULK INSERT is for SQL Server not for MySQL which are two completely different database management systems
Okay thanks for your help :) that solved problem 1. any ideas on part 2?
The logic of the script is wrong. If you drop tables you won't be able to load data from csv files. You'll have to create tables first. That would work only if you'd have sql dump files instead of cdv files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.