MySQSLdb: just guessing

Carsten Gaebler cg at schlund.de
Wed Apr 25 04:02:55 EDT 2001


Albert Wagner wrote:
> 
> I can find no docs for MySQLdb, so I am just guessing how to get going.  I
> got this far by trial and error:
> 
> >>> import _mysql
> >>> from _mysql import *
> >>> connection = connect('myhostname', 'mypassword')
> >>>
>

MySQLdb is very poorly documented. But if you browse through the dbtrainer
files in the doc/ directory you'll come across something like this:


import MySQLdb

# Establish a connection:
db=MySQLdb.connect(host='myhost',db='mydatabase',user='itsme',passwd='wonttellyou')

# Fetch a table list
c = db.cursor()
c.execute('show tables')
print c.fetchall()

# fetchall() is bad for huge results.
# use fetchmany() or fetchone()
c.execute('select * from a_huge_table')
while 1:
	rows = c.fetchmany(10)
	if not rows: break
	for row in rows:
	print row


db.close()


cg.



More information about the Python-list mailing list