python连接MySQL、MongoDB、Redis、memcache等数据库的方法


Posted in Python onNovember 15, 2013

用Python写脚本也有一段时间了,经常操作数据库(MySQL),现在就整理下对各类数据库的操作,如后面有新的参数会补进来,慢慢完善。

一,python 操作 MySQL:详情见:
【apt-get install python-mysqldb】

#!/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Purpose:     example for python_to_mysql
# Author:      zhoujy
# Created:     2013-06-14
# update:      2013-06-14
#-------------------------------------------------------------------------------
import MySQLdb
import os
#建立和数据库系统的连接,格式
#conn   = MySQLdb.connect(host='localhost',user='root',passwd='123456',db='test',port=3306,charset='utf8')
#指定配置文件,确定目录,或则写绝对路径
cwd = os.path.realpath(os.path.dirname(__file__))
db_conf = os.path.join(cwd, 'db.conf')
conn   = MySQLdb.connect(read_default_file=db_conf,host='localhost',db='test',port=3306,charset='utf8')
#要执行的sql语句
query  = 'select id  from t1'
#获取操作游标
cursor = conn.cursor()
#执行SQL
cursor.execute(query)
#获取一条记录,每条记录做为一个元组返回,返回3,游标指到第2条记录。
result1 = cursor.fetchone()
for i in result1:
    print i
#返回影响的行数
    print cursor.rowcount
#获取指定数量记录,每条记录做为一个元组返回,返回1,2,游标从第2条记录开始,游标指到第4条记录。
result2 = cursor.fetchmany(2)
for i in result2:
    for ii in i:
        print ii

#获取所有记录,每条记录做为一个元组返回,返回3,4,7,6,游标从第4条记录开始到最后。
result3 = cursor.fetchall()
for i in result3:
    for ii in i:
        print ii
#获取所有记录,每条记录做为一个元组返回,返回3,4,7,6,游标从第1条记录开始
#重置游标位置,0为偏移量,mode=absolute | relative,默认为relative
cursor.scroll(0,mode='absolute')
result3 = cursor.fetchall()
for i in result3:
    for ii in i:
        print ii
#以下2种方法都可以把数据插入数据库:
#(one)
for i in range (10,20):
    query2 = 'insert into t1 values("%d",now())' %i
    cursor.execute(query2)
    #提交
    conn.rollback()
#(two)
rows = []
for i in range (10,20):
    rows.append(i)
query2 = 'insert into t1 values("%s",now())'
#executemany 2个参数,第2个参数是变量。
cursor.executemany(query2,rows)
#提交
conn.commit()
#选择数据库
query3 = 'select id from dba_hospital'
#重新选择数据库
conn.select_db('chushihua')
cursor.execute(query3)
result4 = cursor.fetchall()
for i in result4:
    for ii in i:
        print ii
#不定义query,直接执行:
cursor.execute("set session binlog_format='mixed'")
#关闭游标,释放资源
cursor.close()
'''
+------+---------------------+
| id   | modifyT             |
+------+---------------------+
|    3 | 2010-01-01 00:00:00 |
|    1 | 2010-01-01 00:00:00 |
|    2 | 2010-01-01 00:00:00 |
|    3 | 2010-01-01 00:00:00 |
|    4 | 2013-06-04 17:04:54 |
|    7 | 2013-06-04 17:05:36 |
|    6 | 2013-06-04 17:05:17 |
+------+---------------------+
'''

注意:在脚本中,密码写在脚本里面很容易暴露,这样可以用一个配置文件的方式来存密码,如db.conf:

[client]
user=root
password=123456

二,python 操作 MongoDB:

#!/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Purpose:     example for python_to_mongodb
# Author:      zhoujy
# Created:     2013-06-14
# update:      2013-06-14
#-------------------------------------------------------------------------------
import pymongo
import os
#建立和数据库系统的连接,创建Connection时,指定host及port参数
conn   = pymongo.Connection(host='127.0.0.1',port=27017)
#admin 数据库有帐号,连接-认证-切换库
db_auth = conn.admin
db_auth.authenticate('sa','sa')
#连接数据库
db = conn.abc
#连接表
collection = db.stu
#查看全部表名称
db.collection_names()
#print db.collection_names()
#访问表的数据,指定列
item = collection.find({},{"sname":1,"course":1,"_id":0})
for rows in item:
    print rows.values()
#访问表的一行数据
print collection.find_one()
#得到所有的列
for rows in collection.find_one():
    print rows
