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!

4
  • Can you update your question to include the code necessary to reproduce the problem? Commented Jun 7, 2020 at 14:06
  • Sure, just a moment. Commented Jun 7, 2020 at 14:08
  • Probably you just need to commit() after inserting, this might help: stackoverflow.com/questions/29621532/… Commented Jun 8, 2020 at 7:46
  • Yes, it solved my problem. Thanks for you help :D Commented Jun 9, 2020 at 19:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.