2022-05-21 03:11:57 +00:00
|
|
|
// compile with
|
|
|
|
|
// gcc connect_example.c -o connect_example -ltaos
|
2025-10-13 08:57:47 +00:00
|
|
|
#include <inttypes.h>
|
2022-05-21 03:11:57 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2025-10-13 08:57:47 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <taos.h>
|
2025-06-16 11:50:42 +00:00
|
|
|
#include <unistd.h> // for sleep()
|
2022-05-21 03:11:57 +00:00
|
|
|
int main() {
|
2025-10-13 08:57:47 +00:00
|
|
|
int32_t code = 0;
|
2022-05-21 03:11:57 +00:00
|
|
|
const char *host = "localhost";
|
|
|
|
|
const char *user = "root";
|
|
|
|
|
const char *passwd = "taosdata";
|
2024-08-12 07:01:34 +00:00
|
|
|
const char *db = NULL; // if don't want to connect to a default db, set it to NULL or ""
|
|
|
|
|
uint16_t port = 6030; // 0 means use the default port
|
2022-05-21 03:11:57 +00:00
|
|
|
TAOS *taos = taos_connect(host, user, passwd, db, port);
|
2025-10-13 08:57:47 +00:00
|
|
|
if (taos == NULL) {
|
|
|
|
|
fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
|
|
|
|
|
taos_errstr(NULL));
|
|
|
|
|
taos_cleanup();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
code = taos_options_connection(taos, TSDB_OPTION_CONNECTION_USER_IP, "192.168.1.1");
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
fprintf(stderr, "Failed to set user ip, ErrCode: 0x%x, ErrMessage: %s.\n", taos_errno(taos), taos_errstr(taos));
|
|
|
|
|
taos_close(taos);
|
|
|
|
|
taos_cleanup();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-16 11:50:42 +00:00
|
|
|
sleep(5);
|
|
|
|
|
{
|
|
|
|
|
TAOS_RES *res = taos_query(taos, "show connections");
|
|
|
|
|
if (taos == NULL) {
|
|
|
|
|
fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
|
|
|
|
|
taos_errstr(NULL));
|
|
|
|
|
taos_cleanup();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taos_free_result(res);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 09:10:21 +00:00
|
|
|
fprintf(stdout, "Connected to %s:%hu successfully.\n", host, port);
|
2024-08-03 06:31:45 +00:00
|
|
|
|
|
|
|
|
/* put your code here for read and write */
|
|
|
|
|
|
|
|
|
|
// close & clean
|
|
|
|
|
taos_close(taos);
|
2022-05-21 03:11:57 +00:00
|
|
|
taos_cleanup();
|
|
|
|
|
}
|