C + + and Mysql connection encountered problem rollup _mysql

Source: Internet
Author: User

The most recent exposure to a lot of databases was that it had been in contact with SQL Server, but it started to connect to MySQL because of the needs of the project. Below let me this rookie talk about the next experience bar.

For C + + connection MySQL, I do not like to download more than one software mysqlodbc, so using MySQL's own API function to connect:

1, use the way of the API connection, need to load MySQL header file and lib file.

Add \mysql\mysql Server 5.1\include to the VS2010 additional include directory. Find it in the directory where MySQL is installed. Copy the Libmysql.dll and Libmysql.lib files to the project directory under construction. Then include the following in the header file:

MySQL requires the header files and library files 
#include "winsock.h" 
#include "mysql.h" 
#pragma comment (lib, "LibmySQL.lib")

2, to encode

(1) Connection MySQL database

Header file definition data source pointer MYSQL M_sqlcon;

Connect the MySQL database
try 
{ 
mysql_init (&m_sqlcon); 
localhost: server root for account password test for database name 3306 is Port 
if (!mysql_real_connect (&m_sqlcon, "localhost", "root", "root", "" Test ", 3306,null,0)" 
{ 
AfxMessageBox ("Database connection Failed!") (_t); 
return FALSE; 
} 
return TRUE; 
} 
catch (...) 
{return 
FALSE; 

(2) Close the database

 
 

(3) Create a table

char* pquery = "CREATE table if not exists ds_building (ID VARCHAR (), Name VARCHAR (255), Descs VARCHAR (255), PRIMARY KEY (I D) " 
If" (Mysql_real_query (&m_sqlcon,pquery, (UINT) strlen (pquery))!=0) 
{ 
const char* pCh = Mysql_ Error (&m_sqlcon); 
return FALSE; 

With MySQL API interface:

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 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, and the SQL command is used to create the 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. This function is not recommended, and the SQL command drop DATABASE is used.

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 recently invoked MySQL function.

12.mysql_error () returns an error message for the recently invoked MySQL function.

13.mysql_escape_string () The escape special character of a string used in an SQL statement.

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 the columns in the current row.

18.mysql_fetch_row () Gets the next row from the result collection.

19.mysql_field_seek () Places the column cursor on a specified column.

20.mysql_field_count () returns the number of result columns for the most recent query.

21.mysql_field_tell () returns the position of the field cursor 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 that was generated by the previous query 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 the column that matches a simple regular expression.

33.mysql_list_processes () Returns a table of 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 with a result set heavy.

36.mysql_num_rows () returns the number of rows in a result collection.

37.mysql_options () Sets the connection option for mysql_connect ().

38.mysql_ping () Check to see if the connection to the server is working and reconnect if necessary.

39.mysql_query () executes a SQL query that is specified as a null-terminated string.

40.mysql_real_connect () connects a MySQL server.

41.mysql_real_query () executes a 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 the rows in the result collection, using the value returned from Mysql_row_tell ().

44.mysql_row_tell () returns the row cursor position.

45.mysql_select_db () connects to 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-by-row retrieval of the result set.

The problems encountered:

At first I used VS2015 to connect MySQL, found that there has been unable to resolve the external symbols of this error, looked for a long time to see the linker those found nothing wrong ah. I found out for a long time because the VS default platform is 32-bit, and my MySQL is 64-bit. 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 have the next mysql32 bit of Lib and DLL.

The above is a small set to introduce the C + + and MySQL connection problems summarized, I hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.