Commit 62724dba authored by aoh's avatar aoh
Browse files

Add db.py


Signed-off-by: aoh's avataraoh <aoh@lysator.liu.se>
parent 62e598ae
import pymysql
class DB():
def __init__(self):
pass
def __enter__(self):
self.connection = pymysql.connect(host='lysns.lysator.liu.se',
user='lysns',
password='hunter2',
db='lysns',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
return self
def __exit__(self, type, value, traceback):
self.connection.close()
def get_user_ids(self):
with self.connection.cursor() as cursor:
cursor.execute('SELECT id FROM domain')
return [item.get("id") for item in cursor.fetchall()]
#!/usr/bin/env python3
import pymysql.cursors
from db import DB
connection = pymysql.connect(host='lysns.lysator.liu.se',
user='lysns',
......@@ -28,3 +29,7 @@ try:
finally:
connection.close()
with DB() as d:
print(d.get_user_ids())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment