配置环境:
include:mysql.h文件
lib:libmysql.lib文件
dll:;libmysql.dll文件
连接代码:
MYSQL m_sqlCon;
MYSQL_RES *m_res; MYSQL_ROW row;//初始化数据库
mysql_init(&m_sqlCon); // 设置连接数据库字符串 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("访问数据库失败!")); return; }mysql_query(&m_sqlCon,"SET NAMES 'GB2312'");//解决从数据库中读取数据后汉字乱码显示的问题
//定义sql语句
const char *sql = "select * from table";
//执行查询
m_res = mysql_store_result(&m_sqlCon);
//如果为空则返回
if(NULL == m_res)
{ return; }//重复读取行,把数据放入列表中,直到row为NULL
while (row = mysql_fetch_row(m_res))
{ for (int rols = 0; rols < 2; rols++) { CString myreaddata(row[rols]); list.AddString(myreaddata); }}//执行插入
const char *sql = "insert into tb_rdchange(way_id,sp_way_id) values(4,4)";
if(mysql_real_query(&m_sqlCon, sql,(unsigned long)strlen(sql))) { return; }