Python 3.2: 使用pymysql连接Mysql

在python 3.2 中连接MYSQL的方式有很多种,例如使用mysqldb,pymysql。本文主要介绍使用Pymysql连接MYSQL的步骤

1        安装pymysql

·        确保python3.2已经正确安装

·        从 https://pypi.python.org/pypi/PyMySQL3 下载安装包

·        解压到指定的路径

·        在命令行模式下进入安装目录,执行 python setup.py install命令

 

2        SimpleSample

'''

Created onDec 17, 2013

 

@author:aihua.sun

'''

importpymysql;

 

defaccess_mysql():

    conn = pymysql.connect(host='localhost', port=3306,user='root',db='test')

    cur = conn.cursor()

    cur.execute("SELECT * FROM dictionarylimit 10")

    for r in cur:

       print("row_number:"+str(cur.rownumber))

        print("id:"+str(r[0])+"key:"+str(r[1])+" mean:"+str(r[2]))

    cur.close()

    conn.close()

 

if __name__== '__main__':

    access_mysql();


 

3        Referenceresources

http://pymssql.sourceforge.net/ref_pymssql.php

 

posted on 2013-12-19 12:21  我的小人生  阅读(552)  评论(0编辑  收藏  举报