/* * Copyright (c) 2019 TAOS Data, Inc. * * This program is free software: you can use, redistribute, and/or modify * it under the terms of the GNU Affero General Public License, version 3 * or later ("AGPL"), as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ // TAOS standard API example. The same syntax as MySQL, but only a subset // to compile: gcc -o stmt_insert_demo stmt_insert_demo.c -ltaos #include #include #include #include #include "taos.h" /** * @brief execute sql only. * * @param taos * @param sql */ void executeSQL(TAOS *taos, const char *sql) { TAOS_RES *res = taos_query(taos, sql); int code = taos_errno(res); if (code != 0) { printf("%s\n", taos_errstr(res)); taos_free_result(res); taos_close(taos); exit(EXIT_FAILURE); } taos_free_result(res); } /** * @brief check return status and exit program when error occur. * * @param stmt * @param code * @param msg */ void checkErrorCode(TAOS_STMT *stmt, int code, const char *msg) { if (code != 0) { printf("%s. code: %d, error: %s\n", msg,code,taos_stmt_errstr(stmt)); taos_stmt_close(stmt); exit(EXIT_FAILURE); } } typedef struct { int64_t ts; float current; int voltage; float phase; } Row; int num_of_sub_table = 10; int num_of_row = 10; /** * @brief insert data using stmt API * * @param taos */ void insertData(TAOS *taos) { // init TAOS_STMT *stmt = taos_stmt_init(taos); // prepare const char *sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)"; int code = taos_stmt_prepare(stmt, sql, 0); checkErrorCode(stmt, code, "failed to execute taos_stmt_prepare"); for (int i = 1; i <= num_of_sub_table; i++) { char table_name[20]; sprintf(table_name, "d_bind_%d", i); char location[20]; sprintf(location, "location_%d", i); // set table name and tags TAOS_MULTI_BIND tags[2]; // groupId tags[0].buffer_type = TSDB_DATA_TYPE_INT; tags[0].buffer_length = sizeof(int); tags[0].length = (int32_t *)&tags[0].buffer_length; tags[0].buffer = &i; tags[0].is_null = NULL; tags[0].num = 1; // location tags[1].buffer_type = TSDB_DATA_TYPE_BINARY; tags[1].buffer_length = strlen(location); tags[1].length =(int32_t *) &tags[1].buffer_length; tags[1].buffer = location; tags[1].is_null = NULL; tags[1].num = 1; code = taos_stmt_set_tbname_tags(stmt, table_name, tags); checkErrorCode(stmt, code, "failed to set table name and tags\n"); // insert rows TAOS_MULTI_BIND params[4]; // ts params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; params[0].buffer_length = sizeof(int64_t); params[0].length = (int32_t *)¶ms[0].buffer_length; params[0].is_null = NULL; params[0].num = 1; // current params[1].buffer_type = TSDB_DATA_TYPE_FLOAT; params[1].buffer_length = sizeof(float); params[1].length = (int32_t *)¶ms[1].buffer_length; params[1].is_null = NULL; params[1].num = 1; // voltage params[2].buffer_type = TSDB_DATA_TYPE_INT; params[2].buffer_length = sizeof(int); params[2].length = (int32_t *)¶ms[2].buffer_length; params[2].is_null = NULL; params[2].num = 1; // phase params[3].buffer_type = TSDB_DATA_TYPE_FLOAT; params[3].buffer_length = sizeof(float); params[3].length = (int32_t *)¶ms[3].buffer_length; params[3].is_null = NULL; params[3].num = 1; for (int j = 0; j < num_of_row; j++) { struct timeval tv; gettimeofday(&tv, NULL); long long milliseconds = tv.tv_sec * 1000LL + tv.tv_usec / 1000; // current timestamp in milliseconds int64_t ts = milliseconds + j; float current = (float)rand() / RAND_MAX * 30; int voltage = rand() % 300; float phase = (float)rand() / RAND_MAX; params[0].buffer = &ts; params[1].buffer = ¤t; params[2].buffer = &voltage; params[3].buffer = &phase; // bind param code = taos_stmt_bind_param(stmt, params); checkErrorCode(stmt, code, "failed to bind param"); } // add batch code = taos_stmt_add_batch(stmt); checkErrorCode(stmt, code, "failed to add batch"); // execute batch code = taos_stmt_execute(stmt); checkErrorCode(stmt, code, "failed to exec stmt"); // get affected rows int affected = taos_stmt_affected_rows_once(stmt); printf("table %s insert %d rows.\n", table_name, affected); } taos_stmt_close(stmt); } int main() { TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 6030); if (taos == NULL) { printf("failed to connect to server\n"); exit(EXIT_FAILURE); } // create database and table executeSQL(taos, "CREATE DATABASE IF NOT EXISTS power"); executeSQL(taos, "USE power"); executeSQL(taos, "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS " "(groupId INT, location BINARY(24))"); insertData(taos); taos_close(taos); taos_cleanup(); }