各位老师:我在wxwidgets下对数据库进行写入操作,当我调用这个函数的时候,插入数据次数只能插入100次就弹出:不能建立连接 对话框,请问这个是什么原因?请老师指点,谢谢。
void DataBaseDialog::InsertDataBase( wxCommandEvent& event )
{
wxDbConnectInf *DbConnectInf = NULL; // 定义数据库连接信息指针DB connection information
wxDb *Conn = NULL; // 定义数据库连接指针Database connection
wxDbTable *table = NULL; // 定义数据表指针Data table to mysql

  DbConnectInf = new wxDbConnectInf(0, wxT(""), wxT(""), wxT(""));//这里定义的内容基本没用,但不定义会报错

Conn = new wxDb(DbConnectInf->GetHenv());

bool DBfailOnDataTypeUnsupported=!true;// if(!Conn->Open(wxT("driver={mysql odbc 5.1 driver};server=192.168.100.13;DSN=icdata_db;charset=gb2312;database=icdata_db;uid=root;pwd=duangexin;port=3306;"),DBfailOnDataTypeUnsupported))//使用驱动程序的方式打开数据库
{
wxMessageBox("不能建立连接","DB CONNECTION ERROR", wxOK | wxICON_EXCLAMATION);
} table = new wxDbTable(Conn, wxT("ictable"),10,wxT(""),!wxDB_QUERY_ONLY, wxT("")); //定义保存列内容的变量
wxString sst;
wxChar ChipNumbers[100];
wxChar ChipCounts[100];
wxChar ChipTypes[100];
wxChar InspectorNumbers[100];
wxChar CheckTimes[100];
wxChar BugTypes[100];
wxChar CheckItems[100];
wxChar CheckResults[100];
wxChar ErrorCounts[100];
wxChar ErrorPercents[100];

//有几列就定义几列
table->SetColDefs(0, wxT("ChipNumber"), DB_DATA_TYPE_VARCHAR,ChipNumbers, SQL_C_WXCHAR, sizeof(ChipNumbers), true, true);
table->SetColDefs(1, wxT("ChipCount"), DB_DATA_TYPE_VARCHAR, ChipCounts,SQL_C_WXCHAR, sizeof(ChipCounts), true, true);
table->SetColDefs(2,wxT("ChipType"),DB_DATA_TYPE_VARCHAR,ChipTypes,SQL_C_WXCHAR,sizeof(ChipTypes),true,true);
table->SetColDefs(3, wxT("InspectorNumber"), DB_DATA_TYPE_VARCHAR,InspectorNumbers, SQL_C_WXCHAR, sizeof(InspectorNumbers), true, true);
table->SetColDefs(4, wxT("CheckTime"), DB_DATA_TYPE_VARCHAR, CheckTimes,SQL_C_WXCHAR, sizeof(CheckTimes), true, true);
table->SetColDefs(5,wxT("BugType"),DB_DATA_TYPE_VARCHAR,BugTypes,SQL_C_WXCHAR,sizeof(BugTypes),true,true);
table->SetColDefs(6, wxT("CheckItem"), DB_DATA_TYPE_VARCHAR,CheckItems, SQL_C_WXCHAR, sizeof(CheckItems), true, true);
table->SetColDefs(7, wxT("CheckResult"), DB_DATA_TYPE_VARCHAR, CheckResults,SQL_C_WXCHAR, sizeof(CheckResults), true, true);
table->SetColDefs(8,wxT("ErrorCount"),DB_DATA_TYPE_VARCHAR,ErrorCounts,SQL_C_WXCHAR,sizeof(ErrorCounts),true,true);
table->SetColDefs(9, wxT("ErrorPercent"), DB_DATA_TYPE_VARCHAR,ErrorPercents, SQL_C_WXCHAR, sizeof(ErrorPercents), true, true); //打开DbTable对象
if( !table->Open())
{
wxMessageBox("不能打开数据库结构表!");
return;
} //获取当天操作时间
wxDateTime now = wxDateTime::Now();
wxString strDate = now.Format(wxT("%Y-%m-%d"));

//  //添加新记录
  wxStrcpy(ChipNumbers, wxT("2323"));      //ChipNumbers所绑定的字段将添加一条记录
  wxStrcpy(ChipCounts,wxT("3"));
  wxStrcpy(ChipTypes,wxT("3"));
  wxStrcpy(InspectorNumbers,wxT("13"));
  wxStrcpy(CheckTimes,wxT("2009-12-29"));
  wxStrcpy(BugTypes,wxT("3"));
  wxStrcpy(CheckItems,wxT("3"));
wxStrcpy(CheckResults,wxT("false"));
wxStrcpy(ErrorCounts,wxT("3"));
  wxStrcpy(ErrorPercents,wxT("3"));  
if(!table->Insert())

{

//判断添加是否成功
wxMessageBox("添加失败!");
return;

}

//提交更新数据库

table->GetDb()->CommitTrans(); //查询记录,实际上就是用SQL语句来执行
wxString sql = "select * from ictable";//根据自己的需要写查询语句 //wxMessageBox(sql);     //测试SQL语句是否正确
if(!table->QueryBySqlStmt(sql))//执行SQL语句
{
//判断是否执行成功
wxMessageBox("执行成功!");
return;
}    m_listCtrl1->DeleteAllItems();
//m_listCtrl1->Hide();
int i = 0; //表格行数
while (table->GetNext())//获得当前游标位置下一条记录
{
m_listCtrl1->InsertItem(i,ChipNumbers);
m_listCtrl1->SetItem(i,1,ChipCounts);
m_listCtrl1->SetItem(i,2,ChipTypes);
m_listCtrl1->SetItem(i,3,InspectorNumbers);
m_listCtrl1->SetItem(i,4,CheckTimes);
m_listCtrl1->SetItem(i,5,BugTypes);
m_listCtrl1->SetItem(i,6,CheckItems);
m_listCtrl1->SetItem(i,7,CheckResults);
m_listCtrl1->SetItem(i,8,ErrorCounts);
m_listCtrl1->SetItem(i,9,ErrorPercents);
i++;
} //m_listCtrl1->Show();
//关闭DbTable对象
if(table)
{
wxDELETE(table);
table = 0;
}

//关闭连接
if(Conn)
{
wxDbFreeConnection(Conn);
Conn = 0;
wxDbCloseConnections();
}

//释放环境
DbConnectInf->FreeHenv();}

