码迷,mamicode.com
首页 > 数据库 > 详细

python 连接数据库

时间:2018-08-03 14:20:04      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:set   ble   fetchall   style   char   color   import   sel   ldb   

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()

python 连接数据库

标签:set   ble   fetchall   style   char   color   import   sel   ldb   

原文地址:https://www.cnblogs.com/jinbaobao/p/9413113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!