Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将大于9的数字传递给psycopg2 select语句_Python_Python Requests_Psycopg2 - Fatal编程技术网

Python 如何将大于9的数字传递给psycopg2 select语句

Python 如何将大于9的数字传递给psycopg2 select语句,python,python-requests,psycopg2,Python,Python Requests,Psycopg2,我有一个简单的烧瓶应用程序。该应用程序与电影评论数据库连接。我构建了一个函数来检索数据,该函数采用max_count参数。但由于某种原因,我只能通过一个小于10的数字。如果我通过3级或9级,应用程序运行良好。这是我的密码: from flask import Flask, request import requests import psycopg2 from psycopg2.extras import RealDictCursor import json app = Flask(__name

我有一个简单的烧瓶应用程序。该应用程序与电影评论数据库连接。我构建了一个函数来检索数据,该函数采用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)

我毫无例外地尝试了代码,错误是:

psycopg2.errors.SyntaxError: syntax error at or near "%"\nLINE 1: SELECT text FROM data_input limit %s

问题是如何将参数传递给
.execute()

您需要像这样传递一个元组:

cursor.execute(insert, (parameters,)) # a comma is necessary to make a tuple
目前,当有多个数字时,它认为这些是独立的参数

cursor.execute(insert, (parameters,)) # a comma is necessary to make a tuple