def post(self): ret = { "code":200, "msg":"", "data":[], } body = self.request.body params = json.loads(body) dbType = params.get("dbType") dbName = params.get("dbName") dbHost = params.get("dbHost") dbUsername = params.get("dbUsername") dbPassword = params.get("dbPassword") dbPort = params.get("dbPort") db = DBUtil() sql = """SELECT 1 FROM rq_db_configs WHERE cname = '%s' AND host = '%s' AND port = '%s' AND dbname = '%s' """ % (dbType, dbHost, dbPort, dbName) res = db.fetch_all(sql) if res: ret["code"] = 400 ret["msg"] = "数据源已经存在" else: sql = """INSERT INTO rq_db_configs(`cname`, `host`, `port`, `dbname`, `username`, `password`) VALUES('%s', '%s', '%s', '%s', '%s', '%s')""" % (dbType, dbHost, dbPort, dbName, dbUsername, dbPassword) db.execute(sql) self.write(json.dumps(ret))
def post(self, *args, **kwargs): #1 获取客户端提交的内容 name = self.get_argument('username') password = self.get_argument('password') city = self.get_argument('city') avatar = self.request.files.get('avatar', None) #2 判断内容的有效性(判空) if name and password and city: #3 处理头像图片,获得存储头像图片的名称 avatar_file = None #头像图片存储的名称 if avatar: file = avatar[0] #httpFile对象 avatar_file = str(time.time()) + file.filename avatar_body = file.body writer = open('mystatic/images/{}'.format(avatar_file), 'wb') writer.write(avatar_body) writer.close() # 4 写入数据库 pwd = md5(password) # params = (name,pwd,city,avatar_file) # config={ # 'host':'127.0.0.1', # 'port':3306, # 'user':'******', # 'password':'******', # 'database':'blogdb', # 'charset':'utf8' # } # try: # connection = pymysql.connect(**config) # cursor = connection.cursor() # sql='insert into ' \ # 'tb_user(user_name,user_password,user_city,user_avatar)' \ # ' values(%s,%s,%s,%s)' # cursor.execute(sql,params) # cursor.connection.commit()#提交修改 # self.redirect('/') try: dbutil = DBUtil() params = dict(name=name, password=pwd, city=city, avatar_file=avatar_file) dbutil.saveuser(**params) self.redirect('/') #5 处理错误 except Exception as e: print(e) #(1062, "Duplicate entry 'abc' for key 'user_name'") if avatar_file: if exists('mystatic/images/' + avatar_file): remove('mystatics/images/' + avatar_file) self.redirect('/register?regsit=dberror&error=dbfail') else: self.redirect('/register?regist=fail&error=reinput')
def get(self): db = DBUtil() sql = """select id, cname from rq_db_configs""" data = db.fetch_all(sql) ret = { "code":200, "msg":"", "data":data, } self.write(json.dumps(ret))
def __init__(self,handlers,modules,tpath,spath): super().__init__(handlers=handlers,ui_modules=modules,template_path=tpath,static_path=spath) self.db = DBUtil()