0

So i have a simple mysql class that i made:


import mysql.connector as connector

username = 'username'
dbname   =  'database'
host = 'localhost'
password = 'password'
port = 3306


class Connection:
    def __init__(self):
        self.connection = connector.connect(user=username, password=password,host=host,port=port,database=dbname,use_unicode=True)


    def execute_and_fetch(self,cmd):
        cursor = self.connection.cursor(dictionary=True)
        cursor.execute(cmd)

        try:
            rows = cursor.fetchall()
        except connector.errors.InterfaceError:
            return None

        cursor.close()

        return rows

    def commit(self):
        self.connection.commit()


    def close(self):
        self.connection.close()

and when i want to avoid sql errors like "you can't execute this command right now", or something like that, i have to use it like this:

from db import Connection

database = Connection()
result = database.execute_and_fetch("SELECT nickname FROM users WHERE id=3")

users = [] 

#i mean, there must be a proper way to deal with the result?
for item in results:
  users.append(item['nickname'])

print(users)

database.close() 

#now i need to do another command and to avoid the sql out of commands error i always have to close a connection and reopen it

database = Connection()
result = database.execute_and_fetch("SELECT nickname FROM users WHERE id=3")

......

database.close()

i want to know the shortest and 100% correct way to deal with it every time i try to execute a command or even fetch the results from that command Thanks in advance

Someone
  • 79
  • 10
  • You shouldn't need to do that if you called `fetchall()`. – Barmar Jun 30 '20 at 22:05
  • @Barmar, well without closing the cusor and the connection everytime i will get an error 'out of commands', im using this class on a flask server, plus fetchall is already in the class so im calling it, i know that i have set the dictionary param to True, but is there a proper way to get the result without doing a loop everytime? – Someone Jun 30 '20 at 22:10
  • Do you mean "commands out of sync"? – Barmar Jun 30 '20 at 22:14
  • @Barmar Yes, it was appearing before i close the connection and reopen it everytime. – Someone Jun 30 '20 at 22:15
  • https://stackoverflow.com/questions/3632075/why-is-mysqli-giving-a-commands-out-of-sync-error As it says there, you can't start a new query until you've fetched the rows of the previous query. But `fetchall()` does that. – Barmar Jun 30 '20 at 22:15
  • @Bramar yeah but fetchall is in the class already? – Someone Jun 30 '20 at 22:16
  • @Bramar the code is working without any problem because of me closing and re-opening the connection, i don't want to do that anymore – Someone Jun 30 '20 at 22:16
  • I know that. I'm saying that you shouldn't need to do that if you use `fetchall()`. That error shouldn't be happening in the first place, and I can't explain it. – Barmar Jun 30 '20 at 22:17
  • @Barmar Ok, Thank you is there a shortest way to get the result without looping and creating a new array ? – Someone Jun 30 '20 at 22:18
  • That's what `fetchall()` does! – Barmar Jun 30 '20 at 22:19
  • @Barmar it does, but it return it like this: 'column': 'value', i need it to get just the values – Someone Jun 30 '20 at 22:20
  • @Barmar i guess i figure it out, i can use it like this : `for i in range(0, len(rows)): print(rows[i]['column'])` – Someone Jun 30 '20 at 22:21
  • `for row in rows: print(row['column'])` – Barmar Jun 30 '20 at 22:23
  • What does that have to do with performing a second query? – Barmar Jun 30 '20 at 22:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216984/discussion-between-someone-and-barmar). – Someone Jun 30 '20 at 22:24

0 Answers0