mardi 8 décembre 2015

SQLite: How to get absolute row number

I'm trying to get the absolute record number of a row after it has been returned by a query (in Perl). Let's say I query my table as follows:

my $stmt = "SELECT * FROM MAIN WHERE item='widget' LIMIT 1;";
my $sth  = $dbh->prepare( $stmt );
my $rv   = $sth->execute();

How can I find out the absolute row number of the returned row? I tried using ROWID:

my $stmt = "SELECT ROWID, * FROM MAIN WHERE item='widget' ORDER BY ROWID LIMIT 1;";
my $sth  = $dbh->prepare( $stmt );
my $rv   = $sth->execute();
my $row  = $sth->fetchrow_hashref();

if( defined( $row ) ) {
  $RecordNumber = $row->{'rowid'};
}

This works if you never ever delete a row from the table.

But Row IDs never change. So if you delete rows from the table, the rowids no longer match the absolute row number in the table.

I hope my question is clear (English is not my native language).

Aucun commentaire:

Enregistrer un commentaire