Jump to content

Importing CSV file to database


dc_jt

Recommended Posts

If you are using mysql database, this query will do the trick

 

LOAD DATA INFILE "C:\\YOURFILE.CSV" INTO TABLE YOURTABLE FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';

 

NOTE: Here you are using  '  ,  ' as delimiter for fields, this means if some field in your tuple/ row of csv file has data such as text which in turn has comma in it, it will be broken down into two fields. So "condition" your csv file before you use this query.

 

for simple example, try to import a file with these two lines in a test table with three fileds

 

1234,abcd,blue  :  will go ok

1234,ab,cd,blue  :  will no be imported as it will break ab,cd (which is one field in example) into two fields

 

goodluck

 

 

Link to comment
Share on other sites

  • 1 month later...

Can I put the csv file on my server and load it that way instead of from my C drive?

 

Also, do the field names in the csv file have to match the sql table, or does it just need to be the same number of fields per row?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.