mysql提供了很多语言的API,包括C、C++、PHP、Perl,Python等。在该程序中用的是C API,代码具有通用性。具体代码如下:
MYSQL mysql;
MYSQL_RES *mysql_result;
MYSQL_ROW mysql_row;
char tmp[64];
wxString str;
if(mysql_init(&mysql)==NULL)
{
wxMessageDialog dlg(this,_("unable to initialize MYSQL struct!"));
dlg.ShowModal();
}
if(mysql_real_connect(&mysql,"202.117.10.254", "xjtugrid", "xjtugrid","grid_context_monitor", 3306, NULL,0)==NULL)
{
wxMessageDialog dlg(this,_("unable to connect MYSQL!"));
dlg.ShowModal();
return;
}
if(mysql_query(&mysql,"select * from user") != 0)
{
wxMessageDialog dlg(this,_("unable to query MYSQL!"));
dlg.ShowModal();
return;
}
mysql_result=mysql_store_result(&mysql);
if(mysql_result == NULL)
{
wxMessageDialog dlg(this,_("unable to query MYSQL!"));
dlg.ShowModal();
return;
}
while(mysql_row = mysql_fetch_row(mysql_result))
{
sprintf(tmp,"%s",mysql_row[0]);
wxString wx_string(tmp,wxConvUTF8);
str += wx_string;
}
wxMessageDialog dlg(this,str);
dlg.ShowModal();