#插入
collection.insert({"sno":100,"sname":"jl","course":{"D":80,"S":85}})
#或
u = dict(sno=102,sname='zjjj',course={"D":80,"S":85})
collection.insert(u)
#得到行数
print collection.find().count()
print collection.find({"sno":100})
#排序,按照某一列的值。pymongo.DESCENDING:倒序;pymongo.ASCENDING:升序。按照sno倒序
item = collection.find().sort('sno',pymongo.DESCENDING) 
for rows in item:
    print rows.values()
#多列排序
item = collection.find().sort([('sno',pymongo.DESCENDING),('A',pymongo.ASCENDING)])
#更新,第一个参数是条件,第二个参数是更新操作,$set,%inc,$push,$ne,$addToSet,$rename 等
collection.update({"sno":100},{"$set":{"sno":101}})
#更新多行和多列
collection.update({"sno":102},{"$set":{"sno":105,"sname":"SSSS"}},multi=True)
#删除,第一个参数是条件,第二个参数是删除操作。
collection.remove({"sno":101})
'''
sno:学号;sname:姓名;course:科目
db.stu.insert({"sno":1,"sname":"张三","course":{"A":95,"B":90,"C":65,"D":74,"E":100}})
db.stu.insert({"sno":2,"sname":"李四","course":{"A":90,"B":85,"X":75,"Y":64,"Z":95}})
db.stu.insert({"sno":3,"sname":"赵五","course":{"A":70,"B":56,"F":85,"G":84,"H":80}})
db.stu.insert({"sno":4,"sname":"zhoujy","course":{"A":64,"B":60,"C":95,"T":94,"Y":85}})
db.stu.insert({"sno":5,"sname":"abc","course":{"A":87,"B":70,"Z":56,"G":54,"H":75}})
db.stu.insert({"sno":6,"sname":"杨六","course":{"A":65,"U":80,"C":78,"R":75,"N":90}})
db.stu.insert({"sno":7,"sname":"陈二","course":{"A":95,"M":68,"N":84,"S":79,"K":89}})
db.stu.insert({"sno":8,"sname":"zhoujj","course":{"P":90,"B":77,"J":85,"K":68,"L":80}})
db.stu.insert({"sno":9,"sname":"ccc","course":{"Q":85,"B":86,"C":90,"V":87,"U":85}})
'''

计算Mongodb文档中各集合的数目:

import pymongo
conn   = pymongo.Connection(host='127.0.0.1',port=27017)
db = conn.abc    #abc文档
for tb_name in db.collection_names():     #循环出各集合名
    Count = db[tb_name].count()            #计算各集合的数量
    if Count > 2:                                 #过滤条件
        print tb_name + ':' + str(Count) 
'''
conn   = pymongo.Connection(host='127.0.0.1',port=27017)
db = conn.abc
for tb_name in db.collection_names():
    print tb_name + ':' 
    exec('print ' + 'db.'+tb_name+'.count()')      #变量当集合的处理方式
OR
conn   = pymongo.Connection(host='127.0.0.1',port=27017)
db = conn.abc
for tb_name in db.collection_names():
    mon_dic=db.command("collStats", tb_name)      #以字典形式返回
    print mon_dic.get('ns'),mon_dic.get('count')
'''

三,python 操作 Redis:

#!/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Purpose:     example for python_to_mongodb
# Author:      zhoujy
# Created:     2013-06-14
# update:      2013-06-14
#-------------------------------------------------------------------------------
import redis
f = open('aa.txt')
while True:
    line = f.readline().strip().split(' # ')
    if line == ['']:
        break
    UserName,Pwd,Email = line
#    print name.strip(),pwd.strip(),email.strip()
    rc = redis.StrictRedis(host='127.0.0.1',port=6379,db=15)
    rc.hset('Name:' + UserName,'Email',Email)
    rc.hset('Name:' + UserName,'Password',Pwd)
f.close()
alluser = rc.keys('*')
#print alluser
print "===================================读出存进去的数据==================================="
for user in alluser:
    print ' # '.join((user.split(':')[1],rc.hget(user,'Password'),rc.hget(user,'Email')))

四,python 操作 memcache:

