Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete row (SQL) [bcb]

Status
Not open for further replies.

ik0

Programmer
Jun 2, 2010
14
MK
I want to delete a row in the base according from the data in the column.... when i use this code
Code:
DELETE FROM Table WHERE Column1=1 AND Column2=1
it works ok, it deletes only one row.... but when i use
Code:
DELETE FROM Table WHERE Column1 LIKE '%" + Edit->Text + "%' AND Column2 LIKE '%" + Edit1->Text + "%'";
and in edit1 are written value from column1 by click in edit1 is written value by column2 by click.... if in the grid i have 6 rows and i select the first row and then delete it it works, but when i select the last row and I delete, then the whole table is deleted... where is the problem here ?
 
full code
Code:
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
AnsiString ab= "DELETE FROM Table WHERE Column1 LIKE '%" + Edit->Text + "%' AND Column2 LIKE '%" + Edit1->Text + "%'";
ADOQuery1->SQL->Add( ab );
ADOQuery1->Open();
 
there is a mistake in the code - instead this code ADOQuery1->Open(); it should be this one ADOQuery1->ExecSQL();
 
if in the grid i have 6 rows and i select the first row and then delete it it works, but when i select the last row and I delete, then the whole table is deleted.
I'm not sure I understand. Are you selecting rows in a visual table to delete or are you only entering data in an edit box?

To me it looks like your delete statement would look like:
Code:
DELETE FROM Table WHERE Column1 LIKE '%1%' AND Column2 like '%1%'
With those wildcards (%), I think it would delete anything in the table that has a "1" anyplace in either column.

Here is a trick I use. To see what the statement looks like before I run it, I do something like:
Code:
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
AnsiString ab= "DELETE FROM Table WHERE Column1 LIKE '%" + Edit->Text + "%' AND Column2 LIKE '%" + Edit1->Text + "%'";
Application->Messagebox(ab, "Testing", MBOK); // temp
//ADOQuery1->SQL->Add( ab );
//ADOQuery1->ExecSQL();
This will show you the code instead of running it. I also create a temporary SELECT statement instead of a DELETE statement, display that, and then run it in an SQL manager to see what the results are. That way I know I what I'm
going to delete before I permanently code a DELETE.


James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top