linux python脚本学习 连接redis,和数据库

版本:
python 2.7
前提条件
pip install MySQL-python
pin install redis

 #!/usr/bin/python
 #-*- coding: UTF-8 -*-

import redis
import MySQLdb

#打开数据库连接
db  =  MySQLdb.connect("ip","username","password","database",charset='utf8')
#连接redis
r = redis.StrictRedis(host='ip',port=6379,db=0)
cursor = db.cursor()
cursor.execute("SELECT id from api_material")

data = cursor.fetchall()
for row in data:
   name = row[0]
   sql = "select flower from user where id = '%s'" % name;
   flower = cursor.execute(sql)
   print("flower: %s" % flower)
   r.zincrby(name='自己的key',value=name,amount=flower)
   print("公会id : %s" % name)
cursor.close()
#关闭数据库连接
db.close()

你可能感兴趣的:(database)