首页 > 代码库 > python连接mysql

python连接mysql

1. 安装mysql

mysqld --installnet start mysql

 

2. 下载 安装mysql和python的connector

http://downloads.mysql.com/archives/c-python/

 

3. python 连接mysql 的代码

#!/usr/local/bin/python# -*- coding: utf-8 -*-import MySQLdbtry:    conn = MySQLdb.connect("127.0.0.1","root","00000","test")    cursor = conn.cursor()    cursor.execute("SELECT VERSION()")    data = cursor.fetchone()    print "Database version : %s " % data    cursor.close()    conn.close()except MySQLdb.Error,e:    print "Mysql Error %d: %s" % (e.args[0], e.args[1])finally:    print End
Database version : 5.5.41-log End

 

python连接mysql