简体   繁体   中英

How to create table from PDO in yii2?

Hy guys, How to create table in PDO yii2? this code in my controller

$db = new yii\db\Connection([
        'dsn' => 'pgsql:host=localhost;dbname=mydata',
        'username' => 'local',
        'password' => 'bukapeta',
        'charset' => 'utf8',
    ]);


 $commandR = "CREATE TABLE mencoba(nama_tempat TEXT ,tanggal TEXT ,waktu TEXT ,alamat TEXT ,jenis_tempat TEXT ,keterangan TEXT ,foto_lokasi TEXT ,latitude TEXT ,longitude TEXT ,tanggal_buat TEXT ,update_terakhir TEXT ,pembuat TEXT);"
 $command = $db->createCommand($commandR);
 $command->execute();

在此处输入图片说明

but this error please help me..

add $db->open()

$db = new \yii\db\Connection([
    'dsn' => 'pgsql:host=localhost;dbname=mydata',
    'username' => 'local',
    'password' => 'bukapeta',
    'charset' => 'utf8',
]);
$db->open();

You can use the method createTable of the queryBuilder . For example :

$queryBuilder = rows = (new \yii\db\Query());
$sql = $queryBuilder->createTable('myTable', [
    'id' => 'pk',
    'myColumn' => 'string',
    'myOtherColumn' => 'number'
]);

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