par
moogli » 28 déc. 2011, 23:49
heu
http://dev.mysql.com/doc/refman/5.0/en/ ... icate.html ?
1er lien de
cette recherche
sinon pour la méthode avec procédure stockée
La table de test:
CREATE TABLE `furax69` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`numero` int(11) NOT NULL,
`budget` decimal(8,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
La procédure Stockée
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `procFurax69`(
IN num INT,
IN bud decimal(10,2),
out ret boolean)
BEGIN
declare nb int;
declare tmp boolean;
set @tmp := false;
select count(*) into nb from furax69 where numero = num ;
if nb = 0 then
insert into furax69 (numero,budget) value(num,bud);
set @tmp := true;
else
update furax69 set budget=bud where numero = num;
set @tmp := true;
end if;
select @tmp into ret;
END$$
DELIMITER ;
Le code php avec PDO
<?php
$fichiercsv = file('furax69.csv');
try {
$pdo = new PDO("mysql:host=localhost;dbname=test;charsey=utf8","root","yyRu2TKEvyYpzFLK");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('call procFurax69(:numero,:budget,@sortie)');
foreach($fichiercsv as $line){
$csv = str_getcsv($line, ';');
/*
$num = 12;
$bud = 45550.52;
*/
$stmt->bindParam(':numero', $csv[0], PDO::PARAM_INT);
$stmt->bindParam(':budget', $csv[1], PDO::PARAM_INT);
//$stmt->bindParam(':sortie', $retour, PDO::PARAM_STR,5);
$stmt->execute();
$s = $pdo->query('select @sortie')->fetch(PDO::FETCH_OBJ);
echo 'La procédure à retourné : ', $s->{'@sortie'};
}
}
catch (PDOException $e){
echo '<pre style="word-wrap:break-word;">';
echo $e->getMessage();
echo '</pre>';
}
?>
Le fichier de test
Code : Tout sélectionner
123456;45789.8
456789;789.5
7832;486.48
159753;486.58
458652;965.12
546;456.25
456.15;48996.52
résultat
Code : Tout sélectionner
mysql> select * from furax69;
+----+--------+----------+
| id | numero | budget |
+----+--------+----------+
| 1 | 123456 | 45789.80 |
| 2 | 456789 | 789.50 |
| 3 | 7832 | 486.48 |
| 4 | 159753 | 486.58 |
| 5 | 458652 | 965.12 |
| 6 | 546 | 456.25 |
| 7 | 456 | 48996.52 |
+----+--------+----------+
7 rows in set (0.00 sec)
un tuto sur les procédure stockée pour mysql
un tuto sur PDO
Ce code à le mérite d'être a peux prêt standard, donc fonctionnel quelque soit le SGBD
@+
heu [url]http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html[/url] ?
1er lien de [url=http://www.google.fr/search?q=mysql+on+duplicate+key]cette recherche[/url]
sinon pour la méthode avec procédure stockée
La table de test:
[sql]CREATE TABLE `furax69` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`numero` int(11) NOT NULL,
`budget` decimal(8,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1[/sql]
La procédure Stockée
[sql]-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `procFurax69`(
IN num INT,
IN bud decimal(10,2),
out ret boolean)
BEGIN
declare nb int;
declare tmp boolean;
set @tmp := false;
select count(*) into nb from furax69 where numero = num ;
if nb = 0 then
insert into furax69 (numero,budget) value(num,bud);
set @tmp := true;
else
update furax69 set budget=bud where numero = num;
set @tmp := true;
end if;
select @tmp into ret;
END$$
DELIMITER ;[/sql]
Le code php avec PDO
[php]<?php
$fichiercsv = file('furax69.csv');
try {
$pdo = new PDO("mysql:host=localhost;dbname=test;charsey=utf8","root","yyRu2TKEvyYpzFLK");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('call procFurax69(:numero,:budget,@sortie)');
foreach($fichiercsv as $line){
$csv = str_getcsv($line, ';');
/*
$num = 12;
$bud = 45550.52;
*/
$stmt->bindParam(':numero', $csv[0], PDO::PARAM_INT);
$stmt->bindParam(':budget', $csv[1], PDO::PARAM_INT);
//$stmt->bindParam(':sortie', $retour, PDO::PARAM_STR,5);
$stmt->execute();
$s = $pdo->query('select @sortie')->fetch(PDO::FETCH_OBJ);
echo 'La procédure à retourné : ', $s->{'@sortie'};
}
}
catch (PDOException $e){
echo '<pre style="word-wrap:break-word;">';
echo $e->getMessage();
echo '</pre>';
}
?>[/php]
Le fichier de test
[code]
123456;45789.8
456789;789.5
7832;486.48
159753;486.58
458652;965.12
546;456.25
456.15;48996.52
[/code]
résultat
[code]mysql> select * from furax69;
+----+--------+----------+
| id | numero | budget |
+----+--------+----------+
| 1 | 123456 | 45789.80 |
| 2 | 456789 | 789.50 |
| 3 | 7832 | 486.48 |
| 4 | 159753 | 486.58 |
| 5 | 458652 | 965.12 |
| 6 | 546 | 456.25 |
| 7 | 456 | 48996.52 |
+----+--------+----------+
7 rows in set (0.00 sec)[/code]
[url=http://www.siteduzero.com/tutoriel-3-34590-procedure-stockee.html]un tuto sur les procédure stockée pour mysql[/url]
[url=http://www.siteduzero.com/tutoriel-3-34790-pdo-interface-d-acces-aux-bdd.html]un tuto sur PDO[/url]
Ce code à le mérite d'être a peux prêt standard, donc fonctionnel quelque soit le SGBD ;)
@+