Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.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 Mysql查询的HTML字段_Python_Html_Mysql_Flask_Textfield - Fatal编程技术网

Python Mysql查询的HTML字段

Python Mysql查询的HTML字段,python,html,mysql,flask,textfield,Python,Html,Mysql,Flask,Textfield,大家好,我的社区 我对烧瓶还不熟悉,然而,尽管学习曲线非常陡峭,但有一个问题我还没有弄清楚 我使用的是一个非常简单的HTML搜索表单,用户在表单中键入一个城市的名称,该输入被传递到一个Mysql查询,并将输出返回到一个表中 一切正常,除了我无法将变量传递到Mysql。。。如果我修复了这个查询,它就会工作 我试着使用表单,发布和获取需求,但我不知道我哪里出了问题。 我传递的变量数据不是机密的,所以我不担心它是否显示在URL中 这里只是简单的形式(我猜不正确) 数据库助手Mysql(PLN的值应根据

大家好,我的社区

我对烧瓶还不熟悉,然而,尽管学习曲线非常陡峭,但有一个问题我还没有弄清楚

我使用的是一个非常简单的HTML搜索表单,用户在表单中键入一个城市的名称,该输入被传递到一个Mysql查询,并将输出返回到一个表中

一切正常,除了我无法将变量传递到Mysql。。。如果我修复了这个查询,它就会工作

我试着使用表单,发布和获取需求,但我不知道我哪里出了问题。 我传递的变量数据不是机密的,所以我不担心它是否显示在URL中

这里只是简单的形式(我猜不正确)

数据库助手Mysql(PLN的值应根据表单中的输入进行更改

import pymysql
class DBHelper:

def table_inputs(self):
        connection = self.connect()
        PLN="**City_Name**"
        try:
            query = "SELECT Plant_Geo, Plant_Code, Plant_Name, GMID, Material_Name, GROUP_CONCAT(DISTINCT Vendor_Name_GS ORDER BY Vendor_Name_GS) as VNGS, sum(2014_USD), sum(2015_USD), sum(2016_USD) FROM invoice_report WHERE plant_code like '%s' GROUP BY GMID ORDER BY sum(2015_USD) DESC" %(PLN);
            with connection.cursor(pymysql.cursors.DictCursor) as cursor:
                cursor.execute(query)
            return cursor.fetchall()
        finally:
            connection.close()

提前感谢您的帮助。

我认为您需要在
元素上设置操作,而不是
,并且您希望将其指向同一烧瓶端点(我假设?):

然后,更新view函数以测试city是否已提交,并将其提交给助手类:

@app.route('/table')
def table():
    // the second argument is the default if "City_Name" is not submitted
    city = request.args.get('City_Name', 'New York')
    try:
        tabledata = DB.table_inputs(city)
    except Exception as e:
        print(e)
        tabledata = None
    return render_template("table.html", tabledata=tabledata)

PJ Santoro,非常感谢你!这100%有效。非常感谢你的帮助。很好,很高兴我能帮助你!如果你能接受这个答案,那就太好了。PJ Santoro已经有一段时间了,我去年一直在使用这个代码。但是最近我升级到macOS Sierra和MariaDB 10.2.9,代码(完全相同)现在抛出了一个错误“TypeError:“NoneType”对象不可编辑”。我不知道为什么,但如果您有任何想法,我将不胜感激。也许可以尝试将
%s
更改为
…可能是DBAPI中的一个小更改…不熟悉MariaDBPS:只是在上面做了一些编辑…1)更好的做法是直接将参数传递到execute函数中,以便所有内容都正确转义2)添加了except子句,因为我假设有一个错误,
tabledata
None
结尾……因此您遇到了错误
from flask import Flask, render_template, request, url_for
from dbhelper_single_search import DBHelper


app = Flask(__name__)
DB = DBHelper()


@app.route('/table')
def table():
    try:
        tabledata = DB.table_inputs()
    except Exception as e:
        print(e)
        tabledata = None
    return render_template("table.html", tabledata=tabledata)


if __name__ == '__main__':
    app.run(port=5000, debug=True)
import pymysql
class DBHelper:

def table_inputs(self):
        connection = self.connect()
        PLN="**City_Name**"
        try:
            query = "SELECT Plant_Geo, Plant_Code, Plant_Name, GMID, Material_Name, GROUP_CONCAT(DISTINCT Vendor_Name_GS ORDER BY Vendor_Name_GS) as VNGS, sum(2014_USD), sum(2015_USD), sum(2016_USD) FROM invoice_report WHERE plant_code like '%s' GROUP BY GMID ORDER BY sum(2015_USD) DESC" %(PLN);
            with connection.cursor(pymysql.cursors.DictCursor) as cursor:
                cursor.execute(query)
            return cursor.fetchall()
        finally:
            connection.close()
<form method="GET" action>
    <div class="form-group">
        <div class="col-sm-3">
            <input type="text" placeholder="City Name" name="City_Name" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-2">
            <input type="submit" value="SEARCH" class="btn btn-primary btn-block">
        </div>
    </div>
</form>
import pymysql


class DBHelper:

    def table_inputs(self, city):
        connection = self.connect()
        PLN = "**%s**" % city
        try:
            query = "SELECT Plant_Geo, Plant_Code, Plant_Name, GMID, Material_Name, GROUP_CONCAT(DISTINCT Vendor_Name_GS ORDER BY Vendor_Name_GS) as VNGS, sum(2014_USD), sum(2015_USD), sum(2016_USD) FROM invoice_report WHERE plant_code like '%s' GROUP BY GMID ORDER BY sum(2015_USD) DESC";
            with connection.cursor(pymysql.cursors.DictCursor) as cursor:
                # actually better to pass parameters like this:
                cursor.execute(query, (PLN,))
            return cursor.fetchall()

        except Exception as err:
            # this may also help find errors generated above...
            print(err)

        finally:
            connection.close()
@app.route('/table')
def table():
    // the second argument is the default if "City_Name" is not submitted
    city = request.args.get('City_Name', 'New York')
    try:
        tabledata = DB.table_inputs(city)
    except Exception as e:
        print(e)
        tabledata = None
    return render_template("table.html", tabledata=tabledata)