1

the data base data is stored as the image shows. the table is products.

enter image description here

the data in excel is as the following shows. enter image description here

but when i open it by notepad++. it shows like this:

enter image description here

1, the data's format which i prepared is right? if not? how to correct it?

  1. supposed the csv filed named product.csv. when i using LOAD DATA INFILE .when should i put the csv file into? could i load it from my local computer to my site's server?

i want to update the price, if the price is null, then using 0 to fill. this is my command. but it doesn't work

 LOAD DATA  LOCAL INFILE 'D:\\product.csv' REPLACE INTO TABLE products
    FIELDS TERMINATED BY '  '
    LINES TERMINATED BY '\r\n'
    IGNORE 1 LINES
    (products_model, @var1)
    SET products_price = IF(@var1 = '', 0, @var1); -- Replace empty string with 0

i put the above command in phpmyadmin, it can run, but the data doesn't be updated. what's wrong with it?

ps: is there a way to use PHP to get this? if have, how to do it?

4
  • dev.mysql.com/doc/refman/5.1/en/load-data.html Commented Nov 4, 2011 at 1:54
  • the data's format which i prepared is right? if not? how to correct it? Commented Nov 4, 2011 at 1:58
  • 1
    CSV format is the right format, Also, PHPmyadmin has a way to import Excel as Excel (Although this might be a tool inside Mysql and not phpmyadmin...) Commented Nov 4, 2011 at 2:01
  • when i using the command, but it doesn't work, what's wrong with it? Commented Nov 4, 2011 at 2:32

2 Answers 2

1

I haven't used the setup you're working with, but it looks like the code is trying to read a tab-delimited file. The file is comma-delimited.

So, maybe all you need to do is change:

FIELDS TERMINATED BY '  '

to

FIELDS TERMINATED BY ','
Sign up to request clarification or add additional context in comments.

5 Comments

when i change it, then run the command, the products_price still no updated.
REPLACE INTO TABLE products...is REPLACE right? i want to update the price.
Look at the documentation for the functions you're working with. Understand them, then change your parameters to give them the information they need.
i have read it lots of times,but still don't know how to use thm
Maybe you should ask multiple specific questions about the parts you don't understand. Figuring it out on your own because you understand the API will be much more valuable that someone solving the problem for you.
1

Probably this issue is solved already, but just to give some extra input (also for others who visit this page):

Did you check your excel sheet? It seems that you have your data (price) in a different column than your sql database. In excel the price is located in sixth column, in your sql it is located in the THIRD column... you can see that in your notepad csv file. Each comma represent a column.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.