解决方案 »

  1.   

    应该是你每次写完后没有关闭 connection. 
    这样到达 max_connections          | 100 的上限了。
    及时关闭你的连接。
    mysql> show variables like '%connect%';
    +--------------------------+-------------------+
    | Variable_name            | Value             |
    +--------------------------+-------------------+
    | character_set_connection | latin1            |
    | collation_connection     | latin1_swedish_ci |
    | connect_timeout          | 10                |
    | init_connect             |                   |
    | max_connect_errors       | 10                |
    | max_connections          | 100               |
    | max_user_connections     | 0                 |
    +--------------------------+-------------------+
    7 rows in set (0.00 sec)mysql>
      

  2.   

    if(table) 

    wxDELETE(table); 
    table = 0; 
    } //关闭连接 
    if(Conn) 

    wxDbFreeConnection(Conn); 
    Conn = 0; 
    wxDbCloseConnections(); 
    } //释放环境 
    DbConnectInf->FreeHenv(); 这个语句难道有错???我仔细查查~~~
      

  3.   

    提供你的信息。
    show variables like '%connect%';
    还有
     show processlist;以供分析。
      

  4.   

    if(table) 

    wxDELETE(table); 
    table = 0; 
    } //关闭连接 
    if(Conn) 

    Conn->Close();
    wxDbFreeConnection(Conn); 
    Conn = 0; 
    wxDbCloseConnections(); 
    } //释放环境 
    DbConnectInf->FreeHenv(); 
    谢谢老师 我加上 Conn->Close();
    一切OK啦。