I have a table "SiteCode" with following schema :
CREATE TABLE SiteCode(
ID INTEGER PRIMARY KEY,
Code TEXT(3) UNIQUE NOT NULL DEFAULT 0);
Through the following code I can Open a DB & Access the elements of DB and perform the query execution perfectly.
Now I wish to add a small code snippet which could check if the element that a user wish to delete exists .
For example: Suppose table SiteCode have following Entries in Code column : 400, 401, 402 and 403 and user enters 444 as the input to DELETE query then it shall return an error.
At present, if a user enters 444 as input to the query then that query gets executed successfully without checking the Code column and without any Error.
How do i approach this problem. Kindly Help.
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);
}
Aucun commentaire:
Enregistrer un commentaire