查询“ SELECT * ...”未返回实际值

时间:2019-12-14 18:57:43

标签: c++ mysql c++11 mysql-connector

我正在制作一些命令行数据库仪表板以供学习。我用php和c#制作了相同的仪表板,它们都可以正常工作。

但是C ++中的这个并没有给我应有的值。

我的代码就是这个:

    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <windows.h>
    #include <sstream>
    #include <mysql.h>
    #include <stdio.h>
    #include <vector>

    using namespace std;

    int q = 0;

    int numTotalUsers = 0;
    int numUsers2 = 0;
    int percentage = 0;
    int activeUsers = 0;

    int gatherTotalUsers();
    int gatherPercentage();
    int gatherActiveUsers();
    //int gatherTotalQuests();
    //int gatherActiveQuests();
    int showValues();

    int main()
    {   
        gatherTotalUsers();
        return 0;
    }

    int gatherTotalUsers()
    {
        // mysql
        MYSQL_ROW row;
        MYSQL_RES* res;

        // users
        MYSQL* connU;
        connU = mysql_init(0);
        connU = mysql_real_connect(connU,"******","Pfw7lneUyi","********","Pfw7lneUyi",3306, NULL, 0);

        stringstream sU;
        sU << "SELECT * FROM usageTrack";

        string uT_query = sU.str();
        const char* _q = uT_query.c_str();

        q = mysql_query(connU, _q);

        if(!q)
        {
            res = mysql_store_result(connU);

            while(row = mysql_fetch_row(res))
            {
                numTotalUsers = (int)row[0];
            }
        }else
        {
            cout << "1";
        }

        gatherPercentage();
    }

    int gatherPercentage()
    {
        // mysql
        MYSQL_ROW row;
        MYSQL_RES* res;

        // users
        MYSQL* connU;
        connU = mysql_init(0);
        connU = mysql_real_connect(connU,"******","Pfw7lneUyi","*******","Pfw7lneUyi",3306, NULL, 0);

        stringstream sU2;
        sU2 << "SELECT * FROM usageTrack WHERE usedTimes >= 2";

        string uT2_query = sU2.str();
        const char* _q = uT2_query.c_str();

        q = mysql_query(connU, _q);

        if(!q)
        {
            res = mysql_store_result(connU);

            while(row = mysql_fetch_row(res))
            {
                numUsers2 = (int)row[0];
            }
        }else
        {
            cout << "2";
        }

        percentage = ((int)numUsers2 * 100) / (int)numTotalUsers;

        gatherActiveUsers();
    }

    int gatherActiveUsers()
    {
        // mysql
        MYSQL_ROW row;
        MYSQL_RES* res;

        // users
        MYSQL* connU;
        connU = mysql_init(0);
        connU = mysql_real_connect(connU,"*****","Pfw7lneUyi","*********","Pfw7lneUyi",3306, NULL, 0);

        stringstream aU;
        aU << "SELECT * FROM activeUsers";

        string aU_query = aU.str();
        const char* _q = aU_query.c_str();

        q = mysql_query(connU, _q);

        if(!q)
        {
            res = mysql_store_result(connU);

            while(row = mysql_fetch_row(res))
            {
                activeUsers = (int)row[0];
            }
        }else
        {
            cout << "3";
        }

        showValues();
    }

    int showValues()
    {
        int r;

        cout << "Total Users: " << numTotalUsers << "\n% of returning users: " << percentage << "\nActive Users: " << activeUsers;

        cout << "\n\n[1] - refresh\n\n>>";

        cin >> r;

        if(r == 1)
        {
            showValues();
        }else
        {
            return 0;
        }
    }

代码可以编译和调试,没有任何问题。 “错误”是我在上一个函数中获得的值是:

  • 总用户:19147440
  • 回访用户的百分比:100
  • 活动用户:19223608

它应该给的值是:

  • 用户总数:140
  • 回访用户的百分比:22%
  • 活动用户:1

(这些最后的值取自用php制作的仪表板)

0 个答案:

没有答案