Some tips on connecting VC ++ to MySQL

Source: Internet
Author: User
Tags mysql functions mysql gui mysql gui tools

This article mainly introduces some tips about connecting VC ++ to MySQL. The legendary MySQL database has good performance, so I am going to test it today, if the results are really good, replace the database with MySQL. but I didn't expect it to take two hours.

Alas! But now we have solved the problem. Well, now we can record the things we learned in the last two hours to meet our needs ~

MySQL is open-source, and it is easy to go online to MySQL connector,

The remaining scripts run a bunch of command lines. to record the configuration of mysql, I have to wake up. If mysqlinstanceconfig.exe stops at the last step of the Start Service and reports an error, install MySQL again. No matter how configured, it cannot be done. but you can change it manually, but I don't understand it. Just reinstall it directly. there are many MySQL management Tools and PHP version. I use MySQL GUI Tools 5.0, which is an official version. It is quite useful.

MySQL provides the c api. Of course, I will use it directly, because it is said to be faster than using ADO. in the installation directory of MySQL, you will find the include and lib folders (in full installation mode), which are the header files and library files of the C interface respectively. The library files only use libmysql. lib is fine. For header files, you only need to reference mysql in the code. h,

Of course, mysql is compiled. h also references other files in the same directory, So I copied all the files in the include folder to the include folder in VC ++, and only libmysql. lib is copied to the lib folder of VC ++.

I have created an MFC program. because MySQL requires network support, select "Windows Socket" in the MFC Program Creation wizard ", otherwise, a SOCKET-related error will be reported during compilation (here I 've been tossing for about half an hour -. -| ). mysql. h must be in windows. h and sockets header files are referenced later. In MFC, I will put it in stdafx. the last line of h. finally, do not forget to add libmysql to the connector parameters. lib connection. Otherwise, an error is reported: XXX function is not declared.

MySQL functions used in connecting VC ++ to MySQL:

MYSQL * mysql_init (NULL) // initialize a MYSQL object, which will be used in subsequent operations

MYSQL * mysql_real_connect (MYSQL * mysql, const char * host, const char * user, const char * passwd, const char * db, unsigned int port, const char * unix_socket, unsigned long client_flag) // establish a connection. db is the database to be accessed. The unix_socket and client_flag are usually NULL. the first parameter is returned if the connection is successful. Otherwise, the connection fails.

Nt mysql_query (MYSQL * mysql, const char * stmt_str) // when a query is executed, 0 is returned.

MYSQL_RES * mysql_store_result (MYSQL * mysql) // obtain the returned result set. If the query fails, 0 is returned.

Unsigned int mysql_num_fields (MYSQL_RES * result) // gets the number of fields in the returned result set.

MYSQL_ROW mysql_fetch_row (MYSQL_RES * result) // retrieves a row of records and moves it to the next record. If the return value is NULL, all rows are traversed or errors occur. MYSQL_ROW type is a string array, subscript is [0, columnLength-1]

Void mysql_free_result (MYSQL_RES * result) // release the result set

Void mysql_close (MYSQL * mysql) // close the connection

Here is the specific code ~ Test ~)

 
 
  1. MYSQL mysql;  
  2. mysql_init(&mysql);  
  3. ASSERT(mysql_real_connect(&mysql, "127.0.0.1", "alacky", "password", "Test", 3306, 0, 0));  
  4. ASSERT(mysql_query(&mysql, "SELECT * FROM maxTest") == 0);  
  5.  MYSQL_RES *result;  
  6. result = mysql_store_result(&mysql);  
  7. MYSQL_ROW row;  
  8. ULONG colLen = mysql_num_fields(result);  
  9. CString datas = "";  
  10.  while(row = mysql_fetch_row(result))  

Traverse each record

 
 
  1. {  
  2. for(ULONG i=0; i<colLen; i++)   
  3. {  
  4. datas += row[i] ? row[i] : "NULL";  
  5. datas += "\t";  
  6. }  
  7. datas += "\n\n";  
  8. }  
  9. mysql_free_result(result);  
  10. mysql_close(&mysql); 

The above content is an introduction to the MySQL notes of VC ++. I hope you will have some gains.

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.