如何使用C++检查 sqlite 列中是否"value does not exists"
How to check if "value does not exists" in sqlite column using C++
我有一个表"SiteCode",具有以下架构:
CREATE TABLE SiteCode(
ID INTEGER PRIMARY KEY,
Code TEXT(3) UNIQUE NOT NULL DEFAULT 0);
通过以下代码,我可以打开一个DB&访问数据库的元素并完美地执行查询。
现在我想添加一个小代码片段,它可以检查用户想要删除的元素是否存在。
例如:假设表SiteCode在Code列中有以下条目: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-SiteCoden"<< endl;
cin>>CodeA;
if(cin.fail()) {
cout<<"nPlease enter only digitsn"<<endl;
}
else {
if((CodeA >= 000) && (CodeA <= 998)) {
sqlite3_bind_int(stmt4, Code_x, CodeA);
}
else {
cout<< "Valid Site Code Should be between 000 to 998n" <<endl;
}
} //else loop of (cin.fail) ends here
}
int step = sqlite3_step(stmt4);
if(SQLITE_DONE != step) {
cout<<"nERROR while inserting into tablen"<< 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);
}
使用sqlite3_changes()
函数查询最近执行的查询影响了多少行:
// ...
int step = sqlite3_step(stmt4);
if(SQLITE_DONE != step) {
cout<<"nERROR while inserting into tablen"<< endl;
}
else if (sqlite3_changes(db) == 0) {
cout<<"nNo records deleted"<<endl;
}
else {
cout<<"nRecord DELETION completed"<<endl;
}
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- 为什么static_assert错误:即使我传递常量"expression must have a constant value"?
- 为什么我会" void value not ignored as it ought to be"?
- 方法错误"not all control paths return a value"和方法不返回值
- 将系数存储在头文件的数组中("does not name a type"错误)
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- 如何在 c++ 中理解这样的代码 [request->headers().Method()->value().getStringView())]
- std::<key-value>不同类型的对向量
- c++ visual studio 64bit | save registers value
- 初始化数组、"memset"或" {//value} "的最佳方法是什么?
- 如何解决类成员函数中的"return a value"错误?
- bsoncxx: document::view vs document::value
- .value( "key" , default) 不适用于空的 json 对象吗?
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 如何在C++中设置演员的"Render Custom Depth Pass"和"Depth Stencil Value"?
- 由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编
- 如何解决"'mutex' in namespace 'std' does not name a type"?
- C++ 迭代器错误"does not refer to a value"
- 如何使用C++检查 sqlite 列中是否"value does not exists"
- "Does not refer to value" C++ 过程中的错误