Recent

Author Topic: Help with custom record and array  (Read 2609 times)

nugax

  • Full Member
  • ***
  • Posts: 232
Help with custom record and array
« on: February 08, 2018, 08:39:07 pm »
I know this is due to my lack of pascal. Can someone help me make an array of a custom type:

Type:

Code: Pascal  [Select][+][-]
  1. type
  2.   userRecord = packed record
  3.     firstname, lastname, handle, user_number, password, datejoined, email, affils, fromwhere, status, bbssysop,
  4.     bbsname, lastcall, notes: string[255];
  5.     seclevel: integer;
  6.   end;  


Code To create & use array: (not complete, i was messing with it.)

Code: Pascal  [Select][+][-]
  1. function get_user_list_mainmenu(): user_handler.userRecord;
  2. type
  3.    arrayUsers : array [0..10000]  of user_hander.userRecord;
  4.  
  5. var
  6.   Connect: TSQLite3Connection;
  7.   Trans: TSQLTransaction;
  8.   Query: TSQLQuery;
  9.   db_name: string[255];
  10.   sql: string[100];
  11.   usersRecord : arrayUsers;
  12.  
  13. begin
  14.  
  15.    //Set Database
  16.   db_name := cbbs_info.sDataDir + '/cbbs_users.dbl';
  17.   //Setup SQL
  18.   sql := 'SELECT * FROM cbbs_users';
  19.  
  20.   //Code to check for handle
  21.   Connect := TSQLite3Connection.Create(nil);
  22.   Query := TSQLQuery.Create(nil);
  23.   Trans := TSQLTransaction.Create(Connect);
  24.   Connect.Transaction := Trans;
  25.   Connect.DatabaseName := db_name;
  26.   Trans.StartTransaction;  // opens Connect, EInOutError if SQLite not installed
  27.   Query.SQL.Text := sql;
  28.   Query.Database := Connect;  //Connection for SQL
  29.   Query.Open;
  30.   usersRecord[1].
  31.  
  32. end;
  33.      
-Nugax

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Help with custom record and array
« Reply #1 on: February 08, 2018, 08:54:18 pm »
Never mind, i got the array working.


Would this code load the array using sqlite?


Code: Pascal  [Select][+][-]
  1. sql := 'SELECT * FROM cbbs_users';
  2.  
  3.   //Code to check for handle
  4.   Connect := TSQLite3Connection.Create(nil);
  5.   Query := TSQLQuery.Create(nil);
  6.   Trans := TSQLTransaction.Create(Connect);
  7.   Connect.Transaction := Trans;
  8.   Connect.DatabaseName := db_name;
  9.   Trans.StartTransaction;  // opens Connect, EInOutError if SQLite not installed
  10.   Query.SQL.Text := sql;
  11.   Query.Database := Connect;  //Connection for SQL
  12.   Query.Open;
  13.   for Count := 1 to 10000 do
  14.   begin
  15.     usersRecord[Count].user_number := Query.FieldByName('user_number').AsString;
  16.     usersRecord[Count].handle := Query.FieldByName('handle').AsString;
  17.     usersRecord[Count].firstname := Query.FieldByName('firstname').AsString;
  18.     usersRecord[Count].lastname := Query.FieldByName('lastname').AsString;
  19.     usersRecord[Count].password := Query.FieldByName('password').AsString;
  20.     usersRecord[Count].datejoined := Query.FieldByName('datejoined').AsString;
  21.     usersRecord[Count].email := Query.FieldByName('email').AsString;
  22.     usersRecord[Count].affils := Query.FieldByName('affils').AsString;
  23.     usersRecord[Count].fromwhere := Query.FieldByName('fromwhere').AsString;
  24.     usersRecord[Count].status := Query.FieldByName('status').AsString;
  25.     usersRecord[Count].bbssysop := Query.FieldByName('bbssysop').AsString;
  26.     usersRecord[Count].bbsname := Query.FieldByName('bbsname').AsString;
  27.     usersRecord[Count].seclevel := Query.FieldByName('seclevel').AsInteger;
  28.     usersRecord[Count].lastcall := Query.FieldByName('lastcall').AsString;
  29.     usersRecord[Count].notes := Query.FieldByName('notes').AsString;
  30.   end;
  31.  
  32. end;          
-Nugax

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Help with custom record and array
« Reply #2 on: February 08, 2018, 09:14:31 pm »
Never mind, i got the array working.


Would this code load the array using sqlite?


