Solved

LOAD DATA LOCAL INFILE

Posted on 2015-01-19
1
140 Views
Last Modified: 2015-01-21
I found this code which seems to work but I need to make sure it does not add duplicate records and that it updates existing records with like key values.

Will the code below Insert only or will it Replace the records with same primary key.
If not whats the mysql statement to Replace when doing a LOAD DATA INLINE??


========== IMPORT RECORDS INTO MYSQL FROM CSV FILE  ===================



    $csvfile1 = 'property_a.csv';
   
    $sql = mysql_query("LOAD DATA LOCAL INFILE '$csvfile1' INTO TABLE table_a FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES
TERMINATED BY '\n' IGNORE 1 LINES") or die (mysql_error());
    if (!$sql) {        // problem
        echo mysql_error() ."<br>";
    } else {
        echo "success!";

        }
0
Comment
Question by:leadcodev
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40559193
just apply REPLACE:
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
he REPLACE and IGNORE keywords control handling of input rows that duplicate existing rows on unique key values:

    If you specify REPLACE, input rows replace existing rows. In other words, rows that have the same value for a primary key or unique index as an existing row. See Section 13.2.7, “REPLACE Syntax”. 

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now