#td-engine
#tdengine
Вопрос:
Я обнаружил ошибку выполнения stmt: [0x8000060b] Данные метки времени вне диапазона при использовании TDengine C API.
Ниже приведен воспроизводимый код, сохраните его как stmt-now.c
дополните gcc stmt-now.c -l taos -o stmt-now
и запустите stmt-now
.
В коде я хочу использовать инструкцию sql insert into ? using stb tags(?) values(now, ?)
для вставки значений в таблицы multitple с помощью TDengine C STMT API, следуя документации API https://www.taosdata.com/en/documentation/connector#c-cpp .
Спасибо за ваше внимание.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <taos.h>
#include <unistd.h>
void execute_simple_sql(void *taos, char *sql) {
TAOS_RES *result = taos_query(taos, sql);
if (result == NULL || taos_errno(result) != 0) {
printf("failed to %s, Reason: %sn", sql, taos_errstr(result));
taos_free_result(result);
exit(EXIT_FAILURE);
}
taos_free_result(result);
}
void print_result(TAOS_RES *res) {
if (res == NULL) {
exit(EXIT_FAILURE);
}
TAOS_ROW row = NULL;
int num_fields = taos_num_fields(res);
TAOS_FIELD *fields = taos_fetch_fields(res);
while ((row = taos_fetch_row(res))) {
char temp[256] = {0};
taos_print_row(temp, row, fields, num_fields);
printf("get result: %sn", temp);
}
}
int main() {
void *taos = NULL;
taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("Cannot connect to tdengine servern");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(
taos, "create table if not exists stb (ts timestamp, n int) tags(b int)");
TAOS_STMT *stmt = NULL;
char *stmt_sql = calloc(1, 1048576);
stmt = taos_stmt_init(taos);
sprintf(stmt_sql, "insert into ? using stb tags(?) values(now, ?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
char *name = calloc(1, 20);
sprintf(name, "tb");
int b = 1;
TAOS_BIND *tags = calloc(1, sizeof(TAOS_BIND));
tags->buffer_length = TSDB_DATA_TYPE_INT;
tags->buffer_length = sizeof(uint32_t);
tags->buffer = amp;b;
tags->length = amp;tags->buffer_length;
tags->is_null = NULL;
assert(taos_stmt_set_tbname_tags(stmt, name, tags) == 0);
int32_t v = 2;
TAOS_BIND *value = calloc(1, sizeof(TAOS_BIND));
value->buffer_type = TSDB_DATA_TYPE_INT;
value->buffer_length = sizeof(uint32_t);
value->buffer = amp;v;
value->length = amp;value->buffer_length;
value->is_null = NULL;
assert(taos_stmt_bind_param(stmt, value) == 0);
assert(taos_stmt_add_batch(stmt) == 0);
int errno = taos_stmt_execute(stmt);
if (errno != 0) {
char *errstr = taos_stmt_errstr(stmt);
printf("stmt execute error: [0x%0x] %s", errno, errstr);
}
free(value);
free(stmt_sql);
free(name);
free(tags);
assert(taos_stmt_close(stmt) == 0);
}