Bonjour,
J'ai un tableau de données .CSV que je dois importer dans une table.
Extrait du fichier csv :
matricule;nom
1;toto
2;tata
3;titi
J'ai essayé d'utiliser psqlCopyFromArray mais j'ai en retour le message suivant :
Fatal error: Uncaught PDOException: SQLSTATE[22P04]: Bad copy file format: 7 ERROR: literal newline found in data HINT: Use "\n" to represent newline.
Voici ma méthode :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| require_once 'connect_bd.php' // connection à la base de données
$filename = $_FILES['userfile']['tmp_name']; //recuperation du fichier envoyé en POST via le formulaire
//Stockage des données dans un tableau et suppression des éventuelles lignes vides
$data=[];
$file=fopen($filename,"r");
while (!feof($file)){
$ligne= fgets($file,1024);
array_push($data,$ligne);
}
print_r($data) //=> Array ( [0] => matricule;nom [1] => 1;TOTO [2] => 2;tata [3] => 3;titi )
$fields=array_shift($data); // Retrieve & remove the field list
$fields=str_replace(";",",",$fields);
echo($fields); //=>matricule,nom
print_r($data); //=> Array ( [0] => 1;TOTO [1] => 2;tata [2] => 3;titi )
$delimiter = ";";
$null_as=null;
$result=$pdo->pgsqlCopyFromArray('geolis.r_import',$data,$delimiter,$null_as,$fields); |
Message d'erreur :
Fatal error: Uncaught PDOException: SQLSTATE[22P04]: Bad copy file format: 7 ERROR: literal newline found in data HINT: Use "\n" to represent newline. CONTEXT: COPY r_import, line 3 in C:\ms4w\Apache\htdocs\cyneclic\fdcm\www\espace_prive\geolis\geolis_query2.php:45 Stack trace: #0 C:\ms4w\Apache\htdocs\cyneclic\fdcm\www\espace_prive\geolis\geolis_query2.php(45): PDO->pgsqlCopyFromArray('geolis.r_import', Array, ';', '', 'matricule,nom\r\n') #1 {main} thrown in C:\ms4w\Apache\htdocs\cyneclic\fdcm\www\espace_prive\geolis\geolis_query2.php on line 45
Si quelqu'un peut me donner une piste pour résoudre ce problème, je suis preneur.
Bon courage à tous
Partager