PDO将数据库类型更改为SQLITE会引发错误

I have this database class, when I' am using Database Type as "mysql" everything works fine but changing from "mysql" to "sqlite" throws error on the page.

Here is the error message

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

This is the complete Class Code.

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'));
duanqiang5722
duanqiang5722 是的,表确实存在。
接近 6 年之前 回复
dtwxt88240
dtwxt88240 讨厌明白-但你的sqlite数据库中是否有一个名为users的表?如果是这样,那么肯定“$this->datab=newPDO(”mysql:host“不会想要mysql:host??
接近 6 年之前 回复
dongxiequ3724
dongxiequ3724 那些MYSQL_ATTR_INIT_COMMAND和“localhost”怎么样?SQLite不会那样工作。
接近 6 年之前 回复

1个回答



我能够通过在XAMPP上注释掉pdo sqlite扩展来解决这个问题。</ p>
</ div>

展开原文

原文

I was able to solve this issue by commenting out the pdo sqlite extensions on XAMPP.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