Rinalds Gudriks Rinalds Gudriks - 5 months ago 24
SQL Question

Login validation with database

PROBLEM

Hello! I need to make login in Delphi 2010. I have MS Access database where I have fields (loginID, username, password). So I have 2 TEdit components for username and password. I have connected to database in delphi and all connections are working.

So problem is that I don't know how to check if text from TEdit is in database. I need something like query.

Example:

if Edit1.Text in login.username then

something

else

something else


Hope you understand what I need, but anyway you can ask a questions for more information. I will be thankful for solution.

WHAT I TRYED

ADOQuery1.SQL.Text := 'SELECT FROM login WHERE username = :Edit1.Text';
ADOQuery1.ExecSQL;


I don't know if it works.

How to check like this:

if ADOQuery1 execute then

something

else

something else

Answer

You need to properly parameterize your query, and use the Parameters. (You also need to write valid SQL, which for a SELECT requires a list of columns you want returned.)

ADOQuery1.SQL.Text := 'SELECT UserName FROM login WHERE username = :User';
ADOQuery1.Parameters.ParamByName('User').Value := Edit1.Text;

You then need to open (not execute) the query, and see if it returned a value.

ADOQuery1.Open;
if not ADOQuery1.IsEmpty then
begin
  // User exists. Do whatever you need next.
end
else
  // Do what you want if user not found

I'd strongly suggest that if you're going to try and use databases, you buy a book or find a tutorial. SELECT is one of the most basic operations you'll need to perform.

Comments