Z
Z
zaordu2021-04-03 21:54:37
MySQL
zaordu, 2021-04-03 21:54:37

How to remove connection is already closed error?

Here is the actual function where the error occurs:

def Onclicke(self):
        flag = 0
        login = (self.lineEdit.text(), )
        passw = (self.lineEdit_2.text(),)
        strana = str(self.comboBox.currentText())
        print(strana)
        with connection:
            curs = connection.cursor()
            curs.execute("SELECT username, код_покупателя FROM клиент "
                         "ORDER BY код_покупателя")
            test = curs.fetchall()
            last = test[len(test)-1][1]
            last += 1
            curs.execute("SELECT username FROM клиент")
            logs = curs.fetchall()  # выполнение запроса
            for row in logs:
                if row == login:
                   flag = 1
            if flag == 1:
                self.label_3.setStyleSheet("color: rgb(255, 0, 0);")
                self.label_3.setText("congratulations!")

            else:
                self.label_3.setStyleSheet("color: rgb(65, 211, 89);")
                self.label_3.setText("Логин уже существует")
                curs.execute("INSERT into клиент values (%s, %s, %s, %s)", (login[0], last, strana, passw[0]))
                connection.commit()
        curs.close()
        connection.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MinTnt, 2021-04-03
@MinTnt

When we open files with the with statement, it automatically closes them at the end.
So just add another tab before curs.close() . And remove the connection.close() line

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question