update c sample code

This commit is contained in:
sheyanjie-qq 2024-08-14 16:40:19 +08:00
parent 59a91bb86c
commit 8aa9fb82d3
6 changed files with 282 additions and 279 deletions

View File

@ -43,8 +43,7 @@ static int DemoCreateDB() {
TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power");
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, printf("Failed to create database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(result));
taos_errstr(result));
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
@ -59,8 +58,7 @@ static int DemoCreateDB() {
result = taos_query(taos, sql); result = taos_query(taos, sql);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to create stable power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, printf("Failed to create stable power.meters, ErrCode: 0x%x, ErrMessage: %s\n.", code, taos_errstr(result));
taos_errstr(result));
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;

View File

@ -23,52 +23,52 @@
#include "taos.h" #include "taos.h"
static int DemoInsertData() { static int DemoInsertData() {
// ANCHOR: insert_data // ANCHOR: insert_data
const char *host = "localhost"; const char *host = "localhost";
const char *user = "root"; const char *user = "root";
const char *password = "taosdata"; const char *password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
// connect // connect
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
taos_errstr(NULL));
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
// insert data, please make sure the database and table are already created // insert data, please make sure the database and table are already created
const char* sql = "INSERT INTO " \ const char *sql =
"power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " \ "INSERT INTO "
"VALUES " \ "power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') "
"(NOW + 1a, 10.30000, 219, 0.31000) " \ "VALUES "
"(NOW + 2a, 12.60000, 218, 0.33000) " \ "(NOW + 1a, 10.30000, 219, 0.31000) "
"(NOW + 3a, 12.30000, 221, 0.31000) " \ "(NOW + 2a, 12.60000, 218, 0.33000) "
"power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " \ "(NOW + 3a, 12.30000, 221, 0.31000) "
"VALUES " \ "power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') "
"VALUES "
"(NOW + 1a, 10.30000, 218, 0.25000) "; "(NOW + 1a, 10.30000, 218, 0.25000) ";
TAOS_RES *result = taos_query(taos, sql); TAOS_RES *result = taos_query(taos, sql);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { 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)); printf("Failed to insert data to power.meters, ErrCode: 0x%x, ErrMessage: %s\n.", code, taos_errstr(result));
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
taos_free_result(result); taos_free_result(result);
// you can check affectedRows here // you can check affectedRows here
int rows = taos_affected_rows(result); int rows = taos_affected_rows(result);
printf("Successfully inserted %d rows into power.meters.\n", rows); printf("Successfully inserted %d rows into power.meters.\n", rows);
// close & clean // close & clean
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return 0; return 0;
// ANCHOR_END: insert_data // ANCHOR_END: insert_data
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) { return DemoInsertData(); }
return DemoInsertData();
}

View File

