您的位置:首页 > 编程语言 > C语言/C++

C语言连接mysql数据库

2016-02-14 18:12 435 查看
官方文档

一.安装Connector/C

以源码方式安装,依赖cmake,如果系统中没有cmake需要先安装,

cmake安装:cmake官网,下载cmake源码,解压,./bootstrap,make,make install;

Connector/C安装:

1)下载mysql-connector-c-6.1.6-src.tar.gz, (下载)[]http://dev.mysql.com/downloads/connector/c/]

2)tar -xvf mysql-connector-c-6.1.6-src.tar.gz;cd mysql-connector-c-6.1.6-src

3)cmake -G “Unix Makefiles” -DCMAKE_INSTALL_PREFIX=/mydir/mysql

4)make; make install

安装后的文件结构如下:

$ tree /mydir/mysql
/mydir/mysql
├── bin
│   ├── my_print_defaults
│   ├── mysql_config
│   └── perror
├── COPYING
├── docs
│   ├── ChangeLog
│   ├── INFO_BIN
│   └── INFO_SRC
├── include
│   ├── big_endian.h
│   ├── byte_order_generic.h
│   ├── byte_order_generic_x86.h
│   ├── decimal.h
│   ├── errmsg.h
│   ├── keycache.h
│   ├── little_endian.h
│   ├── m_ctype.h
│   ├── m_string.h
│   ├── my_alloc.h
│   ├── my_byteorder.h
│   ├── my_compiler.h
│   ├── my_config.h
│   ├── my_dbug.h
│   ├── my_dir.h
│   ├── my_getopt.h
│   ├── my_global.h
│   ├── my_list.h
│   ├── my_pthread.h
│   ├── mysql
│   │   ├── client_authentication.h
│   │   ├── client_plugin.h
│   │   ├── client_plugin.h.pp
│   │   ├── get_password.h
│   │   ├── plugin_auth_common.h
│   │   ├── plugin_trace.h
│   │   ├── psi
│   │   │   ├── mysql_file.h
│   │   │   ├── mysql_idle.h
│   │   │   ├── mysql_mdl.h
│   │   │   ├── mysql_memory.h
│   │   │   ├── mysql_ps.h
│   │   │   ├── mysql_socket.h
│   │   │   ├── mysql_sp.h
│   │   │   ├── mysql_stage.h
│   │   │   ├── mysql_statement.h
│   │   │   ├── mysql_table.h
│   │   │   ├── mysql_thread.h
│   │   │   ├── mysql_transaction.h
│   │   │   ├── psi_base.h
│   │   │   ├── psi.h
│   │   │   └── psi_memory.h
│   │   ├── service_my_snprintf.h
│   │   └── service_mysql_alloc.h
│   ├── mysql_com.h
│   ├── mysql_com_server.h
│   ├── mysqld_ername.h
│   ├── mysqld_error.h
│   ├── mysql_embed.h
│   ├── mysql.h
│   ├── mysql_time.h
│   ├── mysql_version.h
│   ├── my_sys.h
│   ├── my_xml.h
│   ├── sql_common.h
│   ├── sql_state.h
│   ├── sslopt-case.h
│   ├── sslopt-longopts.h
│   ├── sslopt-vars.h
│   └── typelib.h
├── lib
│   ├── libmysqlclient.a
│   ├── libmysqlclient_r.a -> libmysqlclient.a
│   ├── libmysqlclient_r.so -> libmysqlclient.so
│   ├── libmysqlclient_r.so.18 -> libmysqlclient.so.18
│   ├── libmysqlclient_r.so.18.3.0 -> libmysqlclient.so.18.3.0
│   ├── libmysqlclient.so -> libmysqlclient.so.18
│   ├── libmysqlclient.so.18 -> libmysqlclient.so.18.3.0
│   └── libmysqlclient.so.18.3.0
└── README

6 directories, 74 files


二.测试代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mysql/mysql.h>

#define MAX_COLUMA_LEN 32

int main()
{
MYSQL my_conn;
MYSQL_RES *result;
MYSQL_ROW sql_row;
MYSQL_FIELD *fd;

char column[MAX_COLUMA_LEN][MAX_COLUMA_LEN];
int res;

mysql_init(&my_conn);

if(NULL == mysql_real_connect(&my_conn, "192.168.x.x", "root", "xxx", "dbname", 3306, NULL, 0))
{
printf("connect failed !");
return -1;
}

//printf("&my_conn:%ld, sock:%ld", &my_conn, sock);

res = mysql_query(&my_conn, "select * from tabname");

if(!res)
{
result=mysql_store_result(&my_conn);//保存查询到的数据到result
if(result)
{
int i,j;
printf("the result number is %lu\n ",(unsigned long)mysql_num_rows(result));
for(i=0;fd=mysql_fetch_field(result);i++)//获取列名
{
bzero(column[i],sizeof(column[i]));
strcpy(column[i],fd->name);
}
j=mysql_num_fields(result);
for(i=0;i<j;i++)
{
printf("%s\t",column[i]);
}
printf("\n");
while(sql_row=mysql_fetch_row(result))//获取具体的数据
{
for(i=0;i<j;i++)
{
printf("%s\t",sql_row[i]);
}
printf("\n");
}

mysql_free_result(result);
result = NULL;
}

}
else
{
perror("select");
}

mysql_close(&my_conn);

return 0;
}


Makefile

all:test_mysql.c
gcc -o  testmysql test_mysql.c -I/mydir/mysql/include -L/mydir/mysql/lib -lmysqlclient -g


测试OK!

更多用法参考:MySQL C API
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: