Solved

LOAD DATA LOCAL INFILE

Posted on 2015-01-19
1
159 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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…

688 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