Python mysql output from function not working -


i'm relatively new @ python have more experience in java, understand of concepts. however, keep having issues mysql , passing information function using mysql use in function later.

i need make complex mysql queries multiple return field, don't want running multiple sql queries each sql field smash database. saying below small example of i'm trying achieve.

i wanted function run sql query (def connectory_mysql()) took parameters elsewhere, (this part works) take output of sql query , pass main function use. main function needs use different column results of sql query different parameters.

i can return result , assign result1 appears , looks dictionary when printed, i'm unable split/use different keys or data result1 = connector_mysql(subserialnum, ldev, today_date) if splituse keys in dictionary in sql function before returning ie ldev_cap = result['ldev_cap'] can print individual elements within sql function... however, cant seem pass parameters main function , split them out??

i must have missed easy or not understanding something... assistance or appreciated...

...     result1 = connector_mysql(subserialnum, ldev, today_date)  print(result1)  #this works , appears dictionary, can split                   ## elements like: ldev_cap = result1['ldev_cap']   ##-> dosn't work here.....???? if return dictonary..                 #and i'm unsure how split them when pass paramaters                  #back if split dictionary in sql function.  ...  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 ldevs sub_serial=%(serial)s "         "and ldev_id=%(lun)s , data_date=%(todayd)s")      connection = pymysql.connect(host=db_server,                                  user=db_user,                                  password=db_pass,                                  db=db_name,                                  cursorclass=pymysql.cursors.dictcursor)      try:         connection.cursor() cursor:             cursor.execute(sql_query, {'serial': subserialnum, 'lun': ldev, 'todayd': today_date})             result = cursor.fetchone()              while result:                 ldev_cap = result['ldev_cap']   #here dictionary acts                                          #expected , can assign value                 ldev_usdcap = result['ldev_usdcap']                  print(result)                 return ldev_cap, ldev_usdcap   #here can return       finally:         connection.close() 

any or assistance apricated...

cheers graham

first of all, should familiar python style guide writing python code.

based on existing code, result1 return tuple contained value of (ldev_cap, ldef_usdcap) (it not directory). access return result result1[0] corresponding return value of ldev_cap or result1[1] corresponding return value of ldev_usdcap.

alternatively, since returning 2 data, can access each return data using

ldev_cap, ldev_usdcap = connector_mysql(subserialnum, ldev, today_date) 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -