DISQUS

Tech-Recipes: Import CSV file directly into MySQL | MySQL | Tech-Recipes

  • Rafael · 1 year ago
    Thank you. Short and sweet. Very straight to the point.
  • Ng · 1 year ago
    Thanks, buddy!
  • robert · 1 year ago
    thanks pal! finally somethin' working :)
  • ashikur · 1 year ago
    Thank you!!! it really works !!!

    Copied the csv file into /var/lib/mysql/mydb at my ubuntu box.

    *mydb is the name of my database
  • Chris · 11 months ago
    Thank you Thank you. Very nice.
  • priyadarshan · 11 months ago
    very very superb 1 line query.. i love it !!!!!!!!!!!!!!
  • ir · 9 months ago
    help!! can this be possible??
    (idnum,username,md5(password))
    how about if i used md5 in my password??
    and i want my password field converted into md5.
    email me pls. ir_bs756@yahoo.com
  • ashish · 9 months ago
    hey
    please send full code of importing file into mysql database. at my email (ashish@tiainterweb.com)
    thanks
  • Okpali D Great · 6 months ago
    the above code is can do it. You need to specify the url of the csv file with / slash and not \ .
  • zild · 9 months ago
    Can MySQL automatically assign the column names using the column headers from the csv file?
  • vasu · 7 months ago
    where to place the csv file...
  • Partha Kar · 7 months ago
    Thank you very much.
  • Dave · 6 months ago
  • Dave · 6 months ago
    @ir:
    If the passwords in your CSV file are not already MD5() then just import them as per the instructions above then run this query:

    UPDATE `users` SET `password` = MD5(`password`);

    @ashish:
    That is the "full code". There is no more. The only step you have to do before this is to create a table that matches the CSV file you already have. No one can tell you how to do that without having your CSV file. Don't forget to modify the query above to match the table you have just created.

    @zlid:
    No it can't. Fields are given names and types at the same time when the table is created. To assign the names automatically, MySQL would have to also assign the column types automatically which would mean scanning down through the entire CSV file to make sure that there were no letters in a column that started with numbers and finding the longest string in a column that started with a string and so on. There are too many options in table creation to allow it to be left up to software. NULL or NOT NULL ? DATE, DATETIME, TIME, CHAR, VARCHAR or TEXT ? What if you have a column full of numbers but one Excel error message that looks like "#value" ?

    You will have to create the table yourself before importing the CSV file.
  • Okpali D Great · 6 months ago
    thanks very much
  • Mindy · 4 months ago
    Ok, I must be missing something here. Are there more explicit instructions for this? Do I paste this code using phpmyadmin and putting it into the sql box? I'm a little lost here.
  • Mindy · 4 months ago
    I wanted to add the fact that I already have my database set up in MySQL with all the appropriate tables. I created a test CSV file and messed around with it a bit but wasn't sure where and how I was supposed to implement it using the code you have given.
  • Gopi · 4 months ago
    Thanks kaly...
  • jaky · 2 months ago
    Thank you mate!
  • Sari · 2 months ago
    Thank You!!! I have 7 MB CSV file successfully imported to Mysql in 1.6 seconds!
  • eqcc · 1 month ago
    Thanks for the info! It worked perfectly! Thanks again!
  • akkhra · 1 month ago
    Thanks, it is so important. But, I have 5 columns, it doesn't complet, it left the last column. It emty.Could you help me.
  • ecalvillo · 1 day ago
    Thank you very much for this excelent recomendation, it works good.

    Ecalvillo.