PostGreSQL使用PDO创建表


PostGreSQL CREATE TABLE with PDO PHP

我尝试在我的PostgreSQL数据表中创建一个简单的表,通过执行下面的createttables函数:

    public function createTables() {
        try {
            $db = new PDO('pgsql:host='.$this->PARAM_hote.';port='.$this->PARAM_port.';dbname='.$this->PARAM_nom_bd.';user='.$this->PARAM_utilisateur.';password='.$this->PARAM_mot_passe);
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql ='CREATE TABLE IF NOT EXISTS test (id INT(11) AUTO_INCREMENT PRIMARY KEY, prename VARCHAR(50) NOT NULL);';
            $db->exec($sql);
        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    }

但我仍然有:

testSQLSTATE[42601]: Syntax error: 7 ERREUR: erreur de syntaxe sur ou près de « ( » LINE 1: CREATE TABLE IF NOT EXISTS test (id INT(11) AUTO_INCREMENT P... ^ 

我不知道为什么…

Thanks for help

postgresql不支持自增。用serial代替。并且integer类型总是4字节。

test (id serial PRIMARY KEY,

serial类型意味着带有not null约束和附加序列的整数。

http://www.postgresql.org/docs/current/static/datatype-numeric.html DATATYPE-SERIAL