Python Forum
sql select data in row number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sql select data in row number
#1
Hi,

Is there any way to code sql to select a single row by index number WITHOUT looping through every record.

E.g: if I have a database of 1000 records, is there any way I can print record 556 without knowing anything about that row other than its index position within the datbase?



Thanks
Reply
#2
That depends on the database engine.
Most have a medthod that allows selecting by rowid, but
be aware that that rowid is not immutable and can change at any moment,
Reply
#3
well, this is over-simplified...
is it just one table, because with multiple tables, JOINs between, etc.... it's not that easy to index the row. by design you may have some auto-increment id field that usually will serve as a primary key, but it's may be missing certain values (e.g. after deletion of records, etc.)
with simple small table like 1000 records you may execute SELECT * FROM TABLE and then fetchall()
this will result in a tuple of tuples and you may retrieve record with index 555 (index is zero-based as you know)
same apply for a more complex detabase, but index will depend on SORT BY clause

EDIT: Of course, Larz60+ advice about rowid (if available) is much more relevant :-)
Reply
#4
(Mar-09-2018, 07:38 PM)Larz60+ Wrote: That depends on the database engine.
Most have a medthod that allows selecting by rowid, but
be aware that that rowid is not immutable and can change at any moment,

I'm using sqlite3

(Mar-09-2018, 07:56 PM)buran Wrote: well, this is over-simplified...
is it just one table, because with multiple tables, JOINs between, etc.... it's not that easy to index the row. by design you may have some auto-increment id field that usually will serve as a primary key, but it's may be missing certain values (e.g. after deletion of records, etc.)
with simple small table like 1000 records you may execute SELECT * FROM TABLE and then fetchall()
this will result in a tuple of tuples and you may retrieve record with index 555 (index is zero-based as you know)
same apply for a more complex detabase, but index will depend on SORT BY clause

EDIT: Of course, Larz60+ advice about rowid (if available) is much more relevant :-)

Thanks
Reply
#5
DSo here's an example of why you cannot rely on it
Output:
sqlite> Create table TEMP1 ('First' text, 'Last' text); sqlite> .schema TEMP1 CREATE TABLE TEMP1 ('First' text, 'Last' text); sqlite> insert into temp1 values('John', 'Smith'); sqlite> insert into temp1 values('Fred', 'Fuller'); sqlite> insert into temp1 values('Jack', 'Rabbit'); sqlite> select rowid from TEMP1; 1 2 3 sqlite> DELETE from TEMP1 where rowid == 2; sqlite> select rowid from TEMP1; 1 3 sqlite> select rowid, * from TEMP1; 1|John|Smith 3|Jack|Rabbit sqlite> VACUUM; sqlite> select rowid, * from TEMP1; 1|John|Smith 2|Jack|Rabbit sqlite>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 928 Nov-14-2023, 08:46 AM
Last Post: perfringo
  Filtering a data frame according to number of occurences of an observation Menthix 1 1,875 May-31-2021, 10:50 PM
Last Post: supuflounder
  Select data between DW felmback 2 2,004 Mar-21-2021, 01:49 PM
Last Post: ibreeden
  Mysql returning number of rows not data AkaAndrew123 4 2,804 Jun-10-2019, 02:31 PM
Last Post: AkaAndrew123
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 2,233 Feb-14-2019, 08:34 PM
Last Post: woooee
  read select data from one file and write to another Shameendra 1 2,493 Oct-16-2018, 06:46 PM
Last Post: ichabod801
  user input to select and print data from another class python TyTheChosenOne 6 4,122 Aug-30-2018, 05:53 PM
Last Post: TyTheChosenOne
  select Data input from csv file SeanBassett 6 4,951 Jan-21-2018, 10:06 PM
Last Post: SeanBassett
  problem with select and update the data into the database chris0147 1 3,176 Aug-18-2017, 09:38 PM
Last Post: chris0147

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020