无法弄清楚为什么我的 IF 语句在 SQL 中被跳过C++
can't figure out why my IF statement is being skipped in SQL with C++
我似乎无法弄清楚为什么我的IF语句被跳过了。将 SQL 与 C++ 结合使用。程序跳过我的前两个 IF 语句并跳转到 else 分支。不知道为什么要这样做。这是我的编码。
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; }
}
该代码允许单个 SSN 分配多个 UMN,但第一个 if
语句没有考虑到这一点。 它正在检查仅分配了 1 个 UMN 的 SSN。 如果给定的 SSN 分配了多个 UMN,则SELECT
将返回count > 1
,并且流将跳转到您的else
块。
此外,您的第二个if
语句格式不正确。 if (answer == 'y'|| 'Y')
将始终评估为true
.您需要在每组条件中指定answer
变量,如下所示:if ((answer == 'y') || (answer == 'Y'))
。
试试这个:
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;
}
}
相关文章:
- 如何使用c++在VS 2019上运行SQL查询
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 在C++中调用 MS SQL 存储过程
- 尝试创建表会给出 SQL 逻辑错误
- 在 MySQL/Connector C++中使用 SQL 联接
- Qt PL/SQL - 赋值运算符 - 字符串缓冲区太小
- PostgresSQL - SQL Ready 语句与字符串转义,防止 SQL 注入攻击
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- 使用 SQLConfig数据源创建 SQL Server DSN 失败:关键字-值对无效
- 使用 Qt5 SQL 进行异步数据库访问的策略
- Qt/SQL - 从 QSqlQuery exec Stored Procedure 获取列类型和名称?
- 如何将QTime保存在SQL Server Express表中并读回?
- 为 Sql 服务器实现 odbc 包装器.将数据库数据读取为字符或要求驱动程序将数据转换为 C 类型
- 使用 python 或 c ++ 中的 sql 根据输入数据输出输出示例
- 无法使用 SQL VDI(虚拟设备接口)创建条带化备份
- sql记录集函数的状态设计模式
- Qt SQL LIKE语句返回错误
- 无法从 SQL Server 检索数据
- 如何在将Visual Studio C++控制台应用程序连接到Microsoft SQL Server时修复"
- 如何在 sql 查询中获取 soci::rowset<std::string>