SQL数据库不显示条目

2024-03-29 01:17:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想开发一个简单的博客应用程序。目前只有用户名字段和文本(besedilo)的base。我运行后,它显示没有错误。但数据不存储在数据库中,以后也不会显示。你知道吗

你知道吗应用程序类型你知道吗

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)

你知道吗索引.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>

你知道吗邮政.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 %}

你知道吗亚马尔配电箱你知道吗

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')

Tags: nameimportformconfigappdbrequesthtml
1条回答
网友
1楼 · 发布于 2024-03-29 01:17:41

我强烈怀疑罪魁祸首是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()-我不确定有没有问题。你知道吗

告知你的进展。你知道吗

相关问题 更多 >