Code: Pascal  [Select][+][-]
  1. sql := 'SELECT * FROM cbbs_users';
  2.  
  3.   //Code to check for handle
  4.   Connect := TSQLite3Connection.Create(nil);
  5.   Query := TSQLQuery.Create(nil);
  6.   Trans := TSQLTransaction.Create(Connect);
  7.   Connect.Transaction := Trans;
  8.   Connect.DatabaseName := db_name;
  9.   Trans.StartTransaction;  // opens Connect, EInOutError if SQLite not installed
  10.   Query.SQL.Text := sql;
  11.   Query.Database := Connect;  //Connection for SQL
  12.   Query.Open;
  13.   for Count := 1 to 10000 do
  14.   begin
  15.     usersRecord[Count].user_number := Query.FieldByName('user_number').AsString;
  16.     usersRecord[Count].handle := Query.FieldByName('handle').AsString;
  17.     usersRecord[Count].firstname := Query.FieldByName('firstname').AsString;
  18.     usersRecord[Count].lastname := Query.FieldByName('lastname').AsString;
  19.     usersRecord[Count].password := Query.FieldByName('password').AsString;
  20.     usersRecord[Count].datejoined := Query.FieldByName('datejoined').AsString;
  21.     usersRecord[Count].email := Query.FieldByName('email').AsString;
  22.     usersRecord[Count].affils := Query.FieldByName('affils').AsString;
  23.     usersRecord[Count].fromwhere := Query.FieldByName('fromwhere').AsString;
  24.     usersRecord[Count].status := Query.FieldByName('status').AsString;
  25.     usersRecord[Count].bbssysop := Query.FieldByName('bbssysop').AsString;
  26.     usersRecord[Count].bbsname := Query.FieldByName('bbsname').AsString;
  27.     usersRecord[Count].seclevel := Query.FieldByName('seclevel').AsInteger;
  28.     usersRecord[Count].lastcall := Query.FieldByName('lastcall').AsString;
  29.     usersRecord[Count].notes := Query.FieldByName('notes').AsString;
  30.   end;
  31.  
  32. end;          
ignoring the untestable setup procedure, I have to say yes, it should initialise the data to the single random record first fetched from the sqlite database.
two minor adjustments
1) after query.open always call query.first
2) avoid constant loops even when the number of records is known to be static. use a repeat until query.eof construct something along the lines of
Code: Pascal  [Select][+][-]
  1. ...
  2. ...
  3. ...
  4.   Query.Open;
  5.   Query.First;
  6.   Count := Low(usersRecord);
  7.   Repeat
  8.     usersRecord[Count].user_number := Query.FieldByName('user_number').AsString;
  9.     usersRecord[Count].handle := Query.FieldByName('handle').AsString;
  10.     usersRecord[Count].firstname := Query.FieldByName('firstname').AsString;
  11.     usersRecord[Count].lastname := Query.FieldByName('lastname').AsString;
  12.     usersRecord[Count].password := Query.FieldByName('password').AsString;
  13.     usersRecord[Count].datejoined := Query.FieldByName('datejoined').AsString;
  14.     usersRecord[Count].email := Query.FieldByName('email').AsString;
  15.     usersRecord[Count].affils := Query.FieldByName('affils').AsString;
  16.     usersRecord[Count].fromwhere := Query.FieldByName('fromwhere').AsString;
  17.     usersRecord[Count].status := Query.FieldByName('status').AsString;
  18.     usersRecord[Count].bbssysop := Query.FieldByName('bbssysop').AsString;
  19.     usersRecord[Count].bbsname := Query.FieldByName('bbsname').AsString;
  20.     usersRecord[Count].seclevel := Query.FieldByName('seclevel').AsInteger;
  21.     usersRecord[Count].lastcall := Query.FieldByName('lastcall').AsString;
  22.     usersRecord[Count].notes := Query.FieldByName('notes').AsString;
  23.     Query.Next;
  24.     Inc(count)
  25.   until Query.Eof;
  26.  
  27. end;
that will load all the saved records to your array. If your goal was to simple initialize 1000 array cells to a single record then your previous code is more accurate.

EDIT:
oops, forgot to increase the count variable as well. Corrected.
« Last Edit: February 08, 2018, 09:25:51 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 7607
Re: Help with custom record and array
« Reply #3 on: February 08, 2018, 10:55:29 pm »
And using a

 With UsersRecord[Count] Do begin
   
End;
 Query.Next;
 Inc(Count);

etc..
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Help with custom record and array
« Reply #4 on: February 08, 2018, 11:25:54 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   userRecord = packed record
  3.     firstname, lastname, handle, user_number, password, datejoined, email, affils, fromwhere, status, bbssysop,
  4.     bbsname, lastcall, notes: string[255];
  5.     seclevel: integer;
  6.   end;
Wasting of memory. One record = 3,5kB.
10000 records = 35MB.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018