MFC-related MYSQL database operations

Source: Internet
Author: User
I remember someone said that the current software is basically connected to the database. If a software is not connected to the database, it will basically be useless. Although such a statement is somewhat one-sided, the importance of databases to software is beyond doubt. The connection to the database using MFC should not be a fresh proposition. There are several methods. I tried it and I felt like using MyS.

I remember someone said that the current software is basically connected to the database. If a software is not connected to the database, it will basically be useless. Although such a statement is somewhat one-sided, the importance of databases to software is beyond doubt. The connection to the database using MFC should not be a fresh proposition. There are several methods. I tried it and I felt like using MyS.

I remember someone said that the current software is basically connected to the database. If a software is not connected to the database, it will basically be useless. Although such a statement is somewhat one-sided, the importance of databases to software is beyond doubt.

There are several methods to connect to the database with MFC. I tried it and it is quite convenient to use the C language API provided by MySQL, therefore, related operations are recorded here.

To use the C language API provided by MySQL, you must first include the header file directory of the API, that is, add the "include" folder of the MySQL installation directory under the "include" directory in the MFC project properties. Because the API is packaged in the form of a dynamic link library, you must add the "lib" folder of the MySQL installation directory under the "library directory" in the MFC project attribute. The result is as follows:


Because it is a dynamic link library, after completing the above configuration, you need to install libmysql In the MySQL installation directory. copy the dll file to the MFC project directory. If you want to release it later, package it. Last night, we only needed to include the MySQL header file in the MySQL header file to use the database normally. The Code is as follows:

# Include "winsock. h "// because the database is connected over the network, it must contain the network header file # include" mysql. h "// There is nothing to say about this. The mysql header file should naturally contain # pragma comment (lib," libmySQL. lib ") // Add dependencies. You can also set them in project properties.

After completing the above configuration, we can use the APIS provided by MySQL normally. The following describes the specific operations in different categories.

Connect to the database:

MYSQL m_sqlCon; MYSQL_RES * res; // The record function returns MYSQL_ROW row; // The number of record rows mysql_init (& m_sqlCon); // initialize the database object if (! Mysql_real_connect (& m_sqlCon, "localhost", "root", "123", "test", 3306, NULL, 0) // localhost: the server address. You can enter the IP address directly; root: account; 123: password; test: Database Name; 3306: network port {AfxMessageBox (_ T ("database connection failed! "); Return;} else // If the connection is successful, continue to access the database. The subsequent operation code is basically put here {AfxMessageBox (_ T (" the database connection is successful! ");} Mysql_close (& m_sqlCon); // close the Mysql connection

 

Obtain table data in the database:

Mysql_query (& m_sqlCon, "set names 'gb2312 '"); // sets the database character format to solve Chinese garbled characters. if (mysql_real_query (& m_sqlCon, "select * from material attribute ", (unsigned long) strlen ("select * from material attributes") // query the "material attributes" table {return;} res = mysql_store_result (& m_sqlCon) in the database ); // obtain the storage result set if (NULL = res) // if it is NULL, {return;} int listrow = 0; while (row = mysql_fetch_row (res) is returned )) // read the row repeatedly and put the data in the list until the row is NULL {for (int rols = 0; rols <cols; rols ++) // cols is the number of columns in the data table {CString myreaddata (row [rols]); if (rols = 0) {list-> InsertItem (listrow, myreaddata ); // Add the first student data} else {list-> SetItemText (listrow, rols, myreaddata) ;}} listrow ++ ;}
Clear data table content:
Mysql_query (& m_sqlCon, "set names 'gb2312'"); // sets the database character format to solve Chinese garbled characters. char * mysqlstatements = "DELETE material attributes. * FROM material attributes "; // SQL operation statement mysql_real_query (& m_sqlCon, mysqlstatements, (unsigned long) strlen (mysqlstatements); // clear the" material attributes "table

Insert data table content:
Sprintf_s (mysqlstatements, "insert into material attributes (item No., item name, category, measurement unit, sample unit price, batch unit price) value ('% s',' % s ', '% s',' % s') ", mysqlinsertpoint [0], mysqlinsertpoint [1], mysqlinsertpoint [2], mysqlinsertpoint [3], mysqlinsertpoint [4], limit [5]); // SQL operation statement mysql_real_query (& m_sqlCon, mysqlstatements, (unsigned long) strlen (mysqlstatements )); // insert data to the "material attributes" table









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.