【问题标题】:PDO changing database type to SQLITE throws errorPDO 将数据库类型更改为 SQLITE 会引发错误
【发布时间】:2014-12-17 08:43:29
【问题描述】:

我有这个数据库类,当我将数据库类型用作“mysql”时,一切正常,但从“mysql”更改为“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'));

【问题讨论】:

  • MYSQL_ATTR_INIT_COMMAND"localhost" 呢? SQLite 不能那样工作。
  • 讨厌问显而易见的问题-但是您的 sqlite 数据库中有一个名为 users 的表吗?如果是这样,那么肯定 "$this->datab = new PDO("mysql:host" 不会想要 mysql:host??
  • 是的,该表确实存在。

标签: php mysql sqlite pdo


【解决方案1】:

我能够通过注释掉 XAMPP 上的 pdo sqlite 扩展来解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2021-07-17
    • 2016-05-11
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2013-12-02
    相关资源
    最近更新 更多