A
A
Andrey Volchara2020-10-27 18:06:55
Python
Andrey Volchara, 2020-10-27 18:06:55

How to write values ​​to PostgreSQL database?

My discord.py bot had a sqlite3 db due to heroku constantly invalidating the db after the bot is reloaded. Decided to switch to postgresql. When I tried to write data to a table, as I did on sqlite, I got syntax errors .. or an error where the data was perceived as the name of the table.
This is part of the code using sqlite3.

for guild in client.guilds:
        for member in guild.members:
            if cursor.execute(f"SELECT id FROM users WHERE id = {member.id}").fetchone() is None:
                cursor.execute(f"INSERT INTO users VALUES (\"{member}\", {member.id}, 0, 0, 1)")
            else:
                pass

This is what I have done already with postgresql
for guild in client.guilds:
        for member in guild.members:
            cursor.execute(f"SELECT id FROM users WHERE id = {member.id}")
            if cursor.fetchone() is None:
                pass # Здесь должна быть запись member, member.id, cash(0), rep(0), lvl(1)
            else:
                pass

How can I make an entry?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2020-10-28
@vollchara

The syntax is similar there, variables can also be passed, only '%s' is used for substitution

cursor.execute("INSERT INTO users VALUES (%s, %s, %s, %s, %s)", (member, member.id, 0, 0, 1))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question