5、通过api操作mysql的小例子

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 今天写了个简单的mysql程序,通过C语言。 示例代码 View Code #include "stdio.h"#include "mysql.h"#include #include "stdlib.

今天写了个简单的mysql程序,通过C语言。

示例代码

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif View Code
#include "stdio.h"
#include
"mysql.h"
#include
<pthread.h>
#include
"stdlib.h"
#include
"string"
#include
"iostream"

using namespace std;

typedef
struct SqlInfo
{
string server;
string user;
string passwd;
string db;
}SqlInfo;

void* showdatabase(void* arg)
{
SqlInfo
*p = (SqlInfo*)arg;

MYSQL
*mysql_test;
MYSQL_RES
*ret;
MYSQL_ROW row;

// char *server = "localhost";
// char *user = "root";
// char *passwd = "1234"; //伪密码
// char *db = "mysql";

mysql_test
= mysql_init(NULL);
if (!mysql_real_connect(mysql_test, p->server.c_str(), p->user.c_str(), p->passwd.c_str(), p->db.c_str(), 0, NULL, 0))
{
fprintf(stderr,
"%s\n", mysql_error(mysql_test));
return NULL;
}

if (mysql_query(mysql_test, "show databases"))
{
fprintf(stderr,
"%s\n", mysql_error(mysql_test));
return NULL;
}
ret
= mysql_use_result(mysql_test);

printf(
"Show Databases:\n");
// while((row = mysql_fetch_row(ret)) != NULL)
// {
// printf("%s\n", row[0]);
// }

//equal to the below
unsigned int num_fields;
unsigned
int i;

num_fields
= mysql_num_fields(ret);
while ((row = mysql_fetch_row(ret)))
{
unsigned
long *lengths;
lengths
= mysql_fetch_lengths(ret);
for(i = 0; i < num_fields; i++)
{
printf(
"[%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL");
}
printf(
"\n");
}

mysql_free_result(ret);
mysql_close(mysql_test);
}

int main(int argc, char* argv[])
{
if (argc < 5)
{
cout
<< "Usage: app server user passwd db" << endl;
return -1;
}

SqlInfo my_info;
my_info.server
= argv[1];
my_info.user
= argv[2];
my_info.passwd
= argv[3];
my_info.db
= argv[4];

pthread_t thread1;

pthread_create(
&thread1, NULL, showdatabase, (void*)&my_info);
pthread_join(thread1,NULL);

return 0;
}

1、遇到的问题

1)error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory

再网上下载了一个so文件。

//locate libmysqlclient.so.16

复制文件到路径:

    /usr/local/mysql/lib/mysql/libmysqlclient.so.16

修改文件:

    vi /etc/ld.so.conf

    增加一行 /usr/local/mysql/lib/mysql

然后执行ldconfig生效。

2、关于C语言中调用mysql API

gcc -o showdata $(mysql_config --cflags) showdata.cpp  $(mysql_config --libs)

    mysql_config为MySQL中一个特殊的脚本,为编译MySQL客户端,并连接到MySQL服务器提供有用的信息。【3

    当然也要吧在makefile中指定如上选项,或

INC+=-I/usr/include/mysql
LIB=-lmysqlclient -lpthread -lssl

附:

mysql的安装:【45

要注意的一点是,安装的mysql版本要与操作系统版本匹配。

安装完后,通过以下命令设置密码:

/usr/bin/mysqladmin -u root password 'new-password'

/usr/bin/mysqladmin -u root -h love password 'new-password'

Alternatively you can run:

/usr/bin/mysql_secure_installation


You can start the MySQL daemon with:
rcmysql start
You can test the MySQL daemon with mysql-test package

参考

1http://download.csdn.net/source/3528499

2http://blog.sina.com.cn/s/blog_4ab24dd50100wnkv.html

3http://blog.sina.com.cn/s/blog_59d470310100i16r.html

4http://www.iteye.com/topic/647152

5http://www.mysql.com/downloads/mysql/#downloads

【6】 http://wenku.baidu.com/view/5e093ddc5022aaea998f0fbc.html

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
Java API Maven
HDFS的API操作
HDFS的API操作
26 0
|
1月前
|
SQL 关系型数据库 MySQL
Python怎么操作Mysql数据库
Python怎么操作Mysql数据库
51 0
|
1月前
|
SQL 关系型数据库 MySQL
python如何操作mysql数据库
python如何操作mysql数据库
24 0
|
2月前
|
SQL 存储 算法
mysql常用指令操作
mysql常用指令操作
|
22天前
|
存储 SQL 关系型数据库
【MySQL】4. 表的操作
【MySQL】4. 表的操作
21 0
|
2月前
|
API
Poi 中文API文档 「40种操作 Excel文件的姿势」
Poi 中文API文档 「40种操作 Excel文件的姿势」
117 0
|
3月前
|
关系型数据库 MySQL
MySQL对小数进行四舍五入等操作
MySQL对小数进行四舍五入等操作
25 0
|
3月前
|
JSON API 数据格式
在钉钉开放平台中,创建或更新OA审批表单模板需要通过API接口进行操作
在钉钉开放平台中,创建或更新OA审批表单模板需要通过API接口进行操作【1月更文挑战第20天】【1月更文挑战第97篇】
52 1
|
1月前
|
SQL 关系型数据库 MySQL
|
3月前
|
SQL 关系型数据库 MySQL
MySQL | 数据库的管理和操作【表的增删改查】(二)
MySQL | 数据库的管理和操作【表的增删改查】(二)