使用editbox msaccess delphi 7从数据库中删除记录 [英] delete a record from a database with a editbox msaccess delphi 7

查看:79
本文介绍了使用editbox msaccess delphi 7从数据库中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是delphi的新手(或该问题的任何编码),并且过去一周我一直在学习SQL,但是我尝试使用此代码从Msaccess数据库中查找和删除记录的方法无效.当我运行它时,它不会给出任何错误,当我单击按钮时,它什么也不会做,它会显示消息,但不会从表中删除记录.我一直在使用此代码:

I am new to delphi( or any coding for that matter) and i have been learning SQL for the past week but this code that i have tried using to find and delete a record from a Msaccess database doesn't work. It doesn't give any error when i run it, it just doesn't do anything when i click the button, It displays the message but it doesn't delete the record from the table. I have been using this code:

begin
ADOQuery1.SQL.Text := 'SELECT * FROM Admins WHERE Name = '''+Edtname.text+'''';
ADOQuery1.Open;
if ADOQuery1.IsEmpty then
ShowMessage('User not found')
else
begin
ADOQuery1.Close;
ADOQuery1.SQL.Text := 'DELETE FROM Admins WHERE Name = '''+EdtName.Text+'''';
ADOQuery1.ExecSQL;
ShowMessage('Information was Deleted');
end;
ADOquery1.Free;
end;

有关数据库的信息:

Collumns     Type
========     ====
Name         Text
Surname      Text
Dateadded    Date/time
Password     Text
Adminnumber  Number

请尽可能多地提供有关此错误发生原因的信息,正如我所说的那样,我仍在学习,并在此先感谢您:)

Please give as much information as possible as to why this error occured, as I said im still learning and thank you in advance :)

推荐答案

如果您坚持使用字符串连接(尽管有相反的建议),至少应消除尝试计算单引号并使用 QuotedStr (再次,该链接指向XE4文档,但该功能存在在Delphi 7中也是如此).

If you insist on using string concatenation (despite all advice to the contrary), at least eliminate the noise of trying to count the single quotes and use QuotedStr (once again, the link is to XE4 documentation, but the function exists in Delphi 7 as well).

我在回答其他问题时提供的相同信息也适用于此.名称在MS Access中仍然是保留保留字,因此仍需要用 [] 包围.每次使用它都需要它,这也是为什么我建议您在相距太远之前更改字段名称的原因.

The same information I provided in my answer to your other question also applies here. Name is still a reserved word in MS Access, and therefore still needs to be surrounded by []. It will need that every single time you use it, which is also why I suggested you change the field name before you got too far along.

您发布的代码显示ADOQuery在最后被释放,但没有显示正在创建.我已经添加了该代码,以便使之有意义.您将需要用一个连接字符串替换数据库的连接字符串.我还从 ADOQuery1 中更改了ADOQuery的名称(这将与默认名称的表单上的任何现有ADOQuery冲突),因为您的代码似乎仅为此块代码创建了一个新代码.代码.如果实际上您已经在表单或数据模块上使用了它,则应该删除 try Create ConnectionString 行>最终 Free 和下一个 end ,然后将所有 TempQuery 变量重命名为 ADOQuery1 .

The code you posted shows the ADOQuery being freed at the end, but does not show it being created. I've added that code so that it makes sense; you'll need to replace the connection string with one to your database. I've also changed the name of the ADOQuery from ADOQuery1 (which will conflict with any existing ADOQuery on your form with the default name) because your code appears to be creating a new one for just this block of code. If in fact you're using one already on the form or datamodule, you should delete the try, Create, ConnectionString, and lines finally, Free, and the next end, and rename all of the TempQuery variables back to ADOQuery1.

var
  NumRows: Integer;
  TempQuery: TADOQuery;
begin
  TempQry := TADOQuery.Create(nil);
  try
    TempQuery.ConnectionString := 'Use your own connection string here';

    TempQuery.SQL.Text := 'SELECT * FROM Admins WHERE [Name] = ' +
                           QuotedStr(Edtname.text);
    TempQuery.Open;
    if TempQuery.IsEmpty then
    begin
      ShowMessage('User ' + EdtName.Text + ' not found!');
      Exit;
    end;

    TempQuery.Close;
    TempQuery.SQL.Text := 'DELETE FROM Admins WHERE [Name] = ' +
                           QuotedStr(EdtName.Text);
    TempQuery.ExecSQL;
    NumRows := TempQuery.RowsAffected;
    ShowMessage(IntToStr(NumRows) + ' were deleted');
  finally
    TempQuery.Free;
  end;
end;

但是,再次使用参数化查询会更好.它仅增加了两行代码,并消除了 SQL注入 ExecSQL 行:

Once again, however, this would be better using parameterized queries. It only adds two additional lines of code, and eliminates the security risks involved with SQL injection at the ExecSQL line:

TempQuery.SQL.Text := 'SELECT * FROM Admins WHERE [Name] = :UserName';
TempQuery.Parameters.ParamByName('UserName').Value := EdtName.Text;
TempQuery.Open;

TempQuery.SQL.Text := 'DELETE FROM Admins WHERE [Name] = :UserName';
TempQuery.Parameters.ParamByName('UserName').Value := EdtName.Text;
TempQuery.ExecSQL;

这篇关于使用editbox msaccess delphi 7从数据库中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