# _*_ coding:utf-8 _*_ import redis import MySQLdb import json def process_item(): # 建立redis數據庫鏈接 rediscli = redis.Redis(host='127.0.0.1', port=6379, db='0') # 建立mysql數據庫鏈接 mysqlcli = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', password='123456', db="DG") offset = 0 while True: # 將數據從redis數據庫中pop出來 source,data = rediscli.blpop("dongguanquestion:items") item = json.loads(data) # 建立mysql操做遊標對象,能夠執行myql語句 cursor = mysqlcli.cursor() cursor.execute("insert into dg (表中的字段名) values(%s ...) " % [item['字段1'], item['字段2'], item['字段3'], item['字段n']]) # 提交事務 mysqlcli.commit() # 關閉遊標 cursor.close() offset += 1 print offset if __name__ == "__main__": process_item()