After trying for a full day, I'm hoping someone here can help me make below script work. I've combined information from multiple threads ( example ) and websites, but can't get it to work.
I'm trying to get a MariaDB10 database called 'stock_db' on my Synology NAS to load all *.csv files from a specific folder (where I save downloaded historical prices of stocks) and add these to a table called 'prices'. The files are all equally named "price_history_' isin '.csv". Below SQL statement works when running it individually from HeidiSQL on my Windows machine:
LOAD DATA LOW_PRIORITY LOCAL INFILE 'D:\\Downloads\\price_history_NL0010366407.csv'
IGNORE INTO TABLE `stock_db`.`prices`
CHARACTER SET utf8
FIELDS TERMINATED BY ';'
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 2 LINES
(@vdate, @vprice)
SET
isin = 'NL0010366407',
date = STR_TO_DATE(@vdate, '%d-%m-%Y'),
price = @vprice
;
Unfortunately, when I try to batch loading all csv's from a folder on my NAS through below script, I keep getting the same error.
#!/bin/bash
for filename in ./price_history/*.csv; do
echo $filename
isin=${filename:30:12}
echo $isin
/volume1/@appstore/MariaDB10/usr/local/mariadb10/bin/mysql -u root -p \
"LOAD DATA LOW_PRIORITY LOCAL INFILE '$filename'\
IGNORE INTO TABLE 'stock_db.prices'\
CHARACTER SET utf8\
FIELDS TERMINATED BY ';'\
OPTIONALLY ENCLOSED BY '"'"'"'\
ESCAPED BY '"'"'"'\
LINES TERMINATED BY '\r\n'\
IGNORE 2 LINES (@vdate, @vprice)\
SET\
isin = '$isin',\
date = STR_TO_DATE(@vdate, '%d-%m-%Y'),\
price = @vprice;"
done
ERROR 1102 (42000): Incorrect database name
If someone can help me do this without having to re-enter the user's password or putting the password in the script, this would be really nice bonus!
Got the 'Incorrect Database Error' resolved by adding '-e' option Now I have a new error on the csv files:
ERROR 13 "Permission Denied"
While the folder and files are full access for everyone. Anyone any thoughts to this? Thanks a lot!
Try to set database using -D option: change the first line to /volume1/@appstore/MariaDB10/usr/local/mariadb10/bin/mysql -D stock_db -u root -p \\ ...
You may have an error in this line IGNORE INTO TABLE 'stock_db.prices'\\
- try to remove the single quotes.
Create file .my.cnf in your user's home directory and put the following information into it: [client] password="my password"
Info about option files.
'stock_db.prices'
Incorrect quoting. This will work since neither are keywords:
stock_db.prices
This will also work:
`stock_db`.`prices`
Note that the db name and the table name are quoted separately, using backtics.
I can't predict what will happen with this nightmare:
'"'"'"'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.