Guest User

Untitled

a guest
Nov 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. dbconnection = pymysql.connect(host=mysql_hostname, user=mysql_username, password=mysql_pass, db=mysql_schema, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
  2. cursor = dbconnection.cursor()
  3. _SQL = ("""
  4. select * ...
  5. """)
  6.  
  7. cursor.execute(_SQL)
  8. result = cursor.fetchall()
  9. for row in result:
  10. print(row)
  11. print("n")
  12. # How can I access each key and each value for example: STATUS is the value and 3 is the key
  13. # I want to do something like this:
  14. #'if the value of 'CAP' > 1: change the value of status where the ID key
  15. # cursor.execute("UPDATE <table> SET row[STATUS]='1' WHERE ID='row[ID]'")
  16.  
  17. {'STATUS': 3, 'ID': 10, 'CAP': 1}
  18. {'STATUS': 3, 'ID': 11, 'CAP': 2}
  19. {'STATUS': 3, 'ID': 12, 'CAP': 3}
  20.  
  21. for row in result:
  22. for key,value in row.items():
  23. print('The key is %s'%key)
  24. print('The value is %s'%value)
  25.  
  26. for row in result:
  27. if row["CAP"] > 1:
  28. cursor.execute("UPDATE <table> SET row[STATUS]='1' WHERE ID='row[ID]'")
  29. else:
  30. continue
  31.  
  32. import pymysql
  33.  
  34. connection = pymysql.connect(host="localhost", user="root", passwd="", database="myraces")
  35. # cursor = connection.cursor()
  36. cursor = pymysql.cursors.DictCursor(connection)
  37.  
  38. query = "SELECT * FROM `races`"
  39. cursor.execute(query)
  40. rows = cursor.fetchall()
  41. for row in rows:
  42. for key, value in row.items():
  43. print(key, value)
Add Comment
Please, Sign In to add comment