python deal with mysql
2016-03-09 10:35
721 查看
0. install
yum -y install python-dev pip install MySQL-python
这个报名容易记错。是MySQL-python,注意大小写。
1.连接数据库
import MySQLdb db = MySQLdb.connect ("localhost","username","passwd","DBNAME",charset='utf8')
2.执行sql语句
“`sqs = “select host from processlist”
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
for record in results:
col1 = record[0]
col2 = record[1]
3.close
db.close()4.doc
文档很短。这是他蛋疼的地方,毕竟外部库。http://mysql-python.sourceforge.net/MySQLdb.html
最后总结一下workflow
db = MySQLdb.connect()
cursor = db.cursor()
sql = “”
cursor.execute()
results = cursor.fetchall()
do with results
db.close()
相关文章推荐
- Python 知识库,不错!
- 一篇文章让你彻底搞清楚Python中self的含义
- python 面向对象编程-进阶篇
- python 库
- 001第一部分[python基础]
- Python3.3 学习笔记
- python编程之函数思想
- How to set Selenium Python WebDriver default timeout?
- 如何入门 Python 爬虫?
- python之os.path模块
- Python日志模块logging的使用
- python小点心--filter函数
- python之os模块
- 基于python和amap(高德地图)web api的爬虫,用于搜索某POI点
- python open mode取值组合
- python解无忧公主数学题108
- Python多进程编程
- Python & Hadoop
- [python] LDA处理文档主题分布代码入门笔记
- Python Logging使用