数据库连接登录

mysql_real_connect

MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user,
                         const char *passwd, const char *db, unsigned int port,
                         const char *unix_socket, unsigned long client flag)
  • const char *host 是地址,null的时候默认是本机
  • unsigned int port 是端口号,默认是3306。有多个版本mysql的话需要改端口号
  • unix_socket:套接字,相当于是一个文件,目的将文件里面的数据与网卡交互。默认是0或者NULL
  • client flag:标志位。使用时可以去查。其中CLIENT_OPTIONAL_RESULTSET_METADATA包含表信息(包含字段名,类型),如果不需要这些数据,可以把它关掉
  • 执行成功时,他会返回一个FALSE!!

mysql_options(设置项)

  • 超时设定

    int to = 3;
    int re = mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, &to);
    //mysql_options可能执行失败,失败返回一个非0的值
    if (re != 0)	
    {
    	cout << "mysql_options failed!" << mysql_error(&mysql) << endl;
    }
    
  • 自动重连

    //自动重连
    int recon = 1;
    re = mysql_options(&mysql, MYSQL_OPT_RECONNECT, &recon);
    if (re != 0)
    {
    	cout << "mysql_options failed!" << mysql_error(&mysql) << endl;
    }
    

还有其他的关键字可以参考它的定义,这里不一一写了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值