Thread: Python Paging

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0

    Question Python Paging


    Hello Everyone,

    Just wondering if anyone knows how to page through recordsets using Python or can point me to the right direction? What I mean by paging is displaying your results from a DB one page at a time with a specific number of rows per page and then you can move back and forth from one page to another just like how you would navigate through threads in this forum.

    Is there a separate module you have to install? I'm fairly new and would appreciate your help. Thanks in advance!
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You mean you want to build one right. Its really pretty easy if your db is set up for it. If you have an ID column in your database you can use that to show the required field i.e. the following pseudocode.

    Code:
    #!/usr/bin/env python
    
    import cgi
    
    form = cgi.FieldStorage()
    
    #number of results to display per page.
    limit = 50
    
    if page in form:
        page = form['page'].value
        query = 'SELECT * FROM table_name WHERE ID > %s LIMIT %d' % (page, limit)
    
    ...
    get results form the db using the 'query' above.
    ...
    Note: this is only ment to show the basic idea, if your using MySQLdb for instance you should probably assemble the query inside the execute('query with %s', arguments) method.

    Hope that made a little sence,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0
    Thanks for the reply netytan! Does this mean that you will have to query the database every time you move from page to page? I used to code a little bit of ASP and I remember that it has the ability to store results in a recordset so that it doesn't call the DB each time you move from page to page. Is there a similar functionality in Python?
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    That really depends, you can store the results in a file as easy as punch and process that each time the page is loaded its up to you. Otherwise yes you will have to query the database each time.

    You still have to work out a way to refreshing this file say if the DB changes.

    But no, CGI doesn't have much in the way of High level catching like PHP and ASP/ASP.NET may do since this would require a long running process which sadly normal CGI just isn't .

    Maybe with mod_python but i'm not sure, http://www.modpython.org/

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0
    Thanks Mark! I guess recordsets are out of the question. How about displaying let's say 10 rows at a time from the DB and so forth (or back) regardless of having incremental IDs as a column? You know any code on top of your head for this?
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You might want to look at the documentation for MySQL, you might be able to use LIMIT with two options but since i have no way of testing this i didn't mention it before.

    Code:
    SELECT * FROM table_name LIMIT %d, %d
    As a rule any well designed database should include a primary key, weather or not this is called ID is another matter!

    Another way to do this would be to read all the values from the DB and use slices though these values to display the ones you want, though not very efficent.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo