У меня есть простое Flask приложение. Приложение соединяется с базой данных mov ie отзывов. Я построил функцию для извлечения данных, которая принимает аргумент max_count. но по какой-то причине я могу передать только число меньше 10. Приложение работает нормально, если я передал 3 или 9. Вот мой код:
from flask import Flask, request
import requests
import psycopg2
from psycopg2.extras import RealDictCursor
import json
app = Flask(__name__)
@app.route('/')
def home():
return "Hello!"
@app.route('/get_total_data_count/<label>', methods=['GET'])
def get_total_data_count(label):
connection = psycopg2.connect(user="barmej", password="password", host="127.0.0.1", port="5432", database="labeling")
cursor = connection.cursor()
try:
if label == 'positive':
cursor.execute("SELECT * FROM data_labeling WHERE label_number = 0 limit 100;")
elif label == 'negative':
cursor.execute("SELECT * FROM data_labeling WHERE label_number = 1 limit 100;")
elif label == 'all':
cursor.execute("SELECT * FROM data_labeling;")
return "The count is " + str(cursor.rowcount)
except:
return "Error! type: positive, negative or all"
cursor.close()
connection.close()
@app.route('/get_data', methods=['GET'])
def get_data_test():
try:
connection = psycopg2.connect(user="barmej", password="password", host="127.0.0.1", port="5432", database="labeling")
cursor = connection.cursor()
max_count = request.args.get('max_count')
sort_order = request.args.get('sort_order')
if sort_order == 'ASC':
insert = "SELECT text FROM data_input ORDER BY ASC limit %s"
parameters = max_count
cursor.execute(insert, parameters)
result = cursor.fetchall()
elif sort_order == 'DESC':
insert = "SELECT text FROM data_input ORDER BY DESC limit %s "
parameters = max_count
cursor.execute(insert, (parameters))
result = cursor.fetchall()
dic = {}
dic['text'] = result
return dic
except:
return "Error!, make sure url include: a max count and either 'ASC' or 'DESC' Argument"
cursor.close()
connection.close()
if __name__ == "__main__":
app.run(debug=True, port=3000)
Вызов выполняется в python оболочке из сервер:
>>> import requests
>>> param = {'max_count': 12, 'sort_order': 'ASC'}
>>> r = requests.get('http://127.0.0.1:3000/get_data', params=param)
>>> r.text
Я пробовал код без исключения, и ошибка:
psycopg2.errors.SyntaxError: syntax error at or near "%"\nLINE 1: SELECT text FROM data_input limit %s