小工具知识网

当前位置: 首页 > 行业知识 > MySQL和语言的连接

行业知识

MySQL和语言的连接

2026-11-10 15:37:43 来源:互联网转载

1.下载开发包

���了之前的基础,就可以使用语言来使用数据库了,实际上原生命令行操作数据库的场景比较少,语言级别的库和包才更加常用,接下我来带您学习如何使用 C/C++ 访问 MySQL 客户端。

然后创建一个可以远程登录的用户,并且拥有对一个 coon 数据库的所有权限。

然后去 官网 安装对应的库。

我们这里只演示 C 语言的调用。

但是这个方法比较麻烦,并且易错,因此这里只是简单讲一下流程,我还是推荐您直接使用 yum 来安装。

但是其实我们之前就安装过了,在使用 yum install -y mysql -community-server 时候,就已经安装过了。

可以使用 ls /usr/include/mysql 查看内部有关 MySQL 的头文件。

若没有找到就使用 yum install -y mysql-devel 下载对应的开发工具。

# 查看对应的头文件# ls /usr/include/mysqlbig_endian.h              my_getopt.h         my_xml.hbinary_log_types.h        my_global.h         plugin_audit.hbyte_order_generic.h      my_list.h           plugin_ftparser.hbyte_order_generic_x86.h  mysql               plugin_group_replication.hdecimal.h                 mysql_com.h         plugin.herrmsg.h                  mysql_com_server.h  plugin_keyring.hkeycache.h                mysqld_ername.h     plugin_validate_password.hlittle_endian.h           mysqld_error.h      sql_common.hm_ctype.h                 mysql_embed.h       sql_state.hm_string.h                mysql.h             sslopt-case.hmy_alloc.h                mysql_time.h        sslopt-longopts.hmy_byteorder.h            mysql_version.h     sslopt-vars.hmy_command.h              mysqlx_ername.h     thr_cond.hmy_compiler.h             mysqlx_error.h      thr_mutex.hmy_config.h               mysqlx_version.h    thr_rwlock.hmy_config_x86_64.h        my_sys.h            typelib.hmy_dbug.h                 my_thread.hmy_dir.h                  my_thread_local.h# ls /lib64/mysqllibmysqlclient.a   libmysqlclient.so.20       libmysqlservices.a  pluginlibmysqlclient.so  libmysqlclient.so.20.3.31  mecab

尝试在 C 中使用 MySQL 的接口。

//main.cc 内部#include #include int main(){    printf("mysql client Version: %s\n", mysql_get_client_info()); //获取客户端版本信息    return 0;}

然后使用 g++ main.cc -L/lib64/mysql -lmysqlclient,在我这边回显的结果为 mysql client Version: 5.7.44。

2.使用 API

补充:接口地址。

接下来我们直接通过一个程序来学习接口的使用。

首先先创建一个表,并且设置对应用户的权限,开发云服务器的端口号。

# 创建表结构mysql> create table test_table( id bigint primary key auto_increment, name varchar(32) not null, age int not null, telphone varchar(32) unique );Query OK, 0 rows affected (0.06 sec)mysql> desc test_table;+----------+-------------+------+-----+---------+----------------+| Field    | Type        | Null | Key | Default | Extra          |+----------+-------------+------+-----+---------+----------------+| id       | bigint(20)  | NO   | PRI | NULL    | auto_increment || name     | varchar(32) | NO   |     | NULL    |                || age      | int(11)     | NO   |     | NULL    |                || telphone | varchar(32) | YES  | UNI | NULL    |                |+----------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)

然后编写代码如下:

//main.cc 内部#include #include #include #include const std::string host = "127.0.0.1"; //也可以使用 localhostconst std::string user = "jimou";const std::string passwd = "**********"; //这里我隐去了我的密码const std::string db = "conn";const unsigned int port = 8080;int main(){    //1.获取客户端版本信息    std::cout         std::cerr         std::cerr         std::string sql;                std::cout             break;        }                if (sql == "quit" || sql == "exit")        {            std::cout             std::cerr             MYSQL_RES* res = mysql_store_result(mySql); //MYSQL_RES 是一个类型,指向的对象可以保存查询的数据,把数据全部当作字符串存储起来,可以看作一个 char** arr[],每个元素都是一个 char* arr[],实际上这些空间也是 new/malloc() 出来的,因此最后一定要记得释放            if (res == nullptr)            {                std::cerr                 //读取行数和列数                my_ulonglong rows = mysql_num_rows(res);                my_ulonglong fields = mysql_num_fields(res);                std::cout                     std::cout                     MYSQL_ROW row = mysql_fetch_row(res); //该函数行为类似 C++ 的迭代器,每次遍历自动移动行                    for (int j = 0; j 

上一篇:稻客文档下载秘籍,一键掌握轻松下载!

下一篇:富通天下服务器怎么设置?