Home > Software design >  Python not writing data to MySQL
Python not writing data to MySQL

Time:10-01

I'm writing code to build a database where I can input the records from a program and print it when needed. I created a database where I'll save all the data. But when I input the data from the front end, it says "Please check your inputs". This is the code for the part I'm having problem with:

def submit_records():
pay_to = values['pay_to']
if pay_to == '':
    sg.popup_error('Missing Pay_to')
amount_words = values['amount_words']
if amount_words == '':
    sg.popup_error('Missing amount in words')
amount = values['amount']
if amount == '':
    sg.popup_error('Missing amount')
address = values['address']
if address == '':
    sg.popup_error('Missing address')
purpose = values['purpose']
if purpose == '':
    sg.popup_error('Missing purpose')
drawer = values['drawer']
if drawer == '':
    sg.popup_error('Missing drawer')
issue_date = values['issue_date']
if issue_date == '':
    sg.popup_error('Missing issue_date')
company = values['company']
if company == '':
    sg.popup_error('Missing company')
else:
    try:
        command = "INSERT INTO db_class.box(pay_to, amount_words, amount, address, purpose, drawer, issue_date, company) VALUES (“   “‘“   values[‘pay_to’]   “‘“   “, “   “‘“   values[‘amount’]   “’”   “, “   “‘“   values[‘amount_words’]   “’”   “, “   “‘“   values[‘address’]   “’”   “, “   “‘“   values[‘purpose’]   “’”   “, “   “‘“   values[‘drawer’]   “’”   “, “   “‘“   values[‘issue_date’]   “’”   “, “   “‘“   values[‘company’]   “’”   “);"
        print(command)
        myCursor.execute(command)
        dbms.commit()
        choice = sg.popup_ok_cancel("Please confirm entry")
        if choice == 'OK':
            clear_records()
            sg.popup_quick('records entered')
        else:
            sg.popup_ok('Edit Entry')
    except:
        sg.popup('Please check your inputs')

And this is the message I get from the console:

INSERT INTO db_class.box(pay_to, amount_words, amount, address, purpose, drawer, issue_date, company) VALUES (“   “‘“   values[‘pay_to’]   “‘“   “, “   “‘“   values[‘amount’]   “’”   “, “   “‘“   values[‘amount_words’]   “’”   “, “   “‘“   values[‘address’]   “’”   “, “   “‘“   values[‘purpose’]   “’”   “, “   “‘“   values[‘drawer’]   “’”   “, “   “‘“   values[‘issue_date’]   “’”   “, “   “‘“   values[‘company’]   “’”   “);

whereas it should show the inputs I made. Can someone please tell me what's going wrong?

CodePudding user response:

this is not true syntax i think.

INSERT INTO db_class.box(pay_to, amount_words, amount, address, purpose, drawer, issue_date, company) VALUES (“   “‘“   values[‘pay_to’]   “‘“   “, “   “‘“   values[‘amount’]   “’”   “, “   “‘“   values[‘amount_words’]   “’”   “, “   “‘“   values[‘address’]   “’”   “, “   “‘“   values[‘purpose’]   “’”   “, “   “‘“   values[‘drawer’]   “’”   “, “   “‘“   values[‘issue_date’]   “’”   “, “   “‘“   values[‘company’]   “’”   “);

It should be like this:

myCursor.execute("""INSERT INTO db_class.box (pay_to, amount_words, amount, address, purpose, drawer, issue_date, company) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)""",(pay_to, amount_words, amount, address, purpose, drawer, issue_date, company))

Would be better to use like this.

  • Related