Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
b886311a
T
TDengine
项目概览
慢慢CG
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b886311a
编写于
6月 03, 2020
作者:
H
Hui Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[add prepare demo file]
上级
be021333
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
195 addition
and
0 deletion
+195
-0
tests/examples/c/prepare.c
tests/examples/c/prepare.c
+195
-0
未找到文件。
tests/examples/c/prepare.c
0 → 100644
浏览文件 @
b886311a
// TAOS standard API example. The same syntax as MySQL, but only a subet
// to compile: gcc -o prepare prepare.c -ltaos
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "taos.h"
void
taosMsleep
(
int
mseconds
);
int
main
(
int
argc
,
char
*
argv
[])
{
TAOS
*
taos
;
TAOS_RES
*
result
;
TAOS_STMT
*
stmt
;
// connect to server
if
(
argc
<
2
)
{
printf
(
"please input server ip
\n
"
);
return
0
;
}
// init TAOS
taos_init
();
taos
=
taos_connect
(
argv
[
1
],
"root"
,
"taosdata"
,
NULL
,
0
);
if
(
taos
==
NULL
)
{
printf
(
"failed to connect to db, reason:%s
\n
"
,
taos_errstr
(
taos
));
exit
(
1
);
}
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
);
}
taos_query
(
taos
,
"use demo"
);
// create table
const
char
*
sql
=
"create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))"
;
if
(
taos_query
(
taos
,
sql
)
!=
0
)
{
printf
(
"failed to create table, reason:%s
\n
"
,
taos_errstr
(
taos
));
exit
(
1
);
}
// sleep for one second to make sure table is created on data node
// taosMsleep(1000);
// insert 10 records
struct
{
int64_t
ts
;
int8_t
b
;
int8_t
v1
;
int16_t
v2
;
int32_t
v4
;
int64_t
v8
;
float
f4
;
double
f8
;
char
bin
[
40
];
char
blob
[
80
];
}
v
=
{
0
};
stmt
=
taos_stmt_init
(
taos
);
TAOS_BIND
params
[
10
];
params
[
0
].
buffer_type
=
TSDB_DATA_TYPE_TIMESTAMP
;
params
[
0
].
buffer_length
=
sizeof
(
v
.
ts
);
params
[
0
].
buffer
=
&
v
.
ts
;
params
[
0
].
length
=
&
params
[
0
].
buffer_length
;
params
[
0
].
is_null
=
NULL
;
params
[
1
].
buffer_type
=
TSDB_DATA_TYPE_BOOL
;
params
[
1
].
buffer_length
=
sizeof
(
v
.
b
);
params
[
1
].
buffer
=
&
v
.
b
;
params
[
1
].
length
=
&
params
[
1
].
buffer_length
;
params
[
1
].
is_null
=
NULL
;
params
[
2
].
buffer_type
=
TSDB_DATA_TYPE_TINYINT
;
params
[
2
].
buffer_length
=
sizeof
(
v
.
v1
);
params
[
2
].
buffer
=
&
v
.
v1
;
params
[
2
].
length
=
&
params
[
2
].
buffer_length
;
params
[
2
].
is_null
=
NULL
;
params
[
3
].
buffer_type
=
TSDB_DATA_TYPE_SMALLINT
;
params
[
3
].
buffer_length
=
sizeof
(
v
.
v2
);
params
[
3
].
buffer
=
&
v
.
v2
;
params
[
3
].
length
=
&
params
[
3
].
buffer_length
;
params
[
3
].
is_null
=
NULL
;
params
[
4
].
buffer_type
=
TSDB_DATA_TYPE_INT
;
params
[
4
].
buffer_length
=
sizeof
(
v
.
v4
);
params
[
4
].
buffer
=
&
v
.
v4
;
params
[
4
].
length
=
&
params
[
4
].
buffer_length
;
params
[
4
].
is_null
=
NULL
;
params
[
5
].
buffer_type
=
TSDB_DATA_TYPE_BIGINT
;
params
[
5
].
buffer_length
=
sizeof
(
v
.
v8
);
params
[
5
].
buffer
=
&
v
.
v8
;
params
[
5
].
length
=
&
params
[
5
].
buffer_length
;
params
[
5
].
is_null
=
NULL
;
params
[
6
].
buffer_type
=
TSDB_DATA_TYPE_FLOAT
;
params
[
6
].
buffer_length
=
sizeof
(
v
.
f4
);
params
[
6
].
buffer
=
&
v
.
f4
;
params
[
6
].
length
=
&
params
[
6
].
buffer_length
;
params
[
6
].
is_null
=
NULL
;
params
[
7
].
buffer_type
=
TSDB_DATA_TYPE_DOUBLE
;
params
[
7
].
buffer_length
=
sizeof
(
v
.
f8
);
params
[
7
].
buffer
=
&
v
.
f8
;
params
[
7
].
length
=
&
params
[
7
].
buffer_length
;
params
[
7
].
is_null
=
NULL
;
params
[
8
].
buffer_type
=
TSDB_DATA_TYPE_BINARY
;
params
[
8
].
buffer_length
=
sizeof
(
v
.
bin
);
params
[
8
].
buffer
=
v
.
bin
;
params
[
8
].
length
=
&
params
[
8
].
buffer_length
;
params
[
8
].
is_null
=
NULL
;
strcpy
(
v
.
blob
,
"一二三四五六七八九十"
);
params
[
9
].
buffer_type
=
TSDB_DATA_TYPE_NCHAR
;
params
[
9
].
buffer_length
=
strlen
(
v
.
blob
);
params
[
9
].
buffer
=
v
.
blob
;
params
[
9
].
length
=
&
params
[
9
].
buffer_length
;
params
[
9
].
is_null
=
NULL
;
int
is_null
=
1
;
sql
=
"insert into m1 values(?,?,?,?,?,?,?,?,?,?)"
;
int
code
=
taos_stmt_prepare
(
stmt
,
sql
,
0
);
if
(
code
!=
0
){
printf
(
"failed to execute taos_stmt_prepare. code:0x%x
\n
"
,
code
);
}
v
.
ts
=
1591060628000
;
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
v
.
ts
+=
1
;
for
(
int
j
=
1
;
j
<
10
;
++
j
)
{
params
[
j
].
is_null
=
((
i
==
j
)
?
&
is_null
:
0
);
}
v
.
b
=
(
int8_t
)
i
%
2
;
v
.
v1
=
(
int8_t
)
i
;
v
.
v2
=
(
int16_t
)(
i
*
2
);
v
.
v4
=
(
int32_t
)(
i
*
4
);
v
.
v8
=
(
int64_t
)(
i
*
8
);
v
.
f4
=
(
float
)(
i
*
40
);
v
.
f8
=
(
double
)(
i
*
80
);
for
(
int
j
=
0
;
j
<
sizeof
(
v
.
bin
)
-
1
;
++
j
)
{
v
.
bin
[
j
]
=
(
char
)(
i
+
'0'
);
}
taos_stmt_bind_param
(
stmt
,
params
);
taos_stmt_add_batch
(
stmt
);
}
if
(
taos_stmt_execute
(
stmt
)
!=
0
)
{
printf
(
"failed to execute insert statement.
\n
"
);
exit
(
1
);
}
taos_stmt_close
(
stmt
);
printf
(
"==== success inset data ====.
\n
"
);
// query the records
stmt
=
taos_stmt_init
(
taos
);
taos_stmt_prepare
(
stmt
,
"SELECT * FROM m1 WHERE v1 > ? AND v2 < ?"
,
0
);
v
.
v1
=
5
;
v
.
v2
=
15
;
taos_stmt_bind_param
(
stmt
,
params
+
2
);
if
(
taos_stmt_execute
(
stmt
)
!=
0
)
{
printf
(
"failed to execute select statement.
\n
"
);
exit
(
1
);
}
result
=
taos_stmt_use_result
(
stmt
);
TAOS_ROW
row
;
int
rows
=
0
;
int
num_fields
=
taos_num_fields
(
result
);
TAOS_FIELD
*
fields
=
taos_fetch_fields
(
result
);
char
temp
[
256
];
// 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
);
}
taos_free_result
(
result
);
taos_stmt_close
(
stmt
);
return
getchar
();
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录