473,624 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

From CSV To MySQL Table

Hi, I've a CSV file with 3 columns

ID - Price1 - Price2

How I would like to update the records of a MySQL tables that has the
same ID of the CSV files.

How Can I do this? Read from CSV and update MySQL table?

Thanks
May 14 '07 #1
5 19078
Hi Valeria,

your idea/question to read from csv and update the table is the right
way.

Open your csv file and read each line using a loop. Check inside your
loop, if an entry in the database exists. if so update it, otherwhise
continue the loop or insert an entry.

See follwing pages for detailed information:
- http://de2.php.net/manual/en/function.fgetcsv.php
- http://de2.php.net/manual/en/ref.mysql.php or http://de2.php.net/manual/en/ref.mysqli.php

An simple example could be:
[snip]
$link = mysql_connect($ mysqlHost, $user, $password) or die('Could not
connect: ' . mysql_error());
$handle = fopen("myfile.c sv", "r");

// loop content of csv file, using comma as delemiter
while (($data = fgetcsv($handle , 1000, ",")) !== false) {
$id = (int) $data[0];
$price = floatval($data[1]);
$price2 = floatval($data[2]);

$query = 'SELECT id FROM my_table';
if (!$result = mysql_query($qu ery)) {
continue;
}

if ($line = mysql_fetch_arr ay($result, MYSQL_ASSOC)) {
// entry exists update
$query = "UPDATE my_table SET price='$price', price2='price2'
WHERE id=$id";
mysql_query($qu ery);
if (mysql_affected _rows() <= 0) {
// no rows where affected by update query
}
} else {
// entry don't exists continue or insert...
}

mysql_free_resu lt($result);
}

fclose($handle) ;
mysql_close($li nk);
[/snap]

purcaholic

May 14 '07 #2
your idea/question to read from csv and update the table is the right
way.

Open your csv file and read each line using a loop. Check inside your
loop, if an entry in the database exists. if so update it, otherwhise
continue the loop or insert an entry.
As an alternative, you can define a (temporary) table based on your CSV
file in MySQL (you'll have to upload the csv file to the database
server), and use an update statement. MySQL has a CSV engine for these
purposes. If this is a regular action and not a one-time only action,
you'll be better of using PHP though.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
May 14 '07 #3
In article <46************ ***********@new s.xs4all.nl>, Willem Bogaerts
wrote:
If this is a regular action and not a one-time only action,
you'll be better of using PHP though.
Why so? I would have thought it would be the reverse.
Surely such a simple (looking) update as this would be quicker and more
efficient joining tables, even temporary ones, than in php?

Regards
Mark

May 14 '07 #4
Why so? I would have thought it would be the reverse.
Surely such a simple (looking) update as this would be quicker and more
efficient joining tables, even temporary ones, than in php?
If you would do this from a CRON job or a PHP page, it needs the rights
to create a table. That is something I like to avoid. A one-time action
as an administrator is less dangerous, as you don't "run unattended"
yourself.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
May 14 '07 #5
In article <46************ *********@news. xs4all.nl>, Willem Bogaerts wrote:
If you would do this from a CRON job or a PHP page, it needs the rights
to create a table. That is something I like to avoid. A one-time action
as an administrator is less dangerous, as you don't "run unattended"
yourself.
Ah, I see. Thank you for the explanation.

Regards
Mark

May 15 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5853
by: Dave | last post by:
I have a MS sql 2000 db that needs to sync with a Mysql db. When a password is changed in the MS SQL table, I need to make sure that the same value is updated in the Mysql db. I dont have any control of the gui that is initiating this event. So, I was thinking a trigger might be an alternate route. I just dont know how to get the username and password values that have just been updated from MS sql to Mysql.
2
3238
by: Raffi | last post by:
I use the --all-databases switch to backup my entire database. Sometimes there's a need to restore individual databases or tables form the backup file. What command should I use for this? Thanks, Raffi
34
5035
by: Karam Chand | last post by:
Hello I have been working with Access and MySQL for pretty long time. Very simple and able to perform their jobs. I dont need to start a flame anymore :) I have to work with PGSQL for my companies current project. I have been able to setup postgresql in my rh box and
48
3846
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a query of all students in both tables with no duplicates. No clue whatsoever.
1
2821
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work. Despite entering the required lines to "my.ini" (the new my.cnf) through notepad AND MySQL Administrator, the cache does not work. So, today I took a peek at the 'Health' tab in MySQL Administrator.
25
2240
by: frizzle | last post by:
Hi there, I have a mySQL system with a news publishing part in it: Admins can create new items with text in it, and they have an option to create 'fulltexts', so you'd get "read more ..." on the front page, click it and read the fulltext. Is there a possibility for mySQL (query) to check if 'fulltext' is empty or not, and only return true or false, so i don't have to put the whole fulltext into the mysql_fetch_array() to decide wether...
15
4591
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
2
2154
by: bwana.mpoa | last post by:
Hi, We're using a mySQL database as a replica of another (Sybase) DB for reporting purposes. The Sybase is part of a real-time mission critical system - hence the separate database where people can run their queries on. Currently a separate application is copying the information we're interested in from Sybase to mySQL in an overnight batch-process. We want to extend this to loading every 15 minutes and are now investigating the...
7
4182
by: Randy | last post by:
Folks: We have a web-based app that's _really_ slowing down because multiple clients are writing their own private data into a single, central database. I guess the previous programmer did things this way because it made things easy. Well, I'm the person that has to put up with the long-term headache. Anywho, someone at work wants things sped up, so what I'm looking at doing is this for each client:
2
3888
by: amit2781 | last post by:
Hi, I have created 4 tables in 'amit' database and then I deleted them. Still I able to get information about the table_schema for the table deleted. After drop table when I fire a query for table_schema from information_schema.tables it will give me result as 'amit'. I didn't understand why it is happnes. See the below steps done. mysql> use amit; mysql> show tables; +----------------+
0
8681
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.