Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql.cursors
- import re
- class bd:
- def __init__(self):
- print("CREATE")
- self.connectMain = pymysql.connect(host='localhost',
- user='root',
- password='root',
- unix_socket="/Applications/MAMP/tmp/mysql/mysql.sock",
- db='bot',
- port=8889,
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- self.cursor = self.connectMain.cursor()
- def __del__(self):
- print("DeLETE")
- self.cursor.close()
- self.connectMain.close()
- def query(self,sql,all=0):
- self.cursor.execute(sql)
- self.connectMain.commit()
- if all != 0:
- result = self.cursor.fetchall()
- else:
- result = self.cursor.fetchone()
- self.connectMain.cursor()
- return result
- def getRandom(self,word):
- try:
- sql = "select a from answer where q like '%{0}%' LIMIT 100".format(word)
- print("SELECT answer: ", sql)
- return self.query(sql,1)
- except:
- sql = "select a from answer where q like 'а' LIMIT 100".format(word)
- print("SELECT answer: ", sql)
- return self.query(sql, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement