python连接mysql

1. 安装mysql

mysqld --install
net start mysql

 

2. 下载 安装mysql和python的connector

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

 

3. python 连接mysql 的代码

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import MySQLdb

try:
    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

 

posted @ 2014-12-30 12:22  carlo-z  阅读(204)  评论(0编辑  收藏  举报