MFC operations related to MySQL database

Source: Internet
Author: User

Remember to read someone said that the current software is basically connected to the database, if a software is not linked to the database, basically no effect. Although this statement is slightly one-sided, but the database of the importance of software is undoubtedly.

MFC connection database should not be a fresh proposition, the method is there are several kinds of, I tried, I feel the use of MySQL provided by the C language API to operate pretty convenient, so this record related operations.

To use the C language API provided by MySQL, first include the header file directory of the API, that is, add the "include" folder of the MySQL installation directory under "Include directory" in the MFC project properties. Because the API is packaged as a dynamic-link library, you also add the "Lib" folder of the MySQL installation directory under "Library directory" in the MFC project properties, as shown in the following:


Because it is a dynamic link library, so after the above configuration, the MySQL installation directory to copy the Libmysql.dll file into the MFC project directory, if you want to release this will be packaged in. Last night, we just need to use this database with MySQL header files in MySQL header files. The code is as follows:

#include "winsock.h"//Because the database is connected through the network, must contain the network phase file # include "Mysql.h"//This is nothing to say, MySQL header file naturally contains  #pragma comment (lib , "LibmySQL.lib")//Additional dependencies can also be set in the project properties

After the above configuration, we have been able to use the MySQL provided by the API, the following categories to talk about the specific operation.

To connect to a database:

MYSQL M_sqlcon;mysql_init (&m_sqlcon);//Initialize Database object if (!mysql_real_connect (&m_sqlcon, "localhost", "root", "123 "," Test ", 3306, NULL, 0))//localhost: Server address, can be directly filled in Ip;root: account number; 123: password; test: database name; 3306: Network Port  {AfxMessageBox (_t (" Database connection Failed! ")); return;} else//connection to continue to access the database, and then the relevant operation code is basically placed in the face of {AfxMessageBox ("database connection succeeded!");} Mysql_close (&m_sqlcon);//close MySQL connection  

Get the table data in the database:

mysql_query (&m_sqlcon, "set NAMES ' GB2312 '");//Set database character format, solve Chinese garbled problem if (Mysql_real_query (&m_sqlcon, "SELECT * From item properties, (unsigned long) strlen ("SELECT * from Item Property"))//Query the database for the Item Properties table  {return;} res = Mysql_store_result (&m_sqlcon);//Gets the stored result set if  (NULL = = res)//returns {return if empty;} int listrow = 0;while (row = mysql_fetch_row (res))//reads the rows repeatedly, putting the data into the list until the row is null  {for (int rols = 0; rols < cols; rols + +) {CString myreaddata (Row[rols]), if (rols==0) {List->insertitem (ListRow, myreaddata);//Add First Student data}else{list- >setitemtext (ListRow, Rols, Myreaddata);}} listrow++;}
clear the contents of the data table:
mysql_query (&m_sqlcon, "set NAMES ' GB2312 '");//Set database character format, solve Chinese garbled problem char *mysqlstatements = "DELETE item attribute. *FROM Item Properties" ;//sql Operation Statement Mysql_real_query (&m_sqlcon, mysqlstatements, (unsigned long) strlen (mysqlstatements));//emptying the Item Properties table  

Insert data table contents:

sprintf_s (mysqlstatements, "INSERT into item attribute (item number, item name, category, Unit of measure, sample unit price, bulk unit price) value ('%s ', '%s ', '%s ', '%s ', '%s ', '%s ')", Mysqlinsertpoint[0], mysqlinsertpoint[1], mysqlinsertpoint[2], mysqlinsertpoint[3], mysqlinsertpoint[4], MYSQLINSERTPOINT[5]); SQL Action Statement mysql_real_query (&m_sqlcon, mysqlstatements, (unsigned long) strlen (mysqlstatements));//Insert data into the Item Properties table










MFC operations related to MySQL database

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.