Writing data to MYSQL database from python not persistent - python

Forgive my ignorance. I'm a rookie trying to learn some programming.
I have the following code implemented in Visual Studio 2010:
def print_tmpsalary():
import sys
import MySQLdb
FD_playerpicker = []
FD_playerpicker = {"8595":["C","Evgeni Malkin","51189","672","2","9600",6.1,"75",False,4,""],"8472":["G","Pekka Rinne","51188","665","15","9400",5.7,"73",False,4,""],"8594":["C","Sidney Crosby","51189","672","5","9300",6.2,"22",False,0,""],"8173":["G","Johan Hedberg","51190","666","1000","8800",5.5,"27",False,0,""],"8592":["G","Ilya Bryzgalov","51189","670","1000","8700",4.8,"59",False,4,""],"8712":["G","Roberto Luongo","51191","677","25","8700",5.4,"55",False,4,""],"8492":["G","Martin Brodeur","51190","666","12","8400",4.4,"59",False,4,""],"8412":["G","Jon Quick","51191","662","1000","8400",5.4,"69",False,4,""],"8612":["G","Marc-Andre Fleury","51189","672","71","8300",5.1,"67",False,4,""],"8781":["G","Cory Schneider","51191","677","1000","8300",6.2,"33",False,0,""],"8353":["G","Jimmy Howard","51188","659","1000","8300",5.4,"57",False,4,""],"8319":["RW","James Neal","51189","672","1000","8200",4.7,"80",False,0,""],"8158":["RW","Ilya Kovalchuk","51190","666","45","8200",4.4,"77",False,4,""],"8253":["G","Michael Leighton","51189","670","1000","8200",6.4,"1",False,0,""],"8556":["C","Claude Giroux","51189","670","120","8000",4.5,"77",False,0,""],"8698":["LW","Daniel Sedin","51191","677","24","7800",4.1,"72",False,0,""],"8393":["G","Scott Clemmensen","51190","661","1000","7700",4.4,"30",False,0,""],"8394":["C","Anze Kopitar","51191","662","1000","7300",3.6,"82",False,4,""],"8608":["D","Kris Letang","51189","672","1000","7200",3.7,"51",False,4,""],"8733":["G","Jose Theodore","51190","661","1000","7200",4.6,"53",False,4,""],"12283":["G","Jacob Markstrom","51190","661","1000","7100",4.6,"7",False,0,""],"8555":["C","Jeff Carter","51191","662","8","7100",2.9,"55",False,0,""],"8478":["LW","Zach Parise","51190","666","6","7000",3.6,"82",False,0,""],"8598":["LW","Chris Kunitz","51189","672","79","7000",3.4,"82",False,0,""],"8335":["C","Henrik Zetterberg","51188","659","13","7000",3.7,"82",False,4,""],"8334":["LW","Pavel Datsyuk","51188","659","3","7000",3.6,"70",False,4,""],"8563":["C","Danny Briere","51189","670","102","6900",3.1,"70",False,0,""],"8703":["RW","Alex Burrows","51191","677","31","6900",3.3,"80",False,4,""],"8694":["C","Henrik Sedin","51191","677","61","6900",3.3,"82",False,4,""],"8562":["LW","Scott Hartnell","51189","670","32","6800",4,"82",False,4,""],"8403":["RW","Justin Williams","51191","662","1000","6800",3.3,"82",False,0,""],"8402":["RW","Dustin Brown","51191","662","1000","6800",3.1,"82",False,4,""],"8554":["C","Mike Richards","51191","662","30","6700",2.7,"74",False,0,""],"12163":["G","Sergei Bobrovsky","51189","670","1000","6700",4,"29",False,0,""],"8765":["RW","Patric Hornqvist","51188","665","1000","6700",3,"76",False,0,""],"8484":["RW","David Clarkson","51190","666","97","6700",3.1,"80",False,0,""],"8474":["C","Patrik Elias","51190","666","51","6700",3.2,"81",False,0,""],"8596":["C","Jordan Staal","51189","672","176","6700",3.3,"62",False,0,""],"8613":["G","Brent Johnson","51189","672","1000","6600",2.8,"16",False,0,""],"8702":["C","Ryan Kesler","51191","677","134","6600",3.2,"77",False,0,""],"8970":["G","Joey MacDonald","51188","659","1000","6500",4.5,"14",False,0,""],"8601":["RW","Pascal Dupuis","51189","672","1000","6500",3.1,"82",False,4,""],"8780":["G","Jonathan Bernier","51191","662","1000","6500",3.5,"16",False,0,""],"8338":["RW","Johan Franzen","51188","659","36","6500",3.4,"77",False,0,""],"8462":["RW","Martin Erat","51188","665","1000","6400",2.9,"71",False,0,""],"8260":["RW","Kris Versteeg","51190","661","158","6400",3.2,"71",False,0,""],"8566":["D","Chris Pronger","51189","670","57","6400",3.2,"13",False,2,"Concussion, knee surgery"],"8845":["G","Brad Thiessen","51189","672","1000","6400",3.1,"5",False,0,""],"8653":["G","Ty Conklin","51188","659","193","6300",3,"15",False,0,""],"14561":["RW","Jaromir Jagr","51189","670","1000","6300",3,"73",False,0,""],"8604":["LW","Tyler Kennedy","51189","672","148","6300",2.9,"60",False,0,""],"8467":["D","Shea Weber","51188","665","157","6300",3.2,"78",False,0,""],"8704":["RW","Mikael Samuelsson","51190","661","117","6200",2.7,"54",False,0,""],"8404":["LW","Wayne Simmonds","51189","670","1000","6200",2.9,"82",False,0,""],"8535":["C","Mike Fisher","51188","665","1000","6200",3,"72",False,0,""],"8797":["LW","James van Riemsdyk","51189","670","1000","6200",2.7,"43",False,2,"Broken left foot"],"8303":["RW","Jakub Voracek","51189","670","124","6200",2.8,"78",False,4,""],"8720":["LW","Tomas Fleischmann","51190","661","1000","6100",3,"82",False,4,""],"8336":["C","Valtteri Filppula","51188","659","1000","6000",3,"81",False,0,""],"8455":["C","David Legwand","51188","665","1000","6000",2.6,"78",False,4,""],"8558":["LW","Simon Gagne","51191","662","69","6000",2.3,"34",False,2,"Upper-body injury"],"12252":["G","Anders Lindback","51188","665","1000","5900",3.4,"16",False,0,""],"8374":["C","Stephen Weiss","51190","661","131","5900",2.8,"80",False,0,""],"8463":["LW","Steve Sullivan","51189","672","1000","5800",2.3,"79",False,0,""],"8709":["D","Alexander Edler","51191","677","147","5800",2.7,"82",False,4,""],"8711":["D","Kevin Bieksa","51191","677","130","5700",2.6,"78",False,0,""],"13932":["RW","Matt Read","51189","670","1000","5700",2.6,"79",False,0,""],"8799":["LW","Petr Sykora","51190","666","1000","5700",2.4,"82",False,4,""],"8355":["C","Mike Comrie","51189","672","1000","5700",1.2,"21",False,0,""],"21546":["RW","Alexander Radulov","51188","665","1000","5600",3.3,"9",False,4,""],"8378":["LW","David Booth","51191","677","38","5600",2.4,"62",False,0,""],"8518":["LW","Christopher Higgins","51191","677","1000","5600",2.7,"71",False,0,""],"8406":["D","Drew Doughty","51191","662","1000","5600",2.2,"77",False,4,""],"8499":["LW","Sean Bergenheim","51190","661","1000","5500",2.4,"62",False,0,""],"15239":["C","Craig Smith","51188","665","1000","5500",2.2,"72",False,0,""],"8466":["D","Ryan Suter","51188","665","198","5500",2.4,"79",False,4,""],"8346":["D","Nicklas Lidstrom","51188","659","26","5500",2.5,"70",False,4,""],"8475":["C","Travis Zajac","51190","666","75","5500",1.5,"15",False,0,""],"12843":["C","Adam Henrique","51190","666","1000","5500",2.5,"74",False,0,""],"8342":["RW","Daniel Cleary","51188","659","1000","5500",2.2,"75",False,0,""],"8443":["RW","Andrei Kostitsyn","51188","665","142","5400",2,"72",False,0,""],"9168":["RW","Todd Bertuzzi","51188","659","1000","5400",2.5,"71",False,4,""],"8268":["D","Brian Campbell","51190","661","173","5400",2.1,"82",False,4,""],"8668":["D","Andrej Meszaros","51189","670","1000","5400",1.9,"62",False,2,"Lower back surgery"],"8444":["LW","Sergei Kostitsyn","51188","665","191","5400",2.2,"75",False,0,""],"12494":["LW","Jiri Hudler","51188","659","1000","5400",2.5,"81",False,0,""],"8468":["D","Dan Hamhuis","51191","677","1000","5400",2.2,"82",False,0,""],"8464":["LW","Colin Wilson","51188","665","1000","5400",2.2,"68",False,0,""],"8877":["D","Jason Garrison","51190","661","1000","5300",2.2,"77",False,0,""],"8811":["RW","Jannik Hansen","51191","677","1000","5300",2.1,"82",False,4,""],"8600":["LW","Matt Cooke","51189","672","1000","5300",2.1,"82",False,0,""],"8568":["D","Kimmo Timonen","51189","670","144","5300",2.3,"76",False,0,""],"8579":["RW","Scottie Upshall","51190","661","1000","5200",1.5,"26",False,0,""],"8736":["G","Manny Legace","51191","677","1000","5200",0,0,False,0,""],"9013":["G","Jason Bacashihua","51189","670","1000","5200",0,0,False,0,""],"8795":["G","Johan Backlund","51189","670","1000","5200",0,0,False,0,""],"8476":["RW","Dainius Zubrus","51190","666","1000","5200",2,"82",False,4,""],"9235":["G","Matt Climie","51191","677","1000","5200",2,"1",False,0,""],"8287":["D","Kyle Quincey","51188","659","1000","5200",2.1,"72",False,0,""],"9440":["LW","Scott Parse","51191","662","1000","5200",1.6,"9",False,2,"Hip surgery"],"8457":["C","Marcel Goc","51190","661","1000","5200",2,"57",False,4,""],"9167":["G","Thomas McCollum","51188","659","1000","5200",-2,"1",False,0,""],"8340":["C","Darren Helm","51188","659","1000","5200",1.7,"68",False,0,""],"11370":["G","Eddie Lack","51191","677","1000","5200",0,0,False,0,""],"8934":["G","Tyler Plante","51190","661","1000","5200",0,0,False,0,""],"8257":["RW","Tomas Kopecky","51190","661","1000","5100",1.7,"80",False,4,""],"8679":["LW","Alexei Ponikarovsky","51190","666","1000","5100",1.8,"82",False,0,""],"8279":["LW","Wojtek Wolski","51190","661","1000","5100",1.4,"31",False,0,""],"8564":["RW","Ian Laperriere","51189","670","1000","5100",0,0,False,2,"Post-concussion syndrome"],"8485":["RW","Nicklas Bergfors","51188","665","1000","5100",0.7,"11",False,0,""],"8691":["D","Ian White","51188","659","1000","5000",2.4,"77",False,0,""],"8707":["D","Sami Salo","51191","677","1000","5000",1.9,"69",False,0,""],"8706":["D","Willie Mitchell","51191","662","1000","5000",1.7,"76",False,0,""],"11240":["LW","Eric Wellwood","51189","670","1000","5000",2.1,"24",False,0,""],"8360":["LW","Dustin Penner","51191","662","1000","5000",1.4,"65",False,0,""],"8700":["LW","Mason Raymond","51191","677","1000","5000",2,"55",False,0,""],"8465":["RW","Jordin Tootoo","51188","665","1000","4900",1.8,"77",False,0,""],"8603":["RW","Max Talbot","51189","670","1000","4900",1.9,"81",False,0,""],"8878":["D","Dmitry Kulikov","51190","661","1000","4900",1.9,"58",False,0,""],"8395":["C","Jarret Stoll","51191","662","1000","4900",1.6,"78",False,0,""],"8348":["D","Niklas Kronwall","51188","659","165","4900",1.9,"82",False,0,""],"8435":["C","Maxim Lapierre","51191","677","1000","4900",1.4,"82",False,0,""],"8365":["RW","Zack Stortini","51188","665","1000","4900",2.2,"1",False,0,""],"9325":["RW","Patrick Eaves","51188","659","1000","4900",1.2,"10",False,2,"Broken jaw"],"8504":["RW","Trent Hunter","51191","662","1000","4800",0.9,"38",False,0,""],"8179":["LW","Marco Sturm","51190","661","145","4800",0.7,"48",False,0,""],"9128":["RW","Brandon Yip","51188","665","1000","4800",1.2,"35",False,0,""],"8375":["C","Steve Reinprecht","51191","677","1000","4800",1.3,"29",False,0,""],"8749":["RW","Matt Halischuk","51188","665","1000","4700",1.7,"73",False,0,""],"8967":["D","Alec Martinez","51191","662","1000","4700",1.3,"51",False,0,""],"8969":["D","Viatcheslav Voynov","51191","662","1000","4700",1.9,"54",False,0,""],"11401":["LW","Gabriel Bourque","51188","665","1000","4700",1.6,"43",False,4,""],"15308":["C","Sean Couturier","51189","670","1000","4700",1.8,"77",False,0,""],"8428":["D","Marek Zidlicky","51190","666","188","4700",1.3,"63",False,4,""],"8196":["C","Paul Gaustad","51188","665","1000","4700",1.4,"70",False,0,""],"8567":["D","Braydon Coburn","51189","670","136","4700",1.5,"81",False,4,""],"8569":["D","Matt Carle","51189","670","1000","4700",1.9,"82",False,0,""],"12259":["D","Brendan Smith","51188","659","1000","4600",1.9,"14",False,0,""],"9145":["D","Ben Lovejoy","51189","672","1000","4600",1.1,"34",False,0,""],"8815":["D","Aaron Rome","51191","677","1000","4600",1.1,"43",False,0,""],"9401":["RW","Brian McGrattan","51188","665","1000","4600",0.7,"30",False,0,""],"8486":["D","Paul Martin","51189","672","113","4600",1.5,"73",False,0,""],"9266":["C","Brayden Schenn","51189","670","1000","4600",1.7,"54",False,4,""],"9218":["C","Mike Santorelli","51190","661","1000","4600",1.2,"60",False,0,""],"8935":["RW","Mike Duco","51191","677","1000","4500",1.3,"6",False,0,""],"8341":["LW","Justin Abdelkader","51188","659","1000","4500",1.5,"81",False,0,""],"12262":["D","Roman Josi","51188","665","1000","4500",1.3,"52",False,0,""],"8561":["RW","Arron Asham","51189","672","1000","4500",1.1,"64",False,4,""],"8587":["D","Zbynek Michalek","51189","672","1000","4500",1.1,"62",False,0,""],"8607":["D","Brooks Orpik","51189","672","1000","4500",1.2,"73",False,0,""],"8343":["RW","Tomas Holmstrom","51188","659","65","4500",1.4,"74",False,0,""],"8879":["C","Shawn Matthias","51190","661","1000","4500",1.6,"79",False,0,""],"8469":["D","Kevin Klein","51188","665","1000","4500",1.2,"66",False,0,""],"10815":["RW","Mark Mancari","51191","677","1000","4400",0.3,"6",False,0,""],"11152":["LW","Zack Kassian","51191","677","1000","4400",1.3,"44",False,0,""],"8397":["LW","Brad Richardson","51191","662","1000","4400",1,"59",False,0,""],"8621":["LW","Jody Shelley","51189","670","1000","4400",0.7,"30",False,0,""],"8359":["LW","Ethan Moreau","51191","662","1000","4400",0.8,"28",False,0,""],"8328":["D","Matt Niskanen","51189","672","1000","4400",1.6,"75",False,0,""],"8511":["D","Jack Hillen","51188","665","1000","4400",0.8,"55",False,0,""],"11237":["C","Zac Rinaldo","51189","670","1000","4400",1.5,"66",False,0,""],"8385":["RW","Michal Repik","51190","661","1000","4400",1.5,"17",False,0,""],"8664":["LW","Drew Miller","51188","659","1000","4400",1.6,"80",False,0,""],"8848":["RW","Chris Conner","51188","659","1000","4300",1.6,"8",False,0,""],"9087":["C","Nick Spaling","51188","665","1000","4300",1.2,"77",False,0,""],"8325":["RW","Krys Barch","51190","661","1000","4300",1,"51",False,0,""],"8181":["RW","Byron Bitz","51191","677","1000","4300",1.7,"10",False,0,""],"9275":["LW","Kyle Clifford","51191","662","1000","4300",1.1,"81",False,0,""],"8409":["D","Matt Greene","51191","662","1000","4300",1,"82",False,0,""],"9299":["RW","Jack Skille","51190","661","1000","4300",1.1,"46",False,0,""],"8872":["D","Alexandre Picard","51189","672","1000","4300",1,"17",False,0,""],"8137":["C","Andrew Ebbett","51191","677","1000","4300",1.8,"18",False,0,""],"8386":["D","Keith Ballard","51191","677","1000","4300",1,"47",False,0,""],"9083":["D","Ryan Ellis","51188","665","1000","4200",1.5,"32",False,0,""],"8296":["C","Samuel Pahlsson","51191","677","1000","4200",1,"80",False,0,""],"8850":["D","Deryk Engelland","51189","672","1000","4200",1.3,"73",False,0,""],"8705":["RW","Steve Bernier","51190","666","1000","4200",1,"32",False,0,""],"8349":["D","Brad Stuart","51188","659","1000","4200",1.4,"81",False,0,""],"8548":["D","Anton Volchenkov","51190","666","1000","4200",0.8,"72",False,0,""],"8166":["D","Pavel Kubina","51189","670","129","4200",1.2,"69",False,0,""],"8671":["D","Matt Walker","51189","670","1000","4100",0.7,"4",False,0,""],"8256":["C","John Madden","51190","661","1000","4100",0.6,"31",False,0,""],"8725":["RW","Matt Bradley","51190","661","1000","4100",0.8,"45",False,2,"Concussion"],"9085":["C","Cal O'Reilly","51189","672","1000","4100",0.4,"33",False,0,""],"9281":["C","Oscar Moller","51191","662","1000","4100",1.5,"13",False,0,""],"8597":["RW","Craig Adams","51189","672","1000","4100",0.9,"82",False,0,""],"8490":["D","Andy Greene","51190","666","1000","4100",1.1,"56",False,0,""],"12192":["C","Jacob Josefson","51190","666","1000","4100",1.1,"41",False,0,""],"9043":["D","Jakub Kindl","51188","659","1000","4100",1.2,"55",False,0,""],"8586":["D","Ed Jovanovski","51190","661","1000","4100",0.8,"66",False,0,""],"8140":["C","Ryan Carter","51190","666","1000","4100",0.7,"72",False,0,""],"8852":["C","Dustin Jeffrey","51189","672","1000","4100",1,"26",False,0,""],"9026":["LW","Steve MacIntyre","51189","672","1000","4000",0.1,"12",False,0,""],"8489":["D","Bryce Salvador","51190","666","1000","4000",0.9,"82",False,0,""],"8161":["LW","Eric Boulton","51190","666","1000","4000",0.5,"51",False,0,""],"8350":["D","Jonathan Ericsson","51188","659","1000","4000",1.1,"69",False,0,""],"8943":["RW","Vladimir Zharkov","51190","666","1000","4000",-0.5,"4",False,0,""],"10883":["D","Matthew Corrente","51190","666","1000","4000",1.2,"22",False,0,""],"8734":["C","Manny Malhotra","51191","677","1000","4000",0.8,"78",False,0,""],"8207":["D","Henrik Tallinder","51190","666","1000","4000",0.6,"39",False,2,"Lower left leg"],"12687":["D","Mark Fayne","51190","666","1000","4000",1,"82",False,0,""],"8966":["C","Trevor Lewis","51191","662","1000","4000",0.9,"72",False,0,""],"8863":["LW","Tom Sestito","51189","670","1000","4000",1.5,"14",False,2,"Torn groin muscle"],"8774":["D","Francis Bouillon","51188","665","1000","4000",0.7,"66",False,0,""],"9008":["D","Marc-Andre Gragnani","51191","677","1000","4000",1.2,"58",False,0,""],"8645":["RW","Cam Janssen","51190","666","1000","4000",0.4,"48",False,0,""],"10851":["D","Andreas Lilja","51189","670","1000","3900",0.9,"46",False,0,""],"9341":["D","Jay Leach","51190","666","1000","3900",0.4,"7",False,0,""],"9279":["C","Andrei Loktionov","51191","662","1000","3900",1,"39",False,0,""],"8330":["D","Nicklas Grossman","51189","670","1000","3900",0.8,"74",False,0,""],"11384":["C","Joakim Andersson","51188","659","1000","3900",0.4,"5",False,0,""],"12765":["D","Matt Taormina","51190","666","1000","3900",1.2,"30",False,0,""],"8170":["D","Boris Valabik","51189","672","1000","3900",0,0,False,0,""],"8324":["LW","Fabian Brunnstrom","51188","659","1000","3900",0.7,"5",False,0,""],"8250":["D","Andrew Alberts","51191","677","1000","3900",0.7,"44",False,0,""],"8965":["LW","Dwight King","51191","662","1000","3900",2.1,"27",False,4,""],"8450":["D","Hal Gill","51188","665","1000","3800",0.7,"76",False,0,""],"8756":["C","Colin Fraser","51191","662","1000","3800",0.8,"67",False,0,""],"9267":["RW","Kevin Westgarth","51191","662","1000","3800",0.7,"25",False,0,""],"8197":["C","Adam Mair","51189","670","1000","3800",0.4,"65",False,0,""],"12193":["LW","Mattias Tedenby","51190","666","1000","3800",0.5,"43",False,0,""],"8690":["D","Garnet Exelby","51188","659","1000","3800",0,0,False,0,""],"9277":["D","Peter Harrold","51190","666","1000","3800",0.8,"11",False,0,""],"9115":["D","Tyson Strachan","51190","661","1000","3800",1,"15",False,0,""],"10912":["D","Marc-Andre Bourdon","51189","670","1000","3800",1.2,"45",False,0,""],"8963":["D","Davis Drewiske","51191","662","1000","3800",1.2,"9",False,0,""],"8855":["C","Joe Vitale","51189","672","1000","3800",1,"68",False,0,""],"9023":["D","Tyler Sloan","51188","665","1000","3800",0.5,"33",False,0,""],"9306":["C","Kyle Wilson","51188","665","1000","3700",-0,"5",False,0,""],"8498":["RW","Richard Park","51189","672","1000","3700",1,"54",False,0,""],"9154":["RW","Bill Thomas","51190","661","1000","3700",0.9,"7",False,0,""],"11225":["RW","Dale Weise","51191","677","1000","3700",0.9,"68",False,0,""],"8876":["D","Keaton Ellerby","51190","661","1000","3700",0.7,"40",False,0,""],"8460":["LW","Jerred Smithson","51190","661","1000","3700",0.5,"69",False,0,""],"8881":["RW","Victor Oreskovich","51191","677","1000","3700",1.8,"1",False,0,""],"14564":["D","Adam Larsson","51190","666","1000","3700",1,"65",False,0,""],"9215":["D","Jonathon Blum","51188","665","1000","3700",0.4,"33",False,0,""],"9276":["LW","Richard Clune","51191","662","1000","3700",0,0,False,0,""],"11304":["D","Erik Gustafsson","51189","670","1000","3600",1,"30",False,0,""],"9257":["D","Nolan Baumgartner","51191","677","1000","3600",0,0,False,0,""],"9170":["D","Doug Janik","51188","659","1000","3600",0.9,"9",False,0,""],"9169":["C","Cory Emmerton","51188","659","1000","3500",0.8,"71",False,0,""],"12291":["D","Chris Tanev","51191","677","1000","3500",0.8,"25",False,0,""],"9048":["D","Oskars Bartulis","51189","670","1000","3500",-0,"13",False,0,""],"9172":["LW","Jan Mursak","51188","659","1000","3500",0.7,"25",False,0,""],"9219":["D","Nolan Yonkman","51190","661","1000","3500",0,"1",False,0,""],"9352":["C","Blair Betts","51189","670","1000","3500",0.6,"75",False,2,"Lower-body injury"],"8891":["D","Mike Weaver","51190","661","1000","3500",0.7,"82",False,0,""],"8849":["D","Simon Despres","51189","672","1000","3500",1.4,"18",False,0,""],"9019":["RW","Andrew Gordon","51191","677","1000","3400",0.5,"37",False,0,""],"10965":["RW","Colin McDonald","51189","672","1000","3400",0.5,"5",False,0,""],"11280":["LW","Aaron Volpatti","51191","677","1000","3400",0.7,"23",False,2,"Torn labrum"],"13631":["RW","Harry Zolnierczyk","51189","670","1000","3400",0.9,"37",False,0,""],"9365":["C","Ryan Craig","51189","672","1000","3400",0.9,"6",False,0,""],"8408":["D","Rob Scuderi","51191","662","1000","3400",0.5,"82",False,0,""],"9336":["C","James Wright","51190","661","1000","3400",-2,"1",False,0,""],"8854":["C","Eric Tangradi","51189","672","1000","3400",0.5,"24",False,0,""],"12447":["D","Erik Gudbranson","51190","661","1000","3300",0.7,"72",False,0,""],"8697":["C","Ryan Johnson","51188","659","1000","3300",0.7,"34",False,0,""],"12738":["C","Jordan Nolan","51191","662","1000","3300",1,"26",False,0,""],"8344":["RW","Jason Williams","51189","672","169","3300",1.1,"8",False,0,""],"9321":["C","Tomas Tatar","51188","659","1000","3200",0.6,"9",False,0,""],"12892":["C","Chris Mueller","51188","665","1000","3200",0.2,"4",False,0,""],"13982":["C","Gustav Nyquist","51188","659","1000","3200",1.4,"18",False,0,""],"9021":["RW","Steve Pinizzotto","51191","677","1000","3200",0,0,False,2,"Dislocated shoulder, surgery"],"8942":["D","Alexander Urbom","51190","666","1000","3200",1.5,"5",False,0,""],"10886":["RW","Stephen Gionta","51190","666","1000","3200",4.8,"1",False,0,""],"9343":["C","Tim Sestito","51190","666","1000","3200",-0,"18",False,0,""],"8570":["D","Ryan Parent","51191","677","1000","3200",-0.5,"4",False,0,""],"10889":["C","Brad Mills","51190","666","1000","3200",0.3,"27",False,0,""],"9295":["C","Mark Cullen","51190","661","1000","3100",0.8,"6",False,0,""],"8962":["RW","Marc-Andre Cliche","51191","662","1000","3100",0,0,False,0,""],"11233":["C","Ben Holmstrom","51189","670","1000","3100",0.3,"5",False,0,""],"11008":["D","Anders Eriksson","51191","677","1000","3100",0,0,False,0,""],"10915":["C","Jonathan Matsumoto","51190","661","1000","3100",0,"1",False,0,""],"11204":["D","Jacob Muzzin","51191","662","1000","3100",0.3,"11",False,0,""],"9084":["D","Teemu Laakso","51188","665","1000","3100",0.3,"9",False,0,""],"21921":["C","Riley Sheahan","51188","659","1000","3000",2.2,"1",False,0,""],"8411":["D","Thomas Hickey","51191","662","1000","3000",0,0,False,0,""],"12635":["C","Bracken Kearns","51190","661","1000","3000",0.5,"5",False,0,""],"12714":["C","Ilari Filppula","51188","659","1000","3000",0,0,False,0,""],"14183":["G","Keith Kinkaid","51190","666","1000","3000",0,0,False,0,""],"14456":["D","Mattias Ekholm","51188","665","1000","3000",-0.3,"2",False,0,""],"8424":["RW","Owen Nolan","51191","677","172","3000",0,0,False,0,""],"9402":["C","Steve Zalewski","51190","666","1000","3000",0.1,"7",False,0,""],"8847":["D","Robert Bortuzzo","51189","672","1000","3000",0.5,"6",False,0,""],"9238":["D","Dan Jancevski","51189","670","1000","3000",0,0,False,0,""],"8661":["LW","Todd Fedoruk","51191","677","1000","3000",0,0,False,0,""],"8817":["D","Yann Sauve","51191","677","1000","3000",0.1,"5",False,0,""],"9070":["D","Shaun Heshka","51191","662","1000","3000",0,0,False,0,""],"8177":["C","Steve Begin","51191","677","1000","3000",-0.1,"2",False,0,""],"9057":["G","Timo Pielmeier","51190","666","1000","3000",-3.6,"1",False,0,""],"8924":["LW","Chris Minard","51188","659","1000","3000",0,0,False,0,""],"9300":["LW","Mark Santorelli","51188","665","1000","3000",0,0,False,0,""],"11214":["LW","Ryan Thang","51188","665","1000","3000",0,"1",False,0,""],"11245":["D","Brian Strait","51189","672","1000","3000",0.3,"9",False,0,""],"11386":["D","Carl Sneep","51189","672","1000","3000",3,"1",False,0,""],"10898":["C","Brodie Dupont","51188","665","1000","3000",0.4,"1",False,0,""],"10840":["D","Ray Macias","51191","662","1000","3000",-0.3,"2",False,0,""],"9358":["RW","Stefan Legein","51191","662","1000","3000",0,0,False,0,""],"8882":["C","Scott Timmins","51190","661","1000","3000",0.1,"19",False,0,""],"12164":["LW","Andrew Rowe","51189","670","1000","3000",0,0,False,0,""]}
try:
conn = MySQLdb.connect(db="FanDueldb",host="localhost",user="root",passwd="password");
print "Connected<br/>"
except:
print "Cannot connect to server.</br>"
sys.exit(1)
try:
cursor = conn.cursor()
for playerdata in FD_playerpicker:
x = FD_playerpicker[playerdata][0]
cursor.execute("INSERT INTO `fandueldb`.`tblfdsalarytmp` (PlayerID,Position,Player,Placeholder1,Placeholder2,PlaceHolder3,Salary,PPG,GamesPlayed,PlaceHolder4,PlaceHolder5,PlaceHolder6) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(playerdata,FD_playerpicker[playerdata][0],FD_playerpicker[playerdata][1],FD_playerpicker[playerdata][2],FD_playerpicker[playerdata][3],FD_playerpicker[playerdata][4],FD_playerpicker[playerdata][5],FD_playerpicker[playerdata][6],FD_playerpicker[playerdata][7],FD_playerpicker[playerdata][8],FD_playerpicker[playerdata][9],FD_playerpicker[playerdata][10]))
cursor.execute("SELECT * FROM tblFDSalarytmp WHERE RecordID = 1")
result = cursor.fetchall()
for record in result:
print record[0] , "-->", record[1]
#cursor.close()
except MySQLdb.Error, e:
print "query failed<br/>"
print e
conn.close()
print "Disconnected<br/>"
sys.exit(0)
The code runs without error, and I can print out results when debugging the code. But when I open MySQL workbench to view the written data, nothing is there. There is an auto increment field called recordID that has been incremented, so I know the data has "touched" the database, but it's as if it gets deleted once the code stops running.
First time using any such database, I know there's something I'm just not understanding. Any help would be very appreciated, thanks everyone!

I believe MySQLdb disables autocommit by default. You'll need to call .commit() on your connection after all your INSERT statements have been executed.
cursor = conn.cursor()
for playerdata in FD_playerpicker:
x = FD_playerpicker[playerdata][0]
cursor.execute("INSERT INTO `fandueldb`.`tblfdsalarytmp` (PlayerID,Position,Player,Placeholder1,Placeholder2,PlaceHolder3,Salary,PPG,GamesPlayed,PlaceHolder4,PlaceHolder5,PlaceHolder6) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(playerdata,FD_playerpicker[playerdata][0],FD_playerpicker[playerdata][1],FD_playerpicker[playerdata][2],FD_playerpicker[playerdata][3],FD_playerpicker[playerdata][4],FD_playerpicker[playerdata][5],FD_playerpicker[playerdata][6],FD_playerpicker[playerdata][7],FD_playerpicker[playerdata][8],FD_playerpicker[playerdata][9],FD_playerpicker[playerdata][10]))
# Commit following all INSERT statements
conn.commit()
From the MySQLdb FAQ:
Starting with 1.2.0, MySQLdb disables autocommit by default, as required by the DB-API standard (PEP-249). If you are using InnoDB tables or some other type of transactional table type, you'll need to do connection.commit() before closing the connection, or else none of your changes will be written to the database.

Related

UnitTesting: Mock pyodbc cursor messages

I've been trying to test the below metheod specially the if block and have tried multiple things like patching, mocking in various combinations of pyodbc but I've not been able to mock the if condition.
def execute_read(self, query):
dbconn = pyodbc.connect(self.connection_string, convert_unicode=True)
with dbconn.cursor() as cur:
cursor = cur.execute(query)
if not cursor.messages:
res = cursor.fetchall()
else:
raise Exception(cursor.messages[0][1])
return res;
# unit test method
#patch.object(pyodbc, 'connect')
def test_execute_read(self, pyodbc_mock):
pyodbc_mock.return_value = MagicMock()
self.assertIsNotNone(execute_read('query'))
I've read the docs of unittest.mock, but I haven't found a way to get this above if condition covered. Thank you.
You would want to patch the Connection class (given the Cursor object is immutable) and supply a return value for covering the if block. Something that may look like:
with patch.object("pyodbc.Connection") as conn:
conn.cursor().messages = []
...
Tried this with sqlite3 and that worked for me.
Here's an example of using the patch object, something I wrote for frappe/frappe:
def test_db_update(self):
with patch.object(Database, "sql") as sql_called:
frappe.db.set_value(
self.todo1.doctype,
self.todo1.name,
"description",
f"{self.todo1.description}-edit by `test_for_update`",
)
first_query = sql_called.call_args_list[0].args[0]
second_query = sql_called.call_args_list[1].args[0]
self.assertTrue(sql_called.call_count == 2)
self.assertTrue("FOR UPDATE" in first_query)

Why cant this specific delete statment remove this row im my SQL Database

I'm trying to perfect this SQL delete statement for Python, where currently I want to just delete this row so I can get the formatting right. However the row just isn't being deleted. Could I have some friendly feedback on why it isn't being removed.
I call my function like this:-
self.DeleteOrderFromBook("Admin", "0", "100", "50", "2021-02-26 20:29:39.297658")
def DeleteOrderFromBook(self,Name,BuyOrSell,Amount,Price,Time):
try:
conn = sqlite3.connect("Books.db")
c = conn.cursor()
fml = "DELETE FROM TraidingBook WHERE Username='{0}' AND BuyOrSell='{1}' AND Amount='{2}' AND Price='{3}' AND TimeStamp='{4}';".format(Name,BuyOrSell,Amount,Price,Time)
print(fml)
c.execute(fml)
conn.commit()
conn.close()
return True
except:
return False
That is the python function for making the string.
DELETE FROM TraidingBook
WHERE Username='Admin'
AND BuyOrSell='0'
AND Amount='100'
AND Price='50'
AND TimeStamp='2021-02-26 20:29:39.297658';
However this line in the Database still exists -
('Admin', 0, 100, 50, '2021-02-26 20:29:39.297658')
Could someone please let me know what I'm doing wrong. Thanks.

Closing a connection causes stored procedure changes to rollback

Upon closing a connection, deletion done by stored procedure DeleteSproc is getting rolled back. What's wrong with this code?
try:
sql = '{CALL dbo.DeleteSproc (?,?,?,?,?,?,?,?)}'
values = (c['brandId'],c['requestUuid'],c['registrationUuid'],i['tuid'],i['tpid'],c['status'],c['responseType'],i['BookingItemIds'])
connBS = pyodbc.connect(l['connectionStrings'][0])
cursorBS = connBS.cursor()
rv = cursorBS.execute(sql, values)
sql = '{CALL dbo.StatusProc (?,?,?)}'
values = (c['requestUuid'],i['tuid'],i['tpid'])
cursorBS.execute(sql, values)
rows = cursorBS.fetchall()
finally:
cursorBS.close()
connBS.close()
I was able to solve this by putting COMMIT at the end of the stored procedure.

Using a Tkinter Optionbox to carry out a MySQL query

I am trying to use a tkinter option menu to select the variable to search for in a table. A problem arises however as the passed variable does not yield any result.
Here is the table:
And here is proof that the SQL syntax is not incorrect.
The problem is due to the string variable being incorrect and returning:
[]
no data.
When I select a variable from the OptionMenu, instead of getting:
jhgfds
I get:
('jhgfds',)
So understandably I get no result.
I have tried using these methods on the :
Creating a non-tkinter variable (`StrEditEvent)
The re method
The [2:-3] method
However these have not worked
import tkinter as tk
import mysql.connector
root=tk.Tk()
EventList=[]
def OptionChanged(*args):
EventSQL=("SELECT * FROM events WHERE eventname=%s")
print(EditEvent.get())
StrEditEvent=EditEvent.get()
print(StrEditEvent)
mycursor.execute(EventSQL,(StrEditEvent,))
myresults=mycursor.fetchall()
print(myresults)
# Adding Tracking Variable EditEvent
EditEvent = tk.StringVar()
EditEvent.trace("w", OptionChanged)
#Connecting To My Database
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="Cranmore1",
database="scoutsdatabase"
)
print(mydb)
mycursor = mydb.cursor()
mycursor.execute("SELECT eventname FROM events")
myresults=mycursor.fetchall()
for i in myresults:
EventList.append(i)
EventToEditOptionMenu = tk.OptionMenu(root,EditEvent,*EventList)
EventToEditOptionMenu.grid(row=1,column=1)
root.mainloop()
Any help would be greatly appreciated.
print(EventList)
[('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]
To get the result as jhgfds you need to iterate over it because it returns the result for the query as a tuple [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]
You can use index to get the specific result you want result[0] or result[2]
def OptionChanged(*args):
EventSQL=("SELECT * FROM events WHERE eventname=%s")
print(EditEvent.get())
StrEditEvent=EditEvent.get()
print(StrEditEvent)
mycursor.execute(EventSQL,(StrEditEvent,))
myresults=mycursor.fetchall()
for result in myresults: # iterate over it
print(result)
print(result[2])
print(result[5])
I've finally worked out the answer! To obtain the string from the tuple, one must use the map() command.
def OptionChanged(*args):
EventSQL=("SELECT * FROM events WHERE eventname=%s")
StrEditEvent=EditEvent.get()
start,mid,end=map(str,StrEditEvent.split("'"))
print(mid)
mycursor.execute(EventSQL,(mid,))
myresults=mycursor.fetchall()
print(myresults)
Thus ('jhgfds',) is converted into jhgfds so the SQL query finds the data from the database.

MySQL Python connector does not commit

I have this function that it's supposed should receive json and store the values on a RDS MySQL db.
def saveMetric(metrics):
cnx = RDS_Connect()
cursor = cnx.cursor()
jsonMetrics = json.loads(metrics)
#print type(jsonMetrics['Metrics'])
# Every 2000 registries, the script will start overriding values
persistance = 2000
save_metrics_query = (
"REPLACE INTO metrics "
"SET metric_seq = (SELECT COALESCE(MAX(row_id), 0) %% %(persistance)d + 1 FROM metrics AS m), "
"instance_id = \'%(instance_id)s\', "
"service = \'%(service)s\' , "
"metric_name = \'%(metric_name)s\', "
"metric_value = %(metric_value)f"
)
for metric in jsonMetrics['Metrics']:
formatData = {}
formatData['persistance'] = persistance
formatData['instance_id'] = arguments.dimensionValue
formatData['service'] = jsonMetrics['Service']
formatData['metric_name'] = metric
formatData['metric_value'] = jsonMetrics['Metrics'][metric]
print save_metrics_query % formatData
try:
cursor.execute(save_metrics_query, formatData, multi=True)
logger('info','Metrics were saved successfully!')
cnx.commit()
except mysql.connector.Error as err:
logger('error', "Something went wrong: %s" % err)
cursor.close()
cnx.close()
RDS_Connect() was already tested and it works just fine. The problem is that after running the function, the data is not saved to the DB. I think there is a problem with the commit but I don't see any errors or warning message. If I run the query manually, the data gets stored.
Here is the query that runs after parsing the json:
REPLACE INTO metrics SET metric_seq = (SELECT COALESCE(MAX(row_id), 0) % 2000 + 1 FROM metrics AS m), instance_id = 'i-03932937bd67622c4', service = 'AWS/EC2' , metric_name = 'CPUUtilization', metric_value = 0.670000
If it helps, this is the json that the function receives:
{
"Metrics": {
"CPUUtilization": 1.33,
"NetworkIn": 46428.0,
"NetworkOut": 38772.0
},
"Id": "i-03932937bd67622c4",
"Service": "AWS/EC2"
}
I'd appreciate some help.
Regards!
UPDATE:
I found that the problem was related to the formatting codes on the query template.
I re wrote the function like this:
def saveMetric(metrics):
cnx = RDS_Connect()
jsonMetrics = json.loads(metrics)
print json.dumps(jsonMetrics,indent=4)
persistance = 2000
row_id_query_template = "SELECT COALESCE(MAX(row_id), 0) % {} + 1 FROM metrics AS m"
row_id_query = row_id_query_template.format(persistance)
save_metrics_query = (
"REPLACE INTO metrics "
"SET metric_seq = (" + row_id_query + "),"
"instance_id = %(instance_id)s,"
"service = %(service)s,"
"metric_name = %(metric_name)s,"
"metric_value = %(metric_value)s"
)
for metric in jsonMetrics['Metrics']:
formatData = {}
formatData['instance_id'] = arguments.dimensionValue
formatData['service'] = jsonMetrics['Service']
formatData['metric_name'] = metric
formatData['metric_value'] = jsonMetrics['Metrics'][metric]
if arguments.verbose == True:
print "Data: ",formatData
print "Query Template: ",save_metrics_query.format(**formatData)
try:
cursor = cnx.cursor()
cursor.execute(save_metrics_query, formatData)
logger('info','Metrics were saved successfully!')
cnx.commit()
cursor.close()
except mysql.connector.Error as err:
logger('error', "Something went wrong: %s" % err)
cnx.close()
As you can see, I format the SELECT outside. I believe the whole problem was due to this line:
"metric_value = %(metric_value)f"
I changed to:
"metric_value = %(metric_value)s"
and now it works. I think the formatting was wrong, given a syntax error (tho I don't know how the exception was never thrown).
Thanks to everyone who took time to help me!
I haven't actually used MySQL, but the docs seem to indicate that calling cursor.execute with multi=True just returns an iterator. If that is true, then it wouldn't actually insert anything - you'd need to call .next() on the iterator (or just iterate over it) to actually insert the record.
It also goes on to advise against using parameters with multi=True:
If multi is set to True, execute() is able to execute multiple statements specified in the operation string. It returns an iterator that enables processing the result of each statement. However, using parameters does not work well in this case, and it is usually a good idea to execute each statement on its own.
tl;dr: remove that parameter, as the default is False.
This was the solution. I changed:
"metric_value = %(metric_value)f"
To:
"metric_value = %(metric_value)s"
Doing some troubleshooting I found a syntax error on the SQL. Somehow the exception didn't show.

Categories

Resources