MFC connects MySQL database to perform queries and inserts

Source: Internet
Author: User

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

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.