如何避免在此代码中存储数据库中的重复项?

时间:2012-08-01 19:03:30

标签: mysql delphi

我有一个用户注册过程,使用MYDAC组件将用户信息存储到我的数据库。目前它允许重复用户,这不是我的意图。我的代码如下,但我不知道问题出在哪里。

procedure TForm1.Button1Click(Sender: TObject);
begin    
  if (edit1.Text <> '') and (edit2.Text <> '') and (edit3.Text <> '') and 
    (edit4.Text <> '') then
  begin
    MyQuery1.Close;
    MyQuery1.SQL.Text := 'select * from uyeler '+
                         'where nick=:0 and mail=:0 and site=:0';

    MyQuery1.Params[0].AsString:=edit1.text;
    MyQuery1.Params[0].AsString:=edit2.text;
    MyQuery1.Params[0].AsString:=edit3.text;

    MyQuery1.open;

    if MyQuery1.RecordCount = 0 then
      MessageDlg('The same information! Try again.', mtError, [mbOK], 0)
    else
      MyQuery1.Close;

    MyQuery1.SQL.Text := 'INSERT INTO uyeler (nick, mail, site, sifre) VALUES '+
                                            '(:nick, :mail, :site, :sifre)';

    MyQuery1.ParamByName('nick').AsString := Edit1.text;
    MyQuery1.ParamByName('mail').AsString := Edit2.text;
    MyQuery1.ParamByName('site').AsString := Edit3.text;
    MyQuery1.ParamByName('sifre').AsString := Edit4.text;
    MyQuery1.Execute;

    Button1.Enabled := False;
    MessageDlg('Mission complate!', mtInformation, [mbOK], 0);

    Edit1.Clear;
    Edit2.Clear;
    Edit3.clear;
    Edit4.Clear;

    PageControl2.Visible := False;
    PageControl1.Visible := True;
  end
  else
  begin
    MessageDlg('Information is missing! Try again.', mtWarning,[mbOK],0);    
  end;
end;

如何防止注册相同?在这种情况下我该怎么做?

3 个答案:

答案 0 :(得分:3)

我通常会在底层MySQL表上使用唯一索引来强制执行此操作。

答案 1 :(得分:1)

您正在检查错误的结果。您需要将测试更改为

if MyQuery1.RecordCount > 0 then // At least one match found already

更好的是,如果MyDac支持,则使用

if not MyQuery1.IsEmpty then  // row already exists.

实际上,你有更多的问题。您有一个不匹配的beginend块,因为现在您始终在运行该方法的插入部分。正如@TLama所说,您也多次使用相同的pameter,为nickmailsite分配所有相同的值。请改用命名参数(如下所示,在SQL和参数赋值中)。

procedure TForm1.Button1Click(Sender: TObject);
var
  UserExists: Boolean;
begin
  Button1.Enabled:=false;

  if (edit1.Text <> '') and (edit2.Text <> '') and 
     (edit3.Text <> '') and (edit4.Text <> '') then
  begin
    MyQuery1.Close;
    MyQuery1.SQL.Text :=' select* from uyeler '+
                        'where nick=:nick and mail=:mail and site=:site';

    MyQuery1.ParamByName('nick').AsString:=edit1.text;
    MyQuery1.ParamByName('mail').AsString:=edit2.text;
    MyQuery1.ParamByName('site').AsString:=edit3.text;
    MyQuery1.open;
    try
      UserExists := not MyQuery1.IsEmpty;
    finally
      MyQuery1.Close;
    end;

    if UserExists then
      MessageDlg('The same information! Try again.', mtError,[mbOK],0)
    else
    begin            // <<--- Added begin
      MyQuery1.SQL.Text :=' INSERT INTO uyeler (nick, mail, site, sifre) VALUES '+
                                              '(:nick, :mail, :site, :sifre)';

      MyQuery1.ParamByName('nick').AsString        := Edit1.text;
      MyQuery1.ParamByName('mail').AsString        := Edit2.text;
      MyQuery1.ParamByName('site').AsString        := Edit3.text;
      MyQuery1.ParamByName('sifre').AsString        := Edit4.text;
      try
        MyQuery1.Execute;
      finally
        MyQuery1.Close;
      end;
    end;                      // <------------ Moved end from below where marked

    MessageDlg('Mission complate!', mtInformation,[mbOK],0);

    Edit1.Clear;
    Edit2.Clear;
    Edit3.clear;
    Edit4.Clear;

    PageControl2.Visible:=false;
    PageControl1.Visible:=true;
  end            // <------------- removed extra end caused by addition above
  else
   MessageDlg('Information is missing! Try again.', mtWarning,[mbOK],0);
end;

答案 2 :(得分:-1)

IMO @Ken White发布的答案应该可以正常工作,但是因为你找到了麻烦。我建议你尝试使用这段代码。它只是执行查询的差异。

我正在考虑字段数据类型为Char或VarChar,因此在输入数据值时“”

procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.Enabled:=false;

  if (edit1.Text <> '') and (edit2.Text <> '') and 
     (edit3.Text <> '') and (edit4.Text <> '') then
  begin
    MyQuery1.SQL.Clear;
    MyQuery1.SQL.Add(' select* from uyeler where nick="'+edit1.text+'"' +
                      'and mail="'+edit2.text+'" and site="'+edit3.text+'"');
    MyQuery1.Execute;

    if not MyQuery1.IsEmpty then   //--- can also use MyQuery1.RecordCount >0 
      MessageDlg('The same information! Try again.', mtError,[mbOK],0)
    else
    begin            //--- no duplicates present 
      MyQuery1.SQL.Clear;
      MyQuery1.SQL.Add(' INSERT INTO uyeler (nick, mail, site, sifre) VALUES '+
                            '("'+edit1.text+'", "'+edit2.text+'","'+edit3.text+'", "'+edit4.text+'")');

     try
        MyQuery1.Execute;
      finally
        MyQuery1.SQL.Clear;
      end;

     MessageDlg('Mission complate!', mtInformation,[mbOK],0);

     Edit1.Clear;
     Edit2.Clear;
     Edit3.clear;
     Edit4.Clear;

     PageControl2.Visible:=false;
     PageControl1.Visible:=true;
    end;                        
  end;                 
  else
   MessageDlg('Information is missing! Try again.', mtWarning,[mbOK],0);
end;