-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
74 lines (61 loc) · 2.78 KB
/
db.py
File metadata and controls
74 lines (61 loc) · 2.78 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
import mysql.connector
from mysql.connector import MySQLConnection
from configparser import ConfigParser
# filename = 'db_config.ini'
def read_db_config(filename='db_config.ini', section='mysql'):
parser = ConfigParser()
parser.read(filename)
db = {}
if parser.has_section(section):
items = parser.items(section)
for key, val in items:
db[key] = val
else:
raise Exception("%s not found in the file %s") % (section, filename)
return db
class DB:
def __init__(self, host=None, database=None, user=None, password=None, filename='db_config.ini'):
# if host is None or database is None or user is None or password is None:
# raise Exception('Enter all needed data for connection')
# else:
# self.connect = mysql.connector.connect(host=host,
# database=database,
# user=user,
#
# self.connect = mysql.connector.connect(host=host,
# database=database,
# user=user,
# password=password)
db_config = read_db_config()
self.connect = MySQLConnection(**db_config)
if self.connect.is_connected():
self.cursor = self.connect.cursor()
else:
Exception("incorrect data")
def fetch_table(self):
return self.cursor.execute("SELECT * FROM main;")
def fetch_row(self):
self.cursor.execute("SELECT * FROM main;")
return self.cursor.fetchone()
# def fetch_post_text(self, post_id):
# query = "SELECT post_text FROM main WHERE id=%s;" % post_id
# return self.cursor.fetchone(query)
# def insert_post_text(self, post_id, user_token, user_secret, post_text):
# query = "INSERT INTO main(id, token, secret, post_text) VALUES (%s,%s, %s, %s);" % \
# (post_id, user_token, user_secret, post_text)
# return self.cursor.execute(query)
def insert_post_id(self, post_id, user_token, user_secret):
query = "INSERT INTO main(id, token, secret) VALUES ('%s', '%s', '%s');" % \
(post_id, user_token, user_secret)
self.cursor.execute(query)
self.connect.commit()
return True
def fetch_post_ids(self, user_token, user_secret):
query = "SELECT id FROM main WHERE token = '%s' AND secret = '%s' ;" % (user_token, user_secret)
self.cursor.execute(query)
return self.cursor.fetchall()
def delete_post_id(self, post_id):
query = "DELETE FROM main WHERE id = '%s';" % post_id
self.cursor.execute(query)
self.connect.commit()
return True