linux系统下c语言连接mysql数据库时有关mysql_error这个函数的返回值的问题,求知情人士

linux系统下c语言连接mysql数据库时有关mysql_error这个函数的返回值的问题,求知情人士,第1张

应该这样用,连接的时候不要指定数据库,分步 *** 作和检测

if(!mysql_real_connect(character_ptr,"localhost","root","24157518",

NULL

,0,NULL,0))

printf("Error

connecting

to

server:

%s\n",

mysql_error(character_ptr))

if(!mysql_select_db(character_ptr,

"test_database"))

printf("Error

connecting

to

database:

%s\n",

mysql_error(character_ptr))

帮你一下吧,我只能粗写,你自己详细参考一下函数库,下面localhost,是数据库服务器的地址,

username,password分别为访问数据库所需要的用户名和密码,database是数据库的名称,请你参考MYSQL结构体以获取更加详细的参数信息

#include <stdlib.h>

#include<stdio.h>

#include "mysql.h"

int main(int argc,char**argv){

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","username","password","database",0,NULL,0)

if(conn_ptr){

printf("Connection success\n")

}else{

printf("Connection failed\n")

}

mysql_close(conn_ptr)

return EXIT_SUCCESS

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/9017951.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-24
下一篇 2023-04-24

发表评论

登录后才能评论

评论列表(0条)

保存