gpt4 book ai didi

python - SQL 数据库不显示条目

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:37 25 4
gpt4 key购买 nike

我正在尝试开发一个简单的博客应用程序。现在的基础只有 user_name 字段和文本(besedilo)。我运行后没有显示任何错误。但数据不存储在数据库中,以后也不会显示。

应用程序.py

from flask import Flask, render_template, request
from flask_mysqldb import MySQL
import yaml
from flask_bootstrap import Bootstrap

app=Flask(__name__)
bootstrap = Bootstrap(app)

db = yaml.load(open('db.yaml'))
app.config['MYSQL_HOST'] = db['mysql_host']
app.config['MYSQL_USER'] = db['mysql_user']
app.config['MYSQL_PASSWORD'] = db['mysql_password']
app.config['MYSQL_DB'] = db['mysql_db']
mysql = MySQL(app)


@app.route('/', methods=['GET','POST'])
def index():
if request.method == 'POST':
form = request.form
user_name = form['user_name']
besedilo = form['besedilo']
cur = mysql.connect.cursor()
cur.execute("INSERT INTO post(user_name, besedilo) VALUES(%s, %s)", (user_name, besedilo))
mysql.connection.commit()
return render_template('index.html')

@app.route('/post')
def post():
cur = mysql.connection.cursor()
result_value = cur.execute("SELECT * FROM post")
if result_value > 0:
post = cur.fetchall()
return render_template('post.html', post=post)




if __name__ == '__main__':
app.run(debug=True)

index.html

{% extends 'base.html' %}

{% block content %}
<h1>Hello</h1>
<form method="post">
NAME:<input type="name" name="user_name">
BESEDILO:<input type="text" name="besedilo">
<input type="submit">
</form>
{% endblock %}

</body>
</html>

post.html

{% extends 'base.html' %}
{% block sub_content %}
<table border = 1>
{% for post in posts %}
<tr>
<td>{{post.user_name}}</td>
<td>{{post.besedilo}}</td>
</tr>
{% endfor %}
</table>

{% endblock %}

db.yaml

mysql_host: 'localhost'
mysql_user: 'root'
mysql_password: 'xxxxxxxx'
mysql_db: 'my_blog'

我错过了什么。我已经安装了所有软件包,字段名称是匹配的。

Database that i set up (with the following commands):
CREATE DATABASE my_blog;
CREATE TABLE post(user_name varchar(30), besedilo varchar(150));

and inserts for fine: with
INSERT INTO post(user_name, besedilo) VALUES ('Alex', 'i have a job to do');
mysql> SELECT * FROM post;
+-----------+----------------+
| user_name | besedilo |
+-----------+----------------+
| Peter | some text |
| Alex | i have a job |
+-----------+----------------+

1.) 更新:

@app.route('/', methods=['GET','POST'])
def index():
if request.method == 'POST':
form = request.form
user_name = form['user_name']
besedilo = form['besedilo']
conn = mysql.connect()
cur = conn.cursor()
cur.execute("INSERT INTO post(user_name, besedilo) VALUES(%s, %s)", (user_name, besedilo))
conn.commit()
return render_template('index.html')

最佳答案

我强烈怀疑罪魁祸首是 if result_value>0

我想无论表中是否存在行,SELECT * FROM post 总是返回 0。

摘自MySQL Documentation :

The use of mysql_num_rows() depends on whether you use mysql_store_result() or mysql_use_result() to return the result set. If you use mysql_store_result(), mysql_num_rows() may be called immediately. If you use mysql_use_result(), mysql_num_rows() does not return the correct value until all the rows in the result set have been retrieved.

尝试排除您的 result_value 检查并查看结果:

@app.route('/post')
def post():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM post")
# if result_value > 0: ## I suppose that line always returns 0
post = cur.fetchall()
return render_template('post.html', post=post)

至于 def index() - 我不确定那里有问题。

告知您的进展。

关于python - SQL 数据库不显示条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com