【问题标题】:add value to new table in mysql为mysql中的新表添加值
【发布时间】:2020-06-11 17:53:31
【问题描述】:

我在烧瓶 mysql 上有一个数据库,用户可以登录到profile 页面。我需要添加描述文本,但它不会插入数据库。可能是什么问题?

app.py

@app.route('/profile', methods=['GET','POST'])
def profile():
    if 'loggedin' in session:
        return render_template('profile.html', id=session['id'], email=session['email'])

    if request.method == 'POST':
        texts = request.form['text']
        cur = mysql.connection.cursor() 
        cur.execute('INSERT INTO users.data(texts) VALUES (%s)', (texts))
        mysql.connection.commit()
        cur.close()
        return render_template('profile.html', texts=texts)

    return redirect(url_for('login'))

profile.html

your id is: {{ id }}
your email is: {{ email }}
your text is: {{ texts }}

<form action="" method="post"> 
<input type="text" class="searchTerm" name="text" placeholder="Add your symptoms">
<button type="submit" value="submit" class="searchButton" name=form>
     <span class="submit-symp">Submit</span>
 </button>
</form>

idemail 出现,但我无法将数据添加到新列并从数据库中取回。

【问题讨论】:

    标签: mysql python-3.x flask


    【解决方案1】:

    python 需要一个包含两个元素的元组,即使一个为空

    cur.execute('INSERT INTO users.data(texts) VALUES (%s)', (texts,))
    

    O 会在 mysql workbench 中测试 sql 命令并对其进行测试,具体取决于可能失败的表设计

    IO 认为你需要更新

    cur.execute('UPDATE users.data  SET texts = %s WHERE id = ?', (texts,session['id']))
    

    【讨论】:

    • 你在mysql中直接测试过查询吗?
    • 是的。并收到此错误Error Code: 1064. You have an error in your SQL syntax;
    • 你能给我一个测试的例子吗?查询在 mysql 8 中运行而没有打嗝
    • 能否请您澄清一下。有什么例子?
    • 检查这个例子并将其修改为你的表db-fiddle.com/f/4yPorU6k3SjQ5nmhgi1wGo/11
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 2018-08-23
    相关资源
    最近更新 更多