請問誰能指導VS2010下MFC 查詢mysql的例子?局域網http://192.168.1.223上通過 phpstudy建立一個mysql數據庫,用戶名root,密碼note2016,端口3306,數據庫名cxdayang,表名user,請問如何編寫一個MFC的登錄小程序(做為例子)。我百度了三天,按教程操作還是出錯,由於C++僅學完基礎語法,MFC僅入門,請有能力的幫忙做出這樣的一個小例子供我參詳好嗎?本人WIN7 64位,VS2010
#include<Windows.h>
#include<string>
#include<iostream>
#include<mysql.h>
#include<stdio.h>
int main()
{
//connectionparams
char *host = "192.168.1.223";
char *user = "root";
char *pass = "note2016";
char *db = "cxdayang";
unsignedint port = 3306; //server port
MYSQL *sock;
MYSQL_RES *res;
MYSQL_FIELD * fd;
MYSQL_ROW row;
char szSqlText[500]="";
inti;
inttcreate=0;
sock=mysql_init(0);
if(sock &&mysql_real_connect(sock,host,user,pass,db,0,NULL,0))
{
std::cout<<"connect mysqlsucceed!"<<std::endl;
//mysql_set_character_set(sock,"utf8");
mysql_query(sock, "set namesgb2312");
//mysql_query(sock, "SET NAMESGBK"); //設置編碼格式,否則在cmd下無法顯示中文
//connectioncharacter set
//waitfor possibility to checksystem/mysqlmysqlets
if(tcreate)
{
sprintf(szSqlText,"create table datatable (id int(3) auto_increment not null primarykey,timedatetime,name char(10) not null,addressvarchar(20),year date,s1char(11) ,s2 int default '1') CHARACTER SET utf8");//構造SQL語句 新建一張表
std::cout<<"Create table tttable!"<<std::endl;
tcreate = 1;
if(mysql_query(sock,szSqlText))//執行Dsql語句,並判斷是否出錯
{
std::cout<<"can't createtable:"<<mysql_error(sock)<<std::endl;
//tcreate = 0;
mysql_close(sock);
return FALSE;
}
}
sprintf(szSqlText,//向mytable這個表插入數據 注意時間格式
"insert intodatatable values('1','2010-10-08', 'bj','深圳一中','2010-10-08','MySQLTest',20)");
std::cout<<"Insert data totable!"<<std::endl;
if(mysql_query( sock, szSqlText))
{
std::cout<<"Can'tinsert data to table"<<mysql_error(sock)<<std::endl;
mysql_close( sock);
return FALSE ;
}
sprintf(szSqlText, "select* from datatable");
std::cout<<"select* fromdatatable!"<<std::endl;
if(mysql_query( sock, szSqlText))//進行數據檢索並檢查有無錯誤
{
mysql_close( sock);
return FALSE ;
}
else
{
std::cout<<"mysql_store_result!"<<std::endl;
res=mysql_store_result( sock) ;//取得查詢結果,保存查詢到的數據到res
i = (int)mysql_num_rows(res ) ;//取得有效記錄數
std::cout<<"Query:"<<szSqlText<<"\n"<<i<<" recordsfound:"<<std::endl;
for( i = 0 ; fd =mysql_fetch_field( res ); i++ )//獲取列名
std::cout<<fd->name<<"\t";
std::cout<<std::endl;//輸出各字段名
while(row =mysql_fetch_row( res))//獲取具體的數據,依次讀取各條記錄
std::cout<<row[0]<<"\t"<<row[1]<<"\t"<<row[2]<<"\t"<<row[3]<<"\t"<<row[4]<<std::endl;
mysql_free_result(res ); //釋放結果資源
}
}
else//連接數據庫出錯
{
std::cout<<"cannot connect tothe sock sever "<<mysql_error(sock)<<std::endl;
mysql_close(sock);
return FALSE;
}
mysql_close(sock);//斷開連接
system("pause");
return TRUE;
}