python 连接数据库

import MySQLdb
import MySQLdb.cursors
#sql语句
sql = "select * from table"
#连接数据库
db = MySQLdb.connect(host="localhost",
user="root",
passwd="root1234",
charset="utf8",
port=3306,
cursorclass=MySQLdb.cursors.DictCursor)
#创建游标
a = db.cursor()
#执行sql
rows = a.execute(sql)
#获取所有查询结果
request = a.fetchall()
print rows
print request
#关闭链接
db.close()
posted @ 2018-08-03 11:25  菜菜_包包  阅读(559)  评论(0编辑  收藏  举报