码迷,mamicode.com
首页 > 数据库 > 详细

redis数据库的数据导入到SQLServer数据库中

时间:2017-11-11 22:10:10      阅读:678      评论:0      收藏:0      [点我收藏+]

标签:分享   pytho   int   def   utf8   json   img   tar   style   

技术分享

 

技术分享

 

#!/usr/bin/python
# -*-coding:utf-8-*-
"""
@author: yugengde
@contact: yugengde@163.com
@file : redis_sqlserver.py
@time: 2017/11/11 16:50
"""

import redis
import pymssql
import json
from multiprocessing import Pool

MSSQL_URL = ‘**.**.**.**‘
MSSQL_USER = ‘sa‘
MSSQL_PASSWD = ‘****‘
MSSQL_DB = ‘amazon‘
MSSQL_CHARSET = ‘utf8‘

r = redis.Redis(host=‘**.**.**.**‘,password=‘******‘)


def insert_sqlserver(item):
# print(‘start insert...‘)
item = json.loads(item)
conn = pymssql.connect(host=MSSQL_URL,user=MSSQL_USER,password=MSSQL_PASSWD,database=MSSQL_DB,charset=MSSQL_CHARSET)
cursor = conn.cursor()
# 数据库插入
sql = "insert into amazon(\
title,brand,classify,sku,price,\
comment_num,comment_av,product_attr,promotion,product_desc,\
product_info,url,save_time,hash_value\
) VALUES (%s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s,%s)"
try:
cursor.execute(sql, (item[‘title‘],item[‘brand‘],item[‘classify‘],item[‘sku‘],item[‘price‘], \
item[‘comment_num‘],item[‘comment_av‘],item[‘product_attr‘],item[‘promotion‘],item[‘product_desc‘], \
item[‘product_info‘],item[‘url‘],item[‘save_time‘],item[‘hash_value‘],))
conn.commit()
# print(‘==================================================================‘)
# print(‘数据存储成功‘, item)
# print(‘==================================================================‘)
except:
print(‘==================================================================‘)
print(‘error‘,item)
print(‘==================================================================‘)
cursor.close()
conn.close()

if __name__ == "__main__":

for i in range(100):
items = r.lrange(‘amaz:items‘,start=1000*i,end=1000*(i+1))
pool = Pool()
pool.map(insert_sqlserver,items)

redis数据库的数据导入到SQLServer数据库中

标签:分享   pytho   int   def   utf8   json   img   tar   style   

原文地址:http://www.cnblogs.com/liyugeng/p/7820014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!