Closing a connection causes stored procedure changes to rollback - python

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.

Related

Fetching data in realtime from database in python

I have a class for multiprocessing in Python which creates 3 different processes. First process is for checking if there is any signal from my hardware and pushing it into a Queue, second process is for getting the data out of the Queue and pushing it into a database and the third processes is for getting the data out of the database and pushing it on a server.
obj = QE()
stdFunct = standardFunctions()
watchDogProcess = multiprocessing.Process(target=obj.watchDog)
watchDogProcess.start()
pushToDBSProcess = multiprocessing.Process(target=obj.pushToDBS)
pushToDBSProcess.start()
pushToCloud = multiprocessing.Process(target=stdFunct.uploadCycleTime)
pushToCloud.start()
watchDogProcess.join()
pushToDBSProcess.join()
pushToCloud.join()
My first two processes are running perfectly as desired, however I am struggling with the third process. The following is the code of my third process :
def uploadCycleTime(self):
while True:
uploadCycles = []
lastUpPointer = "SELECT id FROM lastUploaded"
lastUpPointer = self.dbFetchone(lastUpPointer)
lastUpPointer = lastUpPointer[0]
# print("lastUploaded :"+str(lastUpPointer))
cyclesToUploadSQL = "SELECT id,machineId,startDateTime,endDateTime,type FROM cycletimes WHERE id > "+str(lastUpPointer)
cyclesToUpload = self.dbfetchMany(cyclesToUploadSQL,15)
cyclesUploadLength = len(cyclesToUpload)
if(cyclesUploadLength>0):
for cycles in cyclesToUpload:
uploadCycles.append({"dataId":cycles[0],"machineId":cycles[1],"startDateTime":cycles[2].strftime('%Y-%m-%d %H:%M:%S.%f'),"endDateTime":cycles[3].strftime('%Y-%m-%d %H:%M:%S.%f'),"type":cycles[4]})
# print("length : "+str(cyclesUploadLength))
lastUpPointer = uploadCycles[cyclesUploadLength-1]["dataId"]
uploadCycles = json.dumps(uploadCycles)
api = self.dalUrl+"/cycle-times"
uploadResponse = self.callPostAPI(api,str(uploadCycles))
print(lastUpPointer)
changePointerSQL = "UPDATE lastUploaded SET id="+str(lastUpPointer)
try:
changePointerSQL = self.dbAbstraction(changePointerSQL)
except Exception as errorPointer:
print("Pointer change Error : "+str(errorPointer))
time.sleep(2)
Now I am saving a pointer to remember the last id uploaded, and from there on keep uploading 15 packets. When there is data existing in the DB the code works well, however if there is no existing when the process is initiated and data is sent afterwards then it fails to fetch the data from the DB.
I tried printing the length in realtime, it keeps giving me 0, inspite of data being continuously pushed into the DB in real-time.
In my upload process, I missed out on a commit()
def dbFetchAll(self,dataString):
# dataToPush = self.cycletimeQueue.get()
# print(dataToPush)
dbTry = 1
try:
while(dbTry == 1): # This while is to ensure the data has been pushed
sql = dataString
self.conn.execute(sql)
response = self.conn.fetchall()
dbTry = 0
return response
# print(self.conn.rowcount, "record inserted.")
except Exception as error:
print ("Error : "+str(error))
return dbTry
***finally:
self.mydb.commit()***

Troubleshooting uncooperative For Loop of SQLalchemy results

Looking for a second set of eyes here. I cannot figure out why the following loop will not continue past the first iteration.
The 'servicestocheck' sqlalchemy query returns 45 rows in my test, but I cannot iterate through the results like I'm expecting... and no errors are returned. All of the functionality works on the first iteration.
Anyone have any ideas?
def serviceAssociation(current_contact_id,perm_contact_id):
servicestocheck = oracleDB.query(PORTAL_CONTACT).filter(
PORTAL_CONTACT.contact_id == current_contact_id
).order_by(PORTAL_CONTACT.serviceID).count()
print(servicestocheck) # returns 45 items
servicestocheck = oracleDB.query(PORTAL_CONTACT).filter(
PORTAL_CONTACT.contact_id = current_contact_id
).order_by(PORTAL_CONTACT.serviceID).all()
for svc in servicestocheck:
#
# Check to see if already exists
#
check_existing_association = mysqlDB.query(
CONTACTTOSERVICE).filter(CONTACTTOSERVICE.contact_id ==
perm_contact_id,CONTACTTOSERVICE.serviceID ==
svc.serviceID).first()
#
# If no existing association
#
if check_existing_association is None:
print ("Prepare Association")
assoc_contact_id = perm_contact_id
assoc_serviceID = svc.serviceID
assoc_role_billing = False
assoc_role_technical = False
assoc_role_commercial = False
if svc.contact_type == 'Billing':
assoc_role_billing = True
if svc.contact_type == 'Technical':
assoc_role_technical = True
if svc.contact_type == 'Commercial':
assoc_role_commercial = True
try:
newAssociation = CONTACTTOSERVICE(
assoc_contact_id, assoc_serviceID,
assoc_role_billing,assoc_role_technical,
assoc_role_commercial)
mysqlDB.add(newAssociation)
mysqlDB.commit()
mysqlDB.flush()
except Exception as e:
print(e)
This function is called from a script, and it is called from within another loop. I can't find any issues with nested loops.
Ended up being an issue with SQLAlchemy ORM (see SqlAlchemy not returning all rows when querying table object, but returns all rows when I query table object column)
I think the issue is due to one of my tables above does not have a primary key in real life, and adding a fake one did not help. (I don't have access to the DB to add a key)
Rather than fight it further... I went ahead and wrote raw SQL to move my project along.
This did the trick:
query = 'SELECT * FROM PORTAL_CONTACT WHERE contact_id = ' + str(current_contact_id) + 'ORDER BY contact_id ASC'
servicestocheck = oracleDB.execute(query)

python sqlite3 Getting error: sqlite3.OperationalError: database is locked

Can you help me, I wrote this code:
class Favorits(object):
def __init__(self, graph_):
self.graph_ = graph_
self.conn3 = sqlite3.connect('C:/C/V2.db')
def add_(self):
с3 = self.conn3.cursor()
с3.execute("SELECT COUNT(*) FROM fav")
t_count = с3.fetchall()
self.conn3.commit()
t_count = t_count[0][0]
to_add_rus = self.graph_.text_rus.get('1.0', 'end')
to_add_eng = self.graph_.text_eng.get('1.0', 'end')
to_add_esp = self.graph_.text_esp.get('1.0', 'end')
с3.execute("INSERT INTO fav VALUES(?,?,?,?)", (t_count + 1, to_add_rus, to_add_eng, to_add_esp))
self.conn3.commit()
def rem_(self):
c4 = self.conn3.cursor()
idx = (self.graph_.word_.f_0_to_remove)
idx = idx[0]
print(idx)
c4.execute("DELETE FROM fav WHERE id_=?", (idx,))
self.conn3.commit()
This is a class which I use to add and remove different rows from db (using Tkinter as GUI).
So basically I'm trying to make two different connections to the same db via two different cursors (in order to be able to add and remove words from it). And I constantly get this error:
self.conn3.commit()
sqlite3.OperationalError: database is locked
I already tried differend options, makin' two different cursors, etc. nothing helps.
You need to close the database connection after opening it. Otherwise the connection persists, and so the database believes an edit is underway so locks itself.
You can break the connection with the command self.conn3.close().

Writing data to MYSQL database from python not persistent

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.

SQLAlchemy Misuse Causing Memory Leak?

My program is sucking up a meg every few seconds. I read that python doesn't see curors in garbage collection, so I have a feeling that I might be doing something wrong with my use of pydbc and sqlalchemy and maybe not closing something somwhere?
#Set up SQL Connection
def connect():
conn_string = 'DRIVER={FreeTDS};Server=...;Database=...;UID=...;PWD=...'
return pyodbc.connect(conn_string)
metadata = MetaData()
e = create_engine('mssql://', creator=connect)
c = e.connect()
metadata.bind = c
log_table = Table('Log', metadata, autoload=True)
...
atexit.register(cleanup)
#Core Loop
line_c = 0
inserts = []
insert_size = 2000
while True:
#line = sys.stdin.readline()
line = reader.readline()
line_c +=1
m = line_regex.match(line)
if m:
fields = m.groupdict()
...
inserts.append(fields)
if line_c >= insert_size:
c.execute(log_table.insert(), inserts)
line_c = 0
inserts = []
Should I maybe move the metadata block or part of it to the insert block and close the connection each insert?
Edit:
Q: Does it every stabilize?
A: Only if you count Linux blowing away the process :-) (Graph does exclude Buffers/Cache from Memory Usage)
I would not necessarily blame SQLAlchemy. It could also be a problem of the underlaying driver. In general memory leaks are hard to detect. In any case you should ask on the SQLALchemy mailing list where the core developer Michael Bayer is responding on almost
every question...perhaps a better chance to get real help there...

Categories

Resources