標籤:配置環境 sql資料庫 sql span tor store 返回 讀取資料 sele
配置環境:
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;
}
MFC串連Mysql資料庫執行查詢和插入