简体   繁体   English

FLASK-第1行的'%(site_id)s'附近的SQL语法错误

[英]FLASK - You have an error in your SQL syntax near '%(site_id)s' at line 1

I' like to have a small explanation about Flask, Something I don't understand. 我想对Flask进行一些简短的解释,这是我不了解的内容。 I'm looking to get DATA from Database by an ID. 我正在寻找通过ID从数据库获取数据。 The ID is my route parameter. ID是我的路线参数。 I've create my route, but I'm having an error, And I don't understand What they are requesting in fact ? 我已经创建了路线,但是出现错误,我不明白他们实际上在要求什么? An element from Database ? 数据库中的元素?

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; mysql.connector.errors.ProgrammingError:1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '%(site_id)s' at line 1 检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在第1行附近使用'%(site_id)s'

My route code : 我的路线代码:

    #Construct app
app = Flask(__name__)
app.config.from_object('config')
app.config.from_object('secret_config')

#Database functions
def connect_db () :
    g.mysql_connection = mysql.connector.connect(
        host = app.config['DATABASE_HOST'],
        user = app.config['DATABASE_USER'],
        password = app.config['DATABASE_PASSWORD'],
        database = app.config['DATABASE_NAME']
    )

    g.mysql_cursor = g.mysql_connection.cursor()
    return g.mysql_cursor

    def get_db () :
        if not hasattr(g, 'db') :
            g.db = connect_db()
        return g.db

@app.teardown_appcontext
def close_db (error) :
    if hasattr(g, 'db') :
        g.db.close()



@app.route('/historique/<int:site_id>')
def historique(site_id):

db = get_db()

db.execute('SELECT * FROM sites s JOIN historique h ON h.site_id WHERE `s.site_id = %(site_id)s', {'id': site_id})

entries = db.fetchall()
return render_template('historique.html',  entries = entries)

And Here is my HTML Code 这是我的HTML代码

{% extends 'layout.html' %}

{% block titre %}
    Acceuil
{% endblock %}

{% block body %}
<h1>Historique d'activité pour  {{ entrie.0 }}</h1>
{% for entrie in entries %}
{% endfor %}
{% endblock %}

I only like to understand. 我只喜欢了解。 Thanks a lot for your help. 非常感谢你的帮助。

I think, you have a mistake when you are formatting your string with sql query. 我认为,使用sql查询格式化字符串时会出错。 Try this 尝试这个

@app.route('/historique/<int:site_id>')
def historique(site_id):
    db = get_db()
    query = 'SELECT * FROM sites s JOIN historique h ON h.site_id WHERE `s.site_id = {site_id}'.format(site_id=site_id)
    db.execute(query)
    # ... rest of the code ...

And yes, this backtick before s.site_id is very suspicious. 是的,在此之前反引号s.site_id 十分可疑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 1064SQL语法有错误。 以便在第1行的&#39;%s&#39;附近使用正确的语法”) - 1064, “You have an error in your SQL syntax; for the right syntax to use near '%s' at line 1”) 将CSV数据插入MySQL数据库-“您的SQL语法有误;在第1行的&#39;&#39;附近 - Inserting CSV data into MySQL database - "You have an error in your SQL syntax; near ' ' at line 1 错误 1064 (42000):您的 SQL 语法中存在错误,接近使用 %s - error 1064 (42000) : You have an error in your SQL syntax near to use %s 您的SQL语法有错误; 检查与您的MariaDB服务器版本对应的手册,以便在第1行附近使用正确的语法, - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near at line 1", “你的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在&#39;&#39;第1行&#39;附近使用正确的语法 - “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1” mysql.connector.errors.ProgrammingError: 1064 (42000): 你的 SQL 语法有错误; 在第 1 行的“)”附近使用正确的语法 - mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; for the right syntax to use near ')' at line 1 警告:您的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法 - Warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 \'keyword")\' 附近使用的正确语法 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'keyword")\' 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 '*) 附近使用的正确语法 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 &#39;) 附近使用的正确语法 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM