mirror of
https://github.com/taosdata/TDengine
synced 2026-05-24 10:09:01 +00:00
* test: add Makefile and test script for C ws examples * docs: add c ws new examples * docs: replace c ws examples with c ws new examples * fix: correct log file redirection syntax in c_ws scripts * style: format code * docs: update c/c++ zh doc * docs: add async demo and ws error code * docs: modify c/c++ zh doc * docs: rename asyncdemo.c to async_demo.c * docs: update c ws zh doc * docs: update c ws en docs * docs: fix typos and improve WebSocket connection documentation * docs: refactor code structure for improved readability and maintainability * docs: modify stmt en doc * fix: fix build doc errors * fix: fix return value * fix: delete taos_options * fix: fix return value * docs: update c/c++ connector docs
65 lines
1.9 KiB
C
65 lines
1.9 KiB
C
// to compile: gcc -o insert_data_demo insert_data_demo.c -ltaos
|
|
|
|
#include <inttypes.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "taos.h"
|
|
|
|
static int DemoInsertData() {
|
|
// ANCHOR: insert_data
|
|
const char *host = "localhost";
|
|
const char *user = "root";
|
|
const char *password = "taosdata";
|
|
uint16_t port = 6041;
|
|
int code = 0;
|
|
|
|
code = taos_options(TSDB_OPTION_DRIVER, "websocket");
|
|
if (code != 0) {
|
|
fprintf(stderr, "Failed to set driver option, code: %d\n", code);
|
|
return -1;
|
|
}
|
|
|
|
// connect
|
|
TAOS *taos = taos_connect(host, user, password, NULL, port);
|
|
if (taos == 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;
|
|
}
|
|
|
|
// 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) {
|
|
fprintf(stderr, "Failed to insert data to power.meters, sql: %s, ErrCode: 0x%x, ErrMessage: %s\n.", sql, code,
|
|
taos_errstr(result));
|
|
taos_close(taos);
|
|
taos_cleanup();
|
|
return -1;
|
|
}
|
|
|
|
// you can check affectedRows here
|
|
int rows = taos_affected_rows(result);
|
|
fprintf(stdout, "Successfully inserted %d rows into power.meters.\n", rows);
|
|
|
|
taos_free_result(result);
|
|
|
|
taos_close(taos);
|
|
taos_cleanup();
|
|
return 0;
|
|
// ANCHOR_END: insert_data
|
|
}
|
|
|
|
int main() { return DemoInsertData(); }
|