Speeding up table generation
William Park
parkw at better.net
Thu Mar 9 13:07:38 EST 2000
Try putting your strings into a list, then at the end concatenate the
items. Eg.
a = ['<table>']
a.append('<td>...</td>')
...
a.append('</table>')
print string.join(a)
--William
On Thu, Mar 09, 2000 at 09:37:08AM -0800, Lenny Self wrote:
> 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
>
>
>
>
> --
> http://www.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list