-1

My data is not being stored in db inspite of using connection.commit() in Python flask. 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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
user3457384
  • 593
  • 6
  • 14

1 Answers1

1

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']
Isma
  • 14,604
  • 5
  • 37
  • 51