python与各数据库的交互
from redis import StrictRedis
from pymongo import MongoClient
import pymysql
#redis客户端
redis_cli = StrictRedis(host='localhost',decode_responses=1)
#mongo客户端
client = MongoClient()
db=client.loacl
collenction=db.stu
#MySQL客户端
mysql_conn=pymysql.connect(host='localhost',port=3306,user='root',password='123456',database='python')
mysql_cursor = mysql_conn.cursor()
sql2='select * from yy;'
# sql1 = """insert into yy values ('bb');"""
mysql_cursor.execute(sql2)
data = mysql_cursor.fetchone()
print(data,type(data))
redis_cli.execute_command('flushall')
a = redis_cli.set('name',str(data))
print(a)
re = redis_cli.get('name')
print(re,type(re))
redis_cli.lpush('list',123,123,534,567,678)
s = redis_cli.lrange('list',0,-1)
print(s,type(s))
collenction.save({'name':s})
redis
#创建strictredis对象,与服务器链接
sr = StrictRedis(decode_responses=1)
# 添加键
s1 = sr.set('name','yy')
print(s1) >> 成功添加返回True,失败返回False
# 查找
s1_get = sr.get('name')
添加键对应的值,如果键存在则返回对应的值,如果键不存在则返回None
print(s1_get) >> yy
#删除
result = sr.delete('name')
print(result) 输出响应结果,如果删除成功则返回受影响的键数,否则则返回0
获取所有的键
result=sr.keys()
输出响应结果,所有的键构成1个列表,如果没有键则返回空列表
StrictRedis中的方法
string
set
setex
mset
append
get
mget
key
keys
exists
type
delete
expire
getrange
ttl
hash
hset
hmset
hkeys
hget
hmget
hvals
hdel
list
lpush
rpush
linsert
lrange
lset
lrem
set
sadd
smembers
srem
zset
zadd
zrange
zrangebyscore
zscore
zrem
zremrangebyscore
oracle
import cx_Oracle
注意sid,有时候需要使用本地监听器调用