简体   繁体   English

即使使用 connection.commit() 也无法将数据提交到数据库(使用 Python flask MySQL)

[英]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.

相关问题 connection.commit()性能影响 - connection.commit() performance impact 从 pymysql 更频繁地使用 connection.commit() 到 AWS RDS 是否更昂贵? - Is using connection.commit() from pymysql more frequently to AWS RDS more expensive? Connection.commit() 不会在 Django 测试中保留数据 - Connection.commit() does not persist data in Django Test Postgres:cursor.execute(“COMMIT”)与connection.commit() - Postgres: cursor.execute(“COMMIT”) vs. connection.commit() connection.commit() 和 cursor.execute('commit') 有什么区别? - What is the difference between connection.commit() and cursor.execute('commit')? 执行connection.commit()后,为什么其他cursor.execute()无法正常工作? - After execute connection.commit(), why the others cursor.execute() not work correctly? 任何人都可以告诉我python pyodbc中connection.commit()的意义是什么? - Can anyone tell me what' s the point of connection.commit() in python pyodbc ? 无法使用 flask 将 MySQL 数据库中的数据检索到 html - Not able to retrieve data from a MySQL database into html using flask Python Flask MySQL 提交将插入,不会更新 - Python Flask MySQL commit will insert, will not update 最佳运行时间 Python MySQL connection commit() - Optimal time to run Python MySQL connection commit()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM