Login   Register  
PHP Classes
elePHPant
Icontem

File: cls.SQLite.DAO.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Pieter huiberts  >  SQLite Wrap  >  cls.SQLite.DAO.php  >  Download  
File: cls.SQLite.DAO.php
Role: Class source
Content type: text/plain
Description: SQLite wrapper class
Class: SQLite Wrap
Wrapper to access SQLite databases using PDO
Author: By
Last change:
Date: 2 years ago
Size: 2,708 bytes
 

Contents

Class file image Download
<?php
class SQLiteWrap {
    
/***************************************************************************************

    Wrapper class for SQLite communications
    
    example for SELECT query:
        $myData = new SQLiteWrap($DbLoc, $SQL_str);
        $myData-> runQuery();
        $bodyArray = $myData->getQryResults();
    
    example for INSERT/UPDATE/DELETE/CREATE TABLE queries
        $myData = new SQLiteWrap($DbLoc, $SQL_str);
        $myData-> execQuery();
        $bodyArray = $myData->getQryResults();  --> should return osingle piece of data.
    
    - based on source: http://henryranch.net/software/ease-into-sqlite-3-with-php-and-pdo/

    ----------------------------------------------------------------------------------------
    Date        Author    Comments
    ----------------------------------------------------------------------------------------
    07-aug-11    PJH        Created Class
    
    ***************************************************************************************/
    
    
private $dbLoc// = "../SQLite/FOAM.sqlite";
    
private $SQLStr;
    private 
$qryResults// multi dim-array
    
private $dbPDO;
    
    private function 
setQryResults($value){
        
$this->qryResults $value;
    }
    public function 
getQryResults() {
        return 
$this->qryResults;
    }

    
// Initialize a new instance of tables    
    
function __construct($dbLoc$SQL_str) {
        
$this->dbLoc $dbLoc;
        
$this->dbPDO = new PDO("sqlite:".$this->dbLoc);
        
$this->SQLStr $SQL_str;
    }
    
    public function 
runQuery() {        
        
// run your SELECT query
        
SQLiteWrap::openDB();
        
$db $this->dbPDO;
        
        
// for troubleshooting query problems. 
        // maybe setup a bit to turn it on/off
        
$db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);

        
// execute query
        
$result $db->query($this->SQLStr) or die("<font color='red'>query failed!</font>");
        
        
//push query result in array
        
$myArray = array();
        
        foreach(
$result as $row) {
          
$myArray[] = $row;
        }
                
        
$this->setQryResults($myArray);
        
        
// close the database connection
        
SQLiteWrap::closeDB();

    }

    public function 
ExecQuery() {        
        
// run all other queries
        
$db $this->dbPDO;
        
SQLiteWrap::openDB();
        
        
// execute query
        
$result $db->exec($this->SQLStr);

        
//push query result in array
        
foreach($result as $row){
            
$bodyArray[] = $row;
        }
        
        
// close the database connection
        
SQLiteWrap::closeDB();
        
        
$this->setQryResults($bodyArray);
    }
    
    private function 
closeDB() {
        
// close the database connection
        
$this->dbPDO NULL;
    }

    private function 
openDB() {
        
// open the database connection
        
$db $this->dbPDO;
        if (!@
db) {
            die(
"<font color=red>DB (".$this->dbLoc.") not found.</font>");
        }
    }
}