配置環境: include:mysql.h文件 lib:libmysql.lib文件 dll:;libmysql.dll文件 連接代碼: MYSQL m_sqlCon; MYSQL_RES *m_res; MYSQL_ROW row; //初始化資料庫 mysql_init(&m_sqlCon); ...
配置環境:
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;
}