import memcache
mc = memcache.Client(['127.0.0.1:11211'],debug=1)
#!/usr/bin/env python
#coding=utf-8
import MySQLdb
import memcache
import sys
import time
def get_data(mysql_conn):
#    nn = raw_input("press string name:")
    mc = memcache.Client(['127.0.0.1:11211'],debug=1)
    t1 =time.time()
    value = mc.get('zhoujinyia') 
    if value == None:
        t1 = time.time()
        print t1
        query = "select company,email,sex,address from uc_user_offline where realName = 'zhoujinyia'"
        cursor= mysql_conn.cursor()
        cursor.execute(query)
        item = cursor.fetchone()
        t2 = time.time()
        print t2
        t = round(t2-t1)
        print "from mysql cost %s sec" %t 
        print item
        mc.set('zhoujinyia',item,60)
    else :
        t2 = time.time()
        t=round(t2-t1)
        print "from memcache cost %s sec" %t
        print value
if __name__ =='__main__':
    mysql_conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='123456',db='member',port=3306,charset='utf8')
    get_data(mysql_conn)
Python 相关文章推荐
python去掉字符串中重复字符的方法
Feb 27 Python
Python使用htpasswd实现基本认证授权的例子
Jun 10 Python
Python ORM框架SQLAlchemy学习笔记之数据添加和事务回滚介绍
Jun 10 Python
python爬虫的工作原理
Mar 05 Python
Python基于回溯法子集树模板解决数字组合问题实例
Sep 02 Python
和孩子一起学习python之变量命名规则
May 27 Python
Python之列表的插入&替换修改方法
Jun 28 Python
Python3 列表,数组,矩阵的相互转换的方法示例
Aug 05 Python
Django+zTree构建组织架构树的方法
Aug 21 Python
python 解决flask 图片在线浏览或者直接下载的问题
Jan 09 Python
Python3操作YAML文件格式方法解析
Apr 10 Python
从0到1使用python开发一个半自动答题小程序的实现
May 12 Python
python实现代理服务功能实例
Nov 15 #Python
Python(Tornado)模拟登录小米抢手机
Nov 12 #Python
Python3实现的腾讯微博自动发帖小工具
Nov 11 #Python
python正则分组的应用
Nov 10 #Python
python Django连接MySQL数据库做增删改查
Nov 07 #Python
使用Python下载Bing图片(代码)
Nov 07 #Python
python中文乱码的解决方法
Nov 04 #Python
You might like
eWebEditor v3.8 商业完整版 (PHP)
2006/12/06 PHP
基于PHP的cURL快速入门教程 (小偷采集程序)
2011/06/02 PHP
关于PHP的curl开启问题探讨
2014/04/08 PHP
php mb_substr()函数截取中文字符串应用示例
2014/07/29 PHP
php arsort 数组降序排序详细介绍
2016/11/17 PHP
php生成二维码图片方法汇总
2016/12/17 PHP
List the Stored Procedures in a SQL Server database
2007/06/20 Javascript
javaScript Array(数组)相关方法简述
2009/07/25 Javascript
JS+CSS实现下拉列表框美化效果(3款)
2015/08/15 Javascript
JS实现的DIV块来回滚动效果示例
2017/02/07 Javascript
JavaScript中object和Object的区别(详解)
2017/02/27 Javascript
Element-UI踩坑之Pagination组件的使用
2018/10/29 Javascript
vue axios封装及API统一管理的方法
2019/04/18 Javascript
Angular value与ngValue区别详解
2019/11/27 Javascript
Vue列表循环从指定下标开始的多种解决方案
2020/04/08 Javascript
VUE和Antv G6实现在线拓扑图编辑操作
2020/10/28 Javascript
在Python的循环体中使用else语句的方法
2015/03/30 Python
Python的Django框架中的数据库配置指南
2015/07/17 Python
Python中的super()方法使用简介
2015/08/14 Python
python 检查是否为中文字符串的方法
2018/12/28 Python
Python遍历文件夹 处理json文件的方法
2019/01/22 Python
PyQt5实现五子棋游戏(人机对弈)
2020/03/24 Python
彻底理解Python中的yield关键字
2019/04/01 Python
python 实现批量替换文本中的某部分内容
2019/12/13 Python
基于python实现地址和经纬度转换
2020/05/19 Python
python seaborn heatmap可视化相关性矩阵实例
2020/06/03 Python
美国鲜花递送:UrbanStems
2021/01/04 全球购物
优秀班集体先进事迹材料
2014/05/28 职场文书
英语分层教学实施方案
2014/06/15 职场文书
校运会口号
2014/06/18 职场文书
优秀共产党员演讲稿
2014/09/04 职场文书
教师工作自我鉴定范文
2014/09/14 职场文书
无犯罪记录证明样本
2015/06/16 职场文书
生活委员竞选稿
2015/11/21 职场文书
Python基础之数据类型知识汇总
2021/05/18 Python
mysql分组后合并显示一个字段的多条数据方式
2022/01/22 MySQL