Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  yordan : (16 June 2013 - 05:41 PM) You're Welcome, Agyat!
@  agyat : (16 June 2013 - 07:38 AM) Thanks Yordan...
@  velma : (16 June 2013 - 12:06 AM) I Have Asked Opa To Check For A Backup.. He'll Let Me Know Soon :)
@  velma : (16 June 2013 - 12:05 AM) T_T It Seems That Someone Has Deleted That Topic Since I Found The Url Of The Topic But It Gives Me An Error
@  yordan : (15 June 2013 - 10:31 PM) @velma : It's A Tuto On How To Create A Login Program.
@  yordan : (15 June 2013 - 10:31 PM) Happy Birthday To Youuuuuu Agyat!
@  yordan : (15 June 2013 - 10:31 PM) Ba$
@  agyat : (15 June 2013 - 04:41 PM) :(
@  agyat : (15 June 2013 - 04:41 PM) Where The Hall I Were? 15Th Is Almost At End And No-One Wished Me "happy Birthday"!!!
@  velma : (14 June 2013 - 10:39 AM) Which Tutorial Is He Searching For?
@  velma : (14 June 2013 - 10:38 AM) Which Tutorial Is He Searching For?
@  yordan : (14 June 2013 - 07:47 AM) Ok, Have A Look Tomorrow.
@  yordan : (13 June 2013 - 03:19 PM) @velma, Can You Have A Look At Feelay's Problem? Seems That His Tutorial Is Not Searchable Today.
@  Feelay : (13 June 2013 - 08:11 AM) Oh, Haha
@  velma : (12 June 2013 - 05:39 PM) T_T Lately My Levels Of Procrastination..... **sigh**
@  velma : (12 June 2013 - 05:38 PM) I'll Do It Later
@  velma : (12 June 2013 - 05:38 PM) Procrastinators.. People Who Keep Saying "i'll Do This In A Bit"
@  Feelay : (12 June 2013 - 02:05 PM) Deal Punishments To What?
@  velma : (12 June 2013 - 01:27 PM) T_T We Should Deal Punishments To Procrastinators... Especially Me
@  Feelay : (12 June 2013 - 12:06 PM) As Well As Making It More Secure.

Replying to Delphi - Tfilestream


Post Options

    • Can't make it out? Click here to generate a new image

  or Cancel


Topic Summary

Posted 25 August 2010 - 09:23 AM

Storing results in a variableDelphi - Tfilestream

I'm working on a delphi appllication,I'm using this

with TOracleQuery.Create(nil) do begin   Session := OracleSession1;   Close ;   SQL.Clear ;   Iresults := SQL.Add('select (max(user_id)+1) as max_user_id ');   SQL.Add('from scs_users');

if tblUser.RecordCount <= 0 then begin   if edtSCSUser.Text <> '' then   begin TblUser.Insert; TblUser.FieldByName('USER_NAME').AsString   := UpperCase((edtSCSUser.Text)); TblUser.FieldByName('USER_LEVEL').AsString := Trim(cmbSCSAccess.Value); TblUser.FieldByName('password').AsString := DoEncrypt('AMCOAL1','PWD'); TblUser.FieldByName('password_date').AsDateTime := Now; TblUser.FieldByName('USER_ID').AsInteger := Iresults; TblUser.FieldByName('SCS_ACCESS').AsString := Trim(cmbSCS.Text); TblUser.FieldByName('DH_ACCESS').AsString  := Trim(cmbDH.Text); TblUser.Post; messagedlg('SCS User '+ trim(edtSCSUser.Text)+ 'created successfully', mtInformation, [mbok], 0) ;   end

but the query results are not being updated correctly in the users table, when I run the query in the sqlplus I return correct results but when I try to do the same on the delphi program its not returning the correct result.The system increment the user_id value each time a new user is being created so now I'm trying to carry out the same functionality using the delphi application please assist

-reply by Nomsa

 


Posted 25 June 2008 - 08:36 AM

improvement of the example
Delphi - Tfilestream

Basic but useful (for who does not know about it).

The parameter "(GameName:TFileStream;" is useless. Use a local var instead.

(or it's a typo and you wanted to pass the listbox to save.)



-reply by Loda

overture

Posted 14 February 2005 - 04:01 PM

OK. This tutorial may be helpful to people who use Delphi, for anyone else... it will probably be boring :P.

This tutorial is going to explain briefly how to use the TFileStream Class.

For anyone who has wanted to say... save any data from a listbox and during run-time you wanted to append different information to it, or throughout the applications running time and then save the information when the application closes then you should use the TFileSteam Class. The great thing about this is it can read, write and save the information in the exact order from any component (in this case a listbox). When using it, it writes and saves the data onto the hard-disk in some kind of file - this file type can be chosen during codeing.

When you want to reload the data previously saved all it has to do is stream it back into the memory and specify it to a component. Remember when it is reloaded it does so in its original form (order) which is the whole point of using it in this case.

note: i am showing this code from within a procedure so you need to read on to see how it is called. :P

The reason i want to share the following information i recently had to create it for a programming assignment for college, and instead of me iterating the code i decided to put it all into a single procedure. The scenario was that i needed to create a game in which match fixtures were created and it required me to keep each set of fixtures for future reference(there was a whole lot more to it but it doesn't matter).

This is the basic code which is needed to read the data into the listbox is as follows (i have added comments to explain briefly what each part does)
GameName:=TFileStream.Create(FileName, fmOpenRead); //It creates the stream from the specified filename. It then opens and reads the appropriate file using the 'fmOpenRead' mode. There are many more values to suit wider needs.
GameName.ReadComponent(lb); //read the items into the listbox in the correct order (done automatically)
GameName.Free; //free the memory which this process has taken up

note: 'lb' stands for 'ListBox'

This next block of code is to show you how to save the data:
GameName:=TFileStream.Create(FileName, fmCreate); //creates the file using the 'fmCreate' mode.
GameName.WriteComponent(lb); //This gathers the data from the specified listbox
GameName.Free;

This is the basis for reading, writing and saving data from the listbox. The problem i had was i needed to do this several times for different games and this may have took some time. Instead i decided to make a procedure to perform this for me and me only specifying the FILENAME and GAMENAME.

This is what the whole procedure looks like:
PROCEDURE TfrmMain.LBFileStream(GameName:TFileStream; FileName:String);
BEGIN
  IF FileExists(FileName) THEN
    BEGIN
      GameName:=TFileStream.Create(FileName, fmOpenRead);
      GameName.ReadComponent(lb);
      GameName.Free;
    END;
  GameName:=TFileStream.Create(FileName, fmCreate);
  GameName.WriteComponent(lb);
  GameName.Free;
END;

As you can see i have added a little more checking in the code, i have checked to see whether the file exists or not - if it does then to read the items into the listbox, and if not then to read the listbox and create the appropriate file.

There are two variables which i have included within this procedure - GameName (which is what the file game will be called, and the filename which is what file will be called.

The last thing to do now is to call the procedure to create and read our file. This is the code:

LBFileStream(SpillikinsStream, 'SpillikinsList.dat');

The word Spillikins refers to a type of Game. As you can see the two variables which were declared within the procedure have been used (the ones within the brackets). I decided i wanted to store the items into a .DAT file as to keep the items as secure as possible (.DAT files are not easily editable). A file will be created called 'SpillikinsList.dat'. This will hold the data from the listbox.

You see that the one line of code can be used to call the single procedure multiple times, this saves me writing out the code each time for each game.

I hope this has helped some of you if not informed you of something you might not otherwise have known.

If you see a problem or mistakes please tell me and i will correct them as i am doing this and a lot of other work at the same time. thankyou

overture.

Review the complete topic (launches new window)