From 8aa9fb82d3ad851f1e6507a1458738f594fafe28 Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Wed, 14 Aug 2024 16:40:19 +0800 Subject: [PATCH] update c sample code --- docs/examples/c/create_db_demo.c | 6 +- docs/examples/c/insert_data_demo.c | 86 ++++++------- docs/examples/c/query_data_demo.c | 95 +++++++------- docs/examples/c/sml_insert_demo.c | 197 +++++++++++++++-------------- docs/examples/c/tmq_demo.c | 79 ++++++------ docs/examples/c/with_reqid_demo.c | 98 +++++++------- 6 files changed, 282 insertions(+), 279 deletions(-) diff --git a/docs/examples/c/create_db_demo.c b/docs/examples/c/create_db_demo.c index 45d4a17c5c..7ac54041d8 100644 --- a/docs/examples/c/create_db_demo.c +++ b/docs/examples/c/create_db_demo.c @@ -43,8 +43,7 @@ static int DemoCreateDB() { TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); code = taos_errno(result); if (code != 0) { - printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, - taos_errstr(result)); + printf("Failed to create database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -59,8 +58,7 @@ static int DemoCreateDB() { result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { - printf("Failed to create stable power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, - taos_errstr(result)); + printf("Failed to create stable power.meters, ErrCode: 0x%x, ErrMessage: %s\n.", code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; diff --git a/docs/examples/c/insert_data_demo.c b/docs/examples/c/insert_data_demo.c index 3d655edca3..edc6b0b750 100644 --- a/docs/examples/c/insert_data_demo.c +++ b/docs/examples/c/insert_data_demo.c @@ -23,52 +23,52 @@ #include "taos.h" static int DemoInsertData() { -// ANCHOR: insert_data -const char *host = "localhost"; -const char *user = "root"; -const char *password = "taosdata"; -uint16_t port = 6030; -int code = 0; + // ANCHOR: insert_data + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + int code = 0; -// connect -TAOS *taos = taos_connect(host, user, password, NULL, port); -if (taos == NULL) { - printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); - taos_cleanup(); - return -1; -} + // connect + TAOS *taos = taos_connect(host, user, password, NULL, port); + if (taos == NULL) { + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), + taos_errstr(NULL)); + taos_cleanup(); + return -1; + } -// insert data, please make sure the database and table are already created -const char* sql = "INSERT INTO " \ - "power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " \ - "VALUES " \ - "(NOW + 1a, 10.30000, 219, 0.31000) " \ - "(NOW + 2a, 12.60000, 218, 0.33000) " \ - "(NOW + 3a, 12.30000, 221, 0.31000) " \ - "power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " \ - "VALUES " \ - "(NOW + 1a, 10.30000, 218, 0.25000) "; -TAOS_RES *result = taos_query(taos, sql); -code = taos_errno(result); -if (code != 0) { - printf("Failed to insert data to power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + // insert data, please make sure the database and table are already created + const char *sql = + "INSERT INTO " + "power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " + "VALUES " + "(NOW + 1a, 10.30000, 219, 0.31000) " + "(NOW + 2a, 12.60000, 218, 0.33000) " + "(NOW + 3a, 12.30000, 221, 0.31000) " + "power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " + "VALUES " + "(NOW + 1a, 10.30000, 218, 0.25000) "; + TAOS_RES *result = taos_query(taos, sql); + code = taos_errno(result); + if (code != 0) { + printf("Failed to insert data to power.meters, ErrCode: 0x%x, ErrMessage: %s\n.", code, taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; + } + taos_free_result(result); + + // you can check affectedRows here + int rows = taos_affected_rows(result); + printf("Successfully inserted %d rows into power.meters.\n", rows); + + // close & clean taos_close(taos); taos_cleanup(); - return -1; -} -taos_free_result(result); - -// you can check affectedRows here -int rows = taos_affected_rows(result); -printf("Successfully inserted %d rows into power.meters.\n", rows); - -// close & clean -taos_close(taos); -taos_cleanup(); -return 0; -// ANCHOR_END: insert_data + return 0; + // ANCHOR_END: insert_data } -int main(int argc, char *argv[]) { - return DemoInsertData(); -} +int main(int argc, char *argv[]) { return DemoInsertData(); } diff --git a/docs/examples/c/query_data_demo.c b/docs/examples/c/query_data_demo.c index e58c467ccf..f2c6662d9e 100644 --- a/docs/examples/c/query_data_demo.c +++ b/docs/examples/c/query_data_demo.c @@ -22,61 +22,58 @@ #include #include "taos.h" - static int DemoQueryData() { -// ANCHOR: query_data -const char *host = "localhost"; -const char *user = "root"; -const char *password = "taosdata"; -uint16_t port = 6030; -int code = 0; + // ANCHOR: query_data + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + int code = 0; -// connect -TAOS *taos = taos_connect(host, user, password, NULL, port); -if (taos == NULL) { - printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); - taos_cleanup(); - return -1; -} + // connect + TAOS *taos = taos_connect(host, user, password, NULL, port); + if (taos == NULL) { + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), + taos_errstr(NULL)); + taos_cleanup(); + return -1; + } + // query data, please make sure the database and table are already created + const char *sql = "SELECT ts, current, location FROM power.meters limit 100"; + TAOS_RES *result = taos_query(taos, sql); + code = taos_errno(result); + if (code != 0) { + printf("Failed to query data from power.meters, sql: %s, ErrCode: 0x%x, ErrMessage: %s\n.", sql, code, + taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; + } + TAOS_ROW row = NULL; + int rows = 0; + int num_fields = taos_field_count(result); + TAOS_FIELD *fields = taos_fetch_fields(result); -// query data, please make sure the database and table are already created -const char* sql = "SELECT ts, current, location FROM power.meters limit 100"; -TAOS_RES *result = taos_query(taos, sql); -code = taos_errno(result); -if (code != 0) { - printf("Failed to query data from power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + printf("fields: %d\n", num_fields); + printf("sql: %s, result:\n", sql); + + // fetch the records row by row + while ((row = taos_fetch_row(result))) { + char temp[1024] = {0}; + rows++; + taos_print_row(temp, row, fields, num_fields); + printf("%s\n", temp); + } + printf("total rows: %d\n", rows); + taos_free_result(result); + + // close & clean taos_close(taos); taos_cleanup(); - return -1; + return 0; + // ANCHOR_END: query_data } -TAOS_ROW row = NULL; -int rows = 0; -int num_fields = taos_field_count(result); -TAOS_FIELD *fields = taos_fetch_fields(result); - -printf("fields: %d\n", num_fields); -printf("sql: %s, result:\n", sql); - -// fetch the records row by row -while ((row = taos_fetch_row(result))) { - char temp[1024] = {0}; - rows++; - taos_print_row(temp, row, fields, num_fields); - printf("%s\n", temp); -} -printf("total rows: %d\n", rows); -taos_free_result(result); - -// close & clean -taos_close(taos); -taos_cleanup(); -return 0; -// ANCHOR_END: query_data -} - -int main(int argc, char *argv[]) { - return DemoQueryData(); -} +int main(int argc, char *argv[]) { return DemoQueryData(); } diff --git a/docs/examples/c/sml_insert_demo.c b/docs/examples/c/sml_insert_demo.c index 807fd10501..1107734c78 100644 --- a/docs/examples/c/sml_insert_demo.c +++ b/docs/examples/c/sml_insert_demo.c @@ -21,114 +21,119 @@ #include #include "taos.h" - static int DemoSmlInsert() { -// ANCHOR: schemaless -const char *host = "localhost"; -const char *user = "root"; -const char *password = "taosdata"; -uint16_t port = 6030; -int code = 0; + // ANCHOR: schemaless + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + int code = 0; -// connect -TAOS *taos = taos_connect(host, user, password, NULL, port); -if (taos == NULL) { - printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); - taos_cleanup(); - return -1; -} + // connect + TAOS *taos = taos_connect(host, user, password, NULL, port); + if (taos == NULL) { + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), + taos_errstr(NULL)); + taos_cleanup(); + return -1; + } -// create database -TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); -code = taos_errno(result); -if (code != 0) { - printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, taos_errstr(result)); - taos_close(taos); - taos_cleanup(); - return -1; -} -taos_free_result(result); -printf("Create database power successfully.\n"); + // create database + TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); + code = taos_errno(result); + if (code != 0) { + printf("Failed to create database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; + } + taos_free_result(result); + printf("Create database power successfully.\n"); -// use database -result = taos_query(taos, "USE power"); -code = taos_errno(result); -if (code != 0) { - printf("Failed to execute use power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); - taos_close(taos); - taos_cleanup(); - return -1; -} -taos_free_result(result); + // use database + result = taos_query(taos, "USE power"); + code = taos_errno(result); + if (code != 0) { + printf("Failed to execute use power, ErrCode: 0x%x, ErrMessage: %s\n.", code, taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; + } + taos_free_result(result); -// schemaless demo data -char * line_demo = "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639"; -char * telnet_demo = "metric_telnet 1707095283260 4 host=host0 interface=eth0"; -char * json_demo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}"; + // schemaless demo data + char *line_demo = + "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 " + "1626006833639"; + char *telnet_demo = "metric_telnet 1707095283260 4 host=host0 interface=eth0"; + char *json_demo = + "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, " + "\"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}"; -// influxdb line protocol -char *lines[] = {line_demo}; -result = taos_schemaless_insert(taos, lines, 1, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); -code = taos_errno(result); -if (code != 0) { - printf("Failed to insert schemaless line data, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); - taos_close(taos); - taos_cleanup(); - return -1; -} + // influxdb line protocol + char *lines[] = {line_demo}; + result = taos_schemaless_insert(taos, lines, 1, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); + code = taos_errno(result); + if (code != 0) { + printf("Failed to insert schemaless line data, data: %s, ErrCode: 0x%x, ErrMessage: %s\n.", line_demo, code, + taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; + } -int rows = taos_affected_rows(result); -printf("Insert %d rows of schemaless line data successfully.\n", rows); -taos_free_result(result); + int rows = taos_affected_rows(result); + printf("Insert %d rows of schemaless line data successfully.\n", rows); + taos_free_result(result); -// opentsdb telnet protocol -char *telnets[] = {telnet_demo}; -result = taos_schemaless_insert(taos, telnets, 1, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); -code = taos_errno(result); -if (code != 0) { - printf("Failed to insert schemaless telnet data, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); - taos_close(taos); - taos_cleanup(); - return -1; -} + // opentsdb telnet protocol + char *telnets[] = {telnet_demo}; + result = taos_schemaless_insert(taos, telnets, 1, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); + code = taos_errno(result); + if (code != 0) { + printf("Failed to insert schemaless telnet data, data: %s, ErrCode: 0x%x, ErrMessage: %s\n.", telnet_demo, code, + taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; + } -rows = taos_affected_rows(result); -printf("Insert %d rows of schemaless telnet data successfully.\n", rows); -taos_free_result(result); + rows = taos_affected_rows(result); + printf("Insert %d rows of schemaless telnet data successfully.\n", rows); + taos_free_result(result); -// opentsdb json protocol -char *jsons[1] = {0}; -// allocate memory for json data. can not use static memory. -jsons[0] = malloc(1024); -if (jsons[0] == NULL) { - printf("Failed to allocate memory\n"); - taos_close(taos); - taos_cleanup(); - return -1; -} -(void)strncpy(jsons[0], json_demo, 1023); -result = taos_schemaless_insert(taos, jsons, 1, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NOT_CONFIGURED); -code = taos_errno(result); -if (code != 0) { + // opentsdb json protocol + char *jsons[1] = {0}; + // allocate memory for json data. can not use static memory. + jsons[0] = malloc(1024); + if (jsons[0] == NULL) { + printf("Failed to allocate memory\n"); + taos_close(taos); + taos_cleanup(); + return -1; + } + (void)strncpy(jsons[0], json_demo, 1023); + result = taos_schemaless_insert(taos, jsons, 1, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NOT_CONFIGURED); + code = taos_errno(result); + if (code != 0) { + free(jsons[0]); + printf("Failed to insert schemaless json data, Server: %s, ErrCode: 0x%x, ErrMessage: %s\n.", json_demo, code, + taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; + } free(jsons[0]); - printf("Failed to insert schemaless json data, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + + rows = taos_affected_rows(result); + printf("Insert %d rows of schemaless json data successfully.\n", rows); + taos_free_result(result); + + // close & clean taos_close(taos); taos_cleanup(); - return -1; -} -free(jsons[0]); - -rows = taos_affected_rows(result); -printf("Insert %d rows of schemaless json data successfully.\n", rows); -taos_free_result(result); - -// close & clean -taos_close(taos); -taos_cleanup(); -return 0; -// ANCHOR_END: schemaless + return 0; + // ANCHOR_END: schemaless } -int main(int argc, char *argv[]) { - return DemoSmlInsert(); -} +int main(int argc, char *argv[]) { return DemoSmlInsert(); } diff --git a/docs/examples/c/tmq_demo.c b/docs/examples/c/tmq_demo.c index e7a2425c7d..55ec568dc5 100644 --- a/docs/examples/c/tmq_demo.c +++ b/docs/examples/c/tmq_demo.c @@ -41,14 +41,15 @@ typedef struct { } ConsumerConfig; void* prepare_data(void* arg) { - const char *host = "localhost"; - const char *user = "root"; - const char *password = "taosdata"; - uint16_t port = 6030; - int code = 0; - TAOS *pConn = taos_connect(host, user, password, NULL, port); + const char* host = "localhost"; + const char* user = "root"; + const char* password = "taosdata"; + uint16_t port = 6030; + int code = 0; + TAOS* pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), + taos_errstr(NULL)); taos_cleanup(); return NULL; } @@ -68,7 +69,8 @@ void* prepare_data(void* arg) { pRes = taos_query(pConn, buf); code = taos_errno(pRes); if (code != 0) { - fprintf(stderr, "Failed to insert data to power.meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); + fprintf(stderr, "Failed to insert data to power.meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, + taos_errstr(pRes)); } taos_free_result(pRes); sleep(1); @@ -115,14 +117,15 @@ static int32_t msg_process(TAOS_RES* msg) { // ANCHOR_END: msg_process static int32_t init_env() { - const char *host = "localhost"; - const char *user = "root"; - const char *password = "taosdata"; - uint16_t port = 6030; - int code = 0; - TAOS *pConn = taos_connect(host, user, password, NULL, port); + const char* host = "localhost"; + const char* user = "root"; + const char* password = "taosdata"; + uint16_t port = 6030; + int code = 0; + TAOS* pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), + taos_errstr(NULL)); taos_cleanup(); return -1; } @@ -177,15 +180,16 @@ END: int32_t create_topic() { fprintf(stdout, "Create topic.\n"); - TAOS_RES* pRes; - const char *host = "localhost"; - const char *user = "root"; - const char *password = "taosdata"; - uint16_t port = 6030; - int code = 0; - TAOS *pConn = taos_connect(host, user, password, NULL, port); + TAOS_RES* pRes; + const char* host = "localhost"; + const char* user = "root"; + const char* password = "taosdata"; + uint16_t port = 6030; + int code = 0; + TAOS* pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), + taos_errstr(NULL)); taos_cleanup(); return -1; } @@ -348,7 +352,8 @@ void consume_repeatly(tmq_t* tmq) { code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin); if (code != 0) { - fprintf(stderr, "Failed to seek assignment %d to beginning %ld, ErrCode: 0x%x, ErrMessage: %s.\n", i, p->begin, code, tmq_err2str(code)); + fprintf(stderr, "Failed to seek assignment %d to beginning %ld, ErrCode: 0x%x, ErrMessage: %s.\n", i, p->begin, + code, tmq_err2str(code)); } else { fprintf(stdout, "Seek assignment %d to beginning %ld successfully.\n", i, p->begin); } @@ -417,25 +422,25 @@ int main(int argc, char* argv[]) { return -1; } - ConsumerConfig config = { - .enable_auto_commit = "true", - .auto_commit_interval_ms = "1000", - .group_id = "group1", - .client_id = "client1", - .td_connect_host = "localhost", - .td_connect_port = "6030", - .td_connect_user = "root", - .td_connect_pass = "taosdata", - .auto_offset_reset = "latest" - }; + ConsumerConfig config = {.enable_auto_commit = "true", + .auto_commit_interval_ms = "1000", + .group_id = "group1", + .client_id = "client1", + .td_connect_host = "localhost", + .td_connect_port = "6030", + .td_connect_user = "root", + .td_connect_pass = "taosdata", + .auto_offset_reset = "latest"}; // ANCHOR: create_consumer_2 tmq_t* tmq = build_consumer(&config); if (NULL == tmq) { - fprintf(stderr, "Failed to create native consumer, host: %s, groupId: %s, , clientId: %s.\n", config.td_connect_host, config.group_id, config.client_id); + fprintf(stderr, "Failed to create native consumer, host: %s, groupId: %s, , clientId: %s.\n", + config.td_connect_host, config.group_id, config.client_id); return -1; } else { - fprintf(stdout, "Create consumer successfully, host: %s, groupId: %s, , clientId: %s.\n", config.td_connect_host, config.group_id, config.client_id); + fprintf(stdout, "Create consumer successfully, host: %s, groupId: %s, , clientId: %s.\n", config.td_connect_host, + config.group_id, config.client_id); } // ANCHOR_END: create_consumer_2 diff --git a/docs/examples/c/with_reqid_demo.c b/docs/examples/c/with_reqid_demo.c index 724b682850..b3e4e79e39 100644 --- a/docs/examples/c/with_reqid_demo.c +++ b/docs/examples/c/with_reqid_demo.c @@ -23,59 +23,57 @@ #include "taos.h" static int DemoWithReqId() { -// ANCHOR: with_reqid -const char *host = "localhost"; -const char *user = "root"; -const char *password = "taosdata"; -uint16_t port = 6030; -int code = 0; + // ANCHOR: with_reqid + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + int code = 0; -// connect -TAOS *taos = taos_connect(host, user, password, NULL, port); -if (taos == NULL) { - printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); - taos_cleanup(); - return -1; -} + // connect + TAOS *taos = taos_connect(host, user, password, NULL, port); + if (taos == NULL) { + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), + taos_errstr(NULL)); + taos_cleanup(); + return -1; + } -const char *sql = "SELECT ts, current, location FROM power.meters limit 1"; -// query data with reqid -long reqid = 3L; -TAOS_RES *result = taos_query_with_reqid(taos, sql, reqid); -code = taos_errno(result); -if (code != 0) { - printf("Failed to execute sql with reqId: %ld, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", reqid, host, port, code, taos_errstr(result)); + const char *sql = "SELECT ts, current, location FROM power.meters limit 1"; + // query data with reqid + long reqid = 3L; + TAOS_RES *result = taos_query_with_reqid(taos, sql, reqid); + code = taos_errno(result); + if (code != 0) { + printf("Failed to execute sql with reqId: %ld, ErrCode: 0x%x, ErrMessage: %s\n.", reqid, code, taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; + } + + TAOS_ROW row = NULL; + int rows = 0; + int num_fields = taos_field_count(result); + TAOS_FIELD *fields = taos_fetch_fields(result); + + printf("fields: %d\n", num_fields); + printf("sql: %s, result:\n", sql); + + // fetch the records row by row + while ((row = taos_fetch_row(result))) { + char temp[1024] = {0}; + rows++; + taos_print_row(temp, row, fields, num_fields); + printf("%s\n", temp); + } + printf("total rows: %d\n", rows); + taos_free_result(result); + + // close & clean taos_close(taos); taos_cleanup(); - return -1; -} - -TAOS_ROW row = NULL; -int rows = 0; -int num_fields = taos_field_count(result); -TAOS_FIELD *fields = taos_fetch_fields(result); - -printf("fields: %d\n", num_fields); -printf("sql: %s, result:\n", sql); - -// fetch the records row by row -while ((row = taos_fetch_row(result))) { - char temp[1024] = {0}; - rows++; - taos_print_row(temp, row, fields, num_fields); - printf("%s\n", temp); -} -printf("total rows: %d\n", rows); -taos_free_result(result); - -// close & clean -taos_close(taos); -taos_cleanup(); -return 0; -// ANCHOR_END: with_reqid -} - -int main(int argc, char *argv[]) { - return DemoWithReqId(); + return 0; + // ANCHOR_END: with_reqid } +int main(int argc, char *argv[]) { return DemoWithReqId(); }