Wednesday, March 31, 2010

[Level 2] Use _mysql Module in Python

The sample code for Python to use _mysql module:
#!/usr/bin/python
import _mysql

## you can pass parameter directly if you know the parameter sequence of connect() method.
#cxn=_mysql.connect("localhost","root","admin123","test")

## use vp to pass parameters
#
cxn=_mysql.connect(host="localhost",user="root",passwd="admin123",db="test")
cxn=_mysql.connect(host="localhost",port=3306,user="root",passwd="admin123",db="test")

cxn.query("""SELECT user, host, password FROM mysql.user""")

#cxn.execute("""SELECT user, host, password FROM mysql.user""") ## connection no execute attribute
 
cur=cxn.store_result()
#cur=
cxn.use_result()

while True:
  data=cur.fetch_row()
  if data:
    print(data)
  else:
    break

Wish this helps.

regards,
Stanley Huang