Solved

LOAD DATA LOCAL INFILE

Posted on 2015-01-19
1
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
This video teaches users how to migrate an existing Wordpress website to a new domain.
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…

734 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