def get_all(): # 连接数据库 conn, cur = mysql_util.conn_db() # 查询用户列表(全部用户) result = mysql_util.query(cur, 'SELECT * FROM t_user') u_list = [] for u in result: usr = User(u[1], u[2], u[0], u[3]) u_list.append(usr) # 关闭数据库连接 mysql_util.close_db(conn, cur) return u_list
def get_by_id(id): # 连接数据库 conn, cur = mysql_util.conn_db() # 查询id查询单个用户 result = mysql_util.query(cur, 'SELECT * FROM t_user WHERE id = {_id}'.format(_id=id)) if len(result) > 0: u = result[0] # 关闭数据库连接 mysql_util.close_db(conn, cur) return User(u[1], u[2], u[0], u[3]) else: # 关闭数据库连接 mysql_util.close_db(conn, cur) return None
def login(param): if param['name'] and param['password']: # 连接数据库 conn, cur = mysql_util.conn_db() # 查询用户 sql = "SELECT * FROM t_user WHERE name = '{_name}' and password = '******'".format( _name=str(param['name']), _password=str(md5(param['password']))) result = mysql_util.query(cur, sql) if len(result) > 0: u = result[0] # 关闭数据库连接 mysql_util.close_db(conn, cur) return User(u[1], u[2], u[0], u[3]) else: # 关闭数据库连接 mysql_util.close_db(conn, cur) return None else: return None