m0_64743059 2023-03-30 23:56 采纳率: 33.3%
浏览 18
已结题

SQLGetData()问题

这里我想询问一下,SQLGetData(hstmt, 1, SQL_C_SLONG, b1,20, &len_b1); 这里输出整型之后,想要判断b1或者要用b1来计算的话应该怎么做呀,我这里获取到的是一个id,然后需要做加减法,但是试了一下,错误是“不能将 "SQLCHAR *" 类型的值分配到 "int" 类型的实体”。所以应该怎么做呢?


void printweek()
{
    ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
    string str1 = "use Calendar";
    string str2 = "select time_id,week,festival from Calendar where year = ";
    string str3 = "and month =";
    string str4 = "and day_of_month =";

    string  year, month, day_of_month;

    cout << "请输入年份(1940-2040): ";
    cin >> year;
    cout << "请输入月份(01-12): ";
    cin >> month;
    cout << "请输入日: ";
    cin >> day_of_month;

    string str5 = str2 + year  + str3 + month + str4 + day_of_month ;

    ret = SQLExecDirect(hstmt, (SQLCHAR*)str1.c_str(), SQL_NTS);
    ret = SQLExecDirect(hstmt, (SQLCHAR*)str5.c_str(), SQL_NTS);
    


    if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
    {
         
        SQLCHAR b1[20], b2[20],b3[20];
        SQLLEN len_b1,len_b2, len_b3;

        while (SQLFetch(hstmt) != SQL_NO_DATA)
        {
            SQLGetData(hstmt, 1, SQL_C_SLONG, b1,20, &len_b1);
            SQLGetData(hstmt, 2, SQL_C_CHAR, b2, 20, &len_b2);
            SQLGetData(hstmt, 3, SQL_C_CHAR, b3, 20, &len_b3);

            //想在这边做计算,b1做加减法

            cout << endl;
            cout << "-----------------------"<<endl;
            cout << "距离今天的天数是:"<<b1 <<endl;
            cout << "-----------------------"<<endl;
            cout << "这天是:"<<b2<<endl;
            cout << "-----------------------"<< endl;
            cout << "这天否是节日:"<<b3<<endl;
            cout << "------------------------"<< endl;
            cout << endl;
            
        }
    }

    free();
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2023-03-31 00:04
    关注

    用atoi转换成int

    while (SQLFetch(hstmt) != SQL_NO_DATA)
    {
        SQLGetData(hstmt, 1, SQL_C_SLONG, b1,20, &len_b1);
        SQLGetData(hstmt, 2, SQL_C_CHAR, b2, 20, &len_b2);
        SQLGetData(hstmt, 3, SQL_C_CHAR, b3, 20, &len_b3);
    
        int id = atoi((char*)b1);  // 转换为int类型
        int result = id + 1;  // 进行加法运算
    
        cout << endl;
        cout << "-----------------------"<<endl;
        cout << "距离今天的天数是:"<<result<<endl;
        cout << "-----------------------"<<endl;
        cout << "这天是:"<<b2<<endl;
        cout << "-----------------------"<< endl;
        cout << "这天否是节日:"<<b3<<endl;
        cout << "------------------------"<< endl;
        cout << endl;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 创建了问题 3月30日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效