Guest User

Untitled

a guest
May 29th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.66 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3. $flag=1;
  4. class MyDB extends SQLite3{
  5.     function __construct(){
  6.         $file='orders.sqlite';
  7.         if($GLOBALS['flag']==0)
  8.             $this->open($file,SQLITE3_OPEN_READONLY);
  9.         else if(file_exists($file))
  10.             $this->open($file,SQLITE3_OPEN_READWRITE);
  11.         else{
  12.             $this->open($file,SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
  13.             $this->exec("PRAGMA journal_mode = wal");
  14.         }
  15.         $this->exec("PRAGMA synchronous = OFF");// WE MUST GO FASTER!
  16.         $this->enableExceptions(true);
  17.     }
  18. }
  19. $db=new MyDB();
  20. if(!$db)
  21.     die("Error: ".$db->lastErrorMsg());
  22. $sql=<<<EOF
  23. SELECT ForeignKey_A,
  24.        b.TimeStamp,
  25.        b.Location_ID,
  26.        e.TimeStamp,
  27.        e.Location_ID
  28. FROM (SELECT ForeignKey_A,
  29.              min(TimeStamp) AS TimeStamp,
  30.              Location_ID
  31.       FROM TableA
  32.       GROUP BY ForeignKey_A
  33.      ) AS b
  34. JOIN (SELECT ForeignKey_A,
  35.              max(TimeStamp) AS TimeStamp,
  36.              Location_ID
  37.       FROM TableA
  38.       GROUP BY ForeignKey_A
  39.      ) AS e
  40. USING (ForeignKey_A)
  41. LIMIT 5;
  42. EOF;
  43. $t=microtime(true);
  44. try{
  45.     $req=$db->query($sql);
  46. }
  47. catch(Exception $e){
  48.     echo "Error: ".$db->lastErrorMsg();
  49.     $db->close();
  50.     die("\n");
  51. }
  52. $T=microtime(true)-$t;
  53. echo "Query took $T seconds!\n\n";
  54. $t=microtime(true);
  55. $i=0;
  56. while ($row = $req->fetchArray(SQLITE3_ASSOC)) {
  57.     if($i==0){
  58.         foreach($row as $key => $val){
  59.             echo "$key\t";// Print Column Names
  60.         }
  61.         echo "\n";
  62.     }
  63.     foreach($row as $key => $val){
  64.         echo "$val\t";// Print Row Values
  65.     }
  66.     $i++;
  67.     echo "\n";
  68. }
  69. $db->close();
  70. echo "Total Rows Returned: $i\n\n";
  71. $t=microtime(true)-$t;
  72. echo "Fetching data took $t seconds!\n\n";
  73. $t=$t+$T;
  74. echo "Total Time: $t seconds!\n";
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment