Created
April 24, 2020 16:58
-
-
Save leocavalcante/c6206d1cff5012620d27f6af87470cb2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$input = fopen('data.csv', 'r'); | |
$output = fopen('output.csv', 'w'); | |
$pdo = new PDO('sqlite:db.sqlite'); | |
if (false === $pdo->exec('create table if not exists foo (bar varchar, baz varchar)')) { | |
print_r($pdo->errorInfo()); | |
exit(1); | |
} | |
if (false === ($stmt = $pdo->prepare('insert into foo (bar, baz) values (? , ?)'))) { | |
print_r($pdo->errorInfo()); | |
exit(1); | |
} | |
while ($row = fgetcsv($input, 0, ';')) { | |
$result = $stmt->execute($row); | |
if ($result) { | |
fputcsv($output, array_merge($row, ['OK'])); | |
} else { | |
fputcsv($output, array_merge($row, [$stmt->errorCode()])); | |
} | |
} | |
fclose($output); | |
fclose($input); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment