python连接mysql

故事扮演 提交于 2019-12-07 23:44:29

 

from __future__ import division
import pymysql

coon = pymysql.connect(
    host = '',user = 'r',passwd = 'bQ',
    port =,db = '',charset = ''
)

f =file('ddd.txt')
i = 0 
p_id_dic = {}
lists = []
cur = coon.cursor()
for line in f:
    i += 1
    chunk = line.strip().split('\t')
    p_id = chunk[0]
    price = round(float(chunk[2]) / 1, 2)
   
    p_id_dic[int(p_id)] = price
    lists.append(p_id)
    if i % 1000:
        sql = "select p_id, pay_price,total_price from map_ugc_core.task_package where p_id in (%s)" % ','.join(lists)
        cur.execute(sql)
        res = cur.fetchall()
        for db_pid, db_price,total_price in res:
            if abs(db_price - p_id_dic.get(db_pid)) > 0.01:
                print db_pid, db_price, p_id_dic.get(db_pid), db_price - p_id_dic.get(db_pid), total_price
                lists = []
    if lists:
        for db_pid, db_price, total_price in res:
            if abs(db_price - p_id_dic.get(db_pid)) > 0.01:
                print db_pid, db_price, p_id_dic.get(db_pid), db_price - p_id_dic.get(db_pid), total_price

cur.close()
coon.close()

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!