Problems related to C ++ connection to mysql

Source: Internet
Author: User

Problems related to C ++ connection to mysql

Recently, I have been familiar with a lot of database things. I have been familiar with SQL server, but I started to connect to mysql because of project requirements. Let me talk about this cainiao experience.

For C ++ to connect to mysql, I do not like to download a software mysqlodbc, so I useConnect through mysql's own API functions:

1. To Connect using APIs, You need to load the mysql header file and lib file.

Add \ MySQL Server 5.1 \ include to the additional include directory of VS2010. Find it in the directory where MySql is installed. Copy the libmysql. dll and libmysql. lib files to the created project directory. The header file contains the following content:

  1. // Header files and library files required by mysql
  2. # Include "winsock. h"
  3. # Include "mysql. h"
  4. # Pragma comment (lib, "libmySQL. lib ")

2. Encoding

(1) connect to the mysql database

The data source pointer MYSQL m_sqlCon is defined in the header file;

// Connect to the MySql database

  1. Try
  2. {
  3. Mysql_init (& m_sqlCon );
  4. // Localhost: the root account and password of the server. "test" indicates the database name. "3306" indicates the port number.
  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) shut down the database

  1. Mysql_close (& m_sqlCon );

(3) create a table

  1. Char * pQuery = "create table if not exists DS_Building (id varchar (10), Name VARCHAR (255), Descs VARCHAR (255), primary key (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. }

Attached MySQL API:

    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 () connects to 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. This function is not recommended, but the SQL command CREATE DATABASE is used.
    6. Mysql_data_seek () searches for any row in a query result set.
    7. Mysql_debug () uses the given string for DBUG_PUSH.
    8. Mysql_drop_db () discards a database. This function is not recommended, but the SQL command DROP DATABASE is used.
    9. Mysql_dump_debug_info () allows the server to write debugging information to the log file.
    10. Mysql_eof () determines whether the last row of a result set has been read. This function is opposed; mysql_errno () or mysql_error () can be used on the contrary.
    11. Mysql_errno () returns the error number of the recently called MySQL function.
    12. Mysql_error () returns the error message of the recently called MySQL function.
    13. Use mysql_escape_string () to escape special characters from strings in SQL statements.
    14. Mysql_fetch_field () returns the type of the field in the next table.
    15. Mysql_fetch_field_direct () returns the type of a table field and a field number.
    16. Mysql_fetch_fields () returns an array of all field structures.
    17. Mysql_fetch_lengths () returns the length of all columns in the current row.
    18. Mysql_fetch_row () gets the next row from the result set.
    19. Mysql_field_seek () Place the column cursor on a specified column.
    20. Mysql_field_count () returns the number of result columns of the latest query.
    21. Mysql_field_tell () returns the cursor position for the last mysql_fetch_field () field.
    22. Mysql_free_result () releases the memory used by a result set.
    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 recently executed query.
    28. Mysql_init () obtains or initializes a MYSQL structure.
    29. Mysql_insert_id () returns the ID generated by the previous query for the AUTO_INCREMENT column.
    30. Mysql_kill () kills a given thread.
    31. Mysql_list_dbs () returns the database name that matches a simple regular expression.
    32. Mysql_list_fields () returns the column name that matches a simple regular expression.
    33. Mysql_list_processes () returns a table of the current server thread.
    34. Mysql_list_tables () returns the table name that matches a simple regular expression.
    35. Mysql_num_fields () returns the number of duplicate columns in a result set.
    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 () checks whether the connection to the server is working and reconnects if necessary.
    39. Mysql_query () executes an SQL query specified as an empty string.
    40. Mysql_real_connect () connects to a MySQL server.
    41. Mysql_real_query () executes an SQL query specified as a string with a count.
    42. Mysql_reload () tells the server to reinstall the authorization table.
    43. Mysql_row_seek () searches for rows in the result set and uses the values returned from mysql_row_tell.
    44. Mysql_row_tell () returns the cursor position of the row.
    45. Mysql_select_db () connects to a database.
    46. Mysql_shutdown () disables the database server.
    47. Mysql_stat () returns the server status as a string.
    48. Mysql_store_result () retrieves a complete set of results for the customer.
    49. Mysql_thread_id () returns the ID of the current thread.
    50. Mysql_use_result () initializes the retrieval of a result set in one row and one row.

Problems:

At the beginning, I used VS2015 to connect to mysql and found that the error of external symbols could not be parsed. After searching for a long time, I read the linker and found that there was no error. Later I found it for a long time because VS's default operating platform is 32-bit, while mysql is 64-bit. you only need to change the running platform to x64 in the configuration management of project properties. Of course, you can also use the next mysql32-bit Lib and dll.

 

For more information, see http://www.cnblogs.com/fnlingnzb-learner/p/58220.6.html,.

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.