First, create the project and configure the required header files and libraries (i) Create an empty project
Create an empty project using VS2015
(ii) The folder where the header files required for the project are added
Right-click Project Selection Properties, go to C + + + + General + Add included directories, adding D:\mysql-5.7.19-win64\include (based on your own installed MySQL version and address)
(iii) Add the required library
Still on the property page, go to the linker = General + + Add library directory, adding D:\mysql-5.7.19-winx64\lib (fill in your case)
(iv) Add Libmysql.dll
Add Libmysql.dll (added from the computer's Explorer) in the project's. \x64\debug\. dll file in the D:\mysql-5.7.19-winx64\lib directory
The following error occurs if you perform this step:
Second, code test (a) Add items, copy the following code to run
#include"stdio.h"#include"mysql.h"intMain () {MYSQL* CON;//= Mysql_init ((mysql*) 0);Mysql_res *Res; Mysql_row ROW; Chartmp[ -]; //Database Configuartion Chardbuser[ -] ="Root"; Chardbpasswd[ -] ="123456";//it must be changed Chardbip[ -] ="localhost"; Chardbname[ -] ="Hospital"; Chartablename[ -] ="BL"; Char*query =NULL; intx; inty; intRt//return valueUnsignedintT; intCount =0; Con= Mysql_init ((mysql*)0); if(Con! = NULL && mysql_real_connect (Con, Dbip, Dbuser, dbpasswd, dbname,3306Null0)) { if(!mysql_select_db (Con, dbname)) {printf ("Select successfully the database!\n"); Con->reconnect =1; Query="set names \ ' Gbk\ '"; RT=mysql_real_query (Con, query, strlen (query)); if(RT) {printf ("Error making query:%s!!! \ n", Mysql_error (con)); } Else{printf ("Query%s succeed!\n", query); } } } Else{messageboxa (NULL,"Unable to connect the Database,check your configuration!","", NULL); } //sprintf (tmp, "insert into%s values (%s,%d,%d)", TableName, "null", X, y);//Note how to insert a record into a database that has a self-increment fieldsprintf (TMP,"INSERT into BL values (null, ' X ', ' x ', ' x ', ' x ')"); RT=mysql_real_query (Con, TMP, strlen (TMP)); if(RT) {printf ("Error making query:%s!!! \ n", Mysql_error (con)); } Else{printf ("%s executed!!! \ n", TMP); } sprintf (TMP,"select * from%s", TableName); RT=mysql_real_query (Con, TMP, strlen (TMP)); if(RT) {printf ("Error making query:%s!!! \ n", Mysql_error (con)); } Else{printf ("%s executed!!! \ n", TMP); } Res= Mysql_store_result (con);//Save the results in the res struct while(row =mysql_fetch_row (RES)) { for(t =0; T<mysql_num_fields (RES); t++) {printf ("%s", row[t]); } printf ("...... ... \ n"); Count++; } printf ("Number of rows%d\n", Count); printf ("mysql_free_result...\n"); Mysql_free_result (RES); Mysql_close (con); System ("Pause"); return 0;}
(b) Part of the API that connects MySQL and pulls data out of MySQL is introduced in 1. Mysql_real_connect () 1) function prototype
MySQL *mysql_real_connect (MySQL *mysql, const char *host, const char *user, const char *PASSWD, const char *db, unsigned i NT Port, const char *unix_socket, unsigned int client_flag)
2) Parameters and description
? The first parameter should be the address of an existing MySQL structure. Before calling Mysql_real_connect (), you must call Mysql_init () to initialize the MySQL structure. See the example below.
? The host value can be either a hostname or an IP address. If host is null or the string "localhost", it is assumed to be a connection to the local host. If the OS supports sockets (Unix) or Named pipes (WIN32), use them instead of TCP/IP to connect to the server.
? The user parameter contains the username's MySQL login ID. If user is null, it is assumed to be current. Under UNIX, it is the current login name. Under Windows ODBC, you must explicitly specify the current user name. See 16.4 How to complete various domains in the ODBC Administrator program.
? The passwd parameter contains the password for user. If passwd is null, only the entries in the user table for users with a blank password field will be checked for a match. This allows the database supervisor to set MySQL permissions so that users get different passwords, depending on whether they have specified a password. Note: Do not attempt to encrypt the password before calling mysql_real_connect (); password encryption is automatically handled by the client API.
? DB is the database name. If the DB is not NULL, the connection sets the default database to this value.
? If port is not 0, the value is used as the port number for the TCP/IP connection. Note The host parameter determines the type of connection.
? If Unix_socket is not NULL, the string specifies the socket or the named pipe that should be used. Note The host parameter determines the type of connection.
? The Client_flag value is typically 0, but in very special cases it can be set to a combination of the following flags:
Logo name means a sign
Client_found_rows returns the number of rows found (matched), not the number of rows affected.
Client_no_schema does not allow db_name.tbl_name.col_name syntax. This is for ODBC; If you use this syntax, which causes the parser to produce an error, it is useful for catching errors in some ODBC programs.
Client_compress uses a compression protocol.
CLIENT_ODBC customer is an ODBC customer. This makes mysqld more friendly to ODBC.
This function is used to connect to the database
3) return value
If the connection succeeds, a mysql* connection handle. NULL if the connection failed. For a successful connection, the return value is the same as the first parameter value, unless you pass NULL to the parameter.
4) Error
Cr_conn_host_error
Unable to connect to MySQL server.
Cr_connection_error
Cannot connect to local MySQL server.
Cr_ipsock_error
You cannot create an IP socket.
Cr_out_of_memory
Memory overflow.
Cr_socket_create_error
You cannot create a UNIX socket.
Cr_unknown_host
The IP address of the host name cannot be found.
Cr_version_error
A protocol mismatch caused by an attempt to use a client library of a different protocol version with a server connection. This can happen if you use a very old client library to connect to a new server that is not started with the--old-protocol option.
Cr_namedpipeopen_error;
You cannot create a named pipe on Win32.
Cr_namedpipewait_error;
You cannot wait on a named pipe on a Win32.
Cr_namedpipesetstate_error;
A pipe processor cannot be obtained on the Win32.
2. mysql_select_db () 1) function prototype
int mysql_select_db (MySQL *mysql, const char *db)
2) Parameters and description
Causes the database specified by DB to be the default (current) database on the connection specified by MySQL. In the subsequent query, this database is the default database for tables that do not include an explicit database specifier.
mysql_select_db () fails unless the connected user can be authenticated to allow the database to be used.
3) return value
Success, 0. If an error occurs, nonzero.
4) Error
Cr_commands_out_of_sync
The command is executed in an inappropriate order.
Cr_server_gone_error
The MySQL server is down.
Cr_server_lost
The connection to the server was lost during the query.
Cr_unknown_error
An unknown error occurred.
3. mysql_real_query1) function prototype
int mysql_real_query (MySQL *mysql, const char *query, unsigned int length)
2) Parameters and description
Executes the SQL query pointed to by query, which should be a length-byte string. The query must consist of a single SQL statement. You should not add a terminating semicolon (";") after the statement. or \g.
For queries that contain binary data, you must use Mysql_real_query () instead of mysql_query (), because the binary code data may contain "/" characters, and Mysql_real_query () is faster than mysql_query () , because it calls strlen () on the query string.
3) return value
If the query succeeds, 0. If an error occurs, nonzero.
4) Error
Cr_commands_out_of_sync
The command is executed in an inappropriate order.
Cr_server_gone_error
The MySQL server is down.
Cr_server_lost
The connection to the server was lost during the query.
Cr_unknown_error
An unknown error occurred.
4. mysql_store_result1) function prototype
MYSQL_RES *mysql_store_result(MYSQL *mysql)
2) Parameters and description
Used to return the Mysql_real_query query results
3) return value
Returns the mysql_res structure and returns NULL if the fetch fails
5.
mysql_fetch_row()
1) Function prototypes
Mysql_row *mysql_fetch_row (mysql_res* RES)
2) Parameters and description
for reading Mysql_res
3) return value
Returns the Mysql_row that represents the next line of Mysql_res
C + + Connect MySQL database