pg_db_func.py 1.1 KB
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2020/7/12 16:27
# @Author:bxh
# @file: pg_db_func.py
import psycopg2


# 获得连接
def GetConnect():
    conn = False
    try:
        conn = psycopg2.connect(database="partner_test",
                                user="bianxinhua",
                                password="bianxinhua_123",
                                host="114.55.200.59",
                                port="31543")
    except Exception as err:
        print("连接数据库失败,%s" % err)
    return conn


# 清除数据
def setup_hook_clean_db(companyid):
    """
    初始化时清理数据库中对应公司的历史数据
    :return:
    """
    db = GetConnect()
    cursor = db.cursor()
    companyid = str(companyid)

    try:
        cursor.execute('delete from order_base where company_id=' + companyid)
        db.commit()
        cursor.execute('delete from order_good where company_id=' + companyid)
        db.commit()
        print("delete OK")
    except Exception as err:
        # 发生错误时回滚
        print("this is:", err)
        db.rollback()
    db.close()