demo.c 3.4 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 19 20 21
// to compile: gcc -o demo demo.c -ltaos

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
S
TD-1530  
Shengliang Guan 已提交
22
#include <inttypes.h>
23
#include <taos.h>  // TAOS header file
24

H
hzcheng 已提交
25 26 27 28
int main(int argc, char *argv[]) {
  TAOS *    taos;
  char      qstr[1024];
  TAOS_RES *result;
H
Haojun Liao 已提交
29

H
hzcheng 已提交
30 31 32 33 34
  // connect to server
  if (argc < 2) {
    printf("please input server-ip \n");
    return 0;
  }
H
Haojun Liao 已提交
35

H
hzcheng 已提交
36 37
  // init TAOS
  taos_init();
H
Haojun Liao 已提交
38

H
hzcheng 已提交
39 40
  taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
  if (taos == NULL) {
41
    printf("failed to connect to server, reason:%s\n", "null taos"/*taos_errstr(taos)*/);
H
hzcheng 已提交
42 43
    exit(1);
  }
H
Haojun Liao 已提交
44
  printf("success to connect to server\n");
H
Haojun Liao 已提交
45 46


H
hzcheng 已提交
47
  taos_query(taos, "drop database demo");
S
Shuduo Sang 已提交
48 49 50

  result = taos_query(taos, "create database demo");
  if (result == NULL) {
51
    printf("failed to create database, reason:%s\n", "null result"/*taos_errstr(taos)*/);
H
hzcheng 已提交
52 53 54
    exit(1);
  }
  printf("success to create database\n");
H
Haojun Liao 已提交
55

H
hzcheng 已提交
56
  taos_query(taos, "use demo");
H
Haojun Liao 已提交
57

H
hzcheng 已提交
58
  // create table
S
Shuduo Sang 已提交
59
  if (taos_query(taos, "create table m1 (ts timestamp, ti tinyint, si smallint, i int, bi bigint, f float, d double, b binary(10))") == 0) {
60
    printf("failed to create table, reason:%s\n", taos_errstr(result));
H
hzcheng 已提交
61 62 63
    exit(1);
  }
  printf("success to create table\n");
H
Haojun Liao 已提交
64

H
hzcheng 已提交
65 66
  // sleep for one second to make sure table is created on data node
  // taosMsleep(1000);
H
Haojun Liao 已提交
67

H
hzcheng 已提交
68 69 70
  // insert 10 records
  int i = 0;
  for (i = 0; i < 10; ++i) {
S
TD-1530  
Shengliang Guan 已提交
71
    sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", 1546300800000 + i * 1000, i, i, i, i*10000000, i*1.0, i*2.0, "hello");
S
Shuduo Sang 已提交
72
    printf("qstr: %s\n", qstr);
73 74 75 76 77 78 79 80 81 82 83
    
    // 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 *result = taos_query(taos, qstr);
    if (result) {
      printf("insert row: %i\n", i);
    } else {
      printf("failed to insert row: %i, reason:%s\n", i, "null result"/*taos_errstr(result)*/);
      exit(1);
H
hzcheng 已提交
84
    }
85

H
hzcheng 已提交
86 87 88
    //sleep(1);
  }
  printf("success to insert rows, total %d rows\n", i);
H
Haojun Liao 已提交
89

H
hzcheng 已提交
90 91
  // query the records
  sprintf(qstr, "SELECT * FROM m1");
H
Haojun Liao 已提交
92 93 94
  result = taos_query(taos, qstr);
  if (result == NULL || taos_errno(result) != 0) {
    printf("failed to select, reason:%s\n", taos_errstr(result));
H
hzcheng 已提交
95 96
    exit(1);
  }
H
[TD-98]  
hjxilinx 已提交
97

H
hjxilinx 已提交
98
  TAOS_ROW    row;
H
hzcheng 已提交
99
  int         rows = 0;
S
Shuduo Sang 已提交
100
  int         num_fields = taos_field_count(result);
H
hzcheng 已提交
101
  TAOS_FIELD *fields = taos_fetch_fields(result);
S
Shuduo Sang 已提交
102
  char        temp[1024];
H
Haojun Liao 已提交
103

S
Shuduo Sang 已提交
104
  printf("num_fields = %d\n", num_fields);
H
hjxilinx 已提交
105 106 107 108 109 110 111
  printf("select * from table, result:\n");
  // fetch the records row by row
  while ((row = taos_fetch_row(result))) {
    rows++;
    taos_print_row(temp, row, fields, num_fields);
    printf("%s\n", temp);
  }
H
Haojun Liao 已提交
112

H
hzcheng 已提交
113 114 115 116
  taos_free_result(result);
  printf("====demo end====\n\n");
  return getchar();
}