python操作mysql

http://stackoverflow.com/questions/372885/how-do-i-connect-to-a-mysql-database-in-python

1, 下载MySQLdb: http://sourceforge.net/projects/mysql-python/files/

2, 如下python代码

#!/usr/bin/python
import MySQLdb

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                     user="john", # your username
                      passwd="megajonhy", # your password
                      db="jonhydb") # name of the data base

# you must create a Cursor object. It will let
#  you execute all the query you need
cur = db.cursor() 

# Use all the SQL you like
cur.execute("SELECT * FROM YOUR_TABLE_NAME")

# print all the first cell of all the rows
for row in cur.fetchall() :
    print row[0]

 

posted @ 2014-03-12 03:59  zmiao  阅读(155)  评论(0编辑  收藏  举报