title: Creating a button to delete data from table in qt
body: <p>Using mysql and python, I have created a table within PyQt that will allow a user to frequently update and track their meeting sessions. The only problem is, I do not know how I would go about coding a button that will allow me to individually delete a row of data. </p>
  
  <p>My table looks like so:</p>
  
  <p><img src="https://i.stack.imgur.com/PkLpr.png" alt="enter image description here"></p>
  
  <p>What would be the simplest way to create a button that will appear besides the row of data when new data is inserted that will allow the user to delete that entire row?</p>
  
  <p>Edit:</p>
  
  <pre><code>def deleteRows(self):
      items = self.treeWidget.selectedItems()
      current = self.treeWidget.currentItem()
      for item in items:
          sip.delete(current)
          self.UpdateTree()
  </code></pre>
  
  <p>Edit 2: </p>
  
  <pre><code>import mysql.connector
  from mysql.connector import errorcode
  from datetime import datetime
  
  
  
  class DatabaseUtility: 
      def __init__(self, database, tableName):
          self.db = database
          self.tableName = tableName
  
      f = open('C:\\Users\\Vlad\\Desktop\\Myfiles\\EnterprisePassport\\password.txt', 'r')
      p = f.read(); f.close();
      self.cnx = mysql.connector.connect(user = 'root',
                                  password = p,
                                  host = '127.0.0.1')
      self.cursor = self.cnx.cursor()
  
      self.ConnectToDatabase()
      self.CreateTable()
  
  def ConnectToDatabase(self):
      try:
          self.cnx.database = self.db
      except mysql.connector.Error as err:
          if err.errno == errorcode.ER_BAD_DB_ERROR:
              self.CreateDatabase()
              self.cnx.database = self.db
          else:
              print(err.msg)
  
  def CreateDatabase(self):
      try:
          self.RunCommand("CREATE DATABASE %s DEFAULT CHARACTER SET 'utf8';" %self.db)
      except mysql.connector.Error as err:
          print("Failed creating database: {}".format(err))
  
  def CreateTable(self):
      cmd = (" CREATE TABLE IF NOT EXISTS " + self.tableName + " ("
          " `ID` int(5) NOT NULL AUTO_INCREMENT,"
          " `date` date NOT NULL,"
          " `time` time NOT NULL,"
          " `message` char(50) NOT NULL,"
          " PRIMARY KEY (`ID`)"
          ") ENGINE=InnoDB;")
      self.RunCommand(cmd)
  
  def GetTable(self):
      self.CreateTable()
      return self.RunCommand("SELECT * FROM %s;" % self.tableName)
  
  def GetColumns(self):
      return self.RunCommand("SHOW COLUMNS FROM %s;" % self.tableName)
  
  def RunCommand(self, cmd):
      print ("RUNNING COMMAND: " + cmd)
      try:
          self.cursor.execute(cmd)
      except mysql.connector.Error as err:
          print ('ERROR MESSAGE: ' + str(err.msg))
          print ('WITH ' + cmd)
      try:
          msg = self.cursor.fetchall()
      except:
          msg = self.cursor.fetchone()
      return msg
  
  def AddEntryToTable(self, message):
      date1 = datetime.now().strftime("%y-%m-%d")
      time = datetime.now().strftime("%H:%M")
  
      cmd = " INSERT INTO " + self.tableName + " (date, time, message)"
      cmd += " VALUES ('%s', '%s', '%s' );" % (date1, time, message)
      self.RunCommand(cmd)
  
  def __del__(self):
      self.cnx.commit()
      self.cursor.close()
      self.cnx.close()
  
  
  if __name__ == '__main__':
      db = 'enterprisepassport'
      tableName = 'session'
  
      dbu = DatabaseUtility(db, tableName)
  </code></pre>