insert.php 1.5 KB
Newer Older
1 2 3 4 5 6
<?php

use TDengine\Connection;
use TDengine\Exception\TDengineException;

try {
D
dingbo 已提交
7
    // instantiate
8 9 10 11 12 13 14
    $host = 'localhost';
    $port = 6030;
    $username = 'root';
    $password = 'taosdata';
    $dbname = 'power';
    $connection = new Connection($host, $port, $username, $password, $dbname);

D
dingbo 已提交
15
    // connect
16 17
    $connection->connect();

D
dingbo 已提交
18
    // insert
19 20 21
    $connection->query('CREATE DATABASE if not exists power');
    $connection->query('CREATE STABLE if not exists meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)');
    $resource = $connection->query(<<<'SQL'
D
dingbo 已提交
22 23 24 25
    INSERT INTO power.d1001 USING power.meters TAGS(California.SanFrancisco, 2) VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000) ('2018-10-03 14:38:15.000', 12.60000, 218, 0.33000) ('2018-10-03 14:38:16.800', 12.30000, 221, 0.31000)
    power.d1002 USING power.meters TAGS(California.SanFrancisco, 3) VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000)
    power.d1003 USING power.meters TAGS(California.LosAngeles, 2) VALUES ('2018-10-03 14:38:05.500', 11.80000, 221, 0.28000) ('2018-10-03 14:38:16.600', 13.40000, 223, 0.29000)
    power.d1004 USING power.meters TAGS(California.LosAngeles, 3) VALUES ('2018-10-03 14:38:05.000', 10.80000, 223, 0.29000) ('2018-10-03 14:38:06.500', 11.50000, 221, 0.35000)
26 27
    SQL);

D
dingbo 已提交
28
    // get affected rows
29 30
    var_dump($resource->affectedRows());
} catch (TDengineException $e) {
D
dingbo 已提交
31
    // throw exception
32 33
    throw $e;
}