How to import CSV into MySQL
Hi, im quite a newbie when it comes to MySQL but i heard that its possible to import data from a CSV into a mysql table.
Can anyone enlighten me on how to do it please? Im running CentOS and usiong MySQL Workbench to connect and run queries. I have my CSV formatted so that the first row has the names of my columns on the table that I want to import it to and the following rows are the data that needs importing. Cheers, Chris, |
Quote:
output to file: select * from address into outfile '/tmp/filename.csv' fields terminated by ',' ; load data infile '/tmp/filename.csv' into table address fields terminated by ','; I usually tries things like this on a testdatabase first time I do it and not on production servers :-) |
|
Quote:
Ive tried the following from http://benrobb.com/2007/01/15/howto-...cess-to-mysql/ Code:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' Code:
bind-address = 127.0.0.1 Code:
[mysqld] |
Please, can anyone help?
Thanks. |
I'm not familiar with CentOS and what version of mysql it contains but
this is for mysql 5 http://dev.mysql.com/doc/refman/5.0/...rmissions.html Nils. |
This might be precisely what you don't want, but on my development server I simply become root, then copy the .csv file (usually saved out from OOCalc with default oocalc CSV options) into the same folder where my running MySQL instance stores its .myd etc. files.
Then I get into mysql (mysql -uuser -ppassword database) and execute Code:
load data infile 'psms_contacts.csv' into table psms_contacts fields terminated by ',' enclosed by '"' lines terminated by '\n'; psms_contacts.csv is the file I copied into my MySQL data directory, and psms_contacts is the table that I want this .csv to go into. After of course first making sure the table mentioned has columns that are of types compatible with the content of each relevant column in the .csv file. Note that this is a shortcut, and risky (some would say godawfully stupid) way of doing it - you're not supposed to ever do stuff like this as root. But heck it works, and its quick, and I don't have wrestle as much. Plus my system is a dev system, completely isolated, and all the data on it is expendable. Then I just delete the .csv file from the MySQL data directory. Note this is risky too, delete the wrong file and you can end up crashing your MySQL instance and losing data. For example, in the above, I need to delete psms_contacts.csv NOT EVER: psms_contacts.myd (etc.) as those are MySQL datafiles. |
All times are GMT -5. The time now is 08:29 AM. |