Speeding up table generation

Lenny Self lenny at squiggie.com
Thu Mar 9 18:37:08 CET 2000


Hello.  I am rather new to Python and programming in general.  I have
created th following function in order to display the contents of a query
from a MySQL database in an HTML form.  With the number of records I am
working with the table building portion of this function takes quite some
time ( 7 - 10 seconds).  7 to 10 seconds isn't all that bad but it's
possible the database will grow quite a bit and displaying all of the
records could take quite a while.  Does anyone have any suggestion for me
that might help speed this function up?

I'd really appriciate some help

Lenny Self
lenny at squiggie.com

### Code Below ###

import os
import sys
import MySQL

# Connecting to database server
try:
    dbhandle = MySQL.connect('dbserver,'login','passwd')
    try:
            # selecting database
            dbhandle.selectdb('database')
    except:
            print 'Could not find database'
            sys.exit()
except:
    print 'Could not connect to server'
    sys.exit()

# Querying database for column names
account_columns = dbhandle.do('show columns from table)
# Querying database for body of table
accounts = dbhandle.do('select * from table)

# Creating table
table = '<table><TR>'
a = 0
# Creating column names in table
while a < len(account_columns):
    table = table + '<TD>' + account_columns[a][0] + '</TD>'
    a = a + 1
b = 0
# Creating body of table by going though each record of the query
while b < len(accounts):
    table = table + '</TR><TR>'
    c = 0
    # Going each cell of each record to add to table
    while c < len(accounts[b]):
        table = table + '<TD>' + `accounts[b][c]` + '</TD>'
        c = c + 1
    b = b + 1
table = table + '</TABLE>'
print table







More information about the Python-list mailing list