@ -22,61 +22,58 @@
#include <string.h> #include <string.h>
#include "taos.h" #include "taos.h"
static int DemoQueryData() { static int DemoQueryData() {
// ANCHOR: query_data // ANCHOR: query_data
const char *host = "localhost"; const char *host = "localhost";
const char *user = "root"; const char *user = "root";
const char *password = "taosdata"; const char *password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
// connect // connect
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
taos_errstr(NULL));
taos_cleanup(); taos_cleanup();
return -1; 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";
// query data, please make sure the database and table are already created TAOS_RES *result = taos_query(taos, sql);
const char* sql = "SELECT ts, current, location FROM power.meters limit 100"; code = taos_errno(result);
TAOS_RES *result = taos_query(taos, sql); if (code != 0) {
code = taos_errno(result); printf("Failed to query data from power.meters, sql: %s, ErrCode: 0x%x, ErrMessage: %s\n.", sql, code,
if (code != 0) { taos_errstr(result));
printf("Failed to query data from power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result));
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
TAOS_ROW row = NULL; TAOS_ROW row = NULL;
int rows = 0; int rows = 0;
int num_fields = taos_field_count(result); int num_fields = taos_field_count(result);
TAOS_FIELD *fields = taos_fetch_fields(result); TAOS_FIELD *fields = taos_fetch_fields(result);
printf("fields: %d\n", num_fields); printf("fields: %d\n", num_fields);
printf("sql: %s, result:\n", sql); printf("sql: %s, result:\n", sql);
// fetch the records row by row // fetch the records row by row
while ((row = taos_fetch_row(result))) { while ((row = taos_fetch_row(result))) {
char temp[1024] = {0}; char temp[1024] = {0};
rows++; rows++;
taos_print_row(temp, row, fields, num_fields); taos_print_row(temp, row, fields, num_fields);
printf("%s\n", temp); printf("%s\n", temp);
} }
printf("total rows: %d\n", rows); printf("total rows: %d\n", rows);
taos_free_result(result); taos_free_result(result);
// close & clean // close & clean
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return 0; return 0;
// ANCHOR_END: query_data // ANCHOR_END: query_data
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) { return DemoQueryData(); }
return DemoQueryData();
}

View File

@ -21,114 +21,119 @@
#include <string.h> #include <string.h>
#include "taos.h" #include "taos.h"
static int DemoSmlInsert() { static int DemoSmlInsert() {
// ANCHOR: schemaless // ANCHOR: schemaless
const char *host = "localhost"; const char *host = "localhost";
const char *user = "root"; const char *user = "root";
const char *password = "taosdata"; const char *password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
// connect // connect
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
taos_errstr(NULL));
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
// create database // create database
TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power");
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { 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_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
taos_free_result(result); taos_free_result(result);
printf("Create database power successfully.\n"); printf("Create database power successfully.\n");
// use database // use database
result = taos_query(taos, "USE power"); result = taos_query(taos, "USE power");
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to execute use power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); printf("Failed to execute use power, ErrCode: 0x%x, ErrMessage: %s\n.", code, taos_errstr(result));
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
taos_free_result(result); taos_free_result(result);
// schemaless demo data // schemaless demo data
char * line_demo = "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639"; char *line_demo =
char * telnet_demo = "metric_telnet 1707095283260 4 host=host0 interface=eth0"; "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 "
char * json_demo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}"; "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 // influxdb line protocol
char *lines[] = {line_demo}; char *lines[] = {line_demo};
result = taos_schemaless_insert(taos, lines, 1, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); result = taos_schemaless_insert(taos, lines, 1, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { 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)); 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_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
int rows = taos_affected_rows(result); int rows = taos_affected_rows(result);
printf("Insert %d rows of schemaless line data successfully.\n", rows); printf("Insert %d rows of schemaless line data successfully.\n", rows);
taos_free_result(result); taos_free_result(result);
// opentsdb telnet protocol // opentsdb telnet protocol
char *telnets[] = {telnet_demo}; char *telnets[] = {telnet_demo};
result = taos_schemaless_insert(taos, telnets, 1, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); result = taos_schemaless_insert(taos, telnets, 1, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { 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)); 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_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
rows = taos_affected_rows(result); rows = taos_affected_rows(result);
printf("Insert %d rows of schemaless telnet data successfully.\n", rows); printf("Insert %d rows of schemaless telnet data successfully.\n", rows);
taos_free_result(result); taos_free_result(result);
// opentsdb json protocol // opentsdb json protocol
char *jsons[1] = {0}; char *jsons[1] = {0};
// allocate memory for json data. can not use static memory. // allocate memory for json data. can not use static memory.
jsons[0] = malloc(1024); jsons[0] = malloc(1024);
if (jsons[0] == NULL) { if (jsons[0] == NULL) {
printf("Failed to allocate memory\n"); printf("Failed to allocate memory\n");
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
(void)strncpy(jsons[0], json_demo, 1023); (void)strncpy(jsons[0], json_demo, 1023);
result = taos_schemaless_insert(taos, jsons, 1, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NOT_CONFIGURED); result = taos_schemaless_insert(taos, jsons, 1, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NOT_CONFIGURED);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
free(jsons[0]); 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)); 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_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
free(jsons[0]); free(jsons[0]);
rows = taos_affected_rows(result); rows = taos_affected_rows(result);
printf("Insert %d rows of schemaless json data successfully.\n", rows); printf("Insert %d rows of schemaless json data successfully.\n", rows);
taos_free_result(result); taos_free_result(result);
// close & clean // close & clean
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return 0; return 0;
// ANCHOR_END: schemaless // ANCHOR_END: schemaless
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) { return DemoSmlInsert(); }
return DemoSmlInsert();
}

View File

