Advertisement
Guest User

Untitled

a guest
Feb 27th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. import pymysql.cursors
  2. import re
  3.  
  4. class bd:
  5.     def __init__(self):
  6.         print("CREATE")
  7.         self.connectMain = pymysql.connect(host='localhost',
  8.                                      user='root',
  9.                                      password='root',
  10.                                      unix_socket="/Applications/MAMP/tmp/mysql/mysql.sock",
  11.                                      db='bot',
  12.                                      port=8889,
  13.                                      charset='utf8mb4',
  14.                                      cursorclass=pymysql.cursors.DictCursor)
  15.         self.cursor =   self.connectMain.cursor()
  16.  
  17.     def __del__(self):
  18.         print("DeLETE")
  19.         self.cursor.close()
  20.         self.connectMain.close()
  21.  
  22.     def query(self,sql,all=0):
  23.         self.cursor.execute(sql)
  24.         self.connectMain.commit()
  25.  
  26.         if all != 0:
  27.             result = self.cursor.fetchall()
  28.         else:
  29.             result = self.cursor.fetchone()
  30.         self.connectMain.cursor()
  31.         return result
  32.  
  33.     def getRandom(self,word):
  34.         try:
  35.             sql = "select a from answer where q like '%{0}%' LIMIT 100".format(word)
  36.             print("SELECT answer: ", sql)
  37.             return self.query(sql,1)
  38.         except:
  39.             sql = "select a from answer where q like 'а' LIMIT 100".format(word)
  40.             print("SELECT answer: ", sql)
  41.             return self.query(sql, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement