Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database Components

Status
Not open for further replies.

BoDGie

Programmer
Jun 18, 2003
6
AU
Im looking to build a program that a video shop would run using a database.. I know the basics of delphi plus a little more.. but I've never used the database components.

I've made my tables in Paradox 7.

What im trying to do to start is have a log in screen containing 2 edit boxes. 1 for username and 1 for password. I have a table called 'login' which has the fields 'Username' and 'Password'. The form also contains a button.

When the button is pressed i want to run a query to check if the username and password matches that of which is in the database... Any hints of how i might achieve this?

Thanks.
 
One way would be to add a TTable component onto your form and add the following code to your login button click.
Code:
var
  Username, Password : Variant;
begin
Table1.DatabaseName := 'C:\Your table path';
Table1.TableName := 'Your TableName';
Table1.Active := True;
UserName := Edit1.Text;
Password := Edit2.Text;
if Table1.Locate('USERNAME;PASSWORD', VarArrayOf([Username,Password]), []) then
begin
  //Access granted
end else
begin
  //Access denied
end;
Table1.Close;
end;
There are various different ways of accessing tables but this is probably one of the easiest. You should check the help file for details on the kind of database access you require though.


When your feeling down and your resistance is low, light another cigarette and let yourself go [rockband]
 
The query version might look like:
Code:
   with TQuery.Create(nil) do
   try
       DatabaseName := 'MyAlias';
       SQL.Text := 'SELECT COUNT(*) FROM LOGIN WHERE (USER_NAME = :NAME) AND (PASSWORD = :PASSWORD)';
       ParamByName('NAME').AsString := NameEdit.Text;
       ParamByName('PASSWORD').AsString := PasswordEdit.Text;
       Open;
       if Fields[0].AsInteger <> 1 then
           raise Exception.Create('Invalid login; please try again');
   finally
       Free;
   end;

   //   continue with login
If the Login table has more information (e.g. their screen name or preferences), the use SELECT * instead of SELECT COUNT(*) and test 'if IsEmpty' instead of 'if Fields[0].AsInteger <> 1'. You can access all the columns of the matching row using &quot;FieldByName('SCREEN_NAME').AsString&quot;, etc.

Cheers
 
I was unable to get either of those methods to work :/
 
From a security point of view making a table with usernames and passwords wouldn't be very clever. To easy to crack, you only need to browse your login table.
Use the build-in security of paradox instead.

You can password-protect your tables, and have different passwords for different roles.
The master password has all rights, you can grant read-only, or update rights to an auxiliary password.

You will need a TDatabase component

Set the DatabaseName to: MyDataBase
set loginprompt = true
set AliasName to the alias you have defined for your directory
set Connected = false


Define your tables or queries

and set the DatabaseName proporty to MyDatabase, not the Alias defined in the BDE
set active = false


On your main form put a button, called login


procedure TForm1.Button1Click(Sender: TObject);
begin
Database1.LoginPrompt := true;
table1.Active := true;
table2.Active := true;
//activate all tables and queries
end;


Depending on the password used and the roles defined, you will have some basic security implemented.

BTW the non visual components like queries, tables and the database should be placed on a dataModule


Steven van Els
SAvanEls@cq-link.sr
 
I'm not worried about the security as such as its only for a school assignment.. I just want it to look good.
 
I was unable to get either of those methods to work

A little bit of info? What error did you receive? What is your tablename? What are the username and password fields called in your table?


When your feeling down and your resistance is low, light another cigarette and let yourself go [rockband]
 
I fixed the problem by changing the word 'Password' to Pword... it all works now. Thanks lots!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top