Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 15th, 2004, 11:33 AM
zci zci is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 17 zci User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 31 sec
Reputation 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!

Reply With Quote
  #2  
Old June 15th, 2004, 12:11 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old June 15th, 2004, 01:01 PM
zci zci is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 17 zci User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 31 sec
Reputation 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?

Reply With Quote
  #4  
Old June 15th, 2004, 01:08 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #5  
Old June 15th, 2004, 01:47 PM
zci zci is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 17 zci User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 31 sec
Reputation 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?

Reply With Quote
  #6  
Old June 16th, 2004, 07:35 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python Paging


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway