[英]Cannot insert integer to MySQL in python
In my flask application, I cannot insert user_id
which I get from request.form
and it is an integer to MySQL.在我的 flask 应用程序中,我无法插入从
request.form
获得的user_id
,它是 integer 到 MySQL。 Here is my code:这是我的代码:
from flask import Flask, jsonify, render_template
from flask import request
import socket
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = '192.168.0.101'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PORT'] = '3307'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'msblog_users'
mysql = MySQL(app)
@app.route("/create_post", methods = ['POST', 'GET'])
def create_post():
if request.method == 'GET':
return "You can only send post requests here!"
if request.method == 'POST':
user_id = request.form.get('user_id')
message = request.form.get('message')
cursor = mysql.connection.cursor()
cursor.execute('''INSERT INTO posts (user_id, post)VALUES (%s, %s)''', (int(user_id), message))
mysql.connection.commit()
cursor.close()
return "Done"
I get the following error:我收到以下错误:
TypeError: 'str' object cannot be interpreted as an integer
What should I do?我应该怎么办? I did lots of search but so far nothing!
我做了很多搜索,但到目前为止一无所获!
The error comes from the int(user_id)
part, specifically due to the value being None
.错误来自
int(user_id)
部分,特别是由于值为None
。 You should first make sure it is a valid integer:您应该首先确保它是有效的 integer:
try:
user_id = int(request.form.get('user_id'))
except (ValueError, TypeError):
return <error 400>
Finally, I found the problem.最后,我发现了问题。 It's so strange., the problem was not even related to the insert query: Take a look at the MySQL connection configuration part in my flask code, as you can see below:
太奇怪了,这个问题甚至与插入查询无关:看一下我的flask代码中的MySQL连接配置部分,如下所示:
app.config['MYSQL_HOST'] = '192.168.0.101'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PORT'] = '3307'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'msblog_users'
the port is written between quotations!端口写在引号之间! it's a string but MySQL needs an integer value for the port to be able to connect!
这是一个字符串,但 MySQL 需要一个 integer 值才能使端口能够连接!
here is the quick fix:这是快速修复:
app.config['MYSQL_PORT'] = 3307
It took me a day to solve it!我花了一天时间解决它!
The %s
stands for string, if you want you want that parameters to be an integer, make it %i
. %s
代表字符串,如果您希望该参数为 integer,请将其设为%i
。
INSERT INTO posts (user_id, post)VALUES (%i, %s) ....
If the column post
is a string / text column (varchar probably) you should also quote it as such.如果
post
是一个字符串/文本列(可能是varchar),你也应该这样引用它。
cursor.execute("INSERT INTO posts (user_id, post) VALUES (%i , '%s')" % (1, 'string value')
By the way, this is the old way of formatting strings顺便说一句,这是格式化字符串的旧方法
The new way is doing this:新方法是这样做的:
cursor.execute("INSERT INTO posts (user_id, post) VALUES ({}, '{}')".format(1, 'string value')
Or you can name the parameters:或者您可以命名参数:
"INSERT INTO posts (user_id, post) VALUES ({id}, '{str}')".format( str='string value',id=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.