[英]Not able to Commit data to database (using Python flask MySQL) even after using connection.commit()
My data is not being stored in db inspite of using connection.commit() in Python flask.尽管在 Python 烧瓶中使用了 connection.commit(),但我的数据并未存储在 db 中。 Here is my code:
这是我的代码:
from flask import Flask
from flaskext.mysql import MySQL
app = Flask(__name__)
def getMysqlConnection():
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = "user_name"
app.config['MYSQL_DATABASE_PASSWORD'] = "pass_string"
app.config['MYSQL_DATABASE_DB'] = "db_name"
app.config['MYSQL_DATABASE_HOST'] = "hostname"
mysql.init_app(app)
connection = mysql.connect()
cursor = connection.cursor()
return {"cursor":cursor,"connection":connection}
cursor = getMysqlConnection()['cursor'] #####global variables####
connection = getMysqlConnection()['connection']
def main_function():
cursor.execute("Insert into table_name "
"(col1,col2,col3,col4) "
"Values (%s,%s,%s,%s,%s,%s)",
(val1,vla2,val3,val4)
)
connection.commit()
This is not saving code in the database and I don't know why.这不是在数据库中保存代码,我不知道为什么。
Could it be that the following code initializes the database connection two times?会不会是下面的代码对数据库连接进行了两次初始化?
cursor = getMysqlConnection()['cursor'] #####global variables####
connection = getMysqlConnection()['connection']
Try尝试
db = getMysqlConnection()
cursor = db['cursor']
connection = db['connection']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.