database persistence with mysql, sqlite

coldpizza vriolk at gmail.com
Sat Sep 22 19:11:17 EDT 2007


Hi,

I want to run a database query and then display the first 10 records
on a web page. Then I want to be able to click the 'Next' link on the
page to show the next 10 records, and so on.

My question is how to implement paging, i.e. the 'Next/Prev' NN
records without reestablishing a database connection every time I
click Next/Prev? Is it at all possible with cgi/mod_python?

For example, in a NON-web environment, with sqlite3 and most other
modules, I can establish a database connection once, get a cursor
object on which I run a single 'SELECT * FROM TABLE' statement and
then use cursor.fetchmany(NN) as many times as there are still results
left from the initial query.

How do I do the same for the web? I am not using any high-level
framework. I am looking for a solution at the level of cgi or
mod_python (Python Server Pages under Apache). To call
cursor.fetchmany(NN) over and over I need to pass a handle to the
database connection but how do I keep a reference to the cursor object
across pages? I use mysql and sqlite3 as databases, and I am looking
for an approach that would work with both database types (one at a
time). So far I have successfully used the following modules for
database access: sqlite3, mysqld, and pyodbc.

So far, with mysql I use 'SELECT * FROM TABLE LIMIT L1, L2' where L1
and  L2 define the range for the 'Next' and 'Previous' commands. I
have to run the query every time a click a 'Next/Prev' link. But I am
not sure that this is the best and most efficient way. I suppose using
CURSOR.FETCHMANY(NN) would probably be faster and nicer but how do I
pass an object reference across pages? Is it possible without any
higher-level libraries?

What would be the proper way to do it on a non-enterprise scale?

Would SqlAlchemy or SqlObject make things easier with regard to
database persistence?




More information about the Python-list mailing list