Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DCon(object):
- def __init__(self, db):
- self.mydb = mysql.connector.connect(
- host="localhost",
- user="user",
- password="password",
- database=db
- )
- def __delete__(self, instance):
- self.mydb.close()
- def insert_values(self, table_name, column_string, value_list):
- # example: "INSERT INTO 'customers' ('name, address') VALUES ('john, this_street_11')"
- assert column_string.count(",")+1 == len(value_list)
- formatted_values = str()
- for entry in value_list:
- formatted_values += "'%s', " % entry
- formatted_values = formatted_values[:len(formatted_values)-2]
- sql = "INSERT INTO %s (%s) VALUES (%s)" % (table_name, column_string, formatted_values)
- cursor = self.mydb.cursor()
- cursor.execute(sql)
- self.mydb.commit()
- def select_where(self, table_name, column_list, value_list, get_col=False):
- assert len(column_list) == len(value_list)
- statement = str()
- for pair in range(len(value_list)):
- statement += "%s = '%s' " % (column_list[pair], value_list[pair])
- if pair != len(value_list)-1:
- statement += "AND "
- sql = "SELECT * FROM %s WHERE %s" % (table_name, statement)
- cursor = self.mydb.cursor()
- cursor.execute(sql)
- result = self.convert_query_to_dict(table_name, cursor.fetchall())
- if get_col is not False:
- result = result[get_col]
- return result
- con = DCon("db")
- found_sub = con.select_where("table_name", ["post_id"], [sub.id])
- if not found_sub["post_id"]:
- log_submission(sub)
- while not found_sub["post_id"]:
- time.sleep(0.2)
- del con
- con = DCon("avanzadb")
- found_sub = con.select_where("table_name", ["post_id"], [sub.id])
- print("stuck in loop")
- print(found_sub["post_id"])
- print(sub.id)
Add Comment
Please, Sign In to add comment