Recent

Author Topic: SqLite3  (Read 1841 times)

nugax

  • Full Member
  • ***
  • Posts: 232
SqLite3
« on: February 12, 2018, 04:11:34 pm »
Ok, So I execute and pull in data from a "SELECT * FROM table" statement in pascal.

How do I simply loop through each row? I am successfully pulling the data, but cant figure out how to get to next row once done with this one!
I know easy, and I have searched google and found so much info none of which was helpful.

I dont think you need my code just to tell me how to go to each row. Im sure its a while/for/ or some type of loop but I know someone knows off the top of their head.

Thanks!


-Nugax

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: SqLite3
« Reply #1 on: February 12, 2018, 04:28:33 pm »
http://wiki.lazarus.freepascal.org/SqlDBHowto
Code: Pascal  [Select][+][-]
  1. Program ShowData;
  2.  {$mode objfpc} {$ifdef mswindows}{$apptype console}{$endif}
  3. uses
  4.   DB, Sysutils, sqldb, sqlite3conn;
  5.  
  6. var
  7.   AConnection  : TSQLConnection;
  8.   ATransaction : TSQLTransaction;
  9.   Query        : TSQLQuery;
  10.  
  11. begin
  12.   AConnection := TSQLite3Connection.Create(nil);
  13.   ATransaction := TSQLTransaction.Create(AConnection);
  14.   AConnection.Transaction := ATransaction;
  15.   AConnection.DatabaseName := 'test_dbase';
  16.   Query := TSQLQuery.Create(nil);
  17.   Query.SQL.Text := 'select * from tblNames';
  18.   Query.Database := AConnection;
  19.   Query.Open;
  20.   while not Query.Eof do
  21.   begin
  22.     Writeln('ID: ', Query.FieldByName('ID').AsInteger, 'Name: ' +
  23.                                   Query.FieldByName('Name').AsString);
  24.     Query.Next;
  25.   end;
  26.   Query.Close;
  27.   AConnection.Close;
  28.   Query.Free;
  29.   ATransaction.Free;
  30.   AConnection.Free;
  31. end.

cdbc

  • Hero Member
  • *****
  • Posts: 1074
    • http://www.cdbc.dk
Re: SqLite3
« Reply #2 on: February 12, 2018, 04:34:45 pm »
Hi nugax
Well you've got data in your Dataset with -> "SELECT * FROM table" , then you do something like this:
Code: Pascal  [Select][+][-]
  1. Dataset.First;  // <- important
  2. while not Dataset.EOF do try
  3.   // fetch your data here and process them
  4.   Dataset.next; // will move cursor to the next row !!! important or you will have an endless loop on your hands ;-)
  5. except
  6. ...
  7. end;
  8.  
... or take look at "TDataset" in the source
Hth - regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

nugax

  • Full Member
  • ***
  • Posts: 232
Re: SqLite3
« Reply #3 on: February 12, 2018, 04:40:14 pm »
Got it. Thanks guys!

-Nugax

nugax

  • Full Member
  • ***
  • Posts: 232
Re: SqLite3
« Reply #4 on: February 12, 2018, 06:33:53 pm »
Does query.recordcount return the number of records from the query?

I have 12 records, confirmed in the sqlite db, SELECT * FROM table;
yet, query.recordcount only returns 10.


Hi nugax
Well you've got data in your Dataset with -> "SELECT * FROM table" , then you do something like this:
Code: Pascal  [Select][+][-]
  1. Dataset.First;  // <- important
  2. while not Dataset.EOF do try
  3.   // fetch your data here and process them
  4.   Dataset.next; // will move cursor to the next row !!! important or you will have an endless loop on your hands ;-)
  5. except
  6. ...
  7. end;
  8.  
... or take look at "TDataset" in the source
Hth - regards Benny
-Nugax

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: SqLite3
« Reply #5 on: February 12, 2018, 06:48:40 pm »
Because property  packetrecords is set to 10. Set the value to -1 and al records in selected query are count.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018