paste-server/db.py
2020-05-02 06:37:40 -04:00

53 lines
1.1 KiB
Python

import mysql.connector
import configparser
config = configparser.ConfigParser()
try:
config.read(".config")
except Exception as E:
print(E)
def dbConnect():
mydb = mysql.connector.connect(
host=config["mysql"]["Host"],
user=config["mysql"]["Username"],
passwd=config["mysql"]["Password"],
database=config["mysql"]["Database"]
)
return mydb
def runQuery(query, data=None):
mydb = dbConnect()
c = mydb.cursor()
if data is not None:
c.execute(query, data)
else:
c.execute(query)
if query.lower().startswith("select"):
ret = c.fetchall()
else:
ret = []
mydb.commit()
mydb.close()
return ret
def checkIfPasteExists(id):
query = "SELECT id from pastes where id=%s"
if runQuery(query, (id,)):
return True
return False
def insertPaste(id, paste):
query = "INSERT into pastes (id, paste) values (%s, %s)"
runQuery(query, (id, paste))
def getPaste(id):
query = "select paste from pastes where id=%s"
return runQuery(query, (id,))