Delphi Text File and Array - arrays

I have the following Scenario: In a textfile (justin#us883) the program must extract the password - us883 check if the password typed in is correct to the one in the list and then make a button visible to enter program: Code that is not working:
var
textf:textfile;
oneline,spass,scheck :string;
place,i,icount :integer;
Arrpass : array[1..Maxnames] of string;
begin
scheck := edtpass.Text;
assignfile(textf,'Userlist.txt');
reset(textf);
if fileExists('Userlist.txt')= false then
exit;
while not eof(textf) do
begin
Readln(textf, oneline);
place := pos('#',oneline);
delete(oneline,1,place);
spass := copy(oneline,1,place-1); // get the us883
Arrpass[i] := spass;
for i := Low(Arrpass) to High(Arrpass) do
if Arrpass[i] = spass then
begin
btnenter.Visible := true
end
else
btnenter.Visible := False;
Showmessage('Wrong Password');
end;
closefile(textf);
end;

Maybe you want to do it more delphi like:
var
list : TStringList;
user, password : String;
i : Integer;
begin
user := 'user1';
if fileExists('Userlist.txt')= false then exit;
list := TStringList.create();
list.NameValueSeparator := '#';
list.LoadFromFile('Userlist.txt');
i := list.IndexOfName(user);
if i >=0 then begin
password := list.Values[user];
// check password
end;

Issue 1:
place := pos('#',oneline);
delete(oneline,1,place);
spass := copy(oneline,1,place-1); // get the us883
The third line uses position of # that is not more actual (after deletion).
If you don't expect more symbols after password, just use
spass := oneline;
Issue 2:
How Arrpass array sould be filled?
This line Arrpass[i] := spass; uses uninitialized variable i. And what is a logic to compare just inserted value with itself? Perhaps you want to fill ArrPass before text reading.
Issue 3:
It is worth to check if fileExists before file handling.

Related

Dataset not in edit or insert mode

Im a new user of stack overflow. I have a big project coming up and i keep getting the same error. I’m trying to load data into my database and no matter how many times I try or what I change I still get the same errors. I have labled nessarry variables, tried tbl().Edit and tbl().Insert inside, outside of my loops and in the case statement but i still seem to be getting the same error.
sPlayer1,sPlayer2, iScore1,iScore2 are global variables.
`
procedure TfrmInvigilator.btnSubmitClick(Sender: TObject);
Var
i, j, iRound: Integer;
begin
sPlayer1 := cmbName1.Text; // Assigning values to variables for cmbPlayers
sPlayer2 := cmbName2.Text;
iScore1 := sedScore1.Value; // Assigning values to variables for sedScores
iScore2 := sedScore2.Value;
iRound := cmbRound.ItemIndex; // Assigning values to variable for cmbRound
if (cmbName1.ItemIndex = -1) then
// Displays show message if cbmName1 is blank
begin
ShowMessage('Please select player name');
end
else
begin // If cbmName1 is not blank then:
i := pos(' ', sPlayer1); // Find position of ' ' in cbmName1.Text
sPlayer1 := Copy(sPlayer1, 1, i - 1); // sPlayer1 := Name of player
dmChess.tblPlayerInfo.Locate('Name', sPlayer1, []); // Locates Name in table
Num1 := dmChess.tblPlayerInfo['ID']; // Retrives ID of player
with dmChess do
begin
tblScoreboard.First;
while NOT tblScoreboard.EOF do
begin
tblScoreboard.Locate('ID', Num1, []);
case iRound of
0:
tblScoreboard['Round 1A'] := iScore1;
1:
tblScoreboard['Round 1B'] := iScore1;
2:
tblScoreboard['Round 1C'] := iScore1;
3:
tblScoreboard['Round 1D'] := iScore1;
4:
tblScoreboard['Round 2'] := iScore1;
5:
tblScoreboard['Semi-Final'] := iScore1;
6:
tblScoreboard['Final'] := iScore1;
end;
tblScoreboard.Post;
end;
end;
end;
if (cmbName2.ItemIndex = -1) then
// Displays show message if cbmName2 is blank
begin
ShowMessage('Please slecet player name');
end
else
begin // If cbmName2 is not blank then:
j := pos(' ', sPlayer2); // Find position of ' ' in cbmName2.Text
sPlayer2 := Copy(sPlayer2, 1, j - 1); // sPlayer2 := Name of player
dmChess.tblPlayerInfo.Locate('Name', sPlayer2, []); // Locates Name in table
Num1 := dmChess.tblPlayerInfo['ID']; // Retrives ID of player
end;
end;
`
You can't just set a new value in a DataSet field (table or query), the DataSet must know that you want to change the current record or insert a new one. An exception tells you this. How to solve: before you make changes to any field in the dataset, you need to set the desired mode. DataSet.edit; - to edit the current record, DataSet.Insert - to insert a new record in the current location, DataSet.Append - to add a new record to the end. For this code snippet, add the line tblScoreboard.Edit; after the line tblScoreboard.Locate('ID', Num1, []);
DataSet.Post – save current changes into record and change DataSet mode back to Browsing

Delphi - Re-split an array of string?

Let's say I have a string like this :
string1 := 'me,email1,you,email2,him,email3,them,email4';
To turn this into an array of string I simply do :
array1 := SplitString(string1,',');
This works fine.
But then, I get an array like :
array1[0] -> me
array1[1] -> email1
array1[2] -> you
array1[3] -> email2
array1[4] -> him
array1[5] -> email3
array1[6] -> them
array1[7] -> email4
I've searched a long time how to insert into SQLIte with this but there is no using
for i:= 0 to length(array1) -1
SQLExecute('INSERT INTO table(name,email) VALUES("'+array1[i]+'","'+array1[i+1]+'"');
because index 0 will be inserted as name with index 1 as email, but on the next turn, index 1 will be inserted as name with index 2 as email, when index 1 is en email, and index 2 a name... do you see the problem ?
I thought about re-spliting the first array into a second one by changing the initial string format into :
string1 := 'me-email1,you-email2,him-email3,them-email4';
to split a first time on the ' and a second time on the -, to get a 2 dimensional array, but seems this concept is over my knowledge at the moment :)
Just for the record, the Delphi RAD I'm using is quite recent, and only a few functions / tools are available at the moment.
How would you insert into sql ? Would you keep the original String format, or change it to get a 2 dimensional array ?
Iterate in pairs:
for i := 0 to length(array1) div 2 - 1 do
SQLExecute('INSERT INTO table(name,email) VALUES("'+array1[i*2]+'","'+array1[i*2+1]+'"');
You just should not use per-INSERT FOR-loop here.
It is not suited for it and it is dangerous if your string would have 3 or 7 or any other odd number of elements.
Also splicing random data right into the SQL command is extremely unreliable and fragile. http://bobby-tables.com/
You should either use a WHILE-loop with a sliding fetcher or tick-tock (finite state machine) swinging in one-per-string FOR-loop.
var q: TQuery;
// some Query-component of any library,
// including DBX, AnyDAC/ FireDAC mORMot or any other library you would use
var sa_OK, sa_err1, sa_err2: TArray<string>;
// common preparations for both methods
q.ParamCheck := True;
q.SQL.Text := 'INSERT INTO table(name,email) VALUES( :PName, :PMail )';
q.Prepared := True;
sa_OK := TArray<string>.Create( 'me','email1','you','email2','him','email3','them','email4');
// eeeaaasy example
sa_err1 := TArray<string>.Create( 'me','email1','you','email2','him');
// check this out-of-sync error too - it can happen! you should be pre-aware!
sa_err2 := TArray<string>.Create( 'Sarah O''Connor','email1','"Bobby F. Kennedy"','email2','him'#0'again','email3');
// not the letters you expected - but they can come too
Procedure Method1_Fetcher( const sa: array of string );
var i: integer;
s: string;
function Fetched: Boolean;
begin
Result := i <= High(sa);
if Result then begin
s := sa[i];
Inc(i);
end;
end;
Begin
i := Low(sa);
while true do begin
if not Fetched then break;
q.Params[1].AsWideString := s;
if not Fetched then break;
q.Params[2].AsWideString := s;
// ...you can easily add more parameters for more columns
// if not Fetched then break;
// q.Params[3].Value := s;
// ... or you can make a loop
// FOR j := 0 to q.Params.Count - 1 DO ... q.Params[j] :=...
// only executing insert if ALL the columns were filled
q.ExecSQL;
end;
q.Transaction.CommitWork;
End;
Procedure Method2_TickTock( const sa: array of string );
var i, FSM: integer;
Begin
FSM := 0;
for i := Low(sa) to High(sa) do begin
if FSM = 0 then begin
q.ParamByName('PName').AsWideString := sa[i];
FSM := 1; // next mode of tick-tock
Continue;
end;
if FSM = 1 then begin
q.ParamByName('PMail').AsWideString := sa[i];
q.ExecSQL;
// only executing insert after the last column was filled
FSM := 0; // next mode of tick-tock
Continue;
end;
/// would only come here if we made some mistake above
/// and FSM got impossible value - so no "Continue" was executed
/// show error and exit
raise EInvalidOperation.CreateFmt('FSM = %d', [FSM]);
end;
q.Transaction.CommitWork;
End;
Procedure Method3_SimplifiedFSM( const sa: array of string );
// this method is actually are streamlined method #2
// it can be made because all our steps are TOTALLY identical
// ( sans optional insert execution )
var i, FSM: integer;
Begin
FSM := 0;
for i := Low(sa) to High(sa) do begin
q.Params[ FSM ].AsWideString := sa[i];
Inc( FSM );
if FSM >= q.Params.Count then begin
q.ExecSQL; // only after (if ) last of all columns filled!
FSM := 0;
end;
end;
q.Transaction.CommitWork;
End;
Now you can debug calls like Method1(sa_OK) or Method2(sa_err1) and see how it is working and how it deals with errors

How to change DBLookupComboBox value in Delphi?

I'm trying to change the visible text (value) of a DBLookupComboBox, using :
DBLookupComboBox1.KeyValue:=S;
and
DBLookupComboBox2.KeyValue:=X;
If the KeyValue and S are strings, then everything works fine, and I can set the value.
But when the KeyValue is an integer/Int64 (UID in the DB), and X is an Int64 variable - it doesn't work, and nothing changes.
So for an example, i need to set "Amsterdam" in DBLookupComboBox1, and 1500 in DBLookupComboBox2.
"Amsterdam" from the "City" field of the users, and 1500 as the UID.
What am I doing wrong please?
Thanks
This code is Important for you:
lkcbbArzSource.KeyValue:=2;//c(or another number);
lkcbbArzSource is a dbLookupComboBox object and you can insert only a number in this!why? because is numeric and another side of that there is a field that show a text or String of that numeric in combo.
in below sample code you can see a dbLookupComboBox how to menage and fill with Data-set and add in a grid :
rzSource.Enabled:= True;
lkcbbArzSource.Font.Size:=8;
lkcbbArzSource.Tag := V_Counter;
lkcbbArzSource.Visible:= True;
lkcbbArzSource.Enabled:= True;
lkcbbArzSource.Font.Name:='tahoma';
lkcbbArzSource.ListSource := myDsrSource;
lkcbbArzSource.KeyField := 'Code';
lkcbbArzSource.ListField := 'Des';
lkcbbArzSource.KeyValue:= IntToStr(FieldByName('P_ARZSOURCE').AsInteger);
lkcbbArzSource.OnChange := myBicLookUpChange;
SGrid2.Objects[look_col,lkcbbArzSource.Tag]:= lkcbbArzSource;
SGRID2.Objects[look_col,V_Counter] := nil;
Setting the KeyValue calls the SetKeyValue of the TDBLookupControl which in Delphi 7 appears as:
procedure TDBLookupControl.SetKeyValue(const Value: Variant);
begin
if not VarEquals(FKeyValue, Value) then
begin
FKeyValue := Value;
KeyValueChanged;
end;
end;
procedure TDBLookupComboBox.KeyValueChanged;
begin
if FLookupMode then
begin
FText := FDataField.DisplayText;
FAlignment := FDataField.Alignment;
end else
if ListActive and LocateKey then
begin
FText := FListField.DisplayText;
FAlignment := FListField.Alignment;
end else
begin
FText := '';
FAlignment := taLeftJustify;
end;
Invalidate;
end;
As you can see, your variable x is used as part of a LocateKey.
function TDBLookupControl.LocateKey: Boolean;
var
KeySave: Variant;
begin
Result := False;
try
KeySave := FKeyValue;
if not VarIsNull(FKeyValue) and FListLink.DataSet.Active and
FListLink.DataSet.Locate(FKeyFieldName, FKeyValue, []) then // << ---here
begin
Result := True;
FKeyValue := KeySave;
end;
except
end;
end;
Stepping into these procedures and functions should help you to debug your issue. All are located in the DbCtrls unit..
I have used this and solved my problem.
Use this lines of code at Form1.OnShow :
DBLookupComboBox1.ListSource.DataSet.Locate('City', 'Amsterdam', []);
DBLookupComboBox1.ListSource.DataSet.FieldByName(DBLookupComboBox1.KeyField).Value;
DBLookupComboBox2.ListSource.DataSet.Locate('UID', '1500', []);
DBLookupComboBox2.ListSource.DataSet.FieldByName(DBLookupComboBox2.KeyField).Value;

What is the proper way to dynamically create and call a stored procedure in Delphi using FireDac?

I am relatively new to FireDAC. I want to be able to call a stored procedure "on the fly", dynamically. So far I have the following:
function TForm21.ExecuteStoredProc(aSPName: string; aParams: TADParams): Boolean;
var
LSP: TADStoredProc;
i: Integer;
begin
LSP := TADStoredProc.Create(nil);
try
LSP.Connection := ADConnection1;
LSP.StoredProcName := aSPName;
LSP.Prepare;
for i := 0 to aParams.Count - 1 do
begin
LSP.Params[i].Value := aParams[i].Value;
end;
LSP.ExecProc;
finally
LSP.Free;
end;
Result := True;
end;
I call it with
procedure TForm21.Button1Click(Sender: TObject);
var
LParams: TADParams;
begin
LParams := TADParams.Create;
LParams.Add.Value := 612;
LParams.Add.Value := '2008';
ExecuteStoredProc('HDMTEST.dbo.spARCHIVE_HISTORY_DATA', LParams);
end;
However, the stored procedure fails to execute. That is, the code runs fine, no error message is shown, but the stored procedure doesn't run.
Further info -- it runs fine if I drop a component and set up the params in code.
Anyone have any idea what I am missing?
Seeing as this q has been left unanswered for a while, I thought I'd try to get the code working without using the clues from the comments and found it not quite as easy as I'd imagined.
I immediately got stuck with the SP params. I found this
http://docwiki.embarcadero.com/RADStudio/XE5/en/TFDQuery,_TFDStoredProc_and_TFDUpdateSQL_Questions
which says
"If you have difficulties with manual definition of parameters,
populate the Params collection automatically and check how the
parameters are defined. Then compare that to your code. "
but I couldn't find a way of "automatically" populating the Params. I asked on the EMBA
FireDac newsgroup and the FD author, Dimitry Arefiev, kindly explained that
you can do that by checking that the FetchOptions include fiMeta, and then clearing and setting the FDStoredProc's StoredProcName.
Using a StoredProc in the pubs demo database on my SqlServer defined as follows:
create procedure test(#ANumber int, #AName varchar(20))
as
begin
select
#ANumber * 2 as "Number",
#AName + #AName as "Name"
end
I changed a couple of sections of the OP's code like this
[...]
LSP.Params.Clear;
LSP.StoredProcName := '';
LSP.FetchOptions.Items := LSP.FetchOptions.Items + [fiMeta];
LSP.StoredProcName := aSPName;
LSP.Prepare;
Assert(LSP.ParamCount > 0);
for i := 0 to aParams.Count - 1 do
begin
LSP.Params.ParamByName(aParams[i].Name).Value := aParams[i].Value;
end;
[...]
procedure TForm21.Button1Click(Sender: TObject);
var
LParams: TFDParams;
Param : TFDParam;
begin
LParams := TFDParams.Create;
Param := LParams.Add;
Param.Name := '#ANumber';
Param.Value := 612;
Param := LParams.Add;
Param.Name := '#AName';
Param.Value := '2008';
ExecuteStoredProc('test', LParams);
end;
and it worked fine.
The OP mentions in the q he'd first had the problem that the SP failed to execute
but that he'd found that it worked if he "[dropped] a component and set up the params in code" so I thought I'd include here a console application where of course necessarily everything is done in code. This wasn't difficult, but the time it took me to get the Uses clause right is my main reason for posting this as an answer, for future reference. W/o the correct uses, you get errors complaining about various class factories being missing.
Console app (created and tested in XE6):
program ConsoleStoredProcProject3;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, FireDac.DApt, FireDAC.Stan.Def, FireDAC.Stan.ASync,
FireDAC.Stan.Param, FireDAC.Stan.Option, FireDAC.Comp.Client,
FireDAC.Phys.MSSQL, VCL.ClipBrd;
procedure TestSP;
var
Connection : TFDConnection;
StoredProc : TFDStoredProc;
Param : TFDParam;
begin
Connection := TFDConnection.Create(Nil);
Connection.DriverName := 'MSSQL';
Connection.Params.Values['Server'] := // your server name'';
Connection.Params.Values['Database'] := 'pubs';
Connection.Params.Values['user_name'] := 'user'; // adjust to suit
Connection.Params.Values['password'] := 'password'; // ditto
Connection.LoginPrompt := False;
Connection.Connected := True;
StoredProc := TFDStoredProc.Create(Nil);
StoredProc.Connection := Connection;
StoredProc.FetchOptions.Items := StoredProc.FetchOptions.Items + [fiMeta];
StoredProc.StoredProcName := 'test';
StoredProc.Prepare;
Param := StoredProc.Params.ParamByName('#ANumber');
Param.Value := 333;
Param := StoredProc.Params.ParamByName('#AName');
Param.Value := 'A';
StoredProc.Active := True;
WriteLn(StoredProc.FieldByName('Number').AsInteger);
WriteLn(StoredProc.FieldByName('Name').AsString);
ReadLn;
end;
begin
try
TestSP;
except
on E: Exception do
Clipboard.AsText := E.Message;
end;
end.

open database with initfile

how to open a database local or remote with IniFile.
something like the below.
vBanco : String;
IniFileName : TIniFile;
begin
IniFileName := TIniFile.Create(ExtractFilePath(ParamStr(0))+FileName);
Try
if FileExists (remote+'db\ado.mdb') then
begin
vBanco := Trim(IniFileName.ReadString('acesso','BancoRemto',''));
Dirlocal := Trim(IniFileName.ReadString('acesso','PastasRemto',''));
frmPrincipal.Edit1.text := Dirlocal;
Dirtrabalho := (ExtractFilePath(Application.ExeName));
Conection.ConnectionString := vBanco;
end
else
begin
Try
vBanco := Trim(IniFileName.ReadString('acesso','banco',''));
Dirlocal := Trim(IniFileName.ReadString('acesso','PastasLocais',''));
frmPrincipal.Edit1.text := Dirlocal;
Dirtrabalho := (ExtractFilePath(Application.ExeName));
Finally
end;
end;
Finally
IniFileName.Free;
end;
end;
It seems as if you have your code correct, if you are running into problems make sure the value of the INI Connection string is valid. A Good way to get a valid connect string is to setup the connection at design time then copy and paste it to your config file.
If your problem is with FileExists, here's how you track it down.
Put a breakpoint on the if FileExists line. When it breaks to the debugger, hit CTRL-F7, which will bring up the Evaluate/Modify dialog. Type remote+'db\ado.mdb' into the input box and see what it gives. It'll most likely give you a bad filename.
For example, if remote doesn't end in a backslash, then that will yield an invalid path. You can fix this with the IncludeTrailingPathDelimiter function. Hard to be certain without seeing more of your code, but from my experience that's probably what's going on.
I found my mistake. Instead of checking the existence of a file named "ado.mdb" in a directory based on the global variable remote, I should have been looking for a file named "base_dados.mdb" in a directory determined by a value I read from an INI file.
procedure TfrmDados.ConectionBeforeConnect(Sender: TObject);
const
FileName = 'config.ini';
var
vBanco : String;
IniFileName : TIniFile;
LBasedados : String;
begin
IniFileName := TIniFile.Create(ExtractFilePath(ParamStr(0))+FileName);
vBanco := Trim(IniFileName.ReadString('acesso','BDtrabalho',''));
Dirlocal := Trim(IniFileName.ReadString('acesso','Pastrabalho',''));
LBasedados := Dirlocal+'db\base_dados.mdb';
frmPrincipal.Edit1.text := Dirlocal;
Dirtrabalho := (ExtractFilePath(Application.ExeName));
if FileExists(LBasedados) then
Conection.ConnectionString := vBanco
else
begin
Application.MessageBox('Bade dados do servidor não encontrada vai trabalhar neste Computador!','Ligação Remota FALHOU');
vBanco := Trim(IniFileName.ReadString('acesso','BD_local',''));
Dirlocal := Trim(IniFileName.ReadString('acesso','Pasta_local',''));
frmPrincipal.Edit1.text := Dirlocal;
Dirtrabalho := (ExtractFilePath(Application.ExeName));
Conection.ConnectionString := vBanco;
end;
IniFileName.Free;
end;

Resources