C + + connection to MySQL and related issues

Source: Internet
Author: User

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:

    1. MySQL Required header files and library files
    2. #include "Winsock.h"
    3. #include "Mysql.h"
    4. #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

  1. Try
  2. {
  3. Mysql_init (&m_sqlcon);
  4. localhost: server root for account password test for database name 3306 port
  5. if (!mysql_real_connect (&m_sqlcon, "localhost","root","root","test", 3306,null,0))
  6. {
  7. AfxMessageBox (_t ("Database connection failed!"));
  8. return FALSE;
  9. }
  10. return TRUE;
  11. }
  12. catch (...)
  13. {
  14. return FALSE;
  15. }

(2) Closing the database

    1. Mysql_close (&m_sqlcon);

(3) Create a table

    1. char* pquery = "CREATE table if not exists ds_building (ID varchar), Name varchar (255), Descs varchar (255), PRIMARY KE Y (ID)) "
    2. if (Mysql_real_query (&m_sqlcon,pquery, (UINT) strlen (pquery))!=0)
    3. {
    4. Const char* pCh = mysql_error (&m_sqlcon);
    5. return FALSE;
    6. }

API interface with MySQL:

    1. Mysql_affected_rows () returns the number of rows affected by the latest update, Delete, or insert query.
    2. Mysql_close () closes a server connection.
    3. Mysql_connect () connect a MySQL server. This function is not recommended; use Mysql_real_connect () instead.
    4. Mysql_change_user () Changes the user and database on an open connection.
    5. mysql_create_db () creates a database.  The function is not recommended, and the SQL command is used to CREATE DATABASE.
    6. Mysql_data_seek () Searches for an arbitrary row in a query result collection.
    7. Mysql_debug () makes a dbug_push with the given string.
    8. mysql_drop_db () discards a database.  The function is not recommended, and the SQL command is used to DROP DATABASE.
    9. Mysql_dump_debug_info () lets the server write debug information to the log file.
    10. 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.
    11. Mysql_errno () returns the error number of the most recently called MySQL function.
    12. Mysql_error () returns an error message for the most recently called MySQL function.
    13. Mysql_escape_string () escapes special characters for strings that are used in SQL statements.
    14. Mysql_fetch_field () returns the type of the next table field.
    15. Mysql_fetch_field_direct () returns the type of a table field, giving a number of fields.
    16. Mysql_fetch_fields () returns an array of all the field structures.
    17. Mysql_fetch_lengths () returns the length of all columns in the current row.
    18. Mysql_fetch_row () Gets the next line from the result collection.
    19. Mysql_field_seek () Places the column cursor on a specified column.
    20. Mysql_field_count () returns the number of results columns for the most recent query.
    21. Mysql_field_tell () returns the position of the field cursor used for the last Mysql_fetch_field ().
    22. Mysql_free_result () frees the memory used by a result collection.
    23. Mysql_get_client_info () returns the customer version information.
    24. Mysql_get_host_info () returns a string that describes the connection.
    25. Mysql_get_proto_info () Returns the protocol version used by the connection.
    26. Mysql_get_server_info () returns the server version number.
    27. Mysql_info () returns information about the most recently executed query.
    28. Mysql_init () Gets or initializes a MySQL structure.
    29. MYSQL_INSERT_ID () returns the ID of the previous query that was generated for a auto_increment column.
    30. Mysql_kill () kills a given thread.
    31. Mysql_list_dbs () returns the name of the database that matches a simple regular expression.
    32. Mysql_list_fields () returns the name of a column that matches a simple regular expression.
    33. Mysql_list_processes () Returns a table for the current server thread.
    34. Mysql_list_tables () returns the name of the table that matches a simple regular expression.
    35. Mysql_num_fields () returns the number of columns that a result collection is heavy.
    36. Mysql_num_rows () returns the number of rows in a result set.
    37. Mysql_options () Sets the connection options for mysql_connect ().
    38. Mysql_ping () Check that the connection to the server is working and reconnect if necessary.
    39. Mysql_query () executes an SQL query that is specified as a null-terminated string.
    40. Mysql_real_connect () connect a MySQL server.
    41. Mysql_real_query () executes an SQL query that is specified as a string with a count.
    42. Mysql_reload () tells the server to reload the authorization table.
    43. Mysql_row_seek () Searches for a row in the result set, using the value returned from Mysql_row_tell ().
    44. Mysql_row_tell () returns the row cursor position.
    45. mysql_select_db () connects a database.
    46. Mysql_shutdown () Shut down the database server.
    47. Mysql_stat () returns the server state as a string.
    48. Mysql_store_result () Retrieves a complete set of results to the customer.
    49. MYSQL_THREAD_ID () returns the ID of the current thread.
    50. 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

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.