Description
To use Python to connect to MySQL we need a driver this program is Mysql-python, so we first want to the next corresponding version of the Mysql-python (note that this driver only 32-bit version, so your Python The environment is best if 32 people)
0th step: Download the 32-bit Python and install: http://yunpan.cn/cQQnKwfeIRHq7 access password Def6
First step: Download MySQLdb and install: http://yunpan.cn/cQQtnsLcsGyEr access password 6038
If you finish the above two steps, the environment will be there, we can write the program below.
Example one:
Import MySQLdb #首先要import This package
conn = MySQLdb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' 131417 ', db= ' Studio '); #定义一个连接对象
cursor = Conn.cursor (); #得到返回的cursor对象.
Cursor.execute (' select * from T '); #执行语句
for (x, y) in cursor: #取结果集
print ' x is%s y is%s '% (x, y);
Cursor.close (); #这两个都要close to disconnect
Conn.close ();
Installation of Pthon MySQLdb