简体   繁体   中英

PostGreSQL CREATE TABLE with PDO PHP

I try to do a simple create table in my PostgreSQL datatable, by executing the createTables function bellow:

    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();
        }
    }

But I still have:

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... ^ 

And I don't know why ?...

Thanks for help

There is no autoincrement in postgresql. In instead use serial . And the integer type is always 4 bytes.

test (id serial PRIMARY KEY,

The serial type implies integer with a not null constraint and an attached sequence.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM