SHARE
TWEET

PDO Sqlite bug

a guest Aug 9th, 2016 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. function connect($dsn) {
  3.     try { $pdo = new \PDO($dsn); }
  4.     catch (\PDOException $e) { die('PDO: ' . $e->getMessage() . PHP_EOL); }
  5.     return $pdo;
  6. }
  7.  
  8. function doQuery($pdo, $sql) {
  9.     if ( ($result = $pdo->query($sql)) === false) {
  10.         echo "'$sql' failed: " . print_r($pdo->errorInfo(), true) . PHP_EOL;
  11.     }
  12.     return $result;
  13. }
  14.  
  15. function create($pdo) {
  16.     doQuery($pdo, 'DROP TABLE IF EXISTS public.foo');
  17.     doQuery($pdo, 'CREATE TABLE public.foo ( ii int )');
  18.     doQuery($pdo, 'INSERT INTO public.foo VALUES (42)');
  19. }
  20.  
  21. $pgo = connect('pgsql:host=localhost;dbname=postgres');
  22. $sqo = connect('sqlite::memory:');
  23. doQuery($sqo, "ATTACH DATABASE ':memory:' AS public;") or die();
  24.  
  25. create($pgo);
  26. create($sqo);
  27.  
  28. $pgResult = doQuery($pgo, 'SELECT COUNT(ii) FROM foo');
  29. echo 'Postgres: ';
  30. var_dump($pgResult->fetchColumn());
  31.  
  32. $ltResult = doQuery($sqo, 'SELECT COUNT(ii) FROM foo');
  33. echo 'Sqlite3:  ';
  34. var_dump($ltResult->fetchColumn());
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top