#databases

1 messages ยท Page 163 of 1

indigo flare
#

I'd very much prefer doing this in sql instead of python

#

I already have the query ```sql
INSERT INTO nicknames(guild_id, user_id, {setting})
VALUES($1, $2, $3)

ON CONFLICT (guild_id, user_id)
DO UPDATE SET {setting} = EXCLUDED.{setting}```

gusty mulch
#

hi sorry for the delayed response, just tried thissql ALTER TABLE public.data ALTER COLUMN countchannel TYPE bigint[] USING countchannel::bigint;and got this errorsql ERROR: cannot cast type bigint[] to bigint LINE 3: USING countchannel::bigint; ^ SQL state: 42846 Character: 84

austere portal
#

do you already have data in the db?

gusty mulch
#

sems i do but i did deleted it yesterday, must have forgotten to save

#

okay I deleted the data and am getting the same error

grim zephyr
#

guys what is big int

#

and whats its use

faint blade
torn sphinx
torn sphinx
#

big interger

grim zephyr
#

well i need a little help

grim zephyr
faint blade
grim zephyr
#

need a little help @faint blade

#

that am i always supposed to use client.db to connect to a postgresql

faint blade
#

What do you mean?

grim zephyr
#

i mean am i always supposed to do

self.bot.db = await asyncpg.create_pool(dsn="removed")

or can use

db = await asyncpg.create_pool(dsn="removed")
faint blade
#

You can do whatever you like, that's just setting an attribute vs assigning a variable.

grim zephyr
#

it means i can write whatever i want

#

??

stray moss
#

how do I establish a secure connection between my frontend JavaScript to backend database

grim zephyr
faint blade
#

If you create a transaction yes, but asyncpg doesn't work like the other database wrappers you may have used.

grim zephyr
#

how to commit

#

in postgresql

faint blade
stray moss
faint blade
#

You should implement verification

stray moss
#

hmm

#

I don't think it's possible to hide anything

#

in frontend

#

so even a Authorization header won't do

brazen charm
#

in client side rendering no, you cant hide anything

#

anything can be reverse engineered

faint blade
#

The same way someone can selfbot in Discord

#

But since you don't get free access to Discord's database you can at least not delete all the data

stray moss
#

hmm I wonder how

grim zephyr
#
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\USER\Desktop\ZENESIS\AFK\cogs\afk.py", line 18, in on_message
    await self.bot.db.execute("SELECT user_id FROM Afk WHERE guild_id= $1", (message.guild.id,))
AttributeError: 'Bot' object has no attribute 'db'```
thorny field
#

How can I replace the userdbCheck row with newData variable?
Code:

# Buy stock command
@bot.command()
async def buystock(ctx, arg1, arg2):
    user_id = str(ctx.message.author.id)
    guild_id = str(ctx.message.guild.id)

    cursor.execute(
        "SELECT * FROM main WHERE user=? and guild=?",
        [user_id, guild_id]
    )

    do_query = """SELECT * from main"""
    cursor.execute(do_query)
 
    userdbCheck = cursor.fetchone()

    newData = userdbCheck

    if (userdbCheck is None):
        await ctx.send("You need to be registered in this server to use this command.")
    elif (userdbCheck is not None):
        if (int(arg2) < 0):
            await ctx.send("You can't buy negative shares!")
        elif (int(arg2) == 0):
            await ctx.send("You can't buy 0 shares!")

        for name, place, price in zip(["mango", "megasoft", "elecar", "soulairlines", "givida", "planetmoney", "wallmall", "chickdonald's", "gamestart", "fuber"], range(2, 11), range(0, 9)):
            if (str(arg1.lower()) == name and float(float(arg2) * stocks_list[price][1] <= float(userdbCheck[1]))):
                if (int(newData[place] > 0)):
                    newData[place] = str(int(newData[place]) + int(arg2))
                elif (int(newData[place] == 0)):
                    newData[place] = str(arg2)
grim zephyr
#
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/USER/Desktop/ZENESIS/AFK/main.py", line 19, in on_message
    await bot.db.execute("SELECT user_id FROM AFK_TAB WHERE guild_id= $1", (message.guild.id,))
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\pool.py", line 530, in execute
    return await con.execute(query, *args, timeout=timeout)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 299, in execute
    _, status, _ = await self._execute(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 1625, in _execute
    result, _ = await self.__execute(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 1650, in __execute
    return await self._do_execute(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 1677, in _do_execute
    stmt = await self._get_statement(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 375, in _get_statement
    statement = await self._protocol.prepare(
  File "asyncpg\protocol\protocol.pyx", line 168, in prepare
asyncpg.exceptions.UndefinedTableError: relation "afk_tab" does not exist
```@thorny field
#

can you help me a little

grim zephyr
#

it is saying afk_tab does not exist

thorny field
grim zephyr
#

table

#

in portgres

thorny field
grim zephyr
#

@thorny field

thorny field
#

ok? did it give u these error before? i think ik what's the prob

grim zephyr
thorny field
#

@grim zephyr is it?

grim zephyr
#

cause from my opinion everything is correct

#

@thorny field@thorny field@thorny field

thorny field
grim zephyr
#

@faint blade sry for the ping but can u plz help me

thorny field
#

@grim zephyr srry, idk work with dbs that much. srry i can't provide u help ๐Ÿ˜”

