Configure the Environment:
Include:mysql.h file
Lib:libmysql.lib file
Dll:;libmysql.dll file
Connection code:
MYSQL M_sqlcon;
Mysql_res *m_res;
Mysql_row ROW;
Initializing the database
Mysql_init (&m_sqlcon);
Setting the connection database string
CString Conn_ip,conn_port,conn_dbname,conn_user,conn_password;
if (!mysql_real_connect (&m_sqlcon,conn_ip,conn_user,conn_password,conn_dbname,int_port,null,0))
{
AfxMessageBox (_t ("Failed to Access database!"));
Return
}
mysql_query (&m_sqlcon, "SET NAMES ' GB2312 '");//solve the problem of garbled display of Chinese characters after reading data from database
Defining SQL statements
const char *sql = "SELECT * FROM table";
Execute Query
M_res = Mysql_store_result (&m_sqlcon);
returns if empty
if (NULL = = m_res)
{return;}
Reads rows repeatedly, putting data into the list until row is null
while (row = Mysql_fetch_row (m_res))
{
for (int rols = 0; rols < 2; rols++)
{
CString Myreaddata (Row[rols]);
List. AddString (Myreaddata);
}
}
Perform an Insert
const char *sql = "INSERT into Tb_rdchange (way_id,sp_way_id) values";
if (Mysql_real_query (&m_sqlcon, SQL, (unsigned long) strlen (SQL)))
{
Return
}
MFC connects MySQL database to perform queries and inserts