Home  >  Article  >  Database  >  Getting Started with MySQL: Using C Language to Operate MySQL

Getting Started with MySQL: Using C Language to Operate MySQL

黄舟
黄舟Original
2017-01-19 15:49:282427browse

Basic concepts

C APIs are included in the mysqlclient library file, which is released together with the MySQL source code and is used to connect to the database and execute database queries.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <mysql/mysql.h>
int main()
{
    int     ret = 0;
    MYSQL   mysql;
    MYSQL   *con = NULL;
    con = mysql_init(&mysql);
    if (con == NULL)
    {
        ret = mysql_errno(&mysql);
        printf("func mysql_init() err :%d\n", ret);
        return ret;
    }
    //连接mysql服务器
    //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) ;
    con = mysql_real_connect(&mysql, "localhost", "root", "123456", "mydb2", 0, NULL, 0 );
    if (con == NULL)
    {
        ret = mysql_errno(&mysql);
        printf("func mysql_real_connect() err :%d\n", ret);
        return ret;
    }
    else
    {
        printf("func mysql_real_connect() ok\n");
    }
    mysql_close(&mysql);
    return ret;
}

Programming steps

1 Initialize the MYSQL library by calling mysql_library_init()
2 Initialize the connection handler by calling mysql_init(), and connect to the server by calling mysql_real_connect()
3 Issue SQL statements and process their results
4 Close the connection with the MYSQL server by calling mysql_close()
5 End the use of the MYSQL library by calling mysql_library_end()

Things to note when compiling Question 1:
[mysql01@localhost dm01]$ gcc -o dm11_hello dm11_hello.c -I/usr/include/ -L/usr/lib64/mysql/ -lmysqlclient
/usr/lib64/mysql// libmysqlclient.a(net_serv.cc.o):(.data.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to __gxx_personality_v0'
/usr/lib64/mysql//libmysqlclient.a (password.c.o): In functionscramble_323':

You need to use the c++ dynamic library, add the -lstdc++ option in the compilation option

Question 2
/usr/lib64/mysql/ /libmysqlclient.a(dso_dlfcn.o): In function dlfcn_globallookup':
dso_dlfcn.c:(.text+0x31): undefined reference todlopen'
dso_dlfcn.c:(.text+0x44): undefined reference to dlsym'
dso_dlfcn.c:(.text+0x4f): undefined reference todlclose'
/usr/lib64/mysql//libmysqlclient.a(dso_dlfcn.o): In function dlfcn_pathbyaddr':
dso_dlfcn. c:(.text+0xa0): undefined reference todladdr'
dso_dlfcn.c:(.text+0x101): undefined reference to dlerror'
/usr/lib64/mysql//libmysqlclient.a(dso_dlfcn.o ): In functiondlfcn_bind_func':
dso_dlfcn.c:(.text+0x464): undefined reference to `dlsym'

For forward and reverse calls of the callback function, you need to use the dl function library, in the compilation options Add -ldl option

Question 3

thread_mutex_trylock'
/usr/lib64/mysql//libmysqlclient.a(my_thr_init.c.o): In function my_thread_global_end':
/ pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/mysys/my_thr_init.c:214: undefined reference topthread_key_delete'
/pb2/build/ sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysys/my_thr_init.c:217: undefined reference to pthread_mutexattr_destroy'
/pb2/build/sb_0-127349 09 -1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/mysys/my_thr_init.c:220: undefined reference topthread_mutexattr_destroy'

MySQL's dynamic library uses multi-threading. So add the -lpthread option to the compilation options

4. Question 4

[mysql01@localhost dm01]$ gcc -o dm11_hello dm11_hello.c -I/usr/include/ -L/usr/ lib64/mysql/ -lmysqlclient -ldl -lstdc++ -lpthread
/usr/lib64/mysql//libmysqlclient.a(my_getsystime.c.o): In function my_getsystime':
/pb2/build/sb_0-12734909-1406113305.48 /rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/mysys/my_getsystime.c:44: undefined reference toclock_gettime'
collect2: ld returns 1
[mysql01@localhost dm01] $

Lack of runtime dynamic library and math library, add -lm and -lrt options

Complete gcc compilation command:

gcc -o hello hello.c -I /usr/include/mysql/ -L/usr/lib/i386-linux-gnu/ -lmysqlclient -lm -ldl -lstdc++ -lpthread -lrt

General Makefile preparation

.PHONY:clean all
CC=gcc
CFLAGS=-Wall -g
LFLAGS=-L/usr/lib/i386-linux-gnu/ -lmysqlclient -ldl -lpthread -lm -lrt -lstdc++
BIN=hello 
all:$(BIN)
%.o:%.c
    $(CC) $(CFLAGS)  -c $<  -o   $@ 
hello:hello.o 
    $(CC) $(CFLAGS) $^  $(LFLAGS) -o  $@ 
clean:
    rm -f *.o $(BIN)

The above is Introduction to MySQL: C language to operate MySQL. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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