The Connector section of the official website reconstructs the C language interface description

This commit is contained in:
Yaming Pei 2024-08-03 14:31:45 +08:00
parent 975ce64a14
commit 7f09f18c07
11 changed files with 222 additions and 218 deletions

View File

@ -14,11 +14,17 @@ int main() {
TAOS *taos = taos_connect(host, user, passwd, db, port);
if (taos == NULL) {
int errno = taos_errno(NULL);
char *msg = taos_errstr(NULL);
const char *msg = taos_errstr(NULL);
printf("%d, %s\n", errno, msg);
} else {
printf("connected\n");
taos_close(taos);
printf("failed to connect to server %s, errno: %d, msg: %s\n", host, errno, msg);
taos_cleanup();
return -1;
}
printf("success to connect server %s\n", host);
/* put your code here for read and write */
// close & clean
taos_close(taos);
taos_cleanup();
}

View File

@ -14,7 +14,7 @@
*/
// TAOS standard API example. The same syntax as MySQL, but only a subset
// to compile: gcc -o CCreateDBDemo CCreateDBDemo.c -ltaos
// to compile: gcc -o create_db_demo create_db_demo.c -ltaos
#include <inttypes.h>
#include <stdio.h>
@ -32,22 +32,23 @@ const char *password = "taosdata";
// connect
TAOS *taos = taos_connect(ip, user, password, NULL, 0);
if (taos == NULL) {
printf("failed to connect to server, reason: %s\n", taos_errstr(NULL));
printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL));
taos_cleanup();
return -1;
}
printf("success to connect server %s\n", ip);
// create database
TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power");
int code = taos_errno(result);
if (code != 0) {
printf("failed to create database, reason: %s\n", taos_errstr(result));
printf("failed to create database power, reason: %s\n", taos_errstr(result));
taos_close(taos);
taos_cleanup();
return -1;
}
taos_free_result(result);
printf("success to create database\n");
printf("success to create database power\n");
// use database
result = taos_query(taos, "USE power");
@ -58,13 +59,13 @@ const char* sql = "CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLO
result = taos_query(taos, sql);
code = taos_errno(result);
if (code != 0) {
printf("failed to create table, reason: %s\n", taos_errstr(result));
printf("failed to create stable meters, reason: %s\n", taos_errstr(result));
taos_close(taos);
taos_cleanup();
return -1;
}
taos_free_result(result);
printf("success to create table\n");
printf("success to create table meters\n");
// close & clean
taos_close(taos);

View File

