demo.c 4.8 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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/>.
 */

// TAOS standard API example. The same syntax as MySQL, but only a subet
// to compile: gcc -o demo demo.c -ltaos

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
23
#include <taos.h>  // TAOS header file
H
hzcheng 已提交
24 25 26

void taosMsleep(int mseconds);

H
[TD-98]  
hjxilinx 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
static int32_t doQuery(TAOS* taos, const char* sql) {
  int32_t code = taos_query(taos, sql);
  if (code != 0) {
    printf("failed to execute query, reason:%s\n", taos_errstr(taos));
    return -1;
  }
  
  TAOS_RES* res = taos_use_result(taos);
  TAOS_ROW row = NULL;
  char buf[512] = {0};
  
  int32_t numOfFields = taos_num_fields(res);
  TAOS_FIELD* pFields = taos_fetch_fields(res);
  
  while((row = taos_fetch_row(res)) != NULL) {
    taos_print_row(buf, row, pFields, numOfFields);
    printf("%s\n", buf);
    memset(buf, 0, 512);
  }
  
  taos_free_result(res);
sangshuduo's avatar
sangshuduo 已提交
48 49

  return 0;
H
[TD-98]  
hjxilinx 已提交
50 51
}

H
hzcheng 已提交
52 53 54 55
int main(int argc, char *argv[]) {
  TAOS *    taos;
  char      qstr[1024];
  TAOS_RES *result;
H
hjxilinx 已提交
56
  
H
[TD-98]  
hjxilinx 已提交
57
  
H
hzcheng 已提交
58 59 60 61 62
  // connect to server
  if (argc < 2) {
    printf("please input server-ip \n");
    return 0;
  }
H
hjxilinx 已提交
63
  
H
[TD-98]  
hjxilinx 已提交
64 65
  taos_options(TSDB_OPTION_CONFIGDIR, "~/sec/cfg");
  
H
hzcheng 已提交
66 67
  // init TAOS
  taos_init();
H
hjxilinx 已提交
68
  
H
hzcheng 已提交
69 70 71 72 73 74
  taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
  if (taos == NULL) {
    printf("failed to connect to server, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
  printf("success to connect to server\n");
H
hjxilinx 已提交
75
  
H
[TD-98]  
hjxilinx 已提交
76 77
  doQuery(taos, "create database if not exists test");
  doQuery(taos, "use test");
H
hjxilinx 已提交
78
//  doQuery(taos, "create table t1(ts timestamp, k binary(12), f nchar(2))");
H
hjxilinx 已提交
79 80 81 82 83
  for(int32_t i = 0; i< 100000; ++i) {
    doQuery(taos, "select m1.ts,m1.a from m1, m2 where m1.ts=m2.ts and m1.a=m2.b;");
    usleep(500000);
  }

H
hjxilinx 已提交
84
//  doQuery(taos, "insert into tm0 values('2020-1-1 1:1:1', 'abc')");
85 86 87 88 89 90 91 92 93 94
//  doQuery(taos, "create table if not exists tm0 (ts timestamp, k int);");
//  doQuery(taos, "insert into tm0 values('2020-1-1 1:1:1', 1);");
//  doQuery(taos, "insert into tm0 values('2020-1-1 1:1:2', 2);");
//  doQuery(taos, "insert into tm0 values('2020-1-1 1:1:3', 3);");
//  doQuery(taos, "insert into tm0 values('2020-1-1 1:1:4', 4);");
//  doQuery(taos, "insert into tm0 values('2020-1-1 1:1:5', 5);");
//  doQuery(taos, "insert into tm0 values('2020-1-1 1:1:6', 6);");
//  doQuery(taos, "insert into tm0 values('2020-1-1 1:1:7', 7);");
//  doQuery(taos, "insert into tm0 values('2020-1-1 1:1:8', 8);");
//  doQuery(taos, "insert into tm0 values('2020-1-1 1:1:9', 9);");
95
//  doQuery(taos, "select sum(k),count(*) from m1 group by a");
H
[TD-98]  
hjxilinx 已提交
96 97 98
  
  taos_close(taos);
  return 0;
H
hjxilinx 已提交
99
  
H
hzcheng 已提交
100 101 102 103 104 105
  taos_query(taos, "drop database demo");
  if (taos_query(taos, "create database demo") != 0) {
    printf("failed to create database, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
  printf("success to create database\n");
H
hjxilinx 已提交
106
  
H
[TD-98]  
hjxilinx 已提交
107
  
H
hzcheng 已提交
108
  taos_query(taos, "use demo");
H
hjxilinx 已提交
109
  
H
[TD-98]  
hjxilinx 已提交
110
  
H
hzcheng 已提交
111 112 113 114 115 116
  // create table
  if (taos_query(taos, "create table m1 (ts timestamp, speed int)") != 0) {
    printf("failed to create table, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
  printf("success to create table\n");
H
hjxilinx 已提交
117
  
H
[TD-98]  
hjxilinx 已提交
118
  
H
hzcheng 已提交
119 120
  // sleep for one second to make sure table is created on data node
  // taosMsleep(1000);
H
hjxilinx 已提交
121
  
H
[TD-98]  
hjxilinx 已提交
122
  
H
hzcheng 已提交
123 124 125
  // insert 10 records
  int i = 0;
  for (i = 0; i < 10; ++i) {
H
huili 已提交
126
    sprintf(qstr, "insert into m1 values (%ld, %d)", 1546300800000 + i * 1000, i * 10);
H
hzcheng 已提交
127 128 129 130 131 132
    if (taos_query(taos, qstr)) {
      printf("failed to insert row: %i, reason:%s\n", i, taos_errstr(taos));
    }
    //sleep(1);
  }
  printf("success to insert rows, total %d rows\n", i);
H
hjxilinx 已提交
133
  
H
[TD-98]  
hjxilinx 已提交
134
  
H
hzcheng 已提交
135 136 137 138 139 140
  // query the records
  sprintf(qstr, "SELECT * FROM m1");
  if (taos_query(taos, qstr) != 0) {
    printf("failed to select, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
H
hjxilinx 已提交
141
  
H
[TD-98]  
hjxilinx 已提交
142
  
H
hzcheng 已提交
143
  result = taos_use_result(taos);
H
hjxilinx 已提交
144
  
H
[TD-98]  
hjxilinx 已提交
145
  
H
hzcheng 已提交
146 147 148 149
  if (result == NULL) {
    printf("failed to get result, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
H
[TD-98]  
hjxilinx 已提交
150 151

//  TAOS_ROW    row;
H
hjxilinx 已提交
152 153
  
  TAOS_ROW    row;
H
hzcheng 已提交
154 155 156 157
  int         rows = 0;
  int         num_fields = taos_field_count(taos);
  TAOS_FIELD *fields = taos_fetch_fields(result);
  char        temp[256];
H
hjxilinx 已提交
158
  
H
[TD-98]  
hjxilinx 已提交
159
  
H
hjxilinx 已提交
160 161 162 163 164 165 166 167
  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
hzcheng 已提交
168 169 170 171
  taos_free_result(result);
  printf("====demo end====\n\n");
  return getchar();
}