Using a Tkinter Optionbox to carry out a MySQL query - python

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.

Related

Python binding variables

I am completely lost here:
v_sql = "SELECT widget_name, widget_url FROM widget_calls WHERE widget_name = :widget"
cursor.execute(v_sql, widget=widget_name)
df_wid = pd.read_sql(v_sql, con=connection)
Result:
pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT widget_name, widget_url FROM widget_calls WHERE widget_name = :widget': ORA-01008: not all variables bound
There is 1 bind variable, :widget, and 1 declaration in the execute. So, what am I missing?
The cursor.execute call and the pd.read_sql call are completely unrelated. You're doing the query twice, and throwing away the first result. I would delete the useless cursor.execute.
And for read_sql, you need:
df_wid = pd.read_sql( v_sql, con=connection, params={'widget': widget_name})

Import data from python (probleme with where condition)

I work in Python
I have code that allows me to import a dataset that works fine. However in my dataset I have 3 different patients and I would like to import only the patient that interests me (possible by adding the WHERE statement in the SQL query.
So the following code works:
def importecdata():
query2 = "SELECT TECDATA.[Vol_Recalage_US_VD], TECDATA.[Vol_Recalage_Us_VG], TECDATA.[SUBJID] FROM TECDATA INNER JOIN MEDDATA ON TECDATA.DateTime = MEDDATA.DateTime WHERE TECDATA.[SUBJID]='patient14';"
dftec1 = pd.read_sql(query2, sql_conn, chunksize=100000)
dftec = pd.concat(dftec1)
return(dftec)
It return the patient 14 data
But now I want to put the patient's name as a variable in my function so I made the following code:
def importecdata(patient):
query2 = "SELECT TECDATA.[Vol_Recalage_US_VD], TECDATA.[Vol_Recalage_Us_VG], TECDATA.[SUBJID] FROM TECDATA INNER JOIN MEDDATA ON TECDATA.DateTime = MEDDATA.DateTime WHERE TECDATA.[SUBJID]=patient;"
dftec1 = pd.read_sql(query2, sql_conn, chunksize=100000)
dftec = pd.concat(dftec1)
return(dftec)
I chek and the patient variable got the value patient14. But it don't work... i try to modify the value of the variable patient to 'patient14' it don't work too i have the same error :
invalid column name \xa0: 'patient'. So the code works, the problem is from the "where" condition with the patient variable
(sorry for my english i'm french)
You have to add your patient value in the query string check below code:
def importecdata(patient):
query2 = "SELECT TECDATA.[Vol_Recalage_US_VD], TECDATA.[Vol_Recalage_Us_VG], TECDATA.[SUBJID] FROM TECDATA INNER JOIN MEDDATA ON TECDATA.DateTime = MEDDATA.DateTime WHERE TECDATA.[SUBJID]='{0}';"
query2 = query2.format(patient)
dftec1 = pd.read_sql(query2, sql_conn, chunksize=100000)
dftec = pd.concat(dftec1)
return(dftec)

Issues returning python parameter to main function

problem: Im trying to extract values out of a mysql search to use later within the code.
I have setup a mysql function (connector_mysql) that i use to connect/run the mysql commands.
The problem i'm having is returning these values back out of the mysql function to the rest of the code.
There are two scenarios that i've tried which i think should work but dont when run.
full code sample below...
1. In the mysql function result acts like a dictionary if - using just..
result = cursor.fetchone(variable1, variable2, variable3)
return result
AND in the main function
result1 = connector_mysql(subSerialNum, ldev, today_date)
print(result1)
This appears to work and when printing result1 i get a dictionary looking output:
{'ldev_cap': '0938376656', 'ldev_usedcap': '90937763873'}
HOWEVER...
I cant then use dictionary methods to get or separate the values out.. eg
used_capacity = result1['ldev_cap']
which i would have expected that now 'used_capacity' to represent or equal 0938376656.
Instead i get an error about the object is not able to be subscripted...?
Error below:
File "/Users/graham/PycharmProjects/VmExtrat/VmExtract.py", line 160, in openRead
used_capacity = result1['ldev_cap']
TypeError: 'NoneType' object is not subscriptable
In the mysql function the result acts like a dictionary if I manipulate it and try and return multiple values with the tuple concept - using..
cursor.execute(sql_query, {'serial': subSerialNum, 'lun': ldev, 'todayD': today_date})
result = cursor.fetchone()
while result:
ldev_cap = result['ldev_cap']
ldev_usdcap = result['ldev_usdcap']
return ldev_cap, ldev_usdcap
Here, result acts like a dictionary and i'm able to assign a parameter to the key like:
ldev_cap = result['ldev_cap']
and if you print ldev_cap you get the correct figure...
If I return one figure, the main function line of:
result1 = connector_mysql(subSerialNum, ldev, today_date)
it Works....
HOWEVER...
When then trying to return multiple parameters from the mysql function by doing
return ldev_cap, ldev_usdcap
and in the main function:
capacity, usd_capacity = connector_mysql(subSerialNum, ldev, today_date)
I get errors again
File "/Users/graham/PycharmProjects/VmExtrat/VmExtract.py", line 156, in openRead
capacity, usd_capacity = connector_mysql(subSerialNum, ldev, today_date)
TypeError: 'NoneType' object is not iterable
I think i'm doing the right thing with the dictionary and the tuple but i'm obviously missing something or not doing it correctly, as i need to do this with 4-5 paramaters per sql query,
I didn't want to do multiple querys for the same thing to get the individual paramaters out.
Any help or suggestions would be greatly welcomed...
full code below.
main code:
capacity, usd_capacity = connector_mysql(subSerialNum, ldev, today_date)
print(capacity)
print(usd_capacity)
def connector_mysql(subSerialNum, ldev, today_date):
import pymysql.cursors
db_server = 'localhost'
db_name = 'CBDB'
db_pass = 'secure_password'
db_user = 'user1'
sql_query = (
"SELECT ldev_cap, ldev_usdcap FROM Ldevs WHERE sub_serial=%(serial)s "
"and ldev_id=%(lun)s and data_date=%(todayD)s")
connection = pymysql.connect(host=db_server,
user=db_user,
password=db_pass,
db=db_name,
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
cursor.execute(sql_query, {'serial': subSerialNum, 'lun': ldev, 'todayD': today_date})
result = cursor.fetchone()
#return result #used for returning dict
while result:
ldev_cap = result['ldev_cap']
ldev_usdcap = result['ldev_usdcap']
print(result)
return ldev_cap, ldev_usdcap
finally:
connection.close()

SyntaxError on function definition

I'm trying to pull data from an Excel spreadsheet to MySQL. My script can't find the path to the Excel file, and my IDE (Spyder) is giving an error on this line:
def read_excel(r'C:\\Users\\ParaSystems Limited\\Desktop\\main.xlsx'):
invalid syntax
import openpyxl
import pymysql as mdb
def read_excel(r'C:\\Users\\ParaSystems Limited\\Desktop\\main.xlsx'):
masterdict = {}
wb = openpyxl.load_workbook('main.xlsx')
for sheet in wb:
for arow in range(2, sheet.max_row+1):
if sheet['A'+str(arow)].value:
masterdict[sheet['A'+str(arow)].value] = {
'Equipment Number':sheet['B'+str(arow)].value,
'Number':sheet['C'+str(arow)].value,
'Description':sheet['D'+str(arow)].value,
'Manufacturer':sheet['E'+str(arow)].value,
'Serial Number':sheet['F'+str(arow)].value,
'Country of Manufacturer':sheet['G'+str(arow)].value,
'Functional Location Description':sheet['H'+str(arow)].value,
'Functional Location Number (Short)':sheet['I'+str(arow)].value,
'Functional Location Number':sheet['J'+str(arow)].value,
'COST OF SERVICING AND MAINTENANCE':sheet['K'+str(arow)].value,
'Office Location':sheet['L'+str(arow)].value
}
return masterdict
def inputIntoMySQL(masterdict):
con = mdb.connect(host= '127.0.0.1', user = 'root', password =None,db='scraping')
cur = con.cursor()
with con:
cur.execute("DROP TABLE IF EXISTS main")
cur.execute("CREATE TABLE main (rid INT PRIMARY KEY, EquipmentNumber VARCHAR(75), Description VARCHAR(75),\
Manufacturer VARCHAR(50), SerialNumber INT,CountryOfManufacturer VARCHAR(25), \
FunctionalLocationDescription VARCHAR(50), FunctionalLocationNumberShort VARCHAR(75), FunctionalLocationNumber VARCHAR(25),\
CostOfServicingAndMaintenance DECIMAL(15,2),OfficeLocation VARCHAR(35))")
for i in masterdict:
cur.execute('INSERT INTO DISTRIBUTORS_NESTLE(rid, EquipmentNumber,Description,Manufacturer,SerialNumber,\
CountryOfManufacturer,FunctionalLocationDescription, FunctionalLocationNumberShort,FunctionalLocationNumber\
CostOfServicingAndMaintenance,OfficeLocation) VALUES("%s", "%s", "%s","%s","%s","%s","%s","%s","%s","%s","%s")'
%(i,masterdict[i]['Equipment Number'],masterdict[i]['Description'],
masterdict[i]['Manufacturer'],masterdict[i]['Serial Number'],masterdict[i]['Country of Manufacturer'],
masterdict[i]['Functional Location Description'], masterdict[i]['Functional Location Number (Short)'], masterdict[i]['Functional Location Number'],
masterdict[i]['COST OF SERVICING AND MAINTENANCE'], masterdict[i]['Office Location']))
con.commit()
con.close()
The syntax error is because you're defining a function (read_excel) and you're putting the excel filepath directly in the function definition - with this syntax you the excel filepath isnt assigned to a variable so you wouldn't be able to use it within the function.
def read_excel(r'C:\Users\ParaSystems Limited\Desktop\main.xlsx')#Syntax error
To fix this you could create a parameter and make that particular filepath the default value:
def read_excel(excel_file_path = r'C:\Users\ParaSystems Limited\Desktop\main.xlsx')
Then when you call the function, you can call it without any parameters and the excel_file_path will default to that e.g.
read_excel()#Calls with excel_file_path as your default value
or
read_excel(excel_file_path = r'path\to\another\excel.xlsx') #Calls with excel_file_path as the passed parameter value
If there really isn't any need to call this function on any other excel, just declare it in the read_excel function and leave the parameters blank. e.g.
def read_excel():
excel_file_path = r'C:\Users\ParaSystems Limited\Desktop\main.xlsx'
This is not a valid function definition:
def read_excel(r'C:\\Users\\ParaSystems Limited\\Desktop\\main.xlsx'):
masterdict = {}
wb = openpyxl.load_workbook('main.xlsx')
...
You don't have any named parameters inside the parentheses, just a raw string.
It looks like you actually meant something like:
def read_excel(fname=r'C:\Users\ParaSystems Limited\Desktop\main.xlsx'):
masterdict = {} # [unchanged]
wb = openpyxl.load_workbook(fname) # _Uses_ the parameter.
...
Also, since you are using a raw string (r'...'), you shouldn't need to double the backslashes.
Single backslashes should work.
(You'll have to verify this yourself.
I don't have access to a Windows system, so I can't test this.)

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.

Categories

Resources