Store data from redis database to local mysql database

# _*_ coding:utf-8 _*_

import redis
import MySQLdb
import json


def process_item():
    # Create a redis database connection
    rediscli = redis.Redis(host='127.0.0.1', port=6379, db='0')
    # create mysql database connection
    mysqlcli = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', password='123456', db="DG")
    offset = 0
    while True:
        # pop data from the redis database
        source,data = rediscli.blpop("dongguanquestion:items")

        item = json.loads(data)
        # Create mysql operation cursor object, you can execute mysql statement
        cursor = mysqlcli.cursor()

        cursor.execute("insert into dg (field name in table) values(%s ...) " % [item['field1'], item['field2'], item['field3'], item['field n']])
        # commit transaction
        mysqlcli.commit()
        # close the cursor
        cursor.close()
        offset += 1
        print offset
if __name__ == "__main__":

    process_item()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325139235&siteId=291194637