@ -14,7 +14,7 @@
*/
// TAOS standard API example. The same syntax as MySQL, but only a subset
// to compile: gcc -o CInsertDataDemo CInsertDataDemo.c -ltaos
// to compile: gcc -o insert_data_demo insert_data_demo.c -ltaos
#include <inttypes.h>
#include <stdio.h>
@ -31,10 +31,11 @@ const char *password = "taosdata";
// connect
TAOS *taos = taos_connect(ip, user, password, NULL, 0);
if (taos == NULL) {
printf("failed to connect to server, reason: %s\n", taos_errstr(NULL));
printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL));
taos_cleanup();
return -1;
}
printf("success to connect server %s\n", ip);
// use database
TAOS_RES *result = taos_query(taos, "USE power");
@ -53,7 +54,7 @@ const char* sql = "INSERT INTO "
result = taos_query(taos, sql);
int code = taos_errno(result);
if (code != 0) {
printf("failed to insert rows, reason: %s\n", taos_errstr(result));
printf("failed to insert data to power.meters, ip: %s, reason: %s\n", ip, taos_errstr(result));
taos_close(taos);
taos_cleanup();
return -1;
@ -62,7 +63,7 @@ taos_free_result(result);
// you can check affectedRows here
int rows = taos_affected_rows(result);
printf("success to insert %d rows\n", rows);
printf("success to insert %d rows data to power.meters\n", rows);
// close & clean
taos_close(taos);

View File

@ -14,7 +14,7 @@
*/
// TAOS standard API example. The same syntax as MySQL, but only a subset
// to compile: gcc -o CQueryDataDemo CQueryDataDemo.c -ltaos
// to compile: gcc -o query_data_demo query_data_demo.c -ltaos
#include <inttypes.h>
#include <stdio.h>
@ -32,21 +32,22 @@ const char *password = "taosdata";
// connect
TAOS *taos = taos_connect(ip, user, password, NULL, 0);
if (taos == NULL) {
printf("failed to connect to server, reason: %s\n", taos_errstr(NULL));
printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL));
taos_cleanup();
return -1;
}
printf("success to connect server %s\n", ip);
// use database
TAOS_RES *result = taos_query(taos, "USE power");
taos_free_result(result);
// query data, please make sure the database and table are already created
const char* sql = "SELECT * FROM power.meters";
const char* sql = "SELECT ts, current, location FROM power.meters limit 100";
result = taos_query(taos, sql);
int code = taos_errno(result);
if (code != 0) {
printf("failed to select, reason: %s\n", taos_errstr(result));
printf("failed to query data from power.meters, ip: %s, reason: %s\n", ip, taos_errstr(result));
taos_close(taos);
taos_cleanup();
return -1;
@ -69,6 +70,7 @@ while ((row = taos_fetch_row(result))) {
}
printf("total rows: %d\n", rows);
taos_free_result(result);
printf("success to query data from power.meters\n");
// close & clean
taos_close(taos);

View File

@ -14,7 +14,7 @@
*/
// TAOS standard API example. The same syntax as MySQL, but only a subset
// to compile: gcc -o CSmlInsertDemo CSmlInsertDemo.c -ltaos
// to compile: gcc -o sml_insert_demo sml_insert_demo.c -ltaos
#include <stdio.h>
#include <stdlib.h>
@ -31,22 +31,23 @@ const char *password = "taosdata";
// connect
TAOS *taos = taos_connect(ip, user, password, NULL, 0);
if (taos == NULL) {
printf("failed to connect to server, reason: %s\n", taos_errstr(NULL));
printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL));
taos_cleanup();
return -1;
}
printf("success to connect server %s\n", ip);
// create database
TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power");
int code = taos_errno(result);
if (code != 0) {
printf("failed to create database, reason: %s\n", taos_errstr(result));
printf("failed to create database power, reason: %s\n", taos_errstr(result));
taos_close(taos);
taos_cleanup();
return -1;
}
taos_free_result(result);
printf("success to create database\n");
printf("success to create database power\n");
// use database
result = taos_query(taos, "USE power");

View File

@ -14,7 +14,7 @@
*/
// TAOS standard API example. The same syntax as MySQL, but only a subset
// to compile: gcc -o CStmtInsertDemo CStmtInsertDemo.c -ltaos
// to compile: gcc -o stmt_insert_demo stmt_insert_demo.c -ltaos
#include <stdio.h>
#include <stdlib.h>

View File

