C接口访问MySQL

看了下mysql的API,访问数据库也不是太难,当然啥都是看起来容易,做起来难

Windows开发环境不太熟,懒的去配置一堆东西,而且我mysql所有头文件和库文件都存在E盘,我就借用Cygwin,将头文件和库文件copy到相应的/usr/include和/usr/lib目录下,通过GCC来进行编译,进而运行进行访问

(1)mysql_init初始化连接句柄

#include <mysql.h>

MYSQL *mysql_init(MYSQL *);

这里如果参数传递了一个已经存在的mysql数据结构,就会重新初始化;一般来说传递一个NULL给这个函数,会返回一个指向新分配的连接句柄结构的指针

(2)mysql_read_connect实际连接

上面仅仅是分配和初始化了一个结构,还需要提供实际参数来进行实际连接,mysql.h里

MYSQL *mysql_real_connect(MYSQL *connection,        

const char *server_host, const char *sql_user_name, const char *sql_password, const char *db_name, unsigned int port_number, const char *unix_socket_name, unsigned int flags);

参数指针connection必须指向已经被mysql_init初始化过的结构;server_host既可以是主机名也可以是IP地址;sql_user_name和sql_password是数据库用户名和密码;然后是访问的数据库名,端口号,unix_socket_name为NULL;flags用来对一些定义的位模式进行OR操作,使得改变使用协议的某些特性,这个不太明白,先不管,设置为0

如果无法正常连接,函数就会返回NULL

(3)mysql_close关闭连接

 

所以,就直接访问新创建的数据库newbase:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| newbase            |
| test               |
+--------------------+
4 rows in set (0.00 sec)

来了一段:

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

int main(){
        MYSQL *conn_ptr;
        conn_ptr = mysql_init(NULL);
        if (!conn_ptr){
                printf("mysql_init failed\n");
                exit(0);
        }
        conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "xxxx", "newbase", 3306, NULL, 0);

        if (conn_ptr)
                printf("Connection successful!\n");
        else
                printf("Connection failed\n");

        mysql_close(conn_ptr);
        return 0;
}

可是各种库,各种依赖都处理好了,运行结果就是failed,连接失败:

lihui@2015 /cygdrive/e/mysql/mysql-5.6.22-winx64
$ gcc -g -I/usr/include/mysql lihui.c -L/usr/lib/mysql -lmysqlclient -o connect

lihui@2015 /cygdrive/e/mysql/mysql-5.6.22-winx64
$ ./connect.exe
Connection failed

我在想是否是权限问题,于是CMD里来执行可执行程序:

E:\mysql\mysql-5.6.22-winx64\bin>connect.exe
Connection failed

这下就技穷了,gdb了一把,果然还是实际连接的时候出的问题,不知道是不是我这种连接方式就有问题

8               if (!conn_ptr){
(gdb) n
12              conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "xxxx", "newbase", 3306, NULL, 0);
(gdb) p *conn_ptr
$1 = {net = {vio = 0x0, buff = 0x0, buff_end = 0x0, write_pos = 0x0, read_pos = 0x0, fd = 0, remain_in_buf = 0,
    length = 0, buf_length = 0, where_b = 0, max_packet = 0, max_packet_size = 0, pkt_nr = 0, compress_pkt_nr = 0,
    write_timeout = 0, read_timeout = 0, retry_count = 0, fcntl = 0, return_status = 0x0, reading_or_writing = 0 '\000',
    save_char = 0 '\000', unused1 = 0 '\000', unused2 = 0 '\000', compress = 0 '\000', unused3 = 0 '\000', unused = 0x0,
    last_errno = 0, error = 0 '\000', unused4 = 0 '\000', unused5 = 0 '\000', last_error = '\000' <repeats 511 times>,
    sqlstate = "00000", extension = 0x0}, connector_fd = 0x0, host = 0x0, user = 0x0, passwd = 0x0, unix_socket = 0x0,
  server_version = 0x0, host_info = 0x0, info = 0x0, db = 0x0, charset = 0x3eca56160 <my_charset_latin1>, fields = 0x0,
  field_alloc = {free = 0x0, used = 0x0, pre_alloc = 0x0, min_malloc = 0, block_size = 0, block_num = 0,
    first_block_usage = 0, error_handler = 0x0}, affected_rows = 0, insert_id = 0, extra_info = 0, thread_id = 0,
  packet_length = 0, port = 0, client_flag = 0, server_capabilities = 0, protocol_version = 0, field_count = 0,
  server_status = 0, server_language = 0, warning_count = 0, options = {connect_timeout = 0, read_timeout = 0,
    write_timeout = 0, port = 0, protocol = 0, client_flag = 0, host = 0x0, user = 0x0, password = 0x0,
    unix_socket = 0x0, db = 0x0, init_commands = 0x0, my_cnf_file = 0x0, my_cnf_group = 0x0, charset_dir = 0x0,
    charset_name = 0x0, ssl_key = 0x0, ssl_cert = 0x0, ssl_ca = 0x0, ssl_capath = 0x0, ssl_cipher = 0x0,
    shared_memory_base_name = 0x0, max_allowed_packet = 0, use_ssl = 0 '\000', compress = 0 '\000',
    named_pipe = 0 '\000', unused1 = 0 '\000', unused2 = 0 '\000', unused3 = 0 '\000', unused4 = 0 '\000',
    methods_to_use = MYSQL_OPT_GUESS_CONNECTION, client_ip = 0x0, secure_auth = 0 '\000',
    report_data_truncation = 1 '\001', local_infile_init = 0x0, local_infile_read = 0x0, local_infile_end = 0x0,
    local_infile_error = 0x0, local_infile_userdata = 0x0, extension = 0x0}, status = MYSQL_STATUS_READY,
  free_me = 1 '\001', reconnect = 0 '\000', scramble = '\000' <repeats 20 times>, unused1 = 0 '\000', unused2 = 0x0,
  unused3 = 0x0, unused4 = 0x0, unused5 = 0x0, stmts = 0x0, methods = 0x0, thd = 0x0, unbuffered_fetch_owner = 0x0,
  info_buffer = 0x0, extension = 0x0}
(gdb) n
14              if (conn_ptr)
(gdb) p *conn_ptr
Cannot access memory at address 0x0
(gdb)

睡了,有空再找找原因 ~!

发表评论