grim zephyr
#
Connected to pydev debugger (build 211.7628.24)
connected to database
I AM IN
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/USER/Desktop/ZENESIS/AFK/main.py", line 19, in on_message
    await bot.db.execute("SELECT user_id FROM AFK_TAB WHERE guild_id= $1", (message.guild.id,))
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\pool.py", line 530, in execute
    return await con.execute(query, *args, timeout=timeout)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 299, in execute
    _, status, _ = await self._execute(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 1625, in _execute
    result, _ = await self.__execute(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 1650, in __execute
    return await self._do_execute(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 1677, in _do_execute
    stmt = await self._get_statement(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 375, in _get_statement
    statement = await self._protocol.prepare(
  File "asyncpg\protocol\protocol.pyx", line 168, in prepare
asyncpg.exceptions.UndefinedTableError: relation "afk_tab" does not exist
thorny field
#

wait wait

grim zephyr
#

ok waiting

thorny field
#

change the db name to a lowercase name

#

bcz it gives the error in lower case

grim zephyr
thorny field
#

but ur db name is upercaase

thorny field
grim zephyr
#

i fixed it

#

@thorny field@thorny field@thorny field

#

fixed

#

!!!!

thorny field
#

how?

#

GOOD JOB!!!

#

@grim zephyr

#

@grim zephyr how did u fix it???

grim zephyr
grim zephyr
#

new error ```Ignoring exception in on_message
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:/Users/USER/Desktop/ZENESIS/AFK/main.py", line 19, in on_message
await bot.db.execute("SELECT user_id FROM afknote WHERE guild_id= $1", (message.guild.id,))
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\pool.py", line 530, in execute
return await con.execute(query, *args, timeout=timeout)
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 299, in execute
_, status, _ = await self._execute(
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 1625, in _execute
result, _ = await self.__execute(
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 1650, in __execute
return await self._do_execute(
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncpg\connection.py", line 1697, in _do_execute
result = await executor(stmt, None)
File "asyncpg\protocol\protocol.pyx", line 183, in bind_execute
File "asyncpg\protocol\prepared_stmt.pyx", line 171, in asyncpg.protocol.protocol.PreparedStatementState._encode_bind_msg
asyncpg.exceptions.DataError: invalid input for query argument $1: (853197674899046431,) (expected str, got tuple)

thorny field
#

lol

thorny field
grim zephyr
#

@thorny field (value out of int32 range)

#

what is this

faint blade
thorny field
faint blade
#

I linked you the documentation, a user ID is bigger than 32 bits

grim zephyr
#

ok

thorny field
#

convert into float?

#

idk

grim zephyr
#

@faint blade how to fetch data from postgre

#

@thorny field

thorny field
#

hmmm

#

let me see

grim zephyr
#

how to fetch data from postgre

faint blade
#

Look in the documentation, there should be examples of fetch(), fetchrow() and fetchval().

harsh pulsar
#

the asyncpg docs have plenty of examples

#

(although they are short on explanations)

#

(and they never actually documented the paramstyle)

grim zephyr
#

well what is the use of fetch and fetchrow

faint blade
#

fetch data from postgre

#

As the name suggests ๐Ÿ˜…

grim zephyr
#

i mean what is the difference

faint blade
#

The first returns a list of Row objects, the second returns a Row objects, and the last returns the first value in a Row object.

#

This is useful when you only select a single value. Instead of having to first get the first row from the list, then find the value in that row.

You just get it directly

grim zephyr
#

what is missing

burnt turret
#

the query

#

You aren't telling the database what to fetch

grim zephyr
# burnt turret the query

is it wrong?

await bot.db("SELECT * FROM afknote WHERE guild_id = $1 AND user_id = $2", (ctx.guild.id, ctx.author.id))
    data = await bot.db.fetchrow()```
burnt turret
#

Yeah

#

You should pass the query to fetchrow

grim zephyr
burnt turret
#

the "SELECT *..." is your query from the code you sent (that isn't the only type of query)

grim zephyr
#

ok

burnt turret
#

It basically says "give me all the rows from the afknote table"

grim zephyr
#

ok

wind smelt
#

asyncpg does it differently. Instead of calling the connection with the query then using fetchone and what not

#

you use fetch, fetchrow right away with your query

#

so it should be
data = await bot.db.fetchrow("SELECT * ...")

wind smelt
#

couldn't agree more

grim zephyr
#

well i have got a error

#
    user = user_id[0]
IndexError: list index out of range```
thorny field
#

How can I update a row using a list in SQLite database?
Code:

# Buy stock command
@bot.command()
async def buystock(ctx, arg1, arg2):
    user_id = str(ctx.message.author.id)
    guild_id = str(ctx.message.guild.id)

    cursor.execute(
        "SELECT * FROM main WHERE user=? and guild=?",
        [user_id, guild_id]
    )

    do_query = """SELECT * from main"""
    cursor.execute(do_query)
 
    userdbCheck = cursor.fetchone()

    newData = userdbCheck

    if (userdbCheck is None):
        await ctx.send("You need to be registered in this server to use this command.")
    elif (userdbCheck is not None):
        if (int(arg2) < 0):
            await ctx.send("You can't buy negative shares!")
        elif (int(arg2) == 0):
            await ctx.send("You can't buy 0 shares!")

        for name, place, price in zip(["mango", "megasoft", "elecar", "soulairlines", "givida", "planetmoney", "wallmall", "chickdonald's", "gamestart", "fuber"], range(2, 11), range(0, 9)):
            if (str(arg1.lower()) == name and float(float(arg2) * stocks_list[price][1] <= float(userdbCheck[1]))):
                if (int(newData[place]) > 0):
                    newData[place] = str(int(newData[place]) + int(arg2))
                elif (int(newData[place] == 0)):
                    newData[place] = str(arg2)

I want to replace userdbCheck query data with newData
How can I do that?

storm mauve
#

you can use the UPDATE table SET field = newvalue WHERE field = value
(you can use anything in the WHERE clause, not only the same field you are setting btw)

#

when it comes to executing the query, you can do it the same way you are executing the select ... where user = ? and guild = ?

thorny field
storm mauve
#

what exactly do you want to update, and using which variables?

#

the UPDATE statement is usually used to modify already existing data in one or more rows, if you have to update the entire table then your data structure might not making much use of the advantages of the database

grim vault
thorny field
grim vault
#

Just remove the two lines I quoted.

grim zephyr
#

someone help

#

my code is not responding at all

torpid sky
#

Is there a data type for dictionaries with POSTGRESQL

thorny field
grim vault
#

If you remove the lines it will really check if the context user and guild is in the main table. Before just any user/guild entry in the db would have been sufficient.

sharp sail
#

can someone explain why this may not be working?

#

it has area as an option

#

here is the data

delicate fieldBOT
#

Hey @sharp sail!

It looks like you tried to attach file type(s) that we do not allow (.xlsx). We currently allow the following file types: .gif, .jpg, .jpeg, .mov, .mp4, .mpg, .png, .mp3, .wav, .ogg, .webm, .webp, .flac, .m4a.

Feel free to ask in #community-meta if you think this is a mistake.

sharp sail
#

oh

#

ill ss

thorny field
sharp sail
thorny field
thorny field
sharp sail
thorny field
thorny field
#

see it changed colors

#

@sharp sail ^^^

sharp sail
#

oh wow

#

im not sure thats cool

thorny field
#

yea pretty dope

severe cipher
#

hello! Could somebody tell me some of the best free online file storage options?

prisma girder
torn sphinx
harsh pulsar
#

sqlite for most "simple" use cases

torn sphinx
#

alr imma learn it

hasty halo
#

Hey I am geting an error Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\shrey\AppData\Local\Programs\Python\Python39\lib\tkinter_init_.py", line 1892, in call
return self.func(*args)
File "C:\Users\shrey\Documents\RHA\main.py", line 36, in Post_Request
locals()commands
File "C:\Users\shrey\Documents\RHA\main.py", line 29, in exec2
from APP.models import Files_There
File "C:\Users\shrey\Documents\RHA\APP\models.py", line 2, in <module>
class Files_There(models.Model):
File "C:\Users\shrey\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 108, in new
app_config = apps.get_containing_app_config(module)
File "C:\Users\shrey\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\shrey\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
settings.INSTALLED_APPS
File "C:\Users\shrey\AppData\Local\Programs\Python\Python39\lib\site-packages\django\conf_init_.py", line 82, in getattr
self.setup(name)
File "C:\Users\shrey\AppData\Local\Programs\Python\Python39\lib\site-packages\django\conf_init
.py", line 63, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. Its related to data bases tkinter and django. Can anyone help me

storm mauve
short peak
#

Json best database change my mind

#

Sike

storm mauve
#

Well yeah, NoSQL are just Json with extra steps /s

ripe rapids
#

@remote plinth i have been learning aiosqlite

#

i got my blacklist to work

#

while testing i added myself in the db

#

how would i remove data from db

remote plinth
#

it deletes the whole row where the condition is true

ripe rapids
remote plinth
ripe rapids
#

like a certain id?

nocturne pewter
#

How to connect the sqlite database file to the bot using replit web ;-;?

remote plinth
ripe rapids
remote plinth
nocturne pewter
#

Help me plz ;-;

remote plinth
nocturne pewter
#

K

#

@remote plinth how do i check my database file if it connet?

remote plinth
#

im not sure if it has a is_connected attribute

#

lemme read docs

#

!d sqlite3.connect

#

!d sqlite3.Connection

#

hmm no

#

lemme search docs

#

no again

remote plinth
nocturne pewter
#

:/

remote plinth
#

maybe try checking if db: ...

#

then db.close() or anything

burnt parrot
#

!e

import sqlite3

sqlite3.connect("test.db")
delicate fieldBOT
#

@burnt parrot :x: Your eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "<string>", line 3, in <module>
003 | sqlite3.OperationalError: unable to open database file
remote plinth
#

test.db is not exists

burnt parrot
#

yeah ik

austere portal
burnt parrot
#

ik

austere portal
#

but you can do sqlite3.connect(":memory:")

burnt parrot
#

k

nocturne pewter
#

Do i need to upload my database file to replit .-.?

austere portal
#

Create a database file in replit

nocturne pewter
#

K

brave bridge
#

@nocturne pewter Just make sure not to store any private data in there if the repl is public

#

beacuse everyone will see the file, of course

remote plinth
austere portal
#

Well, now you know it

remote plinth
#

does it reset when you restart the script?

#

the ":memory:"

austere portal
#

yes

nocturne pewter
#

So... how to save data in database file??

austere portal
#

Connect to the database file, do something, and commit. committing will save it

nocturne pewter
#

._.

#

I mean i want when i mute someone by command, bot will save their id and if they leave and rejoin server, the bot will check their id in the list if thier id in the list they still got muted role until i unmute them

austere portal
#

insert the members id to the database and commit, when a member joins (in on_member_join) select all of the ids from the database and check if the members id is in it

grim zephyr
#

@austere portal

#

can i have a little help

austere portal
#

Sorry, I don't know mongo db, ask your question here and some one else will answer it

grim zephyr
#

ok

burnt parrot
#

idk i use sqlite3 / aiosqlite for my discord.py databases

grim zephyr
#

what should i choose to use this as my db for my bot

burnt parrot
#

but

#

do

#

nvm idk

torn sphinx
#

i have question. i have been learning mysql and all people say that postgress more better than mysql. So should i learn postgress or continue using mysql?

#

thanks

harsh pulsar
grim zephyr
storm mauve
austere portal
#

Setting up stuff for the first time is not fun

grim zephyr
#

@austere portal

Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\USER\Desktop\ZENESIS\AFK\cogs\afk.py", line 34, in on_message
    result = data3[0]
IndexError: list index out of range```
#

its not giving me the proper value

austere portal
#

looks like data3 is empty

grim zephyr
# austere portal looks like `data3` is empty
            if message.mentions:
                print("executed mentions part")
                member = [member.id for member in message.mentions]
                data2 = await self.bot.db.fetch("SELECT user_id FROM afknote WHERE guild_id = $1 AND user_id = $2",
                                     message.guild.id, str(member))

                data3 = await self.bot.db.fetch("SELECT note FROM afknote WHERE guild_id = $1 AND user_id = $2",
                                     message.guild.id, str(member))
                result = data3[0]
                print("user_id = "+str(data2))
                print("note = "+str(result))
                print("member mentioned = "+str(member))  ```
#

here

austere portal
#

member is a list

grim zephyr
#

yes member is a list

austere portal
#

why are you casting it to str?

#

if you want to save a list postgres has a list data type

grim zephyr
#

maybe but why is it not getting the data3

pearl vapor
#

it is but your parameters arent correct so it cant find the data

#

supposedly

austere portal
#

yeah

pearl vapor
#

ofcourse we dont know the structure of your database so

#

and you can select several collumns in one query btw

austere portal
#

* for all columns

grim zephyr
austere portal
#

why is the user_id text?

#

id's are integers

grim zephyr
#

though text is fine too

austere portal
#

index the data

grim zephyr
austere portal
#

ok then

grim zephyr
#
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\USER\Desktop\ZENESIS\AFK\cogs\afk.py", line 34, in on_message
    result = data3[0]
IndexError: list index out of range```@austere portal
torn sphinx
austere portal
#

just select everyting you need in a single query

grim zephyr
#

but i want only the note

austere portal
#

then data["note"]

#

asyncpg.Record objects are subscriptable

grim zephyr
austere portal
#

get the value of the note column

grim zephyr
#

can u plz send a example

#

i am getting confused

#

@austere portalIgnoring exception in on_message Traceback (most recent call last): File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "C:\Users\USER\Desktop\ZENESIS\AFK\cogs\afk.py", line 34, in on_message result = data3["note"] TypeError: list indices must be integers or slices, not str

#

didn't work

austere portal
#

data3 is a empty list

grim zephyr
#

ok let me try once

austere portal
#

your query is unable to find anything in the db

grim zephyr
#

??

austere portal
#

ig the ids dont exist in the table

grim zephyr
#

wth how?

#

they are there

grim zephyr
#

i mean nothing is working

austere portal
#

it doesnt exist is the table

grim zephyr
austere portal
#

run the sql query (with the placeholders replaced with actual values) in pgadmin and see

austere portal
#

run the sql query in pgadmin and see

grim zephyr
austere portal
#

right click "Tables" and click query tool

grim zephyr
#

ok

grim zephyr
#

nothing is there

austere portal
#

then the guild_id and member_id doesnt exist in the table

grim zephyr
austere portal
#

show the query you ran

grim zephyr
#

this??\

#

@austere portal

#

@austere portal

#

??

austere portal
#

if the query doesnt return anything that means the data doesnt exist in the table

grim zephyr
#

but how?

austere portal
#

hmm

grim zephyr
#

!paste

delicate fieldBOT
#

Pasting large amounts of code

If your code is too long to fit in a codeblock in discord, you can paste your code here:
https://paste.pydis.com/

After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.

austere portal
#

ig the member_id passed in is not in the db

grim zephyr
#

how can i save it in the db

austere portal
#

insert it

grim zephyr
#

@austere portal

#

can u plz check once

austere portal
#

the user id you are passing in the query doesnt exist in the db

grim zephyr
austere portal
#

what?, if it doesnt exist it will return nothing

#

insert the relevant data

grim zephyr
grim zephyr
#

@austere portal`

#
    await coro(*args, **kwargs)
  File "C:\Users\USER\Desktop\ZENESIS\AFK\cogs\afk.py", line 52, in on_message
    elif f"[{message.author.id}]" == user_id:
UnboundLocalError: local variable 'user_id' referenced before assignment```
austere portal
#

are you defining user_id after the elif statement?

#

and I cant get aiopg to work, great

austere portal
#

ok i give up

grim zephyr
#

man my file is still not working properly

grim zephyr
#

someone help

burnt parrot
#

with what

grim zephyr
#

how can i remove the record object in postgresql

burnt parrot
#

idk sorry

storm mauve
#

what is the "record" object?
a row in a table?

grim zephyr
#

instead of sending "note = no reason provided" its sending "<Record note='No reason provided'>"

storm mauve
#

A PostgreSQL server can have multiple databases, each of which contains multiple tables, each of which is organised in Rows and Columns

#

I advise taking a look at what your data currently looks like using a database explorer tool such as pgAdmin

austere portal
grim zephyr
#

and everything was fine

#

but still the record object but why

storm mauve
#

not sure, I haven't worked with asyncpg but their documentation probably explains that in detail

grim zephyr
#

@whole widget sry for the ping but i need a littile help

native haven
#

Hello, I am reading about mongodb, and pymongo.. I was woundering, is pymongo the package I need to use to work with mongodb in async way. like is pymongo non blocking?

brave bridge
#

You can use motor or wrap the calls to pymongo into threads (using loop.to_thread)

brave bridge
grim zephyr
#

portgresql

brave bridge
#

What is your question?

native haven
grim zephyr
#

how can i remove the record object from my value

brave bridge
#

Is that what you want?

storm mauve
#

it sounded kinda like he wants to extract the value from the record instead of outputting the record object itself

brave bridge
#

oh

#

@grim zephyr Do you want to get the value of note from the record?

brave bridge
#

you need to do record["note"]

grim zephyr
#

ok but where and how

brave bridge
#

it's indexable by key, like a dict

grim zephyr
#

can u give a example

brave bridge
#

can you show your code?

native haven
grim zephyr
#

!paste

delicate fieldBOT
#

Pasting large amounts of code

If your code is too long to fit in a codeblock in discord, you can paste your code here:
https://paste.pydis.com/

After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.

brave bridge
#

ah, I see

grim zephyr
#

line 34 - 36

#

is where i am fetching the note

brave bridge
#

asyncpg returns you Record objects with the rows that you asked for. For example, if you do SELECT foo, bar FROM baz, you'll get back a record like <Record foo=... bar=...>. You can index into it like a dictionary: record["foo"] and record["bar"].

#

In your case, you can do result["note"]

grim zephyr
#

what should i replace it with

brave bridge
#

Replace what?

#
await self.bot.db.fetch("SELECT note FROM afknote WHERE guild_id = $1 AND user_id = $2", message.guild.id, str(member))
``` this will return you a list of `Record` objects. You can access each of those objects like a dict.
#

Have you worked with dictionaries?

grim zephyr
storm mauve
#

you really should learn the basics instead of just asking for people to do everything for you without understand what they are doing

grim zephyr
brave bridge
grim zephyr
#

ok

brave bridge
#

It's fine to use more advanced libraries, but be prepared to learn a lot of stuff while implementing something

grim zephyr
#

ok

grim zephyr
brave bridge
#

You can index a Record by key just like a dict.

#
>>> d = {"note": "this is a note"}
>>> d["note"]
'this is a note'
>>> record
<Record note="this is a note">
>>> record["note"]
'this is a note'
grim zephyr
#

what are>>>

#

??

brave bridge
#

Have you ever used the Python console?

grim zephyr
brave bridge
#

This thing

grim zephyr
#

ye

#

i have seen this in vsc

#

but what is the use of it

grim zephyr
#

??

wind smelt
#

to mark it as input

#

> indicates where your input will be

grim zephyr
#

ok

#

man i need to learn dict properly cause i am still not getting it

severe cipher
#

so i read that the best database to store files from dicord.py is sql? but how do i host it online?

severe cipher
#

like suppose i want to store a file shared by a member of a server, how would i do tht?

burnt parrot
#

oh

#

i suggest you use sqlite3

#

because

#

its uses files

severe cipher
#

but arent sql databases stored locally?

burnt parrot
#

yes

#

through files

severe cipher
#

what if somebody else wants to retrieve the file?

burnt parrot
#

it saves with the bot

#

wait wdym

#

in a bad or good way?

severe cipher
#

like if those files are stored locally how would the others access that?

burnt parrot
#

you can put them a code / file sharing app

severe cipher
#

like i want ppl to store and retrieve info at any time possible while the bot is running....

burnt parrot
#

well

severe cipher
burnt parrot
#

oh

#

module

#

works with it

#

so like

severe cipher
#

i dont quite follow, sorry๐Ÿ˜…

burnt parrot
#

ok

#

sorry

severe cipher
#

np

burnt parrot
#

ok so sqlite3 is a python module which you can import and access the db files through code

#

so like if you wanted a custom per guild prefix

#

you could use that

severe cipher
burnt parrot
#

ok

severe cipher
#

sorry

burnt parrot
#

ok think of the database as a box

#

it can store a bunch of stuff

#

such as fields

severe cipher
#

ok

burnt parrot
#

which can be used to organize the data

torn sphinx
#

But if you are using SQL and hosting your bot online in a ephemeral file system, keep in mind it most likely won't work how you want it to..

Databases are software that are made to store large amounts of data.

burnt parrot
#

true

severe cipher
#

umm so id be better off using a google drive api or something similar ?

torn sphinx
burnt parrot
#

i normaly just use docker to deploy my bots with docker so i can use my own file system

severe cipher
severe cipher
burnt parrot
#

docker just makes deploying apps to the cloud

torn sphinx
severe cipher
#

ok, but are there any free ones tho?

burnt parrot
#

dockers free

#

docker

severe cipher
#

alright, docker it is

#

guys thanks a lot! ๐Ÿ‘

burnt parrot
#

tell me if you need help with deploying it

severe cipher
#

sure thing! I cant thank you enough!

burnt parrot
#

ok

#

https://www.youtube.com/watch?v=z58g7_dHeMA&t=473s this guy explains it better than i would

#discord #bot #python #docker
Learn how to easily deploy your discord bot to any machine using docker. In this video I go over all the steps that you need to take in order to do this with a single command.

Docker download: https://www.docker.com/products/docker-desktop
Docker hub: https://hub.docker.com/
Play with docker: https://labs.play-wit...

โ–ถ Play video
snow niche
#

Sqlite: can I have table names with - example: CREATE TABLE IF NOT EXISTS table-name

harsh pulsar
#

you would need to quote your table name in every query

snow niche
#

Ok

grim zephyr
#

!paste

delicate fieldBOT
#

Pasting large amounts of code

If your code is too long to fit in a codeblock in discord, you can paste your code here:
https://paste.pydis.com/

After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.

untold dust
#
    def load(self):
        database = self.db['database']
        cursorinfo = database.info.find()
        refinecursor = list(cursorinfo)
        self.info = dumps(refinecursor)

    def save(self):
        self.load()
        database = self.db['database']
        info = database.info
        info.delete_many({})
        for player_info in self.info:
            info.insert_one(player_info)```
question: is this the most efficient way to load and save info to mongodb?
torn sphinx
#

Hi

#

well

untold dust
#

hi

torn sphinx
#

I dont rlly use save and load

untold dust
#

so how do u do it?

torn sphinx
#

well

#

@untold dust

#

let me send one

#

player = Player(...)
level_state = Level(...)

# saving
with open('savefile.dat', 'wb') as f:
    pickle.dump([player, level_state], f, protocol=2)

# loading
with open('savefile.dat', 'rb') as f:
    player, level_state = pickle.load(f)```
#

tell me if u need anything here.

grim vault
grim zephyr
#

any pymongo user here

severe cipher
#

hello, i am still struggling to find a way to store discord files through a bot to the cloud/online, is there any other way besides docker ?

torn sphinx
severe cipher
#

umm tbh i dont really know ๐Ÿ˜… ๐Ÿ˜…

torn sphinx
#

So where you want to store the files? To which service?

#

And how do you intent to use these files again

severe cipher
#

so like i have a discord bot, and when a user posts something that they want the bot to remember/store i want it to probably store it somewhere on the cloud, so i am looking for the best option is all

severe cipher
torn sphinx
#

User posts something? Is they always post a file?

severe cipher
#

yes a file, an image a note something of that sort..

torn sphinx
#

If itโ€™s file the easiest would be to just store on the local filesystem.

#

Or if you have a specific service in mind then it would help, because each service would have maybe a different process to upload to it

severe cipher
#

but the issue with that would be like if your system is down then the database is inaccessible right?

torn sphinx
#

Like upload to S3 is different to upload to onedrive

torn sphinx
untold dust
severe cipher
#

i will check it out, thanks

severe cipher
untold dust
severe cipher
#

database hosting could solve this issue right/

untold dust
severe cipher
#

ohh

untold dust
#

bc more downtime

severe cipher
untold dust
#

and you could do a check like if 1 database is down use the backup database

torn sphinx
severe cipher
#

alright but i think i wasnt clear with my question, and sorry to keep asking the same thing over and over but these databases, they wouldnt be available to the bot if the systemit was created in is down right?

torn sphinx
untold dust
#

its a pretty viable option for most ppl

torn sphinx
#

It requires replication and keeping everything in sync. If you understand how to do that then go ahead.

severe cipher
#

what i am confused about is when you create such a database it is created on your system yes? and so if the system isnt online that would mean the database isnt availabe which means the other members of the server wouldnt be able to use that feature since the db isnt available?

untold dust
#

and if you host your program on the cloud using something like heroku you could get around that

severe cipher
#

ohh alrighty i did not know that!

untold dust
#

in theory

torn sphinx
untold dust
#

honestly it only matters if you're bot is in like a couple dozen servers or more

severe cipher
torn sphinx
#

Why would you shut down system when your trying to avoid downtime?

severe cipher
severe cipher
torn sphinx
severe cipher
#

sorry about not being clear about all this actually i understand what i need to do now, im dumb lol

#

And thanks a lot for the help

austere portal
severe cipher
#

using raspberry pi aint too bad right?

austere portal
severe cipher
#

alright thanks

austere portal
severe cipher
#

that shouldnt be a problem ๐Ÿ˜„ , thank you!

fringe geyser
#

e

civic trail
#

Does anyone know if I can have multiple values for a row in a sqlite3 dh

#

Db*

harsh pulsar
#

what do you mean by "multiple values for a row"?

civic trail
#

So we have a table called users

#

And then rows and columns

#

The rows can be

#

Username

#

Password

#

Hobbies

#

And a value of hobbies can be "basketball" which makes the hobbies row have one value

#

And I want to know if it is possible for the hobbies row to have 2 to 3 values

ebon skiff
civic trail
#

Yea

ebon skiff
#

You can use an array inside the database or just seperate them with a comma

#

Both are pretty bad

brave bridge
#

@civic trail
Separating values with a comma isn't a good idea. It will make it really hard and inefficient to filter/group by hobbies in such a system. What you want to do is create a separate table, called user_hobbies, like so:

CREATE TABLE user_hobbies (
    user_id INTEGER REFERENCES users(id),
    hobby TEXT NOT NULL
);

So you'll have a situation like this:

# users
id | name  | age
---+-------+---
 1 | alice | 16
 2 | bob   | 25

# user_hobbies
user_id | hobby
--------+------
      1 | programming
      1 | tennis
      1 | drawing
      2 | drawing
      2 | dancing
      2 | beer
      2 | videogames
#

this is called a many-to-many relationship, where you have many users, each of whom has many (0 or more) hobbies

ebon skiff
#

It is that is the sad part is

#

but then def maria

brave bridge
# brave bridge <@!566961393551540224> Separating values with a comma isn't a good idea. It wil...

If you set up a table like that, you'll be able to create an index: sql CREATE INDEX idx_user_hobbies ON user_hobbies(hobby); An index is a separate structure, similar to a Python dict, that allows you to quickly look up rows: py idx_user_hobbies = { "programming": [1] "drawing": [1, 2] } For example, if you make a query like sql SELECT DISTINCT u.id, u.name, u.age FROM users u JOIN user_hobbies h ON h.user_id = u.id WHERE h.hobby = 'programming'; to find all users who have programming as a hobby, and SQLite won't scan through the entire table -- it will look up which user IDs have a programming tag in the index, and it will only look up those rows.

torn sphinx
#

Do you have any particular issue with it? Most drivers follow the same interface so usage will be the same

#

Use the official connector

civic trail
#

@brave bridge thats awesome

#

But in my blog post website commas will be ok

#

Since im just adding the users who upvoted

#

So can you tell me how to separate with commas?

torn sphinx
#
                            filecontent = filecontent.replace(i, str(ctx.author.id) + "|" + str(nowAmount) + "|" + str(int(timedingdong) + 60 + "|" + int(bank) + "|"))
#

its for my discord bot but it comes up with hundreds of errors

#

i added bank and it broke

#

it saves in a file

#

but it deletes instead of replaces

wraith tundra
#

I'm not sure about this, but is there a way to make a database with OOP? Sorry if this is the wrong channel.

civic trail
#

With oop does he mean object orientated programming

wraith tundra
#

yep

shell ocean
#

Peewee

#

and Django's ORM

pliant spire
#
    class User(Base):
        __tablename__ = "users"

        user = Column(type_=String, primary_key=True)


    Base = declarative_base()
    engine = create_engine(f'sqlite:///{get_project_root()}/db/main_db.db')
    session = sessionmaker(bind=engine)
    session = session()
    # @todo, does this mean all my models have to live here?
    Base.metadata.create_all(engine)
    session.add(User(user='hello_world_l33t'))
    session.commit()
    session.close()
#

Hi guys, I was wondering how Base.metadata does its job?

I'm wondering if i'll need to keep all my models in the same file as the Base.metadata.create_all(engine) call

Is there a way to import that, so the Base knows what all my tables look like?

#

this is sqlalchemy + sqlite

gentle apex
#

is SQL considered a database?

harsh pulsar
storm mauve
#

not sure, but can't you just access the sqlite_schema table?

low shoal
#

so I have this error with asyncpg

PostgresSyntaxError: syntax error at or near "ON"

while using this query

INSERT INTO afk (user_id, last_seen, reason)
    VALUES ($1, $2, $3) RETURNING *
ON CONFLICT (user_id) DO UPDATE
    SET last_seen = EXCLUDED.last_seen,
        reason = EXCLUDED.reason
    RETURNING *;

Some research showed that the problem may be because of old postgresql version but I'm using 13.2
or is that query wrong?

#

well removing the returning * worked

#

although that raised another error

#

but why can't I use returning there

#

ok so now I am getting prolog InvalidColumnReferenceError: there is no unique or exclusion constraint matching the ON CONFLICT specification

#

so my on conflict statement is probably wrong

harsh pulsar
#

you had RETURNING * in two different places, that was probably the original error

low shoal
#

o

harsh pulsar
#

the new error means that there's nothing to be in "conflict" with

#

what's the definition for this table?

#

it looks like user_id is supposed to be the primary key, but for some reason isn't

low shoal
#
CREATE TABLE afk (
   last_seen TIMESTAMP,
   user_id BIGINT NOT NULL,
   reason TEXT,
   PRIMARY KEY (user_id)
);
#

well it should be the primary key

harsh pulsar
#

huh

low shoal
#

yeah probably the on conflict is wrong because wherever I see an example of on conflict it is done differently

low shoal
#

now it works as it should

harsh pulsar
#

double check the docs

wraith tundra
#

thanks

zealous parcel
#

Hi, i use aiomysql but there is a delay in it How do I know the reason for this comment?

zealous parcel
brave bridge
#

@zealous parcel What do you mean by 'delay'?

zealous parcel
brave bridge
zealous parcel
#

no

brave bridge
#

Then you should do that and measure the time

#

Are you using a local database or a demote database? If it's a remote database, there could be a pretty big latency

#

I had hundreds of milliseconds of latency with ElephantSQL

zealous parcel
#

When I turn on the bot it works normally but this happens after a while of running

brave bridge
next sun
#

hello

#

I am facing the following problem

#
psycopg2.OperationalError: server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.
#

on trying to fetch the data I need.

#

it happens that I've tried a few things out of what I found googling but nothing seems to work

#

though, I have found this github discussion

brave bridge
#

@next sun Maybe you have too many open connections?

next sun
#

and there is a dude that has this comment:

#

I'm having the same issue. For now as a workaround had to create wrapper around connection object with used methods to hook them. In cursor() method had to do a SELECT 1 "ping" check and reconnect if needed to return a valid working cursor.

#

but I can't understand how I could implement this workaround

#

can anyone help me?

#

that would probably fix the problem temporarily at least

next sun
#

when running htop

#

I see not script

#

basically I setup a cronjob to run a script everyday

brave bridge
next sun
#

you are right that the script in the ends hangs kind of waiting for something (only when it fails on the db side)

next sun
#

but....

#

what is important in the table?

brave bridge
# next sun I can run

Run this ```sql
SELECT
pid
,datname
,usename
,application_name
,client_hostname
,client_port
,backend_start
,query_start
,query
,state
FROM pg_stat_activity
WHERE state = 'active';

#

that should show the active connections

#

or remove the WHERE clause and see all the connections

next sun
#

is only shows one active

#

the interesting thing is that when I run the script manually second time, no error happens

#

without the where clause it shows about five or six

#

@brave bridge is it ok if I kill connections

jade shell
#

(ping) is there a good tutorial how how to make a data base for a discord bot

prisma girder
gentle roost
#

Is there a convention for naming the return of a database search?

#

I've been doing

vals = cursor.execute("SELECT * from ...", (...,)).fetchall()

vals was temporary but I started using it everywhere out of habit

prisma girder
#

Depending what you receive

#

It can be users, products, accounts and so on

gentle roost
#

Gotcha.

mortal light
#

I was looking to add a database to a project and came across psychopg2 driver. But psycopg3 also exists.

Should I use psychopg3 instead of psycopg2?

austere portal
#

Wait what psycopg3 is there

mortal light
#

I was just going through the basics of psycopg2. How to connect and disconnect to the database. Haven't really done anything substantial, so I can use psycopg3 if it's better.

mortal light
#

it's written in python

austere portal
#

Thanks I'll check it out, I've been using psycopg2 for all this time

mortal light
#

Do you use psycopg2 on regular basis?

austere portal
#

Yes

#

I have been using that for all my projects which use the postgresql database

mortal light
#

At a company or on personal projects?
Just trying to gauge if more people are still using psycopg2 at companies, then I could start off with psycopg2.
And once the project is done, I dont mind refactoring it to psychopg3.

brazen charm
#

generally psycopg2 is still the more popular setup

#

just because psycopg3 is still a WIP

austere portal
#

it has async support, nice

brazen charm
#

asyncpg still king though

#

psycopg2 has such a legacy of weird feature choices and bad performance in places asyncpg will likely never be moved off being king as an async driver

mortal light
brazen charm
#

depends on the company, and generally modules like that are pretty simple to interchange in a good setup

austere portal
mortal light
#

Are queries written the same way in PostgreSQL and in psycopg2?

brazen charm
#

sorta

#

psycopg runs with it's own placeholder setup

#

sql syntax the same otherwise

#

postgres runs with the $n syntax, psycopg2 does mysql style %s placeholders

#

with a awful mix of encouraging string formatting

brave bridge
#

yeah, psycopg has to do its SQL injections query formatting, because $N isn't in the DB API 2 spec

mortal light
#

I dont know either, so I guess I should be using the keyword pscopg2 when I look up how something should be done, instead of postgreSQL.

brazen charm
#

yeah

#

well

#

depends, sql logic -> postgres, placeholder behavour -> psycopg2

#

why my execute many being slow? -> psycopg2

harsh pulsar
#

!pep 249

delicate fieldBOT
#
**PEP 249 - Python Database API Specification v2.0**
Status

Final

Created

29-Mar-2001

Type

Informational

harsh pulsar
brave bridge
mortal light
harsh pulsar
#

this pep is why most python database libraries have very similar APIs

brave bridge
#

but are still incompatible because they have different query syntaxes lemon_pensive

harsh pulsar
mortal light
#

So if someone wrote a database library for python, then they would follow the conventions mentioned in db-api, so developers would find it easy to use this library?

#

Oh ok I see.

dense barn
#

im trying to change my default value of one column from 0 to 1 but it does not update it, ive faced this issue twice now, anyone know why?

austere portal
#

The query?

dense barn
austere portal
#

An alter query will do the job

dense barn
#

i did that to

#

didnt work

#
ALTER TABLE ONLY pokesetspc ALTER COLUMN card_amount SET DEFAULT 1
``` is what i did
austere portal
#

Hmm

#

Any error?

dense barn
#

nope

#

said its successful

austere portal
#

How are you inserting the data?

dense barn
#

just updating it

#
UPDATE pokesetspc...
austere portal
#

The default value of a column will only apply if you are inserting a new row without the column specified

dense barn
#

so does that mean only the new columns will have 1 ?

austere portal
#

Yeah

dense barn
#

bruv

#

how do i change the old ones then?

austere portal
#

Update all of the rows then

dense barn
#

oh well, ig its gonna be 0 then

mortal light
#

When using psycopg2, we would only do conn.commit() if it's some sort of writes/deletes? For read we dont have to do conn.commit()?

brave bridge
#

I think the context manager automatically does it, but I could be wrong, you'll have to check

mortal light
#

I was dropping a table and it didnt drop it without commit

#

There's an option to enable auto commit if that's what you are referring to.

brave bridge
mortal light
brave bridge
mortal light
#

oh no, I am not

brave bridge
#

Are you familiar with transactions in SQL?

mortal light
#

Taking the concept of regular context manager of with to read and write and extending that to psycopg2, I remember reading it's better to use with in case reads and writes are too big, and with has closing file descriptor baked in.

#

I am looking at the with psycopg2 you linked. No, I'm not familiar with transactions in SQL. I just started using databases.

#

A transaction is a logical unit of work that contains one or more SQL statements So SQL transactions would execute multiple sql statements/queries.

brave bridge
#

Imagine that you want to save this object to the database:

@dataclass
class Post:
    id: str
    title: str
    content: str
    tags: list[str]

This is one way to do it: ```py
def save_post(connection, post):
connection.execute(
"INSERT INTO posts (id, title, content) VALUES (%s, %s, %s)",
(post.id, post.title, post.content)
)
connection.executemany(
"INSERT INTO post_tags (id, tag) VALUES (%s, %s)",
[(post.id, tag) for tag in post.tags]
)

app = SlowAPI()

@app.post("/posts")
def insert_post(request):
post = post_from_json(request.json())
if post is None:
return 422
conn = create_db_connection()
try:
save_post(conn, post)
finally:
conn.close()
return 201, {"url": f"/posts/{post.id}"}

#

It's no big deal here, but that could violate some other important business rules.

#

So what you want to do is either execute both statements or none of them. That's called an "atomic operation" -- it either executes fully or doesn't execute at all.

mortal light
#

"INSERT INTO posts (id, title, content) VALUES (%s, %s, %s)", do we always use "%s" regardless of what data type is being stored in the database?

brave bridge
#

Yeah, I think so

#

You could also do %(foo)s, %(bar)s and then pass a dictionary like {"foo": 1, "bar": "baz"}

#

I haven't used psycopg, only asyncpg, so I'm just looking at the docs right now as to how to use transactions

mortal light
#

ok I'll set up basic methods to create, drop table and insert data and then run it through more data and see what needs to get adjusted.

brave bridge
#

in psycopg a transaction is automatically created when you execute the first statement.

#

(or so it seems from the docs)

mortal light
#

After you mentioned context manager and I looked at the example, I didnt think I could use it because how I am structuring my code by creating different methods, but I think I should still be able to use context manager.

brave bridge
mortal light
#

Yeah I was looking at something similar!!

brave bridge
#

what are you writing, if that's not under an NDA?

mortal light
#

Making an API call and then storing some data to the database, so whenever someone runs the program for the next 24 hours it would fetch data from the database instead. And after 24 hours drops the table and makes the call to api again.

It would be 5000 rows with 4 columns each.

brave bridge
#

the reason with doesn't close the connection is probably that it has to play nicely with pools

#

@mortal light Why not use SQLite?

#

it can take 5000 rows just fine

mortal light
#

I thought about that, but the program would run from different computers.

brave bridge
#

Do you actually need to mutate the database from different servers?

mortal light
#

What do you mean by "mutate the database"?

brave bridge
#

why do you want several computers to interact with the database directly?

mortal light
#

If I used SQLite, then the data fetched from api call would be local to that computer. If another user runs the program from a different computer then it would make call to api again.

brave bridge
#

Are you exposing the database directly to the users of your application?

mortal light
#

And I am trying to rely less on this third party API.
Database and the program is for internal use.

brave bridge
#

That sounds like a bad idea, what if someone sends DROP TABLE things;?

#

I would wrap it in an web server (flask/fastapi) and then users would make requests to that API

#

Sounds like less than 100 lines of code, and that would prevent a lot of bad things

mortal light
#

So only people who have this program would be able to run it.

brave bridge
#

You said that different computers would have access to the database, right?

#

maybe I'm misunderstanding the context

mortal light
#

Yeah so the program makes request to API, response data gets stored locally(as of now in excel *facepalm).

If I run the program/script it will make api call and store data on this computer for 24 hours.
If some other person at company runs the program/script from their computer, it will also make call to the api and store data on their computer for 24 hours.

#

So anytime someone new runs the program, it will make call to the api. Third party API is slow and has call limit.

So creating a small database seemed like a reasonable way to address this issue. This way one computer would make api request and then every other computer after it will be fetching data from the database when they run the program.

brave bridge
#

@mortal light What I'm suggesting is instead of exposing the database directly to all the other computers, expose an API, so someone can do

requests.get(f"{YOUR_SERVICE}/bananas?id=42")
``` and get the data they need
mortal light
#

Oh I see what you mean, even though the program is being used internally, it's still a better approach to create an api for it?

brave bridge
#

Yeah

#

because Alice opens a phising link and opens up a hole into your system, then Bob spills coffee on the keyboard which accidentally sends DROP TABLE things; etc.

#

and it's easier to interface with

mortal light
#

And other computers would also be able to make post request if the data is older than 24 hours.

So then I would have to create a server and host this api?

brazen charm
#

you would need to do that regardless

#

but yes

mortal light
#

Yeah I have an RDS instance running for this postgres database.

#

ok I will think about this.

brave bridge
#

@mortal light You could just update the data in the background, right?

#

and while it's updating you either refuse to serve responses, or just serve outdated data if that's fine

proven arrow
brave bridge
#

btw shouldn't excel handle 5000 rows just fine?

#

just attach an API to excel ๐Ÿ˜„

#

and I'm only half joking

mortal light
mortal light
# brave bridge and I'm only half joking

lol I asked something a while back which mentioned excel.
And someone said, "Of course, some people use excel in their projects. It's poor mans database" lol and from their I found out about SQLite to store data locally.

#

But if I get the chance to go back to those projects, I guess I would rewrite the excel portions to utilize SQLite, so there isnt a concern of someone opening one of the excel files.

torn sphinx
#

i have been download postgresql and this happening. Please help me!

dense barn
#

how would i match a column from database 1 to database 2? And then if the matching is correct i want to take something from a certain column of databse 1 and insert it to database 2. any idea how i would do that?

#

and by matching i mean match a lot of things

#

i can try searching on the internet, lmao never though of that

dense barn
#

ok, well that was unexpected, i didnt think it would be this easy but i got it.

green iris
#

When migrating data in Django, is there a way to ser the get natural key behavior? I am unable to import my user data as some names are blank but I want to serialize on the user's email address instead

narrow moth
#

I need to offer the ability to search (read-only) a postgres database via discord but it feels like I'm getting way off track.

Full SQL is too complex for people to learn (realistically) so I ended up making a tokeniser that might be able to build queries it feels like it's just an extra five steps in a weird direction before getting back to just updating a dictionary and using if statements to parse the input into a pyscopg.sql function.

Do I keep going down this road or is there a better way?

tribal cargo
#

Hello, I try to initialize an flask_sql Database. But I get some: can't set attribute error. I'm not sure whats the problem. From what I read it should work. I tried it in an extra task to figure out the problem:

#

models:

#

tracestack

#

Is anybody firm with this issue?

snow niche
#

anyone know sqlite?

torn sphinx
snow niche
#

can i connect to db like this?

#
class Logs(commands.Cog):
    """๐Ÿ“” Set custom mod-logs channel."""
    def __init__(self, bot):
        self.bot = bot
        self.conn = aiosqlite.connect("db's/moderation.db")
        self.c = self.conn.cursor()
    @_set.command()
    @commands.has_permissions(manage_channels=True)
    @commands.bot_has_permissions(manage_channels=True)
    async def modlogs(self, ctx, channel:discord.TextChannel):
        """Set a mod-log channel using this command."""
        conn = await self.conn
        cursor = self.c
        await cursor.execute("INSERT OR REPLACE INTO mod_logs(channel_id, guild_id) VALUES (?, ?)", [channel.id, ctx.guild.id])
        await conn.commit()
        
        await ctx.send(f'Mod-log channel set to {channel.mention}.')
torn sphinx
#

TIAS

#

Try it and see

snow niche
#

my internet is not that good, so thats why it took so long to reply

torn sphinx
#

Should work

#

I think you need to await the connection, though

snow niche
#

i did await self.conn

torn sphinx
#

Hmm, then try it and see

snow niche
#

ok just want to make sure

snow niche
# torn sphinx Hmm, then try it and see

is the INSERT OR REPLACE correct? i want it so like when some one uses this command once again and changes the mod logs channel... i want it to delete the original mod-log channel and replace with the new one, does this work that way?

austere portal
#

You cant use await out side async functions so use loop.run_until_complete

torn sphinx
torn sphinx
torn sphinx
pure sleet
#

you do realize that that is going to run everytime a request is made basically

brave bridge
#

I'm using PostgreSQL, and I want to store blog posts that have a list of media objects as their content, for example: ```js
[
{tag: "header", text: "Using JSON as a database in 5 easy steps"},
{tag: "sub_header", text: "Denial"},
{tag: "paragraph", text: "..."},
{
tag: "horizontal_split",
left: {tag: "sub_header", text: "Anger"},
right: {tag: "sub_header", text: "Bargaining"}
},
...
]

#

JSONB also supports indexing

tribal cargo
pure sleet
tribal cargo
#

Yes, that's why it's in the before first request method. When launching it will run once as long as the server is running

pure sleet
tribal cargo
#

But it is before_first_request(). Let me check that

pure sleet
#

just run it while the app is not in production to create the database

#

sqlalchemy will still connect to it

#

even if it exists

tribal cargo
#

Still doesn't work without the code

#

Even if i run from Terminal

jaunty galleon
#

Is it possible to get the datatypes of each column in asyncpg using some query?

#

Like that it would return (guild_id bigint, something text)

#

(column_name datatype)

narrow moth
#

(oh, but also: sql.Literal() is one of the things I'm meant to fix... )

wind swan
#

I have a question about database design

#

I will get straight to my use case.

#

I am making a discord game, about db. So oc there are transformations in it

#

but I need to somehow store the data of the transformations, including the limiting rules (discord rule that you have to own on order to transfrom)

#

so that thing that came to my mind is:

#

the problem here, that if I have couple of rr (required rules) for the same transformation

#

lets say, to get ss3, you have to own ss1 and saiyan

#

And I can't store multiple values in the same call since NF1

#

So I need help with that

pure sleet
narrow moth
wind swan
#

Someone told my to not follow NF1, because it's not must

#

but im not sure about that

narrow moth
# wind swan MySQL

hmmm, I am not sure I can help with this very much (haven't used MySQL) but it sounds like you'd want an array type.

#

Postgres does nice arrays. I'm looking pretty hard at them lately and they look awesome.

wind swan
wind swan
narrow moth
#

Personally, I'm in a place where I'd promote getting a working concept up and running before worrying about the finer details. Not in the case of injection attacks and such, but maybe work out how to comply with NF1 later.

#

let me mull a minute.

narrow moth
#

I'm not sure. Optimisation is beyond my power level.
Even if you had table with boolean columns for each transformation, it just sounds like a lot more back and forth.
If transformations are linear (i.e. SS2 requires only SS1, SS1 requires only Saiyan, etc) then it's one value per cell, but you'd be traversing a fair bit.

#

I'm sure there's a way to do it, it sounds like it's something people would want to do pretty frequently.

wind swan
#

thanks a lot for trying to help man!

#

I'll think about that and ask other people

snow niche
#

also btw should i await the cursor??

austere portal
#

Yes

narrow moth
austere portal
#

Normally it's used in a async with statement

snow niche
#

alright

snow niche
austere portal
#

Don't think insert or replace is valid

grim vault
#

SQLite does have INSERT OR REPLACE INTO ...

austere portal
#

Oh didn't know that, thanks ๐Ÿ˜ƒ

pure sleet
wind swan
#

all the NF requiring not having more then one object in a cell

pure sleet
#

you can use relationships to keep the data linked

wind swan
#

ill think on somth

quasi aurora
#

I have used a destructor to close a database in OOPs
Do i also have to delete all the other variables declared in the class or will python take care of that?

brave bridge
# quasi aurora I have used a destructor to close a database in OOPs Do i also have to delete a...

You don't need to delete any objects yourself (in fact, you can't do that), Python will delete them if nobody can reach them, That's called "garbage collection".

You shouldn't normally use __del__, it's more of a last resort or a hack. It's not defined in the language when the __del__ method is called, or whether it's called at all. Instead, you should close the database connections explicitly, like you close files.
Depending on the library, it will look somewhat like this:

def transfer_balance(sender_id: int, receiver_id: int, amount: int):
    if amount < 0:
        raise Exception("Negative balance")
    with connect_to_db() as conn:
        with conn.transaction():
            if conn.fetchval("SELECT balance < ? FROM users WHERE id = ?", (amount, sender_id)):
                raise Exception("Not enough money")
            execute("UPDATE users SET balance = balance - ? WHERE id = ?", (amount, sender_id))
            execute("UPDATE users SET balance = balance + ? WHERE id = ?", (amount, receiver_id))
            conn.commit()
quasi aurora
#

I mean, what if I have to close a database after a signal to end the execution of code is sent

brave bridge
#

Connection objects and other resources already define __del__ so that they will be closed in case of a termination.

quasi aurora
#

oh

brave bridge
#

Additionally, the OS will free up all the resources you've taken

quasi aurora
#

ty

quiet birch
#

I have a query on mongodb

#

I have an ID and type key

#

I want them to be unique together but not on their own

#

so I can have multiple of Type and multiple of ID even though they are the same

#

but if ID = 1727417 and type = 0 I should not be able to re insert it due to duplication

harsh pulsar
quiet birch
#

uh

sharp kindle
#

anyone got a good resource for designing a datbase schema

proven arrow
dim sluice
#

pymongo.errors.ServerSelectionTimeoutError: Does anyone know this error?

#

^ I had changed ip for some reason and that is the reason

hasty hinge
#
SELECT *, ROW_NUMBER() OVER(ORDER BY  xp DESC) as rank
FROM
  usuarios
WHERE
  guild = "a"
  AND id = "a";
#

What is wrong on the syntax of this query? I'm using MySQL 8

#

You have an error in your SQL syntax; it seems the error is around: 'rank FROM usuarios WHERE guild = "a" AND id = "a"' at line 6

#

It was because of rank

#

Looks like you can't use vars named rank in MySQL 8 (it used to work on MySQL 5)

grim vault
#

RANK (R); added in 8.0.2 (reserved)

#

It became a keyword.

whole pendant
#

i want to user to change username but when i want to write the username that already existed the clean_username method not working and thus giving IntegrityError at /users/profile-edit/ UNIQUE constraint failed: users_user.username
HOW TO QUERY IN clean_username SO THAT IS SHOW ONLY USERNAME ALREADY EXIST ValidationError

class UserEditFrom(forms.ModelForm):
    username = forms.CharField(label="",max_length=254,widget=forms.TextInput(attrs={'id':"user-name",'type':"text" ,'class':"form__input"  ,))

    def clean_username(self,*args, **kwargs):
        username = self.cleaned_data.get("username")
        if User.objects.filter(username=username).exclude(username=username).exists():
            raise forms.ValidationError('username is alraedy taken try another one')
        return username
austere portal
#

There is a built in UserEditForm class in django, use that it will automatically do the validation

austere portal
#

ok

#

did you do py class Meta: model = User fields = (...)?

whole pendant
austere portal
#

You are filtering and excluding

whole pendant
#

yes this query i want

austere portal
#

so .exists() will return false

whole pendant
#

hmm

keen sundial
#

Fyi mongo bug recently fixed:

You are receiving this email because one or more of your Atlas clusters was previously running a MongoDB version that was susceptible to a recently identified bug.

The bug, which impacts MongoDB versions 4.4.7, 5.0.0 and 5.0.1, may allow some insertions to succeed even though they should fail with a duplicate key error against a unique index. This bug does not allow documents to be inserted with duplicate _id values.

ancient folio
#

Mongo DB is the best for py?

brazen charm
#

thats a very broad question

#

generally the language has nothing to due with the suitability of a database

brazen charm
#

the suitability of a database generally depends on
a) the structure of the data, e.g. is it relational, unstructured, etc...
b) the scale of the application e.g. sqlite is great for small projects but cant scale
c) often cost, cloud services charge different amounts for different databases and offer different management packages

tardy pike
brazen charm
snow niche
#

But then can't we create more files?

tardy pike
snow niche
#

For each topic a file

remote plinth
#

@ripe rapids (sorry for ping, had to)

brazen charm
tardy pike
supple quail
#

Hello guys, how to use SQLite in memory, any good article with example to teach me how, thx

brazen charm
fierce dirge
#

How to update json file with python ?

pure sleet
fierce dirge
#

I have some data in json and I want to add something in it

pure sleet
#

convert it to a dictionary and then update that, once done, just dump the dictionary to a json file

fierce dirge
#

But when I update, updated data is not reflected in the file

fierce dirge
pure sleet
fierce dirge
pure sleet
#

don't just write the dict, convert it to json first, but i guess you know that

fierce dirge
#

Yes

#

But I want in same file

pure sleet
#

bro, you write to the file you want to change no? so it's overwritten with the updated data

fierce dirge
#

Oh yea

#

Yes*

#

I get it

#

Thanks buddy

pure sleet
#

sure

ripe rapids
lone island
#

asyncpg returning json data as string, how can i avoid that and return dict instead ??

brazen charm
#

probably use a custom Record factory / Record class

lone island
brazen charm
#

actually scrap that

lone island
#

not sure thats goona help me

brazen charm
#

you wanted automatic json conversion

#

see the example that tells you how to do that

lone island
remote plinth
#

i closed all my dms...

brazen charm
remote plinth
#

so i was searching for yours

brazen charm
#

you need it when it's without a schema

#

if you're column is already defined as a json datatype then you dont need to type cast

lone island
#

uh

#

My issue is with regular fetch and how it is translated into the json using standard python utility.

brazen charm
#

you will likely want to use $n::json though when inserting data

brazen charm
#

if you set a custom encoding and decoder asyncpg will use that when inserting and fetching the data before it's returned to you

lone island
#

im still not sure how to use them

#

on fetch

brazen charm
#

you dont touch anything with fetch

#

you just set the type codec on the connection

#

and it should do everything else without needing to touch anything

lone island
#

so all i need to do is to set that codec and it'll return json and no str ?

brazen charm
#

it should do yes

lone island
#

alright thanks imma try it then

ripe rapids
remote plinth
#

spaces.

ripe rapids
remote plinth
#

ah k