首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我搞不懂为什么在使用C++的SQL中跳过了IF语句。

我搞不懂为什么在使用C++的SQL中跳过了IF语句。
EN

Stack Overflow用户
提问于 2012-04-19 05:09:23
回答 1查看 180关注 0票数 0

我似乎不明白为什么我的IF语句被跳过了。将SQL与C++结合使用。程序跳过我的前两个IF语句,并跳转到else分支。我完全不确定为什么要这样做。这是我的代码。

代码语言:javascript
复制
void add_technician() {
EXEC SQL BEGIN DECLARE SECTION;
    char sn[10];
    int s = 0;
    char answer;
    int umn;
    char tname[30];
    char tadd[30];
    char tpho[10];
    char tmod[15];
EXEC SQL END DECLARE SECTION;

cout << "Enter social security number:";
cin >> sn;  

EXEC SQL SELECT count(*) into :s from Employees where SSN= :sn;

          if (s == 1)
   { 
cout << "Employee already exists in the database."; 
   cout <<"Would you like to update the union-membership-number?";
   cin >> answer;
   if (answer == 'y'|| 'Y')
   {cout <<"Enter new union member number:";
   cin >> umn;
EXEC SQL 
    INSERT INTO Employee (ssn, union_member_no)
    VALUES (:sn, :umn);
 } 
   }
         else {
 cout << "Enter in union membership number of the new employee: ";
            cin >> umn;
            EXEC SQL INSERT INTO Employees (ssn, union_member_no) 
                   VALUES (:sn, :umn);
            EXEC SQL COMMIT WORK;
             cout << "Enter the address of the technician.";
             cin >> tadd;
 cout << "Enter the name technician name.";
             cin >> tname;

            EXEC SQL INSERT INTO Technicians (address, name , phone)
       VALUES (:tadd, :tname, :tpho);
EXEC SQL COMMIT WORK;
            cout << "Enter airplane model number that you are an expert on." ;
            cin >> tmod;
EXEC SQL INSERT INTO Experts (model_no, ssn)
    VALUES (:tmod);
EXEC SQL COMMIT WORK; }                

}
EN

回答 1

Stack Overflow用户

发布于 2012-04-19 05:47:12

代码允许为单个SSN分配多个UMN,但是第一个if语句没有考虑到这一点。它正在检查仅为其分配了1个UMN的SSN。如果给定的SSN分配了多个UMN,则SELECT将返回count > 1,并且流将跳转到您的else块。

此外,您的第二个if语句的格式也不正确。if (answer == 'y'|| 'Y')的计算结果始终为true。您需要在每组条件中指定answer变量,如下所示:if ((answer == 'y') || (answer == 'Y'))

试试这个:

代码语言:javascript
复制
void add_technician()
{
    EXEC SQL BEGIN DECLARE SECTION;
        char sn[10];
        int s = 0;
        char answer;
        int umn;
        char tname[30];
        char tadd[30];
        char tpho[10];
        char tmod[15];
    EXEC SQL END DECLARE SECTION;

    cout << "Enter social security number:";
    cin >> sn;

    EXEC SQL SELECT count(*) into :s from Employees where SSN= :sn;

    if (s > 0)
    {
       cout << "Employee already exists in the database.";
       cout << "Would you like to add a new union membership number?";
       cin >> answer;
       if ((answer == 'y') || (answer == 'Y'))
       {
           cout << "Enter new union member number:";
           cin >> umn;
           EXEC SQL INSERT INTO Employee (ssn, union_member_no)  VALUES (:sn, :umn);
       }
    }
    else
    {
        cout << "Enter union membership number of the new employee: ";
        cin >> umn;
        EXEC SQL INSERT INTO Employees (ssn, union_member_no) VALUES (:sn, :umn);
        EXEC SQL COMMIT WORK;

        cout << "Enter the address of the technician.";
        cin >> tadd;
        cout << "Enter the name of the technician.";
        cin >> tname;
        EXEC SQL INSERT INTO Technicians (address, name , phone) VALUES (:tadd, :tname, :tpho);
        EXEC SQL COMMIT WORK;

        cout << "Enter airplane model number that the technician is an expert on." ;
        cin >> tmod;
        EXEC SQL INSERT INTO Experts (model_no, ssn) VALUES (:tmod);
        EXEC SQL COMMIT WORK;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10218054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档