June 15th, 2004, 12:33 PM
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!
June 15th, 2004, 01:11 PM
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.
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.
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.
Hope that made a little sence,
June 15th, 2004, 02:01 PM
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?
June 15th, 2004, 02:08 PM
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/
June 15th, 2004, 02:47 PM
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?
June 16th, 2004, 08:35 AM
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.
As a rule any well designed database should include a primary key, weather or not this is called ID is another matter!
SELECT * FROM table_name LIMIT %d, %d
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,