繁体   English   中英

SQL语法给出未知的语法错误

[英]Sql Syntax giving unknown syntax error

我的mysql语句有问题。

基本上,代码在插入时可以完美地工作,但是一旦addnew variable = false并切换为更新,它就会给我一个我无法解决的错误。

编码:

procedure Tadddomain.BitBtn2Click(Sender: TObject);
Var
PrevSql:String;
ID:String;
begin
With Datalive.domains Do
 Begin
  id:=fieldbyname('id').AsString;
  Active:=False;
  prevsql:=sql.Text;
  Sql.Clear;
  Params.Clear;
  Addparam(Datalive.domains,'client_id',ftinteger,datalive.clients.FieldByName('id').AsString);
  Addparam(Datalive.domains,'domain_name',ftString,Edit1.Text);
  Addparam(Datalive.domains,'register_date',ftdate,DateTimePicker1.Date);
  Addparam(Datalive.domains,'registered_until',ftdate,DateTimePicker2.Date);
  if addnew=true then
    Sql.Text:='Insert into domains (client_id,domain_name,register_date,registered_until) VALUES (:client_id,:domain_name,:register_date,:registered_until)'
  Else if addnew=False then
    Sql.Text:='Update domains (domain_name=:domain_name, register_date=:register_date, registered_until=:registered_until) where id='''+id+'''';
  Showmessage(sql.text);
  execsql;
  sleep(100);
  sql.Text:=prevsql;
  active:=True;
  done:=True;
  adddomain.Close;
 End;
end;

错误:

项目project1.exe引发了异常类EZSQLException,并显示消息“ SQL错误:您的SQL语法有错误;请参见第11页上的“ SQL错误:SQL语法错误”。 检查与您的mysql服务器版本相对应的手册,以在第1行的'(domain_name ='asd',register_date ='2014-11-09',registered_until ='2015-11-09')w'附近使用正确的语法'。

任何协助都会很棒,我已经搜寻了很多,但找不到故障。


更新:

我按照下面的建议更改了编辑代码,现在再也看不到任何错误了。 但是也没有任何反应。 它不会编辑记录。

if addnew=true then
    Sql.Text:='Insert into domains (client_id,domain_name,register_date,registered_until) VALUES (:client_id,:domain_name,:register_date,:registered_until)'
  Else if addnew=False then
   Begin
    sql.Add('Update domains');
    sql.Add('set domain_name=:domain_name,');
    sql.Add('register_date=:register_date,');
    sql.Add('registered_until=:registered_until');
    sql.Add('where id=:id');
   End;

更新语法是这样的

Update domains 
set domain_name = :domain_name, 
    register_date = :register_date, 
    registered_until = :registered_until
where id = :id

您声明的“ client_id”参数不是在更新语句中使用的“ id”参数。

where id = :id

应该

where id = :client_id

我发现了问题。 如果在使用sql.add()函数之前定义了参数, sql.add()丢弃这些参数。 当我移动

Addparam(Datalive.domains,'client_id',ftinteger,datalive.clients.FieldByName('id').AsString);
  Addparam(Datalive.domains,'domain_name',ftString,Edit1.Text);
  Addparam(Datalive.domains,'register_date',ftdate,DateTimePicker1.Date);
  Addparam(Datalive.domains,'registered_until',ftdate,DateTimePicker2.Date);

execsql;之前execsql; 问题解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM