-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.py
114 lines (95 loc) · 4.49 KB
/
crud.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from database import Databases
class CRUD(Databases):
def insertDB(self, table, columns, values):
query = f"INSERT INTO {table} ({columns}) VALUES (%s, %s, %s)"
self.cursor.execute(query, values)
self.commit()
def readDB(self, table, column):
sql = " SELECT {column} from {table}".format(column=column, table=table)
try:
self.cursor.execute(sql)
result = self.cursor.fetchall()
except Exception as e:
result = (" read DB err", e)
return result
def updateDB(self, table, column, value, condition):
sql = " UPDATE {table} SET {column}='{value}' WHERE {column}='{condition}' ".format(table=table,
column=column,
value=value,
condition=condition)
try:
self.cursor.execute(sql)
self.db.commit()
except Exception as e:
print(" update DB err", e)
def deleteDB(self, table, condition):
sql = " delete from {table} where {condition} ; ".format(table=table,
condition=condition)
try:
self.cursor.execute(sql)
self.db.commit()
except Exception as e:
print("delete DB err", e)
def insertPressDB(self, title, content, original_url, published_at, image_url, total_content_line, authors,
language, publisher, access_level, category):
sql = ("INSERT INTO press(title, content, original_url, published_at, image_url, total_content_line, author,"
" language, publisher, access_level, category) VALUES "
"(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id;")
try:
self.cursor.execute(sql, (
title, content, original_url, published_at, image_url, total_content_line, authors, language, publisher,
access_level, category))
self.db.commit()
return self.cursor.fetchone()[0]
except Exception as e:
print(" insert DB err: ", e)
def check_exist_url(self, original_url):
sql = "SELECT * from press where original_url = %s"
try:
self.cursor.execute(sql, (original_url,))
result = self.cursor.fetchall()
return result
except Exception as e:
print(" read DB err: ", e)
def insertPressContentDB(self, press_id, line_number, content):
sql = "INSERT INTO press_content_line(press_id, line_number, line_text) VALUES (%s, %s, %s);"
try:
self.cursor.execute(sql, (press_id, line_number, content))
self.db.commit()
except Exception as e:
print(" insert DB err: ", e)
# 문장 끝에 특정 문자가 있는 데이터만 선택
def selectPressContentLine(self):
sql = "SELECT id, line_text, translated_line_text FROM press_content_line WHERE line_text LIKE '%!@!'"
try:
self.cursor.execute(sql)
result = self.cursor.fetchall()
return result
except Exception as e:
print("Read DB Error: ", e)
def updatePressContentLine(self, line_id, line_text, translated_line_text):
sql = "UPDATE press_content_line SET line_text = %s, translated_line_text = %s WHERE id = %s"
try:
self.cursor.execute(sql, (line_text, translated_line_text, line_id))
self.db.commit()
except Exception as e:
print("Update DB Error: ", e)
def update_press_content(self):
# 모든 press 레코드 가져오기
self.cursor.execute("SELECT id, content FROM press")
press_records = self.cursor.fetchall()
for record in press_records:
press_id, content = record
# 기존 content를 3줄로 자르기
trimmed_content = trim_content_to_three_lines(content)
# content 업데이트
self.cursor.execute(
"UPDATE press SET content = %s WHERE id = %s",
(trimmed_content, press_id)
)
# 변경사항 커밋
self.db.commit()
print("Press content updated successfully")
def trim_content_to_three_lines(content):
lines = content.split('\n')
return '\n'.join(lines[:3])