Summary of problems encountered during connection between C ++ and mysql, and summary of mysql

Source: Internet
Author: User

Summary of problems encountered during connection between C ++ and mysql, and summary of 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 use mysql's own API functions for connection:

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:

// Mysql header file and library file # include "winsock. h" # include "mysql. h" # 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 try {mysql_init (& m_sqlCon); // localhost: the server root is the account and password test is the database name 3306 is the 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) shut down the database

mysql_close(&m_sqlCon); 

(3) create a table

char* pQuery = "create table if not exists DS_Building( ID VARCHAR(10),Name VARCHAR(255),Descs VARCHAR(255),PRIMARY KEY (ID))" if(mysql_real_query(&m_sqlCon,pQuery,(UINT)strlen(pQuery))!=0) { const char* pCh = mysql_error(&m_sqlCon); return FALSE; } 

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. special characters used by mysql_escape_string () to escape 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 in the most recent 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 describing the connection.

25. mysql_get_proto_info () returns the Protocol Version Used for 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 matching 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 the SQL query specified as an empty string.

40. mysql_real_connect () connects to a MySQL server.

41. mysql_real_query () executes the 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 using the value 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 () shut down 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 search 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.

The above is a summary of the problems encountered in connecting C ++ and mysql. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.