api_search.py 687 B

1234567891011121314151617181920212223
  1. from .tool.func import *
  2. def api_search_2(conn, name):
  3. curs = conn.cursor()
  4. num = int(number_check(flask.request.args.get('num', '1')))
  5. if not num > 0:
  6. num = 1
  7. if num > 1000:
  8. num = 1
  9. curs.execute("" + \
  10. "select distinct title, case when title like ? then 'title' else 'data' " + \
  11. "end from data where title like ? or data like ? order by case " + \
  12. "when title like ? then 1 else 2 end limit ?",
  13. ['%' + name + '%', '%' + name + '%', '%' + name + '%', '%' + name + '%', str(num)]
  14. )
  15. all_list = curs.fetchall()
  16. if all_list:
  17. return flask.jsonify(all_list)
  18. else:
  19. return flask.jsonify({})