2014-10-21 89 views
0

我有這個數據庫類,當我正在使用的數據庫類型爲‘MySQL的’一切工作正常,但是從‘MySQL的’更改爲‘sqlite的’拋出錯誤的這一頁。PDO更改數據庫類型SQLITE拋出錯誤

以下是錯誤消息

Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[HY000]: General error: 1 no such table: users' in E:\xampp\htdocs\projects\test\pdo\database.php:33 Stack trace: #0 E:\xampp\htdocs\projects\test\pdo\index.php(3): db->select('SELECT * FROM u...') #1 {main} thrown in E:\xampp\htdocs\projects\test\pdo\database.php on line 33 

這是完整的類代碼。

class db{ 
    public $isConnected; 
    protected $datab; 

    /* Database Object */ 
    public function __construct($username, $password, $host, $dbname, $options=array()){ 
     $this->isConnected = true; 
     try { 
      $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
      $this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
      $this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
     } 
     catch(PDOException $e) { 
      $this->isConnected = false; 
      throw new Exception($e->getMessage()); 
     } 
    } 

    /* Disconnect from the database */ 
    public function disconnect(){ 
     $this->datab = null; 
     $this->isConnected = false; 
    } 

    /* Select Query Against the Database */ 
    public function select($query, $params=array()){ 
      try{ 
       $stmt = $this->datab->prepare($query); 
       $stmt->execute($params); 
       return $stmt->fetchAll();  
      }catch(PDOException $e){ 
       throw new Exception($e->getMessage()); 
      }  
    } 

    /* Insert Query Against the Database */ 
    public function insert($query, $params){ 
      try{ 
       $stmt = $this->datab->prepare($query); 
       $stmt->execute($params); 
      }catch(PDOException $e){ 
       throw new Exception($e->getMessage()); 
      }   
    } 

    /* Update Query Against the Database */ 
    public function update($query, $params){ 
      return $this->insert($query, $params); 
    } 

    /* Delete Query Against the Database */ 
    public function delete($query, $params){ 
      return $this->insert($query, $params); 
    } 
} 
$db = new db("root", "", "localhost", "test", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); 
+0

那些'MYSQL_ATTR_INIT_COMMAND'和''localhost「'呢? SQLite不能這樣工作。 – 2014-10-22 07:37:43

+0

討厭顯而易見 - 但是你有沒有一個表在你的sqlite數據庫中調用用戶?如果是這樣,那麼肯定是「$ this-> datab = new PDO(」mysql:host「不會想要mysql:host? – Ukuser32 2014-10-22 11:00:25

+0

是的,表確實存在。 – 2014-10-22 12:07:55

回答

0

我能通過註釋掉XAMPP上的pdo sqlite擴展來解決這個問題。