Notes on connecting VC to MySQL

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

Http://www.oklinux.cn/html/ SQL /other/20080812/59369.html

It is said that MySQL performance is very good. Prepare to test it. If the effect is good, replace the database with MySQL. But I didn't expect it to take two hours! 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 download MySQL dowload online. I am running Windows, and the installation is simple. I will not talk about it here, but don't forget to use the next management tool. mySQL does not have a graphical management tool as MSSQL does.ProgramIf the supervisor 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 filesCodeMySQL. 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 put 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.

Below are the MySQL functions I use:

Quote:
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.

Int 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 (it's rough to write, test it)

Quote:
1. 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 row of records
11 {
12 For (ulong I = 0; I 13 {
14 datas = row? Row: "null ";
15 datas = "\ t ";
16}
17 datas = "\ n ";
18}
19 mysql_free_result (result );
20 mysql_close (& MySQL );

 

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.