Jump to content

Navigate to previous, next record in SQLite db


Recommended Posts

I 'm using SQLite.au3.

What I want to do is navigate through the records of a recordset, go to first, last, previous, next record.

I read about indexes, I also learnt that SQLite automatically creates a column named rowid (which might be useful). And found something relative.

How would you suggest to do this? Maybe there is already some library or code snippet for this functionality?

 

 

 

Edited by panoss
Link to comment
Share on other sites

Hi @panoss :)

SQLite is a great tool to work with, but everything depends about on what do you want to do.

As @Earthshine stated, the best place where to start, is the Help file.

From there, you can see how the different _SQLite* functions are used, and so, you can even see if there is already something that could fit your request.

The expert about SQLite ( as I noticed in these years ), is @jchd.

So, let's see if he could suggest to you more :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

SQLite doesn't automatically store the rows in the order you inserted them, you'd need to index the table otherwise the rows could be in any order and you can't go forwards and backwards the way I think you're wanting to do. Better to just read the whole table into an array and navigate through it instead.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

5 hours ago, BrewManNH said:

Better to just read the whole table into an array and navigate through it instead.

This was my first thought but doesn't sound very...'professional'.

I think the use of indexes is a better approach.

Link to comment
Share on other sites

13 hours ago, panoss said:

This was my first thought but doesn't sound very...'professional'.

This is how SQL works, by design. SQL is based on relational algebra and is (roughly) dealing with mathematical sets and ternary logic.

Sets have no intrinsic order so if you're asked to list elements of the set of natural integers between 1 and 5 inclusive, you're free to say "4, 2, 5, 3, 1" or "3, 4, 5, 2, 1" or "1, 2, 3, 5, 4" or whatever order you want. All these answers are correct. Of course you can define a partial or complete order over many sets and if the task specifies "... in increasing order" then the only correct answer is "1, 2, 3, 4, 5" if "increasing" means > (the greater than mathematical operator).

Moral: in SQL the query SELECT * FROM MyTable may return rows (if any) in any order. In this context, first, last, previous, next have no sense.

Many RDBMS offer windowing functions which allow to navigate ad libidum inside a resultset, but  such functions are not (yet) available in SQLite.
SQLite is like its name says: lite!

Edited by jchd
Typos

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

A note for SQLite users who wonder: the dev team of SQLite is in the process of offering windowing functions.

They are part of the current draft release:
 
But don't forget this is just draft, subject to changes.
Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • 6 months later...

Using PostgreSQL's, specifically and,LAGLEAD should be able to show you the previous and next entries in your table.

select *
from (
    select  id, thread_id, is_notice, title, content,
            lag(id) over (order by is_notice desc, thread_id desc, id asc) as prev,
            lead(id) over (order by is_notice desc, thread_id desc, id asc) as next
    from post
    ) x
where 3 IN (id, prev, next);

Too learn more or get SQLite tutorials from here.

Link to comment
Share on other sites

The definitive source of SQLite docs is https://www.sqlite.org/index.html

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...