SQL数据库没有显示条目

问题描述 投票:1回答:1

我试着去开发一个简单的博客应用程序。有了基础,现在只有USER_NAME场和文本(besedilo)。之后,我运行它,也没有任何错误。但数据不存储在数据库中,并不会显示以后。

app.朋友

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')
python sql
1个回答
0
投票

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

我想它总是返回0 SELECT * FROM post如果行的表存在没有关系。

MySQL Documentation摘录:

使用mysql_num_rows()的取决于你是否使用了mysql_store_result()或了mysql_use_result()返回的结果集。如果使用了mysql_store_result(),mysql_num_rows()可以立即调用。如果使用了mysql_use_result(),直到结果集的所有行已检索mysql_num_rows()不返回正确的值。

尽量排除您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() - 我不知道这里面就有一个问题。

告知你的进步。

© www.soinside.com 2019 - 2024. All rights reserved.