Everfighting
11/29/2017 - 7:17 AM

在数据库内部查询大批量数据

在数据库内部查询大批量数据,条件uid来自多超多行csv文件。

#!/usr/bin/env python                                                                                                                             
# encoding: utf-8                                                                   
                                                                                    
import pandas as pd                                                                 
import psycopg2                                                                     
conn_dv = psycopg2.connect(database="", user="", password="")
                                                                                    
def read_csv(filename):                                                                     
    usr_list = pd.read_csv(filename)                                       
    res  = []                                                                       
    for i in usr_list.values:                                                       
        res.extend(i)                                                               
    return tuple(res)                                                               
                                                                                    
                                                                                    
def sql_to_csv(sql, conn):                                                          
    df = pd.read_sql_query(sql, conn)                                               
    df.to_csv('tmp.csv', index=False)                                               
    return df                                                                       
                      
                                                                                    
if __name__=='__main__':                                                            
    usr_ids = read_csv('name.csv')                                                            
    sql = """select * from usr_info where cust_id = {uid};""".format(uid=usr_ids)
    df = sql_to_csv(sql, conn_dv)