Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysql api--- starts with a small example to get started with mysql programming (1)

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

C APIs is included in the mysqlclient library file and is distributed with the source code of MySQL for connecting to the database and performing database queries

The locate mysql.h command can see that mysql.h is located at / user/include/mysql/mysql.h

A small example takes a look at the general usage of mysql api, which is used to print hello world:

Int main ()

{

Int ret = NULL

MYSQL mysql;MYSQL * connect;//MYSQL_RES * res;//MYSQL_ROW row;//char * query;//int t, r * / mysql_init (& mysql) if (mysql_library_init (0, NULL, NULL)) {fprintf (stderr, "could not initialize MySQL library\ n"); exit (1) / / initialize 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) * / * unsigned int mysql_errno (MYSQL * mysql); const char * mysql_error (MYSQL * mysql) * / connect = mysql_real_connect (& mysql, "localhost", "root", "mysql", "pos", 0, NULL, 0); if (connect = = NULL) {ret = mysql_errno (connect); printf ("func mysql_real_connect () err\ n"); return ret;} else {printf ("ok.\ n");} printf ("connect:%d & mysql:%d\ n", connect, & mysql) Mysql_close (connect); / / mysql_library_end ()

}

From the above, we can see that there are several important functions.

(1) MYSQL mysql_init (MYSQL mysql)

Description

Assigns or initializes MYSQL objects that correspond to mysql_real_connect (). If mysql is a NULL pointer, the function allocates, initializes, and returns a new object. Otherwise, the object is initialized and the address of the object is returned. If mysql_init () allocates a new object, when mysql_close () is called to close the connection. The object is released.

Return value

Initialized MYSQL* handle. If there is not enough memory to allocate new objects, return NULL.

Error

Returns NULL if there is not enough memory.

(2) 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)

Description

Mysql_real_connect () attempts to establish a connection with the MySQL database engine running on the host. Before you can execute any other API functions that require a valid MySQL connection handle structure, mysql_real_connect () must complete successfully.

Parameters are specified as follows:

The first parameter should be the address of the existing MYSQL structure. Before calling mysql_real_connect (), you must call mysql_init () to initialize the MYSQL structure. A variety of connection options can be changed through the mysql_options () call.

The value of "host" must be a hostname or IP address. If "host" is NULL or the string "localhost", the connection is treated as a connection to the local host. If the operating system supports sockets (Unix) or named pipes (Windows), they will be used instead of TCP/IP to connect to the server.

The "user" parameter contains the user's MySQL login ID. If "user" is a NULL or an empty string "", the user is considered the current user. In the UNIX environment, it is the current login. Under Windows ODBC, the current user name must be explicitly specified.

The "passwd" parameter contains the user's password. If "passwd" is NULL, only the entries in the user table (with 1 empty password field) of the user are checked for matches. In this way, the database administrator can set up the MySQL permission system in a specific way, and the user will be given different permissions depending on whether the user has the specified password.

Note: do not try to encrypt the password before calling mysql_real_connect (). Password encryption will be handled automatically by the client API.

"db" is the database name. If db is NULL, the connection sets the default database to that value.

If "port" is not 0, its value is used as the port number of the TCP/IP connection. Note that the "host" parameter determines the type of connection.

If unix_socket is not NULL, the string describes the socket or named pipe that should be used. Note that the "host" parameter determines the type of connection.

The value of client_flag is usually 0, but it can also be set to a combination of the following flags to allow specific features:

1.CLIENT_MULTI_STATEMENTS

Notifies the server that the client may send multiple statements (separated by';') within a single string. If this flag is not set, multiple statements are prohibited from execution.

2.CLIENT_COMPRESS

Use a compression protocol.

3.CLIENT_SSL

Use SSL (encryption Protocol). This option should not be set by the application, it is set inside the client library

Familiar with MySQL development environment

The development header file directory of  mysq is / usr/include/mysql, which can be viewed with the locate mysql.h or find command (find /-name "mysql.h"-type f-print)

The development dynamic library of mysq can be found in libmysqlclient.so under / usr/lib64/mysql.

Compile command:

 gcc-o dm01_hello dm01_hello.c-I/usr/include/mysql-L/usr/lib64/mysql-lmysqlclient (- I specify header file path-L specifies library path)

 typical error 1

/

/ 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'

/ / libmysqlclient.a dlfcn_globallookup undefined reference to `dlopen'

/

Analysis: dlopen, dllsym knows that the missing dl library only needs to add-ldl at compile time

 typical error 2

/

Dm01_hello.c:49: warning: initialization assigns pointers to integers without type conversion

/ 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':

/ pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/sql/password.c:184: undefined reference tofloor' / pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/sql/password.c:184: undefined reference tofloor'

/ pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/sql/password.c:184: undefined reference tofloor' / pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/sql/password.c:184: undefined reference tofloor'

/ pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/sql/password.c:184: undefined reference tofloor' / usr/lib64/mysql/libmysqlclient.a (password.c.o): / pb2/build/sb_0-12734909-1406113305.48/rpm/BUILD/mysqlcom-pro-5.6.20/mysqlcom-pro-5.6.20/sql/password.c:184: more undefined references tofloor' follow

/ usr/lib64/mysql/libmysqlclient.a (my_getsystime.c.o): In function `my_getsystime':

/

Analysis: undefined reference to floor'' can know the lack of math library, link to the math library-lm-lrt!  typical error 3 dm01_hello.c:70: warning: initialize the pointer to an integer without type conversion / / resulting in no link libstdc++.so/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'

Analysis: from undefined reference to `_ _ gxx_personality_v0', we can know that there is no link to libstdc++.so, just-lstdc++

So:

Writing method of MySQL development environment GCC

 gcc-o dm01_hello dm01_hello.c-I/usr/include/mysql-L/usr/lib64/mysql-lmysqlclient-ldl-lpthread-lm-lrt-lstdc++

The above is generally aimed at the problems of 64-bit systems, which is not necessary for 32-bit systems, just

Gcc-o dm01_hello dm01_hello.c-lmysqlclient is fine!

Because the corresponding work has been done in the 32-bit environment!

Is it troublesome to write on it? Do you have a simpler way to write it? Yes, makefile is introduced.

Writing method of MySQL development environment makefile

.PHONY: clean all # declared as a pseudo target

CC=gcc

CFLAGS=-Wall-g

LFLAGS=-L/usr/lib64/mysql-lmysqlclient-ldl-lpthread-lm-lrt-lstdc++

BIN=dm01_hello dm02_query

All:$ (BIN)

% .ovir% .c

$(CC) $(CFLAGS)-c $<-o $@

Dm01_hello:dm01_hello.o

$(CC) $(CFLAGS) $^ $(LFLAGS)-o $@

Dm02_query:dm02_query.o

$(CC) $(CFLAGS) $^ $(LFLAGS)-o $@

Clean:

Rm-f * .o $(BIN)

Outline a few details:

% .ovir% .c

$(CC) $(CFLAGS)-c $<-o $@

Where% .o and% .c will automatically expand

$@ Target $< first dependency $^ all dependencies

As above:

Dm01_hello.o dm02_query.o: dm01_hello.c dm02_query.c

Gcc-Wall-g-c dm01_hello.c-o dm01_hello.o

Gcc-Wall-g-c dm02_query.c-o dm02_query.o

That's probably what it means.

Let's go on to say, please see the next section! Mysql api--- starts with a small example to get started with mysql programming (2)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report