1

So my goal is to insert data into the database. I have a psycopg2 connection pool in my main code and I want to use one of the pool's connection in an other class so I tried to pass the cursor as a parameter. Here I create the connection pool:

conn_pool = psycopg2.pool.ThreadedConnectionPool(1, 20, user="",
                                                 password="",
                                                 host="",
                                                 port="",
                                                 database="")

Here I call the insert function from the User class.

conn = conn_pool.getconn()
cur = conn.cursor()
result = users[thread_id].insert(cur, data)
conn_pool.putconn(conn)

And here is the insert function in the User class

    def insert(self, cur, data):
        username = data["data"]["username"]
        password = data["data"]["password"]
        cur.execute(f"select user_id from users where username like '{username}'")
        raw = cur.fetchone()
        if raw is not None:
            dataj = {"title": "signup_feedback",
                     "feedback": 0}
            self.connection.send(json.dumps(dataj).encode())
            return False
        print("Signup attempt")
        cur.execute("insert into users (username, password, balance, logged_in) " +
                    f"values ('{username}', '{password}', 5000, false)")
        dataj = {"title": "signup_feedback",
                 "feedback": 1}
        self.connection.send(json.dumps(dataj).encode())
        return True

It didn't work. It didn't raise an error, it just didn't do anything. Is there a way to do it? I'm also open for other ideas. Thanks for your help!

Nyikus
  • 23
  • 1
  • 6

0 Answers0