subscribe.c 3.6 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6
// sample code for TDengine subscribe/consume API
// to compile: gcc -o subscribe subscribe.c -ltaos

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
7
#include <taos.h>  // include TDengine header file
weixin_48148422's avatar
weixin_48148422 已提交
8
#include <unistd.h>
9

B
Bomin Zhang 已提交
10

T
tickduan 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
void showme();
float calculate_delta_t(size_t size);
int is_lossless_compressed_data(unsigned char* compressedBytes, size_t cmpSize);

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <taos.h>  // TAOS header file

static void queryDB(TAOS *taos, char *command) {
    
  printf("aaa");
  /*
    int i;
  TAOS_RES *pSql = NULL;
  int32_t   code = -1;

  for (i = 0; i < 5; i++) {
    if (NULL != pSql) {
      taos_free_result(pSql);
      pSql = NULL;
    }
    
    pSql = taos_query(taos, command);
    code = taos_errno(pSql);
    if (0 == code) {
      break;
39
    }
40
  }
weixin_48148422's avatar
weixin_48148422 已提交
41

T
tickduan 已提交
42 43 44 45 46
  if (code != 0) {
    fprintf(stderr, "Failed to run %s, reason: %s\n", command, taos_errstr(pSql));
    taos_free_result(pSql);
    taos_close(taos);
    exit(EXIT_FAILURE);
weixin_48148422's avatar
weixin_48148422 已提交
47
  }
48

T
tickduan 已提交
49 50
  taos_free_result(pSql);
   */
weixin_48148422's avatar
weixin_48148422 已提交
51 52
}

T
tickduan 已提交
53
void Test(TAOS *taos, char *qstr, int i);
weixin_48148422's avatar
weixin_48148422 已提交
54

weixin_48148422's avatar
weixin_48148422 已提交
55
int main(int argc, char *argv[]) {
T
tickduan 已提交
56 57 58 59 60 61 62 63
  //char      qstr[1024];
    
 is_lossless_compressed_data(NULL,0);

  // connect to server
  if (argc < 2) {
    printf("please input server-ip \n");
    return 0;
64 65
  }

T
tickduan 已提交
66
  TAOS *taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
weixin_48148422's avatar
weixin_48148422 已提交
67
  if (taos == NULL) {
T
tickduan 已提交
68
    printf("failed to connect to server, reason:%s\n", "null taos"/*taos_errstr(taos)*/);
H
hzcheng 已提交
69 70
    exit(1);
  }
T
tickduan 已提交
71 72 73
  /*
  for (int i = 0; i < 100; i++) {
    Test(taos, qstr, i);
74
  }
T
tickduan 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  taos_close(taos);
  taos_cleanup();
   */
}
void Test(TAOS *taos, char *qstr, int index)  {
    
  printf("==================test at %d\n================================", index);
  
  queryDB(taos, "drop database if exists demo");
  queryDB(taos, "create database demo");
  //TAOS_RES *result;
  queryDB(taos, "use demo");

  queryDB(taos, "create table m1 (ts timestamp, ti tinyint, si smallint, i int, bi bigint, f float, d double, b binary(10))");
  printf("success to create table\n");

     /*
  int i = 0;
  for (i = 0; i < 10; ++i) {
    sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", (uint64_t)(1546300800000 + i * 1000), i, i, i, i*10000000, i*1.0, i*2.0, "hello");
    printf("qstr: %s\n", qstr);
    
    // note: how do you wanna do if taos_query returns non-NULL
    // if (taos_query(taos, qstr)) {
    //   printf("insert row: %i, reason:%s\n", i, taos_errstr(taos));
    // }
    TAOS_RES *result1 = taos_query(taos, qstr);
    if (result1 == NULL || taos_errno(result1) != 0) {
      printf("failed to insert row, reason:%s\n", taos_errstr(result1));
      taos_free_result(result1);
      exit(1);
106
    } else {
T
tickduan 已提交
107
      printf("insert row: %i\n", i);
108
    }
T
tickduan 已提交
109 110 111 112 113 114 115 116 117 118 119
    taos_free_result(result1);
  }
  printf("success to insert rows, total %d rows\n", i);

  // query the records
  sprintf(qstr, "SELECT * FROM m1");
  result = taos_query(taos, qstr);
  if (result == NULL || taos_errno(result) != 0) {
    printf("failed to select, reason:%s\n", taos_errstr(result));
    taos_free_result(result);
    exit(1);
H
hzcheng 已提交
120 121
  }

T
tickduan 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135
  TAOS_ROW    row;
  int         rows = 0;
  int         num_fields = taos_field_count(result);
  TAOS_FIELD *fields = taos_fetch_fields(result);

  printf("num_fields = %d\n", num_fields);
  printf("select * from table, result:\n");
  // 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);
  }
H
hzcheng 已提交
136

T
tickduan 已提交
137 138 139
  taos_free_result(result);
  printf("====demo end====\n\n");
      */
H
hzcheng 已提交
140
}
T
tickduan 已提交
141