insert_stmt.php 1.5 KB
Newer Older
weixin_47267244's avatar
weixin_47267244 已提交
1 2 3 4 5 6
<?php

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

try {
D
dingbo 已提交
7
    // instantiate
weixin_47267244's avatar
weixin_47267244 已提交
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
weixin_47267244's avatar
weixin_47267244 已提交
16 17
    $connection->connect();

D
dingbo 已提交
18
    // insert
weixin_47267244's avatar
weixin_47267244 已提交
19 20 21 22
    $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)');
    $stmt = $connection->prepare('INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)');

D
dingbo 已提交
23
    // set table name and tags
weixin_47267244's avatar
weixin_47267244 已提交
24
    $stmt->setTableNameTags('d1001', [
25
        // same format as parameter binding
D
dingbo 已提交
26
        [TDengine\TSDB_DATA_TYPE_BINARY, 'California.SanFrancisco'],
weixin_47267244's avatar
weixin_47267244 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        [TDengine\TSDB_DATA_TYPE_INT, 2],
    ]);

    $stmt->bindParams([
        [TDengine\TSDB_DATA_TYPE_TIMESTAMP, 1648432611249],
        [TDengine\TSDB_DATA_TYPE_FLOAT, 10.3],
        [TDengine\TSDB_DATA_TYPE_INT, 219],
        [TDengine\TSDB_DATA_TYPE_FLOAT, 0.31],
    ]);
    $stmt->bindParams([
        [TDengine\TSDB_DATA_TYPE_TIMESTAMP, 1648432611749],
        [TDengine\TSDB_DATA_TYPE_FLOAT, 12.6],
        [TDengine\TSDB_DATA_TYPE_INT, 218],
        [TDengine\TSDB_DATA_TYPE_FLOAT, 0.33],
    ]);
    $resource = $stmt->execute();

D
dingbo 已提交
44
    // get affected rows
weixin_47267244's avatar
weixin_47267244 已提交
45 46
    var_dump($resource->affectedRows());
} catch (TDengineException $e) {
D
dingbo 已提交
47
    // throw exception
weixin_47267244's avatar
weixin_47267244 已提交
48 49
    throw $e;
}