sql incorporating c++

sql incorporating c++

本文关键字:c++ incorporating sql      更新时间:2023-10-16

我正在编写SQL和C ++代码,将技术人员添加到数据库中。 必须执行 3 个 SQL 插入语句。 我在我的代码中包含它,但我的代码不符合或执行要求。 这是问的。

程序应提示用户输入 SSN。

如果该 ssn/employee 已经在 Employee 表中,程序应通知用户该员工已在数据库中,并询问用户是否要更新该员工的工会会员编号;如果用户的回答是肯定的,则程序应向用户询问新的工会成员编号并在数据库中更新它。

如果 SSN/员工尚未在数据库中,则程序应向用户询问该员工的工会会员编号,然后将该记录存储在 Employee 表中。假设员工是技术人员,程序应要求用户存储有关该技术人员的信息(姓名、地址和电话号码),并将其存储在技术人员表中;然后,程序应向用户询问技术人员是专家的飞机型号,并将相应的记录添加到"专家"表中。

这是我的代码:

void add_technician() {
EXEC SQL BEGIN DECLARE SECTION;
    char sn[9];
    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 SSN into :s from Employee where SSN= :sn;
          if (s == sn)
   { 
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; }                
}

当我运行程序时,它会跳过我的前两个 IF 语句。 我似乎不明白为什么。

您将值存储回 s 的方式不正确。

查看一个很好的示例 ESQL 程序员指南

我会说你有一个MySQL方法来解决这个问题。相反,我会做这样的事情(粗略的方法):

int s = 0;
EXEC SQL SELECT rowSSN into :s from Employee where SSN= :sn;
if (s == sn) 
...

s 的值实际上是它本身的行的值。请考虑查看 CURSOR 以通过计数器从查询返回的行数来获得一种方法。

相关文章: