Home >Database >Mysql Tutorial >mysql下面c连接mysql数据库_MySQL

mysql下面c连接mysql数据库_MySQL

WBOY
WBOYOriginal
2016-06-01 13:02:401207browse

1.安装sudo yum install mysql-devel 安装组件和库

2.

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

int main(int argc, const char *argv[])
{
    MYSQL mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;

    mysql_init(&mysql);
    mysql_real_connect(&mysql, "127.0.0.1", "syswj", "123456",
                        "sysDB", 0, NULL, 0  );
    mysql_query(&mysql, "select * from stu");
    result = mysql_store_result(&mysql);
    while((row = mysql_fetch_row(result)))
    {
    printf("%s  %s  %s  %s  %s\n", row[0], row[1], row[2], row[3], row[4], row[5]);
    }
   mysql_free_result(result);
   mysql_close(&mysql);

    return 0;
} 

3.编译:gcc test -L/usr/lib/mysql -lmysqlclient

4.执行:

[syswj@host document]$ ./a.out 
950  李勇  m  20  CS
951  张三  f  23  EN
952  李四  f  24  DC
953  将大  f  14  DC
954  可大  m  27  CS
955  你好  f  27  CC
[syswj@host document]$  

参考资料:http://www.linuxidc.com/Linux/2011-12/49133.htm

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn