|
@@ -0,0 +1,117 @@
|
|
1
|
+#!/usr/bin/python
|
|
2
|
+# -*- coding:utf-8 -*-
|
|
3
|
+
|
|
4
|
+import sys
|
|
5
|
+import os
|
|
6
|
+from pgdb import Connection
|
|
7
|
+import json
|
|
8
|
+
|
|
9
|
+sys.path.append(os.path.abspath('..'))
|
|
10
|
+from util.config import config
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+def get_conn():
|
|
14
|
+ conn = Connection(database=config.get('pgsql', 'db'), host=config.get('pgsql', 'host'),
|
|
15
|
+ user=config.get('pgsql', 'user'), password=config.get('pgsql', 'password'))
|
|
16
|
+ return conn
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+def insert(sql):
|
|
20
|
+ try:
|
|
21
|
+ conn = get_conn()
|
|
22
|
+ cur = conn.cursor()
|
|
23
|
+ cur.execute(sql)
|
|
24
|
+ conn.commit()
|
|
25
|
+ except Exception as e:
|
|
26
|
+ print(e)
|
|
27
|
+ finally:
|
|
28
|
+ cur.close()
|
|
29
|
+ conn.close()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+def update(sql):
|
|
33
|
+ try:
|
|
34
|
+ conn = get_conn()
|
|
35
|
+ conn.execute(sql)
|
|
36
|
+ conn.commit()
|
|
37
|
+ except Exception as e:
|
|
38
|
+ print(e)
|
|
39
|
+ finally:
|
|
40
|
+ conn.close()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+def get_rows(sql):
|
|
44
|
+ try:
|
|
45
|
+ conn = get_conn()
|
|
46
|
+ return conn.query(sql)
|
|
47
|
+ except Exception as e:
|
|
48
|
+ print(e)
|
|
49
|
+ finally:
|
|
50
|
+ conn.close()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+def get_total(sql):
|
|
54
|
+ conn = None
|
|
55
|
+ try:
|
|
56
|
+ conn = get_conn()
|
|
57
|
+ result = conn.query(sql)
|
|
58
|
+ return int(result[0]['count'])
|
|
59
|
+ except Exception as e:
|
|
60
|
+ print(e)
|
|
61
|
+ finally:
|
|
62
|
+ conn.close()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+def demo():
|
|
66
|
+ sql = "select aid,other_info FROM articles where aid = '5afa6e5a-9004-4182-a743-87d77c97b451'"
|
|
67
|
+ rows = get_rows(sql)
|
|
68
|
+ for row in rows:
|
|
69
|
+ print(row)
|
|
70
|
+ other_info = row['other_info']
|
|
71
|
+ print(other_info)
|
|
72
|
+ other_info_json = json.loads(other_info)
|
|
73
|
+ print(other_info_json.get('abstract'))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+def demo1():
|
|
77
|
+ sql = "select count(*) FROM articles"
|
|
78
|
+ total = get_total(sql)
|
|
79
|
+ print(total)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+PAGE_SIZE = 50
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+def demo2(offset):
|
|
86
|
+ sql = "select aid,other_info FROM articles order by aid LIMIT %s OFFSET %s"
|
|
87
|
+ update_sql = '''
|
|
88
|
+ UPDATE articles
|
|
89
|
+ set other_info = %s
|
|
90
|
+ WHERE aid = %s
|
|
91
|
+ '''
|
|
92
|
+ rows = get_rows(sql % (PAGE_SIZE, 0))
|
|
93
|
+
|
|
94
|
+ conn = get_conn()
|
|
95
|
+
|
|
96
|
+ for row in rows:
|
|
97
|
+ aid = row['aid']
|
|
98
|
+ other_info = json.loads(row['other_info'])
|
|
99
|
+ if 'abstract' in other_info:
|
|
100
|
+ del other_info['abstract']
|
|
101
|
+ if 'notes' in other_info:
|
|
102
|
+ del other_info['notes']
|
|
103
|
+ if 'content' in other_info:
|
|
104
|
+ del other_info['content']
|
|
105
|
+ print(json.dumps(other_info), aid)
|
|
106
|
+
|
|
107
|
+ conn.execute(update_sql, (json.dumps(other_info), aid))
|
|
108
|
+ conn.close()
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+def demo3():
|
|
112
|
+ for i in range(0, 100, 50):
|
|
113
|
+ demo2(i)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+if __name__ == "__main__":
|
|
117
|
+ demo1()
|