pg_app_db_func.py
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2020/7/12 16:27
# @Author:bxh
# @file: pg_db_func.py
import psycopg2
from datetime import date, timedelta
import datetime,time
# def getMonth():
# year = str(datetime.date.today().year)
# d = datetime.date.today()
# month = '%02d' % d.month
# if month in ['01', '03', '05', '07', '08', '10', '12']:
# s = str("-01 00:00:00")
# e = str("-31 23:59:59")
# startTime = year + "-" + str(month) + s
# endTime = year + "-" + str(month) + e
# list = [startTime, endTime]
# return list
# elif month == '02':
# s = str("-01 00:00:00")
# e = str("-28 23:59:59")
# startTime = year + "-" + str(month) + s
# endTime = year + "-" + str(month) + e
# list = [startTime, endTime]
# return list
# else:
# s = str("-01 00:00:00")
# e = str("-30 23:59:59")
# startTime = year + "-" + str(month) + s
# endTime = year + "-" + str(month) + e
# list = [startTime, endTime]
# return list
# list_time = getMonth()
# def get_starTimestamp():
# return list_time[0]
# def get_endTimestamp():
# return list_time[1]
# 获得连接
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()
# cursor.execute('delete from partner_info where company_id=' + companyid + 'and id <> 1047')
# db.commit()
print("delete OK")
except Exception as err:
# 发生错误时回滚
print("this is:", err)
db.rollback()
db.close()