我使用Delphi XE3并使用SQLite数据库和DB Express。
这是我的用户验证
代码:
function validateUser(UserName, Password: string): Boolean;
var
AParams: TParams;
SQLTxt: string;
MD5 : TIdHashMessageDigest5;
RecCount: integer;
begin
AParams := TParams.Create(nil);
MD5 := TIdHashMessageDigest5.Create;
try
Result := False;
AParams.CreateParam(ftString, 'username', ptInput).Value := UserName;
AParams.CreateParam(ftString, 'password', ptInput).Value :=
MD5.HashBytesAsHex(MD5.HashString(Password));
SQLTxt := 'SELECT login_id FROM login WHERE '+
'login_username = :username AND login_password = :password ;';
with Form1.sqlqry1 do
begin
SQL.Clear;
SQL.Text := SQLTxt;
Params := AParams;
//Params.Items[0].Value := AParams.Items[0].Value;
//Params.Items[1].Value := AParams.Items[1].Value;
Prepared := true;
Open;
end;
RecCount := Form1.sqlqry1.RecordCount;// I have error here
if RecCount = 0 then
Result := False
else
Result := True;
// end
finally
AParams.Free;
MD5.Free;
end;
end;
应用程序向我显示[0x0005]:不支持操作
错误。
有什么问题吗?为什么?
最佳答案
您也不需要使用 RecordCount(这是您指示发生错误的地方)。使用TDataSet.IsEmpty相反。
此外,来自documentation (强调我的):
Params is a collection of TParam objects that represent the parameters of a query or stored procedure specified by the SQL dataset. When you specify a query by setting the CommandText property (or the SQL property in TSQLQuery), the SQL dataset automatically parse the query and fills Params with a TParam object for every parameter in the query.
您不需要手动创建参数;当您分配 SQL 时,数据集会自动为您执行此操作。
function validateUser(UserName, Password: string): Boolean;
var
SQLTxt: string;
MD5 : TIdHashMessageDigest5;
RecCount: integer;
begin
MD5 := TIdHashMessageDigest5.Create;
try
Result := False;
SQLTxt := 'SELECT login_id FROM login WHERE '+
'login_username = :username AND login_password = :password ;';
with Form1 do
begin
// Clear not needed when setting SQL.Text directly.
sqlqry1.SQL.Text := SQLTxt;
sqlqry1.Params.ParamByName('username').AsString := UserName;
sqlqry1.Params.ParamByName('password').AsString :=
MD5.HashBytesAsHex(MD5.HashString(Password));
sqlqry1.Open;
Result := not sqlqry1.IsEmpty;
end;
finally
MD5.Free;
end;
end;
关于database - Delphi + SQLite + DB Express select 参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22104910/