@ -41,14 +41,15 @@ typedef struct {
} ConsumerConfig; } ConsumerConfig;
void* prepare_data(void* arg) { void* prepare_data(void* arg) {
const char *host = "localhost"; const char* host = "localhost";
const char *user = "root"; const char* user = "root";
const char *password = "taosdata"; const char* password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
TAOS *pConn = taos_connect(host, user, password, NULL, port); TAOS* pConn = taos_connect(host, user, password, NULL, port);
if (pConn == NULL) { 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(); taos_cleanup();
return NULL; return NULL;
} }
@ -68,7 +69,8 @@ void* prepare_data(void* arg) {
pRes = taos_query(pConn, buf); pRes = taos_query(pConn, buf);
code = taos_errno(pRes); code = taos_errno(pRes);
if (code != 0) { 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); taos_free_result(pRes);
sleep(1); sleep(1);
@ -115,14 +117,15 @@ static int32_t msg_process(TAOS_RES* msg) {
// ANCHOR_END: msg_process // ANCHOR_END: msg_process
static int32_t init_env() { static int32_t init_env() {
const char *host = "localhost"; const char* host = "localhost";
const char *user = "root"; const char* user = "root";
const char *password = "taosdata"; const char* password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
TAOS *pConn = taos_connect(host, user, password, NULL, port); TAOS* pConn = taos_connect(host, user, password, NULL, port);
if (pConn == NULL) { 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(); taos_cleanup();
return -1; return -1;
} }
@ -178,14 +181,15 @@ END:
int32_t create_topic() { int32_t create_topic() {
fprintf(stdout, "Create topic.\n"); fprintf(stdout, "Create topic.\n");
TAOS_RES* pRes; TAOS_RES* pRes;
const char *host = "localhost"; const char* host = "localhost";
const char *user = "root"; const char* user = "root";
const char *password = "taosdata"; const char* password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
TAOS *pConn = taos_connect(host, user, password, NULL, port); TAOS* pConn = taos_connect(host, user, password, NULL, port);
if (pConn == NULL) { 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(); taos_cleanup();
return -1; return -1;
} }
@ -348,7 +352,8 @@ void consume_repeatly(tmq_t* tmq) {
code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin); code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin);
if (code != 0) { 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 { } else {
fprintf(stdout, "Seek assignment %d to beginning %ld successfully.\n", i, p->begin); fprintf(stdout, "Seek assignment %d to beginning %ld successfully.\n", i, p->begin);
} }
@ -417,8 +422,7 @@ int main(int argc, char* argv[]) {
return -1; return -1;
} }
ConsumerConfig config = { ConsumerConfig config = {.enable_auto_commit = "true",
.enable_auto_commit = "true",
.auto_commit_interval_ms = "1000", .auto_commit_interval_ms = "1000",
.group_id = "group1", .group_id = "group1",
.client_id = "client1", .client_id = "client1",
@ -426,16 +430,17 @@ int main(int argc, char* argv[]) {
.td_connect_port = "6030", .td_connect_port = "6030",
.td_connect_user = "root", .td_connect_user = "root",
.td_connect_pass = "taosdata", .td_connect_pass = "taosdata",
.auto_offset_reset = "latest" .auto_offset_reset = "latest"};
};
// ANCHOR: create_consumer_2 // ANCHOR: create_consumer_2
tmq_t* tmq = build_consumer(&config); tmq_t* tmq = build_consumer(&config);
if (NULL == tmq) { 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; return -1;
} else { } 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 // ANCHOR_END: create_consumer_2

View File

@ -23,59 +23,57 @@
#include "taos.h" #include "taos.h"
static int DemoWithReqId() { static int DemoWithReqId() {
// ANCHOR: with_reqid // ANCHOR: with_reqid
const char *host = "localhost"; const char *host = "localhost";
const char *user = "root"; const char *user = "root";
const char *password = "taosdata"; const char *password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
// connect // connect
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
taos_errstr(NULL));
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
const char *sql = "SELECT ts, current, location FROM power.meters limit 1"; const char *sql = "SELECT ts, current, location FROM power.meters limit 1";
// query data with reqid // query data with reqid
long reqid = 3L; long reqid = 3L;
TAOS_RES *result = taos_query_with_reqid(taos, sql, reqid); TAOS_RES *result = taos_query_with_reqid(taos, sql, reqid);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { 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)); printf("Failed to execute sql with reqId: %ld, ErrCode: 0x%x, ErrMessage: %s\n.", reqid, code, taos_errstr(result));
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
TAOS_ROW row = NULL; TAOS_ROW row = NULL;
int rows = 0; int rows = 0;
int num_fields = taos_field_count(result); int num_fields = taos_field_count(result);
TAOS_FIELD *fields = taos_fetch_fields(result); TAOS_FIELD *fields = taos_fetch_fields(result);
printf("fields: %d\n", num_fields); printf("fields: %d\n", num_fields);
printf("sql: %s, result:\n", sql); printf("sql: %s, result:\n", sql);
// fetch the records row by row // fetch the records row by row
while ((row = taos_fetch_row(result))) { while ((row = taos_fetch_row(result))) {
char temp[1024] = {0}; char temp[1024] = {0};
rows++; rows++;
taos_print_row(temp, row, fields, num_fields); taos_print_row(temp, row, fields, num_fields);
printf("%s\n", temp); printf("%s\n", temp);
} }
printf("total rows: %d\n", rows); printf("total rows: %d\n", rows);
taos_free_result(result); taos_free_result(result);
// close & clean // close & clean
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return 0; return 0;
// ANCHOR_END: with_reqid // ANCHOR_END: with_reqid
}
int main(int argc, char *argv[]) {
return DemoWithReqId();
} }
int main(int argc, char *argv[]) { return DemoWithReqId(); }