-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.

3
  • i hope you are calling your main function Commented Sep 21, 2017 at 16:43
  • yeah @PaulNicolashunter. I m doing that. Any idea why problem is there? Commented Sep 21, 2017 at 16:45
  • here check this out Commented Sep 21, 2017 at 16:48

1 Answer 1

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']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!!!!! @Isma. You are Awesome Buddy. How on earth I could miss that.... Took my 3 hours....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.