C ++ MFC对话框 - 如何在列表控件中显示MySQL数据库中的项目?

时间:2014-09-03 09:14:24

标签: c++ mysql mfc dialog listcontrol

我正在使用Visual Studio 2005学习C ++ MFC。

我想从Oracle MySQL数据库中获取数据并将其显示在列表控件中。不知怎的,代码正在运行,但是显示很奇怪而且非常糟糕,我觉得我的代码中缺少某些内容。

如何修改代码?数据库中有一个我不想显示哪个字段是UserID。

以下是我提出的代码:

void CPplCMain::OnBnClickedButton1()
{
    unsigned short Port = 3306;
    char *IPAddress = "127.0.0.1";
    char *UserName = "root";
    char *Password = "Root";
    char *DBName = "inomatic";

    MYSQL *ssock;
    MYSQL_RES   *res;
    MYSQL_ROW   row;
    //char execsql[500];
    ssock = (MYSQL *)malloc(sizeof(MYSQL));
    //在某些版本中,不需要该初始化工作,可观看mysql.H以及readme
    mysql_init(ssock);
    if(ssock == NULL)
    {
        MessageBox("EROR: MySQL ssock init error. \n");
        return;
    }
    //MessageBox("MySQL ssock init OK. \n");

    ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0);
    if(!ssock)
    {
        MessageBox("conn fail... \n");
        mysql_errno(ssock);
    }

    if(mysql_select_db(ssock, DBName) != 0)
    {
        MessageBox("select db error. \n");
        return;
    }

    //SQL查询语句
    if(mysql_query( ssock,"SELECT countryName FROM countries"))
    {
        MessageBox("Found", mysql_error(ssock));
    }
    if( !(res = mysql_store_result(ssock)) )
    {
        MessageBox("Disconnected!", mysql_error(ssock));
    }
    while( (row = mysql_fetch_row(res)) )
    {
        for(int i=0 ; i<mysql_num_fields(res); i++)
        {
           TRACE("%s ",row[i],"\n");
           m_CountryList.AddString(row[i]);
        }
        TRACE("\n");
    }

    if(mysql_query( ssock,"SELECT * FROM shop"))
    {
        MessageBox("Shop table Found", mysql_error(ssock));
    }
    if( !(res = mysql_store_result(ssock)) )
    {
        MessageBox("Disconnected!", mysql_error(ssock));
    }
    while( (row = mysql_fetch_row(res)) )
    {
        for(int i=0 ; i<mysql_num_fields(res); i++)
        {
            TRACE("%s ",row[i],"\n");
            //m_ShopList.AddString(row[i]);
            int nIndex = m_ShopListCtrl.InsertItem(i, row[i]);
            m_ShopListCtrl.SetItemText(nIndex, i,  row[i]);
        }
        TRACE("\n");
    }

    if(mysql_query( ssock,"SELECT * FROM peoplecounter"))
    {
        MessageBox("People Counter table Found", mysql_error(ssock));
    }
    if( !(res = mysql_store_result(ssock)) )
    {
        MessageBox("Disconnected!", mysql_error(ssock));
    }
    while( (row = mysql_fetch_row(res)) )
    {
        for(int i=0 ; i<mysql_num_fields(res); i++)
        {
            TRACE("%s ",row[i], "\n");
            //m_PplCounterList.AddString(row[i]);
            int nIndex = m_PplCounterCtrl.InsertItem(i, row[i]);
            m_PplCounterCtrl.SetItemText(nIndex, i,  row[i]);
        }
        TRACE("\n");
    }

    mysql_close(ssock);
    // TODO: Add your control notification handler code here
}

以下是我的数据库中的数据填充后的列表控件的图像:

Image of my list control after filling it with data from my database

正如您在图像中看到的那样,第一行显示正确,但是对于接下来的六行,在第8行正确显示下一个数据集之前,逐行显示重复数据。

1 个答案:

答案 0 :(得分:1)

您的代码的问题是,您在列表控件中为数据库行的每个列条目插入一行。您必须在for循环之前仅在列表控件中插入一行,例如,如下所示:

while(row = mysql_fetch_row(res))
{
    // Insert a new row at the end of the list control.
    // Do this only once per database row.
    int nNewIndex = m_PplCounterCtrl.GetItemCount();
    int nCurIndex = m_PplCounterCtrl.InsertItem(nNewIndex, row[0]);

    // Insert all remaining database column entries.
    // Insert them into the current row of the list control.
    for(int i = 1; i < mysql_num_fields(res); i++)
    {            
        m_PplCounterCtrl.SetItemText(nCurIndex, i, row[i]);
    }
}