且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Delphi的SQLite表中插入整数值

更新时间:2023-12-01 13:15:46

您的 gid 成为一部分SQL语句的名称(因此错误: Unknown column gid )。

您需要使用Delphi gid 变量来构造SQL语句(就像您使用 sdescription ldescription 一样):

Your gid becomes a part of the SQL statement (hence the Error: Unknown column gid).
You need to use the Delphi gid variable to construct the SQL statement (just like you did with sdescription and ldescription):

sSql := 'INSERT INTO emp(usergroup_id, label, description) VALUES (' + InttoStr(gid) + ', ''' + sdescription + ''',''' + ldescription + ''' )';

如果您会使用 Parameters 不会有这样凌乱的查询/代码(也可能受到SQL注入等)。例如:

If You would have used Parameters you wouldn't have such a messy query/code (which is also subject to SQL injection, etc..) e.g.:

qry := TSQLQuery.Create(nil); // or what ever TQuery component you use in your framework
try
  qry.SQLConnection := SQLConnection1;
  qry.SQL.Text := 'INSERT INTO emp(usergroup_id, label, description) VALUES (:usergroup_id, :label, :description)';
  qry.Params.ParamByName('usergroup_id').Value := gid;
  qry.Params.ParamByName('label').Value := sdescription;
  qry.Params.ParamByName('description').Value := ldescription;
  qry.ExecSQL;
finally
  qry.Free;
end;