Here's not sure if anyone is really new novice novice don't know how to build an MFC project should not? is to open vs New Project-MFC-MFC Project-Point based dialog box-and use in static library then click Finish
Note the name is in the English source code in this Https://pan.baidu.com/s/1chRMPK
Tool: vs2013,mysql5.7.6
Operating system: Win64 I'm VS2013 here, and if you copy my project, it might not work in any other version. Just click on the project-Properties-Configuration Properties-The Toolset on the right is changed to your own version.
And I'm here for 64. You might have the project is the WIN 32 to change the project-Properties-top right Configuration Manager-inside the platform changed to X64 otherwise there will be some what variables where to refer to such errors
Set some of the environment variables referencing the file, basically with the OPENCV configuration process almost if you configure OpenCV, you know.
Click on Project--Properties---change to all configurations--Configure->vc++ Directory
Then" include directory "(including directory) that" C:\ProgramFiles\MySQL 5.7.6\include "to add in
then" Lib directory "(library directory) there put" C: \ Programfiles\mysql 5.7.6\lib "and " C: \ Programfiles\mysql 5.7.6\lib\debug "also add in. The above three files are in the installation directory of MySQL (!!!) Notice this is my MySQL installation directory You see your own)
finally add a project-properties-connectors-General -Additional dependencies riga libmysql.lib then copy this MySQL file to your own MFC project debug or place of code // If you don't add it, you'll have to add
in the project
#pragma comment (lib," Libmysql.lib ") also copy the files to the project
#pragma comment (lib," Mysqlclient.lib ")
Okay, here we go:
The first step: In addition to the code to our header file we need to add the header file:
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #include <iostream>
- #include "Winsock.h"
- #include "mysql.h"
- #include "Resource.h"
Mysql.h can not be opened is not configured to check a bit and then copy one into the project.
Step Two:
First on the code: Here click on My dialog button to jump out of the function
void Cmysqlllinkdlg::onbnclickedbutton1 ()
{
TODO: Add control notification Handler code here
MYSQL M_sqlcon;
Mysql_init (&m_sqlcon);
localhost: server root/123456 for account password testa for database name 3306 port
if (!mysql_real_connect (&m_sqlcon, "localhost", "root", "123456", "Testa", 3306, NULL, 0))
{
AfxMessageBox (_t ("Failed to Access database!"));
CString e = mysql_error (&m_sqlcon);//You need to modify the character set in the project properties to "use multibyte character set" or "not set"
MessageBox (e);
Return
}
Else
{
AfxMessageBox (_t ("Success!"));
mysql_query (&m_sqlcon, "SET NAMES ' GB2312 '");//solve the problem of garbled display of Chinese characters after reading data from the database can also be UTF-8
CString name, ID, Student_name, student_id, student_major;
LPSTR sql = (LPSTR) malloc (200); Both formats are available I like to use SQL. Format this
CString SQL;
GetDlgItemText (idc_edit1, name);
GetDlgItemText (idc_edit2, id);
Sql. Format (TEXT ("Select Student_name,student_id,student_majo from Users where father_id= '%s ' and father_name= '%s ';"), ID, name);
sprintf (SQL, "Select Student_name,student_id,student_majo from Users where father_id= '%s ' and father_name= '%s ';", ID1 , name1);
AfxMessageBox (SQL);//Look at the SQL statement
mysql_query (&m_sqlcon, SQL);
Mysql_res *res;//definition Get result set
Mysql_row row;//getting the row data in the result set
res = Mysql_store_result (&m_sqlcon);//Get query results, save the queried data to Res
int num = 0;
This function can only fetch one row of data at a time, but each time the res pointer is retrieved, the while loop can continue to fetch the following data, but pay attention to the storage problem.
if (row = mysql_fetch_row (res))
{
CString NUM1 = row[0], num2 = row[1], num3 = row[2];
Student_name = NUM1;
student_id = num2;
Student_major = num3;
Setdlgitemtext (Idc_edit3, student_name);
Setdlgitemtext (IDC_EDIT4, student_id);
Setdlgitemtext (Idc_edit5, student_major);
}
Else
{
CString Retu (_t ("Incorrect input information! "));
Setdlgitemtext (Idc_edit3, Retu);
}
}
}
Okay, the code might be a little messy or difficult to understand. Let's look at it one by one:
MYSQL M_sqlcon;mysql_init (&m_sqlcon);
This is to create an instance object of MySQL and then initialize it with another way:
MYSQL *sock; Sock=mysql_init (0);
They all mean the same thing.
Here is the connection database and judgment://localhost: server root/123456 for account password testa for database name 3306 Port &m_sqlcon This is needless to say.
!!! Note that the entire project I'm using multibyte needs to change in its own project properties there are no multibyte libraries that can go online to download an install into vs
if (!mysql_real_connect (&m_sqlcon, "localhost", "root", "123456", "Testa", 3306, NULL, 0)) {AfxMessageBox (_t (" Failed to Access database! ")); CString e = mysql_error (&m_sqlcon);//You need to modify the character set in the project properties to "use multibyte character set" or "not set" MessageBox (e); return; Else{afxmessagebox (_t ("Success!")); mysql_query (&m_sqlcon, "SET NAMES ' GB2312 '");//solve the problem of garbled display of Chinese characters after reading data from the database can also be UTF-8 }
All right, these are the simple steps to connect to a database. Next we have a small project to play .....
Step Three:
The above is a simple implementation of the query from MFC to the database of the student information of the small interface.
Here's my database table. Users are placed in a Testa database if the database installation will not be able to refer to my other Installation database blog http://www.cnblogs.com/DOMLX/p/8094659.html
See Code Explanation:
CString name, ID, Student_name, student_id, student_major;
LPSTR sql = (LPSTR) malloc (200); Both formats are available I like to use SQL. Format this
CString SQL;
GetDlgItemText (idc_edit1, name);
GetDlgItemText (idc_edit2, id);
Sql. Format (TEXT ("Select Student_name,student_id,student_majo from Users where father_id= '%s ' and father_name= '%s ';"), ID, name);
sprintf (SQL, "Select Student_name,student_id,student_majo from Users where father_id= '%s ' and father_name= '%s ';", ID1 , name1);
AfxMessageBox (SQL);//Look at the SQL statement
mysql_query (&m_sqlcon, SQL);
Mysql_res *res;//definition Get result set
Mysql_row row;//getting the row data in the result set
res = Mysql_store_result (&m_sqlcon);//Get query results, save the queried data to Res
int num = 0;
This function can only fetch one row of data at a time. Returns a row array but each time the res pointer is retrieved, the while loop can continue to fetch the following data, but pay attention to the storage problem
if (row = mysql_fetch_row (res))
{
CString NUM1 = row[0], num2 = row[1], num3 = row[2];//stores the data in the array
Student_name = NUM1;
student_id = num2;
Student_major = num3;
Setdlgitemtext (Idc_edit3, student_name);//display data to a control in a dialog box
Setdlgitemtext (IDC_EDIT4, student_id);
Setdlgitemtext (Idc_edit5, student_major);
}
Else
{
CString Retu (_t ("Incorrect input information! "));
Setdlgitemtext (Idc_edit3, Retu);
}
Complete these plus your database is open and the tables are created and the data is stored to start the query.
Small Application small project analysis based on C + + MFC connection database