Recent

Author Topic: SQLite : How to refresh the current row to get the last recordID  (Read 8186 times)

swierzbicki

  • Full Member
  • ***
  • Posts: 177
SQLite : How to refresh the current row to get the last recordID
« on: September 10, 2013, 04:44:47 pm »
Hello,

I'm filling a table with a handled device. Sometimes my users are doing mistakes and want to delete the record.
My issue is that ID value is always set to 0. The only solution I have is to refresh the table to get the record with the correct Id.
This is really combersome as my table has hundreds of records.

What can I do to get from the start my record Id ? There is no refreshRecord function or methods available.... (I'm worjing with TSQLQuery et TSQLIteConnection) :

Code: [Select]
         if not SQliteTransaction.Active then
         SQliteTransaction.StartTransaction;
         try

             qrStockEntree.Insert;
             qrStockEntree.fieldbyname('Date').Value := now;
             qrStockEntree.Post;
             qrStockEntree.ApplyUpdates;

             if SQliteTransaction.Active then
             SQliteTransaction.CommitRetaining;


            Showmessage(inttostr(qrStockEntree.fieldbyname('ID).AsInteger)); // ===> ALWAYS RETURN 0 (ID IS AUTOINC FIELD)
         except
           on e: exception do
           begin
             if SQliteTransaction.Active then
             SQliteTransaction.RollbackRetaining;
             MessageDlg ('Erreur',e.message, mtError,
             [mbOk],0)  ;
             exit;
           end;
                 
Thank you
Lazarus 1.6.2
fpc 3.0.0
wince/win32/win64
delphi berlin

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: SQLite : How to refresh the current row to get the last recordID
« Reply #1 on: September 10, 2013, 04:50:31 pm »
It's no surprise
Code: [Select]
Showmessage(inttostr(qrStockEntree.fieldbyname('ID).AsInteger)); // ===> ALWAYS RETURN 0 (ID IS AUTOINC FIELD)
doesn't work: once you posted/committed the record, all fields are cleared again.

See
http://www.sqlite.org/faq.html#q1
which says:
"There is a function named sqlite3_last_insert_rowid() which will return the integer key for the most recent insert operation." and has a link for that function.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

swierzbicki

  • Full Member
  • ***
  • Posts: 177
Re: SQLite : How to refresh the current row to get the last recordID
« Reply #2 on: September 10, 2013, 06:08:43 pm »
As i'm using CommitRetaining all data are still available.
Is it safe to do something like that :

Code: [Select]
             qrStockEntree.Insert;
             qrStockEntree.fieldbyname('Date').Value := now;
             qrStockEntree.Post;
             qrStockEntree.ApplyUpdates;

             if SQliteTransaction.Active then
             SQliteTransaction.CommitRetaining;
             //Data is still displayed on the screen

             
             qrLastID.Open; //SQL.Text := select last_insert_rowid() as Id;
             qrStockEntree.Edit;
             qrStockEntree.fieldbyname('Id').AsInteger := qrLastID.FieldByName('Id').AsInteger;
             qrLastID.close;
             qrStockEntree.Post;
             qrStockEntree.ApplyUpdates;
             //DB will not be updated since OLD_Id equal 0 but correct ID will be displayed
             if SQliteTransaction.Active then
             SQliteTransaction.CommitRetaining;
Lazarus 1.6.2
fpc 3.0.0
wince/win32/win64
delphi berlin

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: SQLite : How to refresh the current row to get the last recordID
« Reply #3 on: September 10, 2013, 07:04:38 pm »
No idea. I'm not much of a GUI guy.

Perhaps that's why I'm a bit confused about your commitretaining remark: isn't data display separate from whether you use commitretaining or an explicit commit/starttransaction?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

swierzbicki

  • Full Member
  • ***
  • Posts: 177
Re: SQLite : How to refresh the current row to get the last recordID
« Reply #4 on: September 10, 2013, 07:56:21 pm »
See this http://www.freepascal.org/docs-html/fcl/sqldb/tsqltransaction.commitretaining.html

I've modified the SQLUpdate query to take others fields than the automatic ID
Lazarus 1.6.2
fpc 3.0.0
wince/win32/win64
delphi berlin

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

LacaK

  • Hero Member
  • *****
  • Posts: 702
Re: SQLite : How to refresh the current row to get the last recordID
« Reply #6 on: September 13, 2013, 07:12:43 am »
Did you tried function GetInsertID of TSQLite3Connection ?

swierzbicki

  • Full Member
  • ***
  • Posts: 177
Re: SQLite : How to refresh the current row to get the last recordID
« Reply #7 on: September 13, 2013, 09:00:55 am »
@Lakak : Thank you. I'll check this out
Lazarus 1.6.2
fpc 3.0.0
wince/win32/win64
delphi berlin

 

TinyPortal © 2005-2018