Link to home
Start Free TrialLog in
Avatar of jameskane
jameskane

asked on

Python and problem in updating a mysql table with simple list.

I have a list which I am using to  update  a mysql table - registrations. and a particular column 'cheapbuy' and row memberID = 3001

The list is [9, 10]  and I have verified that it is 'of type' list.

The page with the mysql update function is shown below.

The error message is also shown below

Thanks for taking a look

james.

User generated imageUser generated image





CGI PAGE
 
#!C:\Python34\python.exe
import cgi,cgitb
import mysql.connector as conn
import collections
import datetime
def htmlTop():
    print("""Content-type:text/html\n\n
        <!DOCTYPE html>
        <html lang="en">
            <head>
                #<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title> My Server-side template</title>
        </head>
        <body>""")

def htmlTail():
    print(""" its done</body>
        </html>""")

def connectDB():
    db=conn.connect(host='localhost' ,user='root' ,passwd='844cheminduplan' ,db='office')
    cursor = db.cursor()
    return db, cursor 

def Updatecheapbuy(db,cursor):
    cursor.execute ("""
       UPDATE registrations
       SET  cheapbuy = {0}
       WHERE memberID={1}
    """.format(cheapbuylist, memberID))
    db.commit()
    db.close  
    

#main program
if __name__== "__main__":
    try:
        htmlTop()
        memberID = 1003
        mydict = ({9: 3, 10: 3, 11: 1, 12: 1})
        cheapbuylist = []
        
        for key, value in mydict.items():
            if value == 3:
                print(key, "this is key")
                cheapbuylist.append(key)
        
             
        
        print(cheapbuylist, "this is cheapbuylist", type(cheapbuylist), memberID, "This is memberID")
        db,cursor=connectDB()
        Updatecheapbuy(db,cursor)
        

        htmlTail()
    except:
        cgi.print_exception()

        

    

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Walter Ritzel
Walter Ritzel
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jameskane
jameskane

ASKER

Hi Walter, sorry, but I'm not sure what you mean 'statement before execute'  ?
OK, that's fixed - needed the string change plus Json.

thanks,

james