# coding:utf8 import pymysql import json class SQLHelper_basic(object): def __init__(self): self.conn_dict = dict(host="192.168.1.81", port=3306, user="root", passwd="admin", charset="utf8") # self.conn_dict = dict(host="http://qcloud-sh1.chewrobot.com", 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: print repr(e) raise e finally: cur.close() conn.close() def query_username_by_userid(self, userid): try: conn = pymysql.connect(**self.conn_dict) sql = "select username from users where id=%d" % int(userid) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() if len(results): return results[0][0] else: return "error" except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_keyword_unit_by_userid(self, userid): try: conn = pymysql.connect(**self.conn_dict) sql = "select text from units where user_id=%d and name='keywords'" % int(userid) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() if len(results): return results[0][0] else: return "error" except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def update_keyword_unit_by_userid(self, userid, keyword_str): try: conn = pymysql.connect(**self.conn_dict) sql = "update units set text = '%s' where name='keywords' and user_id=%d" % (keyword_str, int(userid)) cur = conn.cursor(cursor=pymysql.cursors.DictCursor) effect_row = cur.execute(sql) conn.commit() return effect_row except Exception as e: print repr(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, int(userid)) cur = conn.cursor(cursor=pymysql.cursors.DictCursor) effect_row = cur.execute(sql) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def update_url(self, bot_id, url): try: conn = pymysql.connect(**self.conn_dict) sql = "update users set url = '%s' where id= %d" % (url, bot_id) cur = conn.cursor(cursor=pymysql.cursors.DictCursor) effect_row = cur.execute(sql) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def insert_data_by_id(self, botId, action_data, config_data): try: conn = pymysql.connect(**self.conn_dict) sql = "INSERT INTO user_data (botId,action,config) VALUES (%d, '%s', '%s')" % (int(botId), action_data, config_data) cur = conn.cursor() sta = cur.execute(sql) conn.commit() if sta == 1: return 1 else: return 3 except Exception as e: repr(e) raise e finally: conn.close() cur.close() def update_action_by_id(self, botId, action_data): try: conn = pymysql.connect(**self.conn_dict) sql = "update user_data set action = '%s' where botId=%d" % (action_data, int(botId)) cur = conn.cursor(cursor=pymysql.cursors.DictCursor) effect_row = cur.execute(sql) conn.commit() if effect_row == 1: return 1 else: return 3 except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def update_config_by_id(self, botId, config_data): try: conn = pymysql.connect(**self.conn_dict) sql = "update user_data set config = '%s' where botId=%d" % (config_data, int(botId)) cur = conn.cursor(cursor=pymysql.cursors.DictCursor) effect_row = cur.execute(sql) conn.commit() if effect_row == 1: return 1 else: return 3 except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_data_by_id(self, botId): try: conn = pymysql.connect(**self.conn_dict) sql = "select action,config from user_data where botId=%d" % int(botId) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() if len(results): return results[0][0], results[0][1] else: return "", "" except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_config_list_by_category_id(self, category_id): try: conn = pymysql.connect(**self.conn_dict) sql = "select configData from chatbot_config where categoryId = %d" % int(category_id) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() config_list = [] if len(results): for result in results: config_list.append(json.loads(result[0])) return config_list except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_condition_by_botId(self, botId): try: conn = pymysql.connect(**self.conn_dict) sql = "select conditionData, actionText, conditionId from chatbot_condition where botId = %d" % int(botId) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_active_condition(self, botId): try: conn = pymysql.connect(**self.conn_dict) sql = "select conditionId, conditionData, categoryInfo, actionText from chatbot_condition where active = 1 and botId = %s" % str(botId) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_condition_by_2Id(self, botId, conditionId): try: conn = pymysql.connect(**self.conn_dict) sql = "select conditionData, actionText, categoryInfo from chatbot_condition where botId = %s and conditionId = %d" %(str(botId), int(conditionId)) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_condition_by_category(self, botId, categoryStr): try: conn = pymysql.connect(**self.conn_dict) sql = "select conditionData, actionText, conditionId from chatbot_condition where botId = %s and categoryInfo = '%s'" %(str(botId), categoryStr) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_condition_category_by_id(self, botId): try: conn = pymysql.connect(**self.conn_dict) sql = "select categoryInfo from chatbot_condition where botId = %s" % str(botId) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def insert_condition_by_botid(self, botId, conditionData, actionText, categoryStr, active): try: conn = pymysql.connect(**self.conn_dict) sql = "insert into chatbot_condition (botId, conditionData, actionText, categoryInfo, active) VALUES ('%s', '%s', '%s' , '%s', %s)" % (str(botId), conditionData, actionText, categoryStr, active) cur = conn.cursor() effect_row = cur.execute(sql) insert_id = int(cur.lastrowid) conn.commit() return effect_row, insert_id except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def update_condition_by_2id(self, botId, conditionId, conditionData, actionText, categoryStr, active): try: conn = pymysql.connect(**self.conn_dict) sql = "update chatbot_condition set conditionData = '%s', actionText = '%s', categoryInfo = '%s', active = %s where botId = %s and conditionId=%s" % (conditionData, actionText, categoryStr, active, botId, conditionId) cur = conn.cursor(cursor=pymysql.cursors.DictCursor) effect_row = cur.execute(sql) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def delete_condition_by_2id(self, botId, conditionId): try: conn = pymysql.connect(**self.conn_dict) sql = "delete from chatbot_condition where botId = %s and conditionId = %s" % (botId, conditionId) cur = conn.cursor() effect_row = cur.execute(sql) conn.commit() return effect_row except Exception as e: print repr(e) raise e ''' def insert_config_by_2id(self, bot_id, config_id, category_id, config_data): try: conn = pymysql.connect(**self.conn_dict) sql = "INSERT INTO user_data (botId,action,config) VALUES (%d, '%s', '%s')" % (int(botId), action_data, config_data) cur = conn.cursor() sta = cur.execute(sql) conn.commit() if sta == 1: return 1 else: return 3 except Exception as e: repr(e) raise e finally: conn.close() cur.close() ''' def delete_config_by_2id(self, botId, configId): try: conn = pymysql.connect(**self.conn_dict) sql = "delete from chatbot_config where botId = %d and configId = %d" %(int(botId), int(configId)) cur = conn.cursor() effect_row = cur.execute(sql) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def insert_config_by_2id(self, botId, categoryId, configData, configName): try: conn = pymysql.connect(**self.conn_dict) sql = "INSERT INTO chatbot_config (botId, categoryId, configData, configName) VALUES (%d, %d, '%s', '%s')" % (int(botId), int(categoryId), configData, configName) cur = conn.cursor() effect_row = cur.execute(sql) insert_id = int(cur.lastrowid) conn.commit() return effect_row, insert_id except Exception as e: print repr(e) raise e finally: conn.close() cur.close() def update_config_by_2id(self, botId, configId, configData, categoryId, configName): try: conn = pymysql.connect(**self.conn_dict) sql = "update chatbot_config set configData = '%s' ,categoryId = %s, configName = '%s' where botId=%d and configId=%d" % (configData, str(categoryId), configName, int(botId), int(configId)) cur = conn.cursor(cursor=pymysql.cursors.DictCursor) effect_row = cur.execute(sql) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_config_by_2id(self, botId, categoryId): try: conn = pymysql.connect(**self.conn_dict) sql = "select configId, configData ,categoryId, configName from chatbot_config where botid=%d and categoryId = %d" %(int(botId), int(categoryId)) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return 1, results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_config_by_configId(self, botId, configId): try: conn = pymysql.connect(**self.conn_dict) sql = "select configId, configData ,categoryId, configName from chatbot_config where botid=%d and configId = %d" %(int(botId), int(configId)) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return 1, results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_category_by_botId(self, botId): try: conn = pymysql.connect(**self.conn_dict) sql = "select categoryId, categoryParentId, categoryName from config_categories where botid=%d order by categoryParentId asc" %int(botId) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def insert_category_by_2id(self, botId, categoryParentId, categoryName): try: conn = pymysql.connect(**self.conn_dict) sql = "INSERT INTO config_categories (botId, categoryParentId, categoryName, categoryStatus) VALUES (%d, %d, '%s', '%s')" % (int(botId), int(categoryParentId), categoryName, "avaliable") cur = conn.cursor() effect_row = cur.execute(sql) insert_id = int(cur.lastrowid) print "insert_id", insert_id conn.commit() return effect_row, insert_id except Exception as e: print repr(e) raise e finally: conn.close() cur.close() def update_category_by_2id(self, botId, categoryId, newCategoryName): try: conn = pymysql.connect(**self.conn_dict) sql = "update config_categories set categoryName = '%s' where botId = %d and categoryId = %d" % (newCategoryName, int(botId), int(categoryId)) cur = conn.cursor(cursor=pymysql.cursors.DictCursor) effect_row = cur.execute(sql) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def batch_delete_category(self, botId, categoryIdList): ''' 批量删除分类 ''' args = [(botId, categoryId) for categoryId in categoryIdList] try: conn = pymysql.connect(**self.conn_dict) sql = "delete from config_categories where botId = %s and categoryId = %s" cur = conn.cursor() effect_row = cur.executemany(sql, args) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def batch_delete_config(self, botId, categoryIdList): ''' 批量删除配置文件 ''' args = [(botId, categoryId) for categoryId in categoryIdList] try: conn = pymysql.connect(**self.conn_dict) sql = "delete from chatbot_config where botId = %s and categoryId = %s" cur = conn.cursor() effect_row = cur.executemany(sql, args) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def batch_insert_category(self, botId, categoryData): try: conn = pymysql.connect(**self.conn_dict) sql = "insert into config_categories (botId, categoryId, categoryParentId, categoryName, categoryStatus) values(%s, %s, %s, '%s','%s')" cur = conn.cursor() effect_row = cur.executemany(sql, categoryData) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def clear_user_data(self, botId): try: conn = pymysql.connect(**self.conn_dict) sql1 = "delete from chatbot_config where botId = %s;"% str(botId) sql2 = "delete from chatbot_condition where botId = %s;"% str(botId) sql3 = "delete from config_categories where botId = %s;" % str(botId) cur = conn.cursor() effect_row = cur.execute(sql1) effect_row = cur.execute(sql2) effect_row = cur.execute(sql3) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def batch_insert_config(self, botId, configData): try: conn = pymysql.connect(**self.conn_dict) sql = "insert into chatbot_config (botId, configId, categoryId, configData, configName) values(%s) " sql = sql % (configData[0]) cur = conn.cursor() effect_row = cur.executemany(sql, configData) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def batch_insert_condition(self, botId, conditionData): try: conn = pymysql.connect(**self.conn_dict) sql = "insert into chatbot_condition (botId, conditionId, conditionData, categoryInfo, actionText, active) values(%s,%s,'%s','%s','%s',%s) " cur = conn.cursor() effect_row = cur.executemany(sql, conditionData) conn.commit() return effect_row except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_category_all_by_botId(self, botId): try: conn = pymysql.connect(**self.conn_dict) sql = "select * from config_categories where botid=%d order by categoryParentId asc" %int(botId) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_config_all_by_botId(self, botId): try: conn = pymysql.connect(**self.conn_dict) sql = "select * from chatbot_config where botId = %d" % int(botId) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_condition_all_by_botId(self, botId): try: conn = pymysql.connect(**self.conn_dict) sql = "select * from chatbot_condition where botId = %d" % int(botId) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def query_sleeping_config_by_botId(self, botId): try: conn = pymysql.connect(**self.conn_dict) sql = "select configId, configName from chatbot_config where configName != '' and botId = %s" % (str(botId)) cur = conn.cursor() cur.execute(sql) results = cur.fetchall() conn.commit() return 1, results except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def reload_condition_by_botid(self, botId, conditionId, conditionData, actionText, categoryStr, active): try: conn = pymysql.connect(**self.conn_dict) sql = "insert into chatbot_condition (botId, conditionId, conditionData, actionText, categoryInfo, active) VALUES ('%s', %s,'%s', '%s' , '%s', %s)" % (str(botId), conditionId,conditionData, actionText, categoryStr, active) cur = conn.cursor() effect_row = cur.execute(sql) insert_id = int(cur.lastrowid) conn.commit() return effect_row, insert_id except Exception as e: print repr(e) raise e finally: cur.close() conn.close() def reload_config_by_2id(self, botId, configId, categoryId, configData, configName): try: conn = pymysql.connect(**self.conn_dict) sql = "INSERT INTO chatbot_config (botId, configId, categoryId, configData, configName) VALUES (%d, %d, %d, '%s', '%s')" % (int(botId), int(configId),int(categoryId), configData, configName) cur = conn.cursor() effect_row = cur.execute(sql) insert_id = int(cur.lastrowid) conn.commit() return effect_row, insert_id except Exception as e: print repr(e) raise e finally: conn.close() cur.close() def reload_category_by_2id(self, botId, categoryId, categoryParentId, categoryName): try: conn = pymysql.connect(**self.conn_dict) sql = "INSERT INTO config_categories (botId, categoryId, categoryParentId, categoryName, categoryStatus) VALUES (%d, %d, %d, '%s', '%s')" % (int(botId), int(categoryId),int(categoryParentId), categoryName, "avaliable") cur = conn.cursor() effect_row = cur.execute(sql) insert_id = int(cur.lastrowid) print "insert_id", insert_id conn.commit() return effect_row, insert_id except Exception as e: print repr(e) raise e finally: conn.close() cur.close() class SQLHelper_chat(SQLHelper_basic): ''' 用于在操作chatCN数据库上的数据 ''' def __init__(self): super(SQLHelper_chat, self).__init__() self.conn_dict["db"] = "chatCN" class SQLHelper_data(SQLHelper_basic): ''' 用于保存和修改用户数据 ''' def __init__(self): super(SQLHelper_data, self).__init__() self.conn_dict["db"] = "SimplifiedBot" ''' 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', '')" '''