Recent

Author Topic: Connecting to a database with user and password  (Read 3959 times)

yordan

  • New Member
  • *
  • Posts: 18
Connecting to a database with user and password
« on: January 03, 2011, 11:23:37 am »
Hi, I'm trying the following code:
procedure TForm1.FormShow(Sender: TObject);

   var user: string;
    pass: string;
begin
 // user:='-1' ;
  showmessage(user);
  mysql50connection1.HostName:=Application.GetOptionValue('h','host');
  mysql50connection1.DatabaseName:=Application.GetOptionValue('d','db');
  mysql50connection1.UserName:=Application.GetOptionValue('u','user');
  mysql50connection1.Password:=Application.GetOptionValue('p','pass');
  mysql50connection1.port:=strtoint(Application.GetOptionValue('o','port'));
  pass:=Application.GetOptionValue('l','passw');
  user:=Application.GetOptionValue('k','kod');
  mysql50connection1.Open;
  showmessage(user);
  if user='' then application.Terminate;
  sqlquery4.SQL.Text:= 'select kod from users where kod='+user+' and passw=md5('''+pass+''')';
 // showmessage(sqlquery4.sql.text);
  sqlquery4.Open;

  if sqlquery4.RecordCount=0 then application.Terminate;

end;                     
The problem is that when I run the .exe file without user name and password instead of  terminating the application  I see the empty string variable user(as it's supposed to be) then I get the message:  "" is an illegal integer and when i press OK I am able to enter the table without entering user name and password as run parameters.
Can you tell me how to fix this.
 Thanks :)

eny

  • Hero Member
  • *****
  • Posts: 1646
Re: Connecting to a database with user and password
« Reply #1 on: January 03, 2011, 11:29:13 am »
Maybe 'user' contains whitespace characters?
You could try this:
Code: Pascal  [Select][+][-]
  1. //...
  2. user := trim(Application.GetOptionValue('k','kod'));
  3. //...

<<edit>>
I assume you'll have to 'escape' the user part in the select statement as well (as you've done with the password).
« Last Edit: January 03, 2011, 11:31:07 am by eny »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

yordan

  • New Member
  • *
  • Posts: 18
Re: Connecting to a database with user and password
« Reply #2 on: January 03, 2011, 11:54:31 am »
the problem was in : mysql50connection1.port:=strtoint(Application.GetOptionValue('o','port'));
I replaced that line : if Application.GetOptionValue('o','port')<>'' then mysql50connection1.port:=strtoint(Application.GetOptionValue('o','port'))
and it works OK

 

TinyPortal © 2005-2018