import pymysql class SQLHelper: def __init__(self): self.conn_dict = dict(host="192.168.1.81",port=3306,user="root",passwd="admin",db="chatCN",charset="utf8") def query_userid_by_username(self,username): try: conn = pymysql.connect(**self.conn_dict) sql = "select id from users where username='%s'"%username parameters = (username) cur = conn.cursor() cur.execute(sql) #cur.execute(sql,username) results = cur.fetchall() conn.commit() if len(results): return results[0][0] else : return -1 except Exception as e: raise e finally: cur.close() conn.close() def update_action_text_by_userid(self,userid,action_str): try: conn = pymysql.connect(**self.conn_dict) sql = "update units set text = '%s' where name='actions' and user_id=%d"%(action_str,userid) cur = conn.cursor(cursor=pymysql.cursors.DictCursor) effect_row = cur.execute(sql) conn.commit() return effect_row except Exception as e: raise e finally: cur.close() conn.close() ''' def create_new_bot(self,userName,botName) try: conn = pymysql.connect(**self.conn_dict) sql = "INSERT INTO `users` (`id`, `username`, `nickname`, `password`, `firstname`, `lastname`, `status`, `action`, `timestamp`, `email`, `session_key`, `title`, `description`, `category`, `subcategory`, `welcome`, `terminal`, `url`, `website`, `icon`, `volume`, `user_interactions`, `user_chats`, `active_link`) VALUES (NULL, %s, 'cc', 'abc123', ' ', ' ', 'active', '', CURRENT_TIMESTAMP, ' abc@qiwu.ai', ' ', ' ', '', ' ', ' ', ' ', ' ', '', ' ', ' ', '0', '0', '0', '')"%(userName+"asdfghg"+"botName") cur = conn.cursor() sta = cur.execute(sql) conn.commit() if sta == 1: return 1 else: return 0 except Exception as e: raise e finally: conn.close() cur.close() def insert_action_text_by_userid(self,userid,action_str): try: conn = pymusql.connect(**self.conn_dict) sql = "INSERT INTO `units` (`id`, `user_id`, `name`, `description`, `status`, `timestamp`, `text`, `xml`) VALUES (NULL, %d, 'actions\r\n', '', 'ready', CURRENT_TIMESTAMP, 'xxx', '')" '''