Retrieving New Rows of MySQL Database without Restarting App in Python
Connecting to database in a class with methods to get and insert data solved my problem.
That's the class:
class DBConnection(object): def __init__(self, DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME): self.host = DB_HOST self.port = DB_PORT self.name = DB_NAME self.user = DB_USER self.password = DB_PASSWORD self.con = None def connect_db(self): if self.con is None: self.con = MySQLdb.connect(host = self.host, port = self.port, db = self.name, user = self.user, passwd = self.password) self.con.set_character_set("utf8") return self.con def fetch_db(self, query): self.query = query self.cursor = self.con.cursor() self.cursor.execute("SET NAMES utf8;") self.cursor.execute("SET CHARACTER SET utf8;") self.cursor.execute("SET character_set_connection=utf8;") self.cursor.execute(self.query) self.result = self.cursor.fetchall() return self.result def insert_db(self, query): self.query = query self.cursor = self.con.cursor() self.cursor.execute("SET NAMES utf8;") self.cursor.execute("SET CHARACTER SET utf8;") self.cursor.execute("SET character_set_connection=utf8;") self.cursor.execute(self.query) self.con.commit() return
That's how I connect (before mainloop):
DBC = DBConnection('localhost',3306,'root','','sohbet')con = DBC.connect_db()
And that's how I do queries:
result = DBC.fetch_db("SELECT * FROM ileti WHERE sohbet_id='" + str(sohbet_id) + "' ORDER BY created ASC")
However, I need to check the way of doing the query as I told by mmgp.