Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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 “dict”对象不支持索引_Python_Sqlalchemy - Fatal编程技术网

Python “dict”对象不支持索引

Python “dict”对象不支持索引,python,sqlalchemy,Python,Sqlalchemy,我正在尝试制作一个web应用程序,它有一个搜索按钮,可以搜索postgres表 我得到了一个错误: TypeError:“dict”对象不支持索引 我的代码如下: app.py 我的HTML是: main.html 任何帮助都将不胜感激。SQLAlchemy有一个文本函数,用于将字符串转换为SQLAlchemy文本对象,该对象似乎可以为您正确转义SQL 你需要导入 `从sqlalchemy导入文本' 您需要从查询中删除f,并将=更改为like或ilike i、 e。 res=db.execute

我正在尝试制作一个web应用程序,它有一个搜索按钮,可以搜索postgres表

我得到了一个错误:

TypeError:“dict”对象不支持索引

我的代码如下:

app.py

我的HTML是:

main.html


任何帮助都将不胜感激。

SQLAlchemy有一个文本函数,用于将字符串转换为SQLAlchemy文本对象,该对象似乎可以为您正确转义SQL

你需要导入 `从sqlalchemy导入文本'

您需要从查询中删除f,并将=更改为like或ilike

i、 e。
res=db.executetext从public.options中选择cost,其中optionno类似于“%jn%”。fetchall

您绝对不能使用字符串插值来形成SQL查询。您好,Mahesh,谢谢您的回复。我现在有一个新问题,我有错误代码:sqlalchemy.exc.ProgrammingError:psycopg2.ProgrammingError运算符不存在:real~~未知第1行:从public.options中选择成本,其中optionno类似于“%jn%”…^提示:没有与给定名称和参数类型匹配的运算符。您可能需要添加显式类型转换。[SQL:SELECT cost FROM public.options where optionno like%%jn%%]此错误的背景信息位于:了解原因吗?postgresql数据库中optionno列的数据类型是什么,如果是实数或整数,则查询会给出错误,optionno是字符类型而不是数字。optionno是postgres中的“real”类型,它基本上是一列数字,我正在搜索一个数字。您需要将实数转换为字符您的新查询是db.executetextSELECT cost FROM public.options,其中castoptionno为VARCHAR,如“%jn%”。fetchallThank mahesh,这很有效,但它不会返回任何数据,只是[]任何想法
from flask import Flask, render_template, request
from sqlalchemy import create_engine

app = Flask(__name__)

db_string = "postgres://xi:x@x:5432/xx"

db = create_engine(db_string)

@app.route('/', methods=['GET', 'POST'])
def homepage():
    if request.method == 'POST':
        jn = request.form['jobnumber']
        result_set = db.execute("SELECT cost FROM public.options where optionno = (f'%jn%')").fetchall()
        return render_template('main.html', test=result_set, jn=jn)
    else:
        return render_template('main.html')

    if __name__ == "__main__":
        app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>xxx</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
    <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
</head>

<body>
<p>xxx</p>

<form method="POST" id="jobnumber">
    <input name="jobnumber" type="textbox" placeholder="jobnumber">
</form>

<table> 

<td>
       {{test}}

</td>


</table>

</body>
</html>