您正在使用依赖于用户当前操作系统区域设置的DateToStr() 版本。 DB 具有他们期望日期/时间字符串表达的特定格式,并且操作系统的区域设置可能与这些格式不匹配。您可以使用DateToStr() 的重载版本,它将TFormatSettings 作为输入,这样您就可以指定输出字符串的确切格式(通过TFormatSettings.ShortDateFormat 成员):
var
fmt: TFormatSettings;
ExpireDate: string;
fmt := TFormatSettings.Create;
fmt.ShortDateFormat := 'yyyy-mm-dd';
ExpireDate := DateToStr(IncYear(Today,1), fmt);
此外,日期/时间字符串需要在 SQL 语句中用引号括起来。为此,您可以使用QuotedStr() 函数。
但更重要的是,您的INSERT 语句无论如何都是错误的。
如果您尝试添加新记录,它需要看起来更像这样:
datamodule1.qryDB1.SQL.Text := 'INSERT INTO table (Member_ID, Member_ExpireDate) VALUES (' + SomeIDValue + ', ' + QuotedStr(ExpireDate) + ')';
否则,如果您尝试修改现有记录,则需要改用UPDATE 语句:
datamodule1.qryDB1.SQL.Text := 'UPDATE table SET Member_ExpireDate = ' + QuotedStr(ExpireDate) + ' WHERE Member_ID = ' + SomeIDValue;
无论哪种方式,最好使用参数化查询。让数据库自己为您处理所有必要的格式:
datamodule1.qryDB1.SQL.Text := 'INSERT INTO table (Member_ID, Member_ExpireDate) VALUES (:MemberID, :ExpireDate)';
datamodule1.qryDB1.ParamByName('MemberID').AsInteger := SomeIDValue; // or whatever data type your field uses
datamodule1.qryDB1.ParamByName('ExpireDate').AsDate := IncYear(Today,1);
datamodule1.qryDB1.ExecSQL;
datamodule1.qryDB1.SQL.Text := 'UPDATE table SET Member_ExpireDate = :ExpireDate WHERE Member_ID = :MemberID';
datamodule1.qryDB1.ParamByName('ExpireDate').AsDate := IncYear(Today,1);
datamodule1.qryDB1.ParamByName('MemberID').AsInteger := SomeIDValue; // or whatever data type your field uses
datamodule1.qryDB1.ExecSQL;