python优雅实现批量数据入库

1、如果与数据库交互比较频繁的话,建议使用数据库连接池,示例如下(使用的是python2.7):

from psycopg2.pool import SimpleConnectionPool

from contextlib import contextmanager

from psycopg2 import extras

# 数据库连接池初始化

gppool = SimpleConnectionPool(1, 5, dbname="db_name",

                              user="user",

                              password="pwd",

                              host="192.168.192.168",

                              port="5432")

@contextmanager

def get_cursor():

conn = gppool.getconn()

# 默认自动提交

    conn.autocommit =True

    try:

yield conn.cursor()

finally:

# 放回连接池

        gppool.putconn(conn)

# 查询数据

def get_data(sql_):

conn = gppool.getconn()

cursor = conn.cursor()

try:

cursor.execute(sql_)

finally:

gppool.putconn(conn)

return cursor

# 批量操作

def save_data(dict_list, sql):

# 批量插入

    with get_cursor()as cursor:

extras.execute_batch(cursor, sql, dict_list)

# 关闭连接池

def close_pool():

gppool.closeall()

if __name__ =="__main__":

dict_list = [{"annex":"http://www.zbc.com", "area":"北京"}, {"annex":"http://www.baidu.com/123", "area":"中国"}]

sql ='insert into t_baseinfo(annex,area) values (%(annex)s,%(area)s)'

    save_data(dict_list, sql)

close_pool()

你可能感兴趣的:(python优雅实现批量数据入库)