demo.c 3.8 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
Haojun Liao 已提交
16
// TAOS standard API example. The same syntax as MySQL, but only a subset
H
hzcheng 已提交
17 18
// to compile: gcc -o demo demo.c -ltaos

Z
zhaoyanggh 已提交
19
#include <inttypes.h>
H
hzcheng 已提交
20 21 22
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
23
#include <taos.h>  // TAOS header file
24

25
static void queryDB(TAOS *taos, char *command) {
Z
zhaoyanggh 已提交
26
  int       i;
27 28 29 30 31 32 33 34
  TAOS_RES *pSql = NULL;
  int32_t   code = -1;

  for (i = 0; i < 5; i++) {
    if (NULL != pSql) {
      taos_free_result(pSql);
      pSql = NULL;
    }
Z
zhaoyanggh 已提交
35

36 37 38 39
    pSql = taos_query(taos, command);
    code = taos_errno(pSql);
    if (0 == code) {
      break;
Z
zhaoyanggh 已提交
40
    }
41 42 43 44 45 46 47 48 49 50 51 52
  }

  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);
  }

  taos_free_result(pSql);
}

53
void Test(TAOS *taos, char *qstr, int i);
Y
yihaoDeng 已提交
54

H
hzcheng 已提交
55
int main(int argc, char *argv[]) {
Z
zhaoyanggh 已提交
56
  char qstr[1024];
H
Haojun Liao 已提交
57

H
hzcheng 已提交
58 59 60 61 62
  // connect to server
  if (argc < 2) {
    printf("please input server-ip \n");
    return 0;
  }
H
Haojun Liao 已提交
63

64 65
  TAOS *taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
  if (taos == NULL) {
Z
zhaoyanggh 已提交
66
    printf("failed to connect to server, reason:%s\n", "null taos" /*taos_errstr(taos)*/);
67 68
    exit(1);
  }
69
  for (int i = 0; i < 100; i++) {
70
    Test(taos, qstr, i);
Y
yihaoDeng 已提交
71
  }
72
  taos_close(taos);
Y
yihaoDeng 已提交
73 74
  taos_cleanup();
}
Z
zhaoyanggh 已提交
75
void Test(TAOS *taos, char *qstr, int index) {
Y
yihaoDeng 已提交
76 77 78 79
  printf("==================test at %d\n================================", index);
  queryDB(taos, "drop database if exists demo");
  queryDB(taos, "create database demo");
  TAOS_RES *result;
80
  queryDB(taos, "use demo");
H
Haojun Liao 已提交
81

Z
zhaoyanggh 已提交
82 83
  queryDB(taos,
          "create table m1 (ts timestamp, ti tinyint, si smallint, i int, bi bigint, f float, d double, b binary(10))");
H
hzcheng 已提交
84
  printf("success to create table\n");
H
Haojun Liao 已提交
85

H
hzcheng 已提交
86 87
  int i = 0;
  for (i = 0; i < 10; ++i) {
Z
zhaoyanggh 已提交
88 89
    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");
S
Shuduo Sang 已提交
90
    printf("qstr: %s\n", qstr);
Z
zhaoyanggh 已提交
91

92 93 94 95
    // 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));
    // }
H
Haojun Liao 已提交
96
    TAOS_RES *result1 = taos_query(taos, qstr);
97
    if (result1 == NULL || taos_errno(result1) != 0) {
Z
zhaoyanggh 已提交
98
      printf("failed to insert row, reason:%s\n", taos_errstr(result1));
H
Haojun Liao 已提交
99
      taos_free_result(result1);
100
      exit(1);
101 102
    } else {
      printf("insert row: %i\n", i);
H
hzcheng 已提交
103
    }
H
Haojun Liao 已提交
104
    taos_free_result(result1);
H
hzcheng 已提交
105 106
  }
  printf("success to insert rows, total %d rows\n", i);
H
Haojun Liao 已提交
107

H
hzcheng 已提交
108 109
  // query the records
  sprintf(qstr, "SELECT * FROM m1");
H
Haojun Liao 已提交
110 111
  result = taos_query(taos, qstr);
  if (result == NULL || taos_errno(result) != 0) {
Z
zhaoyanggh 已提交
112
    printf("failed to select, reason:%s\n", taos_errstr(result));
113
    taos_free_result(result);
H
hzcheng 已提交
114 115
    exit(1);
  }
H
[TD-98]  
hjxilinx 已提交
116

H
hjxilinx 已提交
117
  TAOS_ROW    row;
H
hzcheng 已提交
118
  int         rows = 0;
S
Shuduo Sang 已提交
119
  int         num_fields = taos_field_count(result);
H
hzcheng 已提交
120
  TAOS_FIELD *fields = taos_fetch_fields(result);
H
Haojun Liao 已提交
121

S
Shuduo Sang 已提交
122
  printf("num_fields = %d\n", num_fields);
H
hjxilinx 已提交
123 124 125
  printf("select * from table, result:\n");
  // fetch the records row by row
  while ((row = taos_fetch_row(result))) {
J
jiaoqiyuan 已提交
126
    char temp[1024] = {0};
H
hjxilinx 已提交
127 128 129 130
    rows++;
    taos_print_row(temp, row, fields, num_fields);
    printf("%s\n", temp);
  }
H
Haojun Liao 已提交
131

H
hzcheng 已提交
132 133 134
  taos_free_result(result);
  printf("====demo end====\n\n");
}