[英]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.