Client program 1-connect to the MySQL server

Source: Internet
Author: User
Tags command line connect mysql mysql client socket client mysql database port number


6.2 Client Program 1-Connect to Server
Our first MySQL client program is simple: Connect to the server, disconnect, and exit. It's not very useful in itself, but you must know how to do it, because actually doing anything with a MySQL database must be connected to the server. This is a common operation, and the code to develop a connection is to write the code that each client program will use. In addition, this task brings us some simple things to start with. Later, we can increase this client to do some more useful things. Our first client program source code  Щ sash?, including a separate file, client.c:


This source file starts with stdio.h and mysql.h. The MySQL client may include other header files, but in general at least two are the most basic.
The default values for host names, user names, passwords, and database names are fixed inside the code, making things simpler. Later, we will parameterize these values so that they can be specified in the option file or on the command line. The main () function of the program creates and terminates a connection to the server. The following two steps are used to establish a connection:
1) Call Mysql_init () to obtain the connection handler. The MYSQL data type is a structure that includes connection information. Variables of this type are called connection handlers. When we pass NULL to Mysql_init (), it assigns a MySQL variable, initializes it, and returns a pointer to it.
2 call Mysql_real_connect () to create a connection to the server. Mysql_real_connect () can have any number of parameters, such as:
A pointer to the connection handler. This cannot be null; it should be the value returned by Mysql_init ().
Server host. If you specify null or host "localhost", the client connects to a server that is running on the local host using UNIX sockets. If you specify a host name or the IP address of the host, the client is connected to a host named with the TCP/IP connection.
On Windows, this is similar in addition to using TCP/IP connections instead of UNIX sockets (on Windows NT, if the host is null, try to connect using a specified pipe before TCP/IP).
User name and password. If the name is called NULL, the client library sends the logical name to the server. If the password is null, the password is not sent.
Port number and socket file. These are specified as 0 or null to tell the client library to use its default value. If you do not specify a port or socket, these defaults are determined based on the host you want to connect to. Detailed information about these is given in the description of Mysql_real_connect () in Appendix F.
Flag value. Because we do not use any particular connection operation, so it is 0. The options available for this parameter are discussed in detail in the Mysql_real_connect () item in Appendix F. To terminate this connection, you can pass a pointer to the connection handler to Mysql_ close (). When a connection handler is passed Mysql_close () to terminate a connection, the connection handler automatically allocated by Mysql_init () is automatically freed.
To test client program 1, you can compile and connect by using the instructions given in the client program earlier in this chapter, and then run it:
The% CLIENT1 program connects to the server, disconnects, and exits. It's not exciting, but it's a start. However, it is only a start, because there are two important drawbacks:
The client does not have error checking, so it does not really know if it is actually working!
The connection parameters (host name, user name, etc.) are fixed inside the source code. It is better to allow the user to resolve this problem by specifying an option file or a parameter in the command line. These problems are not difficult to deal with. We will deal with them specifically below.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.