Recent contacts with a lot of databases, the original is always in touch with SQL Server, but because the project needs to start the connection to MySQL. Now let me this rookie talk about the experience.
For C + + connection MySQL, I do not like to download a software MYSQLODBC, so the use of MySQL's own API function to connect :
1, using the way the API connection, need to load MySQL header files and Lib files.
Add \mysql\mysql Server 5.1\include in the additional include directory of VS2010. Look in the directory where MySQL is installed. Copy the Libmysql.dll and Libmysql.lib files to the project directory you are building. The following content is then included in the header file:
- MySQL Required header files and library files
- #include "Winsock.h"
- #include "Mysql.h"
- #pragma comment (lib,"LibmySQL.lib")
2, the Code
(1) Connect MySQL database
Header file defines the data source pointer in MYSQL M_sqlcon;
Connect to MySQL Database
- Try
- {
- Mysql_init (&m_sqlcon);
- localhost: server root for account password test for database name 3306 port
- if (!mysql_real_connect (&m_sqlcon, "localhost","root","root","test", 3306,null,0))
- {
- AfxMessageBox (_t ("Database connection failed!"));
- return FALSE;
- }
- return TRUE;
- }
- catch (...)
- {
- return FALSE;
- }
(2) Closing the database
- Mysql_close (&m_sqlcon);
(3) Create a table
- char* pquery = "CREATE table if not exists ds_building (ID varchar), Name varchar (255), Descs varchar (255), PRIMARY KE Y (ID)) "
- if (Mysql_real_query (&m_sqlcon,pquery, (UINT) strlen (pquery))!=0)
- {
- Const char* pCh = mysql_error (&m_sqlcon);
- return FALSE;
- }
API interface with MySQL:
- Mysql_affected_rows () returns the number of rows affected by the latest update, Delete, or insert query.
- Mysql_close () closes a server connection.
- Mysql_connect () connect a MySQL server. This function is not recommended; use Mysql_real_connect () instead.
- Mysql_change_user () Changes the user and database on an open connection.
- mysql_create_db () creates a database. The function is not recommended, and the SQL command is used to CREATE DATABASE.
- Mysql_data_seek () Searches for an arbitrary row in a query result collection.
- Mysql_debug () makes a dbug_push with the given string.
- mysql_drop_db () discards a database. The function is not recommended, and the SQL command is used to DROP DATABASE.
- Mysql_dump_debug_info () lets the server write debug information to the log file.
- Mysql_eof () determines whether the last row of a result collection has been read. This function is objected to; Mysql_errno () or mysql_error () can be used instead.
- Mysql_errno () returns the error number of the most recently called MySQL function.
- Mysql_error () returns an error message for the most recently called MySQL function.
- Mysql_escape_string () escapes special characters for strings that are used in SQL statements.
- Mysql_fetch_field () returns the type of the next table field.
- Mysql_fetch_field_direct () returns the type of a table field, giving a number of fields.
- Mysql_fetch_fields () returns an array of all the field structures.
- Mysql_fetch_lengths () returns the length of all columns in the current row.
- Mysql_fetch_row () Gets the next line from the result collection.
- Mysql_field_seek () Places the column cursor on a specified column.
- Mysql_field_count () returns the number of results columns for the most recent query.
- Mysql_field_tell () returns the position of the field cursor used for the last Mysql_fetch_field ().
- Mysql_free_result () frees the memory used by a result collection.
- Mysql_get_client_info () returns the customer version information.
- Mysql_get_host_info () returns a string that describes the connection.
- Mysql_get_proto_info () Returns the protocol version used by the connection.
- Mysql_get_server_info () returns the server version number.
- Mysql_info () returns information about the most recently executed query.
- Mysql_init () Gets or initializes a MySQL structure.
- MYSQL_INSERT_ID () returns the ID of the previous query that was generated for a auto_increment column.
- Mysql_kill () kills a given thread.
- Mysql_list_dbs () returns the name of the database that matches a simple regular expression.
- Mysql_list_fields () returns the name of a column that matches a simple regular expression.
- Mysql_list_processes () Returns a table for the current server thread.
- Mysql_list_tables () returns the name of the table that matches a simple regular expression.
- Mysql_num_fields () returns the number of columns that a result collection is heavy.
- Mysql_num_rows () returns the number of rows in a result set.
- Mysql_options () Sets the connection options for mysql_connect ().
- Mysql_ping () Check that the connection to the server is working and reconnect if necessary.
- Mysql_query () executes an SQL query that is specified as a null-terminated string.
- Mysql_real_connect () connect a MySQL server.
- Mysql_real_query () executes an SQL query that is specified as a string with a count.
- Mysql_reload () tells the server to reload the authorization table.
- Mysql_row_seek () Searches for a row in the result set, using the value returned from Mysql_row_tell ().
- Mysql_row_tell () returns the row cursor position.
- mysql_select_db () connects a database.
- Mysql_shutdown () Shut down the database server.
- Mysql_stat () returns the server state as a string.
- Mysql_store_result () Retrieves a complete set of results to the customer.
- MYSQL_THREAD_ID () returns the ID of the current thread.
- Mysql_use_result () Initializes a row of rows to retrieve the result collection.
The problem encountered:
At first I used VS2015 to connect MySQL, found that the external symbol has been unable to parse the error, looked for a long time to see the linker those found nothing wrong ah. I found it for a long time because the VS default platform is 32-bit, and my MySQL is 64 bits. For this you only need to change the operating platform to x64 in the configuration management of the project properties. Of course, you can also next mysql32 bit of Lib and DLL.
Reprint Please specify address: http://www.cnblogs.com/fnlingnzb-learner/p/5823466.html, thank you.
C + + connection to MySQL and related issues