C连接mysql

      我是在Ubuntu系统下测试的

      此系统下,mysql安装: apt-get install mysql-server 等了有10多分钟

      然后又装了mysql-client: apt-get install mysql-client

      还不够还需要mysql的开发包: apt-get install libmysql++

     没有权限的话sudo 你懂的

      敲下代码

#include <stdlib.h>

#include "mysql.h"

int main(void){
        MYSQL *conn_ptr;

        conn_ptr=mysql_init(NULL);
        if(!conn_ptr){
                fprintf(stderr,"mysql_init failed \n");
                return  EXIT_FAILURE;
        }

        conn_ptr  = mysql_real_connect(conn_ptr,"localhost","root","111111","mysql",0,NULL,0);

        if(conn_ptr){
                printf("Connection success\n");
        }else{
                printf("Connection failed\n");
        }

        mysql_close(conn_ptr);
        return EXIT_SUCCESS;
}

 编译

gcc -o testmysql testmysql.c

testmysql.c:4:19: fatal error: mysql.h: No such file or directory
compilation terminated.

 

然后修改gcc -o testmysql testmysql.c  -I/usr/include/mysql/

/tmp/ccW7qMov.o: In function `main':
testmysql.c:(.text+0x11): undefined reference to `mysql_init'
testmysql.c:(.text+0x8f): undefined reference to `mysql_real_connect'
testmysql.c:(.text+0xc0): undefined reference to `mysql_close'
collect2: ld returned 1 exit statu

 

最后修改

gcc -o testmysql testmysql.c -I/usr/include/mysql/ -lmysqlclient -Wall -g

成功

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值