我有一个表" SiteCode" ,其中包含以下架构:
CREATE TABLE SiteCode(
ID INTEGER PRIMARY KEY,
Code TEXT(3) UNIQUE NOT NULL DEFAULT 0);
通过以下代码我可以打开数据库&访问数据库的元素并完美地执行查询。
现在我想添加一个小代码片段,它可以检查用户是否希望删除的元素。
例如:假设表 SiteCode 在代码列中包含以下条目: 400 , 401 , 402 和 403 ,用户输入 444 作为DELETE查询的输入,然后它将返回错误。
目前,如果用户输入 444 作为查询的输入,那么该查询会成功执行,而不会检查代码列,也不会有任何错误。
我如何处理这个问题。请帮助。
void DELETE_Site_Code()
{
int CodeA;
int rc = sqlite3_open("/DBsqlite3/empdbv3.db", &db);
if (rc != SQLITE_OK) {
cerr << "Cannot open database [ " << sqlite3_errmsg(db) << " ]" << endl;
sqlite3_close(db);
}
sql = "DELETE FROM SiteCode WHERE Code= @Code;";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt4, 0);
if(SQLITE_OK != rc) {
cerr << "Failed to PREPARE DELETE statement for SiteCode Table [ " << sqlite3_errmsg(db) << " ]" << endl;
sqlite3_close(db);
exit(1);
}
else{
int Code_x = sqlite3_bind_parameter_index(stmt4, "@Code");
cout<< "Enter the Site Code That you wish to DELETE from table-SiteCode\n"<< endl;
cin>>CodeA;
if(cin.fail()) {
cout<<"\nPlease enter only digits\n"<<endl;
}
else {
if((CodeA >= 000) && (CodeA <= 998)) {
sqlite3_bind_int(stmt4, Code_x, CodeA);
}
else {
cout<< "Valid Site Code Should be between 000 to 998\n" <<endl;
}
} //else loop of (cin.fail) ends here
}
int step = sqlite3_step(stmt4);
if(SQLITE_DONE != step) {
cout<<"\nERROR while inserting into table\n"<< endl;
}
else {
cout<<"\nRecord DELETION completed"<<endl;
}
if(step == SQLITE_ROW){
cout<< sqlite3_column_text(stmt4, 0) << endl;
cout<< sqlite3_column_text(stmt4, 1) << endl;
}
sqlite3_finalize(stmt4);
sqlite3_close(db);
}
答案 0 :(得分:0)
使用sqlite3_changes()
函数查询最近执行的查询影响的行数:
// ...
int step = sqlite3_step(stmt4);
if(SQLITE_DONE != step) {
cout<<"\nERROR while inserting into table\n"<< endl;
}
else if (sqlite3_changes(db) == 0) {
cout<<"\nNo records deleted"<<endl;
}
else {
cout<<"\nRecord DELETION completed"<<endl;
}