Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- $flag=1;
- class MyDB extends SQLite3{
- function __construct(){
- $file='orders.sqlite';
- if($GLOBALS['flag']==0)
- $this->open($file,SQLITE3_OPEN_READONLY);
- else if(file_exists($file))
- $this->open($file,SQLITE3_OPEN_READWRITE);
- else{
- $this->open($file,SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
- $this->exec("PRAGMA journal_mode = wal");
- }
- $this->exec("PRAGMA synchronous = OFF");// WE MUST GO FASTER!
- $this->enableExceptions(true);
- }
- }
- $db=new MyDB();
- if(!$db)
- die("Error: ".$db->lastErrorMsg());
- $sql=<<<EOF
- SELECT ForeignKey_A,
- b.TimeStamp,
- b.Location_ID,
- e.TimeStamp,
- e.Location_ID
- FROM (SELECT ForeignKey_A,
- min(TimeStamp) AS TimeStamp,
- Location_ID
- FROM TableA
- GROUP BY ForeignKey_A
- ) AS b
- JOIN (SELECT ForeignKey_A,
- max(TimeStamp) AS TimeStamp,
- Location_ID
- FROM TableA
- GROUP BY ForeignKey_A
- ) AS e
- USING (ForeignKey_A)
- LIMIT 5;
- EOF;
- $t=microtime(true);
- try{
- $req=$db->query($sql);
- }
- catch(Exception $e){
- echo "Error: ".$db->lastErrorMsg();
- $db->close();
- die("\n");
- }
- $T=microtime(true)-$t;
- echo "Query took $T seconds!\n\n";
- $t=microtime(true);
- $i=0;
- while ($row = $req->fetchArray(SQLITE3_ASSOC)) {
- if($i==0){
- foreach($row as $key => $val){
- echo "$key\t";// Print Column Names
- }
- echo "\n";
- }
- foreach($row as $key => $val){
- echo "$val\t";// Print Row Values
- }
- $i++;
- echo "\n";
- }
- $db->close();
- echo "Total Rows Returned: $i\n\n";
- $t=microtime(true)-$t;
- echo "Fetching data took $t seconds!\n\n";
- $t=$t+$T;
- echo "Total Time: $t seconds!\n";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment