Karrigell/Test Mysql

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Example using Mysql database

This simple program can be used to analyze your web server performance

this script might be saved as testmysql.py and tested on a Karrigell server used alone or behind Apache


# import MySQL module
import MySQLdb
import time
t1 = time.clock()
# connect
db = MySQLdb.connect(host="localhost", user="root",   passwd="123456",db="test")
# create a cursor
cursor = MySQLdb.cursors.DictCursor(db)
# execute SQL statement
cursor.execute("SELECT field1,field2 FROM table where field2 like '%%' limit 0,5000")
# get the resultset as a tuple
result = cursor.fetchallDict()
print "Content-Type: text/plain\n\n"
# iterate through resultset
for record in result:
#   print record[0] , "-->", record[1]
    print "%s, %s" % (record['field1'], record['field2'])
    print "<br>"

Now you can get the time spent by the query

t2 = time.clock()
t3 = t2 - t1
print t3