sh and MySQL LOAD DATA


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sh and MySQL LOAD DATA
# 8  
Old 03-11-2009
Oh, I didn't know that.

Am I right in thinking all I need to do is rename the CSV filename to match the table and then do the mysqlimport?

I've done some tests, I do not have the right priveleges for FILE with mysql, and I can't seem to change them either.

Last edited by worchyld; 03-11-2009 at 02:34 PM..
# 9  
Old 03-11-2009
Another easy option is to save your SQL query exactly as you would type it an the MySQL prompt in a text file, then:

Code:
mysql -u yourname -p < myquery.sql

If you can specify the filename in the query (and have permission), then you can do it that way.
# 10  
Old 03-12-2009
Oh, I will try your solution with a dummy table and hopefully it will work!

Thanks!
# 11  
Old 03-12-2009
Quote:
mysql -u yourname -p < myquery.sql
Can you could use this command to load CSV files? I'm going to investigate this.

The problem I see is that my CSV file isn't a SQL query, and secondly I do not have any control over the contents of the CSV file.

There are parts I need to change, such as the date which is stored as text and needs to be converted to a proper date format.

Also there are lines in the CSV file I want to ignore, such as the first 2 lines, and ensure it understands line breaks as being the end of a row.

I'm going to investigate whether you can use the command to load csv files.
# 12  
Old 03-12-2009
You can create a 'LOAD DATA INFILE' query and place it in the ".sql" file (the name is irrelevant). This is what I meant earlier, but I didn't specify that because I mistakenly thought your first post already mentioned it, but instead I see you wrote "MYSQL LOAD DATA," which may have been referring to something else.

The LOAD DATA INFILE syntax allows you to ignore any number of lines from the beginning of the file, and you don't have to convert a string date to a datetime if it's in a reasonable format.

Obviously you can tell the query that your file delimiter is a comma, and if you have strings which may include commas, use the ENCLOSED BY option.

MySQL :: MySQL 5.1 Reference Manual :: 12.2.6 LOAD DATA INFILE Syntax

You keep saying that you have no control over the CSV file. That's not true in any way. If you're able to write a script to rename it or move it to a temp folder, there's no reason why you can't run it through a little sed, awk, or Perl to do a little cleanup first. You're on a *nix command line -- you're empowered!
# 13  
Old 03-12-2009
Thanks for clarification! I appreciate it!
# 14  
Old 03-13-2009
Putting dates into paths

Quote:
Originally Posted by ShawnMilo
Another easy option is to save your SQL query exactly as you would type it an the MySQL prompt in a text file, then:

Code:
mysql -u yourname -p < myquery.sql

If you can specify the filename in the query (and have permission), then you can do it that way.
I've now had the chance to test this, it does indeed work! Thanks a lot! I want to thank you for your help, I really appreciate it. My CSV file extracts its contents into a date-based folder (Format: YYYY-MM-DD), what I'm wondering is, is it possible to tell my `csv_import.sql` (which is where my big LOAD DATA FILE sits) to use the latest date in the folder structure? I tried the following but MySQL complained that there was an error.
Code:
 LOAD DATA LOCAL INFILE '/path/to/inbounddata/data/%Y-%m-%d/traveldata.csv'

Is there a way of either, passing a date argument to my .sql file, or perhaps telling this command to use the latest date as part of the folder structure? Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parallel sqlldr to load data.

I am using SQLLDR to load data in DB.For parallel loading I'm using nohup command. The requirement is: I have different files within a directories. Ex: 1) Dir/folder_A/AE.txt 2) Dir/folder_A/DM.txt 3) Dir/folder_B/CM.txt I need to loop through directories and load the data... (1 Reply)
Discussion started by: Pratiksha Mehra
1 Replies

2. UNIX for Dummies Questions & Answers

Load UNIX data into excel sheet

Hi, i have some data in a temporary file in Unix (the data is taken from the result of an SQL query). Now i want to dump that data into an excel sheet. How to do that. Someone please advise. Thanks Regards, Vinit (3 Replies)
Discussion started by: vinit raj
3 Replies

3. Shell Programming and Scripting

Load data from a flat file to oracle.

I have a flat file with records like Header 123 James Williams Finance2000 124 Pete Pete HR 1500 125 PatrickHeather Engg 3000 Footer The structure is: Eno:4 characters Name:8 characters Surname : 9 characters Dept:7 characters Sal:4characters These are sample... (1 Reply)
Discussion started by: Shivdatta
1 Replies

4. Web Development

script to load data from csv file

hello i want a script to load the data line by line from a csv file into a mysql table (3 Replies)
Discussion started by: srpa01red
3 Replies

5. Shell Programming and Scripting

executing mysql load statement from shell script

Hi, I have a piece of shell script which will connect to mysql database and execute a load statement(which will load datas in a file to the database table).The code is working and the data is in the tables. Now my requirement is, i need to grab the output from the load statement... (4 Replies)
Discussion started by: DILEEP410
4 Replies

6. Shell Programming and Scripting

How to load different type of data in a file to two arrays

Hi, I have tried to find some sort of previous similar thread on this but not quite close to what I want to achieve. Basically I have two class of data in my file..e.g 1,1,1,1,1,2,yes 1,2,3,4,5,5,yes 2,3,4,5,5,5,no 1,2,3,4,4,2,no 1,1,3,4,5,2,no I wanted to read the "yes" entry to an... (5 Replies)
Discussion started by: ahjiefreak
5 Replies

7. Shell Programming and Scripting

Automatically Load data from all files in directory

I'm new in Unix shell scripting and i need someone to help me to make Script that run all time watching my directory that files uploaded to it via FTP (/mydir/incoming_files), if any files exists in it then (if many files exists, then sort files and load them ascending) it‘ll checks the size of the... (1 Reply)
Discussion started by: m_fighter
1 Replies

8. Programming

Load text file into a MySQL field

Hello, maybe this post is offtopic, sorry for the inconveniencies. I found examples about how to populate different fields from a text file (with MySQL, the LOAD DATA INFILE sentece), but not about how to load a complete plain text file into a concrete database field. Could you please tell me if... (1 Reply)
Discussion started by: aristegui
1 Replies

9. Shell Programming and Scripting

Need help in wrting Load Script for a Load-Resume type of load.

hi all need your help. I am wrting a script that will load data into the table. then on another load will append the data into the existing table. Regards Ankit (1 Reply)
Discussion started by: ankitgupta
1 Replies
Login or Register to Ask a Question