---恢复内容开始---

1. 安装MySQL官方API库

c  API   libmysqlclient-dev

c++  API  libmysql++-dev

MySQL c/c++ API

 

2.安装后查看头文件

MySQL c/c++ API

头文件一般就在默认头文件 /usr/include 下

 

3.使用c  API   libmysqlclient-dev简单连接操作

//test_1.c

#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char server[] = "localhost";
char user[] = "capritu";
char password[] = "user";
char database[] = "password";

conn = mysql_init(NULL);

if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0))
{
  fprintf(stderr, "%s\n", mysql_error(conn));
  exit(1);
}

if (mysql_query(conn, "show tables"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

res = mysql_use_result(conn);

printf("MySQL Tables in mysql database:\n");

while ((row = mysql_fetch_row(res)) != NULL)
{
printf("%s \n", row[0]);
}

mysql_free_result(res);
mysql_close(conn);

printf("finish! \n");
return 0;
}

 

4.程序编译

gcc -o -1 test_1.c  -lmysqlclient

 注意需要链接动态库,否则会提示相关函数未定义

 

5.执行

./1

MySQL c/c++ API

 

相关文章:

  • 2021-11-12
  • 2022-01-30
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-08
  • 2021-08-22
  • 2022-12-23
  • 2021-10-05
  • 2021-11-13
  • 2022-12-23
相关资源
相似解决方案