简体   繁体   中英

C++ MFC Dialog - How to display items from a MySQL database in a list control?

I am learning on C++ MFC using Visual studio 2005.

I want to get data from an Oracle MySQL database and display it in a list control. Somehow the code is working, but the display is weird and horrible, I feel that something is missing from my code.

How do I have to modify my code? There is one field in the database I don't want to display which is the UserID.

Below is the code I came up with:

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
}

Here is an image of my list control after filling it with data from my database:

用数据库中的数据填充列表控件后的图像

As you can see in the image, the first row is displayed correctly, but for the next six rows duplicated data is shown row by row, before the next data set is displayed correctly in row eight.

The problem with your code is, that you insert a row into your list control for every column entry of a database row. You have to insert a new row into your list control only once before your for loop, for example, like this:

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]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM