SQL数据库未显示输入内容

时间:2019-02-02 14:12:40

标签: python sql

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

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

1 个答案:

答案 0 :(得分:0)

我非常怀疑罪魁祸首是The headers or library files could not be found for jpeg, a required dependency when compiling Pillow from source. Please see the install instructions at: https://pillow.readthedocs.io/en/latest/installation.html Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-install-Q2fwNi/pillow/setup.py", line 812, in <module> raise RequiredDependencyException(msg) __main__.RequiredDependencyException: The headers or library files could not be found for jpeg, a required dependency when compiling Pillow from source. Please see the install instructions at: https://pillow.readthedocs.io/en/latest/installation.html Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-xZLepb/pillow/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-1_XTeb/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-xZLepb/pillow/

我想if result_value>0总是返回0,与表中是否存在行无关。

摘录自MySQL Documentation

  

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

尝试排除您的SELECT * FROM post检查并查看结果:

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) -我不确定那里是否有问题。

有关通知你的进步。