@ -14,7 +14,7 @@
*/
// TAOS standard API example. The same syntax as MySQL, but only a subset
// to compile: gcc -o CWithReqIdDemo CWithReqIdDemo.c -ltaos
// to compile: gcc -o with_reqid_demo with_reqid_demo.c -ltaos
#include <inttypes.h>
#include <stdio.h>
@ -32,23 +32,24 @@ const char *password = "taosdata";
// connect
TAOS *taos = taos_connect(ip, user, password, NULL, 0);
if (taos == NULL) {
printf("failed to connect to server, reason: %s\n", taos_errstr(NULL));
printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL));
taos_cleanup();
return -1;
}
printf("success to connect server %s\n", ip);
// create database
TAOS_RES *result = taos_query_with_reqid(taos, "CREATE DATABASE IF NOT EXISTS power", 1L);
int code = taos_errno(result);
if (code != 0) {
printf("failed to create database, reason: %s\n", taos_errstr(result));
printf("failed to create database power, reason: %s\n", taos_errstr(result));
taos_free_result(result);
taos_close(taos);
taos_cleanup();
return -1;
}
taos_free_result(result);
printf("success to create database\n");
printf("success to create database power\n");
// use database
result = taos_query_with_reqid(taos, "USE power", 2L);
@ -59,7 +60,7 @@ const char* sql = "SELECT * FROM power.meters";
result = taos_query_with_reqid(taos, sql, 3L);
code = taos_errno(result);
if (code != 0) {
printf("failed to select, reason: %s\n", taos_errstr(result));
printf("failed to query data from power.meters, ip: %s, reason: %s\n", ip, taos_errstr(result));
taos_close(taos);
taos_cleanup();
return -1;
@ -82,6 +83,7 @@ while ((row = taos_fetch_row(result))) {
}
printf("total rows: %d\n", rows);
taos_free_result(result);
printf("success to query data from power.meters\n");
// close & clean
taos_close(taos);

View File

@ -380,16 +380,7 @@ DSN 的详细说明和如何使用详见 [连接功能](../../reference/connecto
下面为建立连接的示例代码,其中省略了查询和写入部分,展示了如何建立连接、关闭连接以及清除资源。
```c
TAOS *taos = taos_connect("localhost:6030", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("failed to connect to server, reason:%s\n", "null taos" /*taos_errstr(taos)*/);
exit(1);
}
/* put your code here for read and write */
taos_close(taos);
taos_cleanup();
{{#include docs/examples/c/connect_example.c}}
```
在上面的示例代码中, `taos_connect()` 建立到客户端程序所在主机的 6030 端口的连接,`taos_close()`关闭当前连接,`taos_cleanup()`清除客户端驱动所申请和使用的资源。

View File

@ -69,7 +69,7 @@ REST API直接调用 `taosadapter` 提供的 REST API 接口,进行数据
</TabItem>
<TabItem label="C" value="c">
```c
{{#include docs/examples/c/CCreateDBDemo.c:create_db_and_table}}
{{#include docs/examples/c/create_db_demo.c:create_db_and_table}}
```
> **注意**:如果不使用 `USE power` 指定数据库,则后续对表的操作都需要增加数据库名称作为前缀,如 power.meters。
</TabItem>
@ -146,7 +146,7 @@ NOW 为系统内部函数,默认为客户端所在计算机当前时间。 NOW
</TabItem>
<TabItem label="C" value="c">
```c
{{#include docs/examples/c/CInsertDataDemo.c:insert_data}}
{{#include docs/examples/c/insert_data_demo.c:insert_data}}
```
**Note**
@ -215,7 +215,7 @@ curl --location -uroot:taosdata 'http://127.0.0.1:6041/rest/sql' \
</TabItem>
<TabItem label="C" value="c">
```c
{{#include docs/examples/c/CQueryDataDemo.c:query_data}}
{{#include docs/examples/c/query_data_demo.c:query_data}}
```
</TabItem>
<TabItem label="REST API" value="rest">
@ -290,7 +290,7 @@ reqId 可用于请求链路追踪reqId 就像分布式系统中的 traceId
</TabItem>
<TabItem label="C" value="c">
```c
{{#include docs/examples/c/CWithReqIdDemo.c:with_reqid}}
{{#include docs/examples/c/with_reqid_demo.c:with_reqid}}
```
</TabItem>
<TabItem label="REST API" value="rest">

View File

@ -246,7 +246,7 @@ writer.write(lineDemo, SchemalessProtocolType.LINE, SchemalessTimestampType.NANO
</TabItem>
<TabItem label="C" value="c">
```c
{{#include docs/examples/c/CSmlInsertDemo.c:schemaless}}
{{#include docs/examples/c/sml_insert_demo.c:schemaless}}
```
</TabItem>

View File

@ -99,7 +99,7 @@ import TabItem from "@theme/TabItem";
</TabItem>
<TabItem label="C" value="c">
```c
{{#include docs/examples/c/CStmtInsertDemo.c}}
{{#include docs/examples/c/stmt_insert_demo.c}}
```
</TabItem>