python连接mysql

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()

相关推荐