Guest User

Untitled

a guest
Aug 9th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. class DCon(object):
  2. def __init__(self, db):
  3. self.mydb = mysql.connector.connect(
  4. host="localhost",
  5. user="user",
  6. password="password",
  7. database=db
  8. )
  9.  
  10. def __delete__(self, instance):
  11. self.mydb.close()
  12.  
  13. def insert_values(self, table_name, column_string, value_list):
  14. # example: "INSERT INTO 'customers' ('name, address') VALUES ('john, this_street_11')"
  15. assert column_string.count(",")+1 == len(value_list)
  16. formatted_values = str()
  17. for entry in value_list:
  18. formatted_values += "'%s', " % entry
  19. formatted_values = formatted_values[:len(formatted_values)-2]
  20. sql = "INSERT INTO %s (%s) VALUES (%s)" % (table_name, column_string, formatted_values)
  21. cursor = self.mydb.cursor()
  22. cursor.execute(sql)
  23. self.mydb.commit()
  24.  
  25. def select_where(self, table_name, column_list, value_list, get_col=False):
  26. assert len(column_list) == len(value_list)
  27. statement = str()
  28. for pair in range(len(value_list)):
  29. statement += "%s = '%s' " % (column_list[pair], value_list[pair])
  30. if pair != len(value_list)-1:
  31. statement += "AND "
  32.  
  33. sql = "SELECT * FROM %s WHERE %s" % (table_name, statement)
  34. cursor = self.mydb.cursor()
  35. cursor.execute(sql)
  36. result = self.convert_query_to_dict(table_name, cursor.fetchall())
  37. if get_col is not False:
  38. result = result[get_col]
  39. return result
  40.  
  41. con = DCon("db")
  42. found_sub = con.select_where("table_name", ["post_id"], [sub.id])
  43. if not found_sub["post_id"]:
  44. log_submission(sub)
  45. while not found_sub["post_id"]:
  46. time.sleep(0.2)
  47. del con
  48. con = DCon("avanzadb")
  49. found_sub = con.select_where("table_name", ["post_id"], [sub.id])
  50. print("stuck in loop")
  51. print(found_sub["post_id"])
  52. print(sub.id)
Add Comment
Please, Sign In to add comment