#databases
1 messages ยท Page 163 of 1
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}```
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
do you already have data in the db?
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
When you want to store very big numbers, what database do you use?
database or data type?
postgre
big interger
well i need a little help
that am i always supposed to use client.db to connect to a postgresql
Read the documentation here, as you will see BIGINT can store bigger numbers.
need a little help @faint blade
that am i always supposed to use client.db to connect to a postgresql
What do you mean?
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 this
You can do whatever you like, that's just setting an attribute vs assigning a variable.
how do I establish a secure connection between my frontend JavaScript to backend database
and am i supposed to commit in postgresql
If you create a transaction yes, but asyncpg doesn't work like the other database wrappers you may have used.
That is a horrible idea, someone can find the database password and username.
You should build a backend that your fronted uses, the backend would connect to the database.
well if my frontend can access the backend, the user can use dev tools to access the backend and THEN access the database right?
Well yes, but then they can only do things that your backend allow.
If you have your frontend complete access to the database, then they would be able to delete it.
If you create a backend, then you can restrict what someone can do.
You should implement verification
hmm
I don't think it's possible to hide anything
in frontend
so even a Authorization header won't do
in client side rendering no, you cant hide anything
anything can be reverse engineered
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
hmm I wonder how
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'```
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)
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
uhhh
it is saying afk_tab does not exist
is that a table or a column?
i havent used that....
@burnt turret
https://paste.pythondiscord.com/kihixufufe.py
here it is
@thorny field
ok? did it give u these error before? i think ik what's the prob
this is the first time i am using portgres
ok, make sure the database is in the same folder as the program file
@grim zephyr is it?
well can u check the code and the db for me
cause from my opinion everything is correct
@thorny field@thorny field@thorny field
i haven't used prostgre but the code looks fine
sed
@faint blade sry for the ping but can u plz help me
@grim zephyr srry, idk work with dbs that much. srry i can't provide u help ๐
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
wait wait
ok waiting
i tried to do that too
but ur db name is upercaase
oh damn
just changed the name of the table and changed some more thing in the code
ohh, cool!
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)
lol
hmm u need to convert the user_id from a tuple to str
You didn't use BIGINT
int variable has some bytes reserved i am guessing u are using bytes over the limit
I linked you the documentation, a user ID is bigger than 32 bits
ok
how to fetch data from postgre
Look in the documentation, there should be examples of fetch(), fetchrow() and fetchval().
the asyncpg docs have plenty of examples
(although they are short on explanations)
(and they never actually documented the paramstyle)
well what is the use of fetch and fetchrow
i mean what is the difference
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
TypeError: fetchrow() missing 1 required positional argument: 'query'```
what is missing
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()```
well can u give a example of query
the "SELECT *..." is your query from the code you sent (that isn't the only type of query)
ok
It basically says "give me all the rows from the afknote table"
ok
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 * ...")
does it better*
๐
couldn't agree more
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?
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)
SQLite - UPDATE Query - SQLite UPDATE Query is used to modify the existing records in a table. You can use WHERE clause with UPDATE query to update selected rows, otherwise all the row
when it comes to executing the query, you can do it the same way you are executing the select ... where user = ? and guild = ?
yea but i am using a list since the column name is a variable
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
Just a note:
do_query = """SELECT * from main"""
cursor.execute(do_query)```
With this part of your code you are completely undoing the where statement from the first cursor. So the next line `userdbCheck = cursor.fetchone()` will just fetch any row from the main table because there is no condition. As soon as one user is registered any user will undo the `if (userdbCheck is None):`.
ohh damn, how to fix it?
Just remove the two lines I quoted.
Is there a data type for dictionaries with POSTGRESQL
will it still work like the way it used to?
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.
can someone explain why this may not be working?
it has area as an option
here is the data
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.
ok removed it ty
cool pfp my guy
thanks
np
how is it color changing?
what color
yea pretty dope
hello! Could somebody tell me some of the best free online file storage options?
Dropbox? Google Drive? Onedrive?
whats best database for discord.py bots?
sqlite for most "simple" use cases
alr imma learn it
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
I don't think that you understand what a Database is?
Django is used to make web apps / web sites
Tkinter is used to make graphical user interfaces
Databases are used to store data (for example: sqlite, postgres, mongodb)
still, your issue is that you're not setting your settings before accessing them
Well yeah, NoSQL are just Json with extra steps /s
@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
DELETE FROM table WHERE condition
it deletes the whole row where the condition is true
thks cutie

and to delete a certain thing would condition be that?
like a certain id?
How to connect the sqlite database file to the bot using replit web ;-;?
like WHERE column_1 = 0 AND column_2 = "test"
thks again cutie

Help me plz ;-;
replit doesn't make a change, just sqlite3.connect(my_database_file)
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
i didn't find anything can help
:/
!e
import sqlite3
sqlite3.connect("test.db")
@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
test.db is not exists
yeah ik
you cant create files in sneak box
ik
but you can do sqlite3.connect(":memory:")
k
Do i need to upload my database file to replit .-.?
Create a database file in replit
K
@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
i never knew this
Well, now you know it
yes
So... how to save data in database file??
Connect to the database file, do something, and commit. committing will save it
._.
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
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
Sorry, I don't know mongo db, ask your question here and some one else will answer it
ok
idk i use sqlite3 / aiosqlite for my discord.py databases
what should i choose to use this as my db for my bot
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
i think postgresql is easier to use and has more useful features, for basic projects. at "big" scale there are specific reasons to choose mysql instead of postgresql, but i think postgresql is a sensible default choice
@harsh pulsar can you help me plz and sorry for the ping
you almost definitely should look up a tutorial and follow along, setting up databases properly can be specially tricky
Setting up stuff for the first time is not fun
@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
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
member is a list
yes member is a list
why are you casting it to str?
if you want to save a list postgres has a list data type
maybe but why is it not getting the data3
yeah
ofcourse we dont know the structure of your database so
and you can select several collumns in one query btw
* for all columns
here
due to the brackets
though text is fine too
index the data
well they are necessary
ok then
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
either is fine to learn at first, they're extremely similar for most basic tasks
i prefer postgres slightly, these are good learning sites: https://www.mysqltutorial.org/ https://www.postgresqltutorial.com/
just select everyting you need in a single query
that is giving me the whole row data
but i want only the note
wait a sec what will it do
get the value of the note column
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
data3 is a empty list
ok let me try once
your query is unable to find anything in the db
ig the ids dont exist in the table
plz help bud how can i get the data
i mean nothing is working
it doesnt exist is the table
it is you saw in the ss
run the sql query (with the placeholders replaced with actual values) in pgadmin and see
wdym?
run the sql query in pgadmin and see
but how can i run in pgadmin
right click "Tables" and click query tool
ok
then the guild_id and member_id doesnt exist in the table
but they are shown in the rows and data
show the query you ran
if the query doesnt return anything that means the data doesnt exist in the table
it is there in the rows but not showing here
but how?
hmm
!paste
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.
ig the member_id passed in is not in the db
but how
how can i save it in the db
insert it
the user id you are passing in the query doesnt exist in the db
so how can i do that
ok
@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```
are you defining user_id after the elif statement?
and I cant get aiopg to work, great
ok i give up
man my file is still not working properly
someone help
with what
how can i remove the record object in postgresql
idk sorry
what is the "record" object?
a row in a table?
well postgresql
instead of sending "note = no reason provided" its sending "<Record note='No reason provided'>"
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
Yes, unlike normal db drivers returning tuples in asyncpg .fetch* returns a record object
i looked into it
and everything was fine
but still the record object but why
not sure, I haven't worked with asyncpg but their documentation probably explains that in detail
@whole widget sry for the ping but i need a littile help
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?
If you don't need to do await when you do I/O operations, it's blocking.
You can use motor or wrap the calls to pymongo into threads (using loop.to_thread)
can u plz help me
What do you need help with?
portgresql
What is your question?
oh, ok. thanks. still playing with it sync way. will take a look at that motor when i get to turn it into async
how can i remove the record object from my value
If you want to delete something from a table, you need to execute a DELETE statement.
https://www.postgresql.org/docs/10/sql-delete.html
Is that what you want?
it sounded kinda like he wants to extract the value from the record instead of outputting the record object itself
^
yep
you need to do record["note"]
ok but where and how
it's indexable by key, like a dict
can u give a example
can you show your code?
sorry. remind me again.. the to_thread() is function from asyncio right?
yeah sorry, I thought it was a loop method
https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread
!paste
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.
can you point to where you're getting a record in your code?
ah, I see
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"]
what should i replace it with
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?
tbh not very much
can u tell me how to do that
you really should learn the basics instead of just asking for people to do everything for you without understand what they are doing
sry
i really dont know where to use it
I recommend reading this article: https://realpython.com/python-dicts/
Dictionaries are pretty important in Python, you should definitely learn how to use them.
ok
It's fine to use more advanced libraries, but be prepared to learn a lot of stuff while implementing something
ok
well i am using a db so how will i use the method u said as i am not using a dict
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'
Have you ever used the Python console?
no
This thing
well why did u write it like this
??
so i read that the best database to store files from dicord.py is sql? but how do i host it online?
wdym
like suppose i want to store a file shared by a member of a server, how would i do tht?
but arent sql databases stored locally?
what if somebody else wants to retrieve the file?
like if those files are stored locally how would the others access that?
you can put them a code / file sharing app
like i want ppl to store and retrieve info at any time possible while the bot is running....
well
so like host the db
i dont quite follow, sorry๐
np
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
could you elaborate this, i am a newbie in databases, sorry.๐
ok
sorry
ok
which can be used to organize the data
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.
true
umm so id be better off using a google drive api or something similar ?
Are you hosting your bot though?
i normaly just use docker to deploy my bots with docker so i can use my own file system
not at the moment
damn i have a lot to learn ๐ฅฒ
docker just makes deploying apps to the cloud
If you are, then I wouldn't suggest getting a file hosting system. Instead a cloud hosting system.
ok, but are there any free ones tho?
tell me if you need help with deploying it
sure thing! I cant thank you enough!
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...
Sqlite: can I have table names with - example: CREATE TABLE IF NOT EXISTS table-name
technically you can, but it's not recommended. use _ instead
you would need to quote your table name in every query
Ok
!paste
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.
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?
hi
I dont rlly use save and load
so how do u do it?
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.
Your first step in save() is self.load() which sets self.info. Then you delete all and reinsert what you just loaded?
any pymongo user here
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 ?
Huh? Whatโs docker got to do with storing files online?
umm tbh i dont really know ๐ ๐
So where you want to store the files? To which service?
And how do you intent to use these files again
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
its more like organising notes and automatically downloading the one which you want when you ask for it.....
User posts something? Is they always post a file?
yes a file, an image a note something of that sort..
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
but the issue with that would be like if your system is down then the database is inaccessible right?
Like upload to S3 is different to upload to onedrive
Depends what kind of database. Downtime is normal and something you have to deal with. You can do things to prevent downtime
me :p
use repl.it
i will check it out, thanks
how do you deal with that ?
you could create multiple databases
database hosting could solve this issue right/
yes but self-hosting generally is worse than others
ohh
bc more downtime
but where should i do this?
idk you could use like mongodb and some other databases
and you could do a check like if 1 database is down use the backup database
Everything can go down like I said. Even Amazon can go down. Just make sure to have backups and keep system up to date.
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?
This is more complex. Not something you should really worry about too much. Unless your bot is really big or downtime has major consequences on your money etc
might not be that complex tho
its a pretty viable option for most ppl
It requires replication and keeping everything in sync. If you understand how to do that then go ahead.
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?
not necessarily on your system in some cases like mongodb its hosted in the cloud
and if you host your program on the cloud using something like heroku you could get around that
ohh alrighty i did not know that!
in theory
Same issue with your bot. If discord is down or your hosting provider is down then you canโt use the bot. Stop worrying about this and just write your code.
honestly it only matters if you're bot is in like a couple dozen servers or more
but isnt that rare in comparison to me shutting down my system every now and then? ๐ ๐
in comparison very rare
Why would you shut down system when your trying to avoid downtime?
but what if somebody else is running this bot, they wouldnt have access to the db either right?
i dont usually run my computerlaptop throughout the day...
Then your bot wouldnโt be running to so not sure what all this stuff your asking is about
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
Use a service like digital ocean/heroku to host your application
using raspberry pi aint too bad right?
you can reduce the downtime by a lot that way
alright thanks
That is not a bad option, but you need to run it 24/7
that shouldnt be a problem ๐ , thank you!
e
what do you mean by "multiple values for a row"?
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
You mean the columns?
Yea
You can use an array inside the database or just seperate them with a comma
Both are pretty bad
@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
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.
Bad how? They just follow the python specification for database drivers. Just like other libs
Do you have any particular issue with it? Most drivers follow the same interface so usage will be the same
Use the official connector
@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?
Btw im talking about this
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
I'm not sure about this, but is there a way to make a database with OOP? Sorry if this is the wrong channel.
With oop does he mean object orientated programming
yep
yep
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
is SQL considered a database?
no SQL is a language that many databases use for querying
anyone please?

not sure, but can't you just access the sqlite_schema table?
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
you had RETURNING * in two different places, that was probably the original error
o
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
CREATE TABLE afk (
last_seen TIMESTAMP,
user_id BIGINT NOT NULL,
reason TEXT,
PRIMARY KEY (user_id)
);
well it should be the primary key
huh
yeah probably the on conflict is wrong because wherever I see an example of on conflict it is done differently
I fixed it by changing PRIMARY KEY to UNIQUE
now it works as it should
that shouldn't be necessary. weird
double check the docs
thanks
Hi, i use aiomysql but there is a delay in it How do I know the reason for this comment?
Can you show your code?
@zealous parcel What do you mean by 'delay'?
When calling it delay until it responds only when I use this aiomysql
Have you tried testing it without the bot?
no
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
When I turn on the bot it works normally but this happens after a while of running
loaclhost
@brave bridge
can you perhaps look at the connection list in your database?
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
@next sun Maybe you have too many open connections?
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
humm
when running htop
I see not script
basically I setup a cronjob to run a script everyday
I don't think htop will show that. Can you run SELECT * FROM pg_stat_activity; on the database?
you are right that the script in the ends hangs kind of waiting for something (only when it fails on the db side)
I think I can
let me see...
I can run
but....
what is important in the table?
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
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
(ping) is there a good tutorial how how to make a data base for a discord bot
Have you seen pinned messages?
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
results?
Depending what you receive
It can be users, products, accounts and so on
Gotcha.
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?
Wait what psycopg3 is there
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.
it's written in python
Thanks I'll check it out, I've been using psycopg2 for all this time
Do you use psycopg2 on regular basis?
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.
generally psycopg2 is still the more popular setup
just because psycopg3 is still a WIP
it has async support, nice
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
Yeah, I think you have the right assessment, mostly because it takes companies few years to adopt a new technology.
depends on the company, and generally modules like that are pretty simple to interchange in a good setup
Yeah, but I like the pep, it is nice
Are queries written the same way in PostgreSQL and in psycopg2?
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
yeah, psycopg has to do its SQL injections query formatting, because $N isn't in the DB API 2 spec
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.
yeah
well
depends, sql logic -> postgres, placeholder behavour -> psycopg2
why my execute many being slow? -> psycopg2
"db-api" and "pep 249" are also good search keywords
!pep 249
also i think psycopg3 is out, or at least in some kind of beta state https://www.psycopg.org/psycopg3/
Python adapter for PostgreSQL
why my execute many being slow? -> pip uninstall psycopg2 && pip install asyncpg 
so is "pep 249"/ "db-api" a wrapper for most common databases? So queries would be consistent across different databases
not a wrapper, but a pattern that database libraries are generally expected to follow
this pep is why most python database libraries have very similar APIs
but are still incompatible because they have different query syntaxes 
except asyncpg, which deliberately deviates from the standard https://magicstack.github.io/asyncpg/current/faq.html#does-asyncpg-support-db-api
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.
precisely
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?
How are you updating the value?
The query?
im just changing it through pg admin
An alter query will do the job
i did that to
didnt work
ALTER TABLE ONLY pokesetspc ALTER COLUMN card_amount SET DEFAULT 1
``` is what i did
How are you inserting the data?
The default value of a column will only apply if you are inserting a new row without the column specified
bruh
so does that mean only the new columns will have 1 ?
Yeah
Update all of the rows then
oh well, ig its gonna be 0 then
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()?
run conn.commit() when you need to commit a transaction.
I think the context manager automatically does it, but I could be wrong, you'll have to check
When you say "context manager automatically does it", do you mean psycopg2 driver automatically does commit?
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.
Are you using psycopg's context managers?
Sorry, I dont know what that is. Can you post a code snippet and I might get an idea what you mean.
A context manager is something you use in a with statement
oh no, I am not
You should look into this: https://www.psycopg.org/docs/usage.html#with-statement
Are you familiar with transactions in SQL?
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.
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.
"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?
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
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.
that's why you want to use transactions:
@app.post("/posts")
def insert_post(request):
post = post_from_json(request.json())
if post is None:
return 422
conn = create_db_connection()
try:
with conn:
with conn.cursor() as cur:
save_post(cur, post)
finally:
conn.close()
return 201, {"url": f"/posts/{post.id}"}
in psycopg a transaction is automatically created when you execute the first statement.
(or so it seems from the docs)
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.
...this is equivalent to
conn = create_db_connection()
try:
with conn:
with conn.cursor() as cur:
save_post(cur, post)
conn.commit()
finally:
conn.close()
conn.rollback()
what are you writing, if that's not under an NDA?
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.
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
I thought about that, but the program would run from different computers.
Do you actually need to mutate the database from different servers?
What do you mean by "mutate the database"?
why do you want several computers to interact with the database directly?
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.
Are you exposing the database directly to the users of your application?
And I am trying to rely less on this third party API.
Database and the program is for internal use.
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
How would someone run DROP TABLE things?
It's a local python program/script with database.
So only people who have this program would be able to run it.
You said that different computers would have access to the database, right?
maybe I'm misunderstanding the context
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.
@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
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?
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
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?
Yeah I have an RDS instance running for this postgres database.
ok I will think about this.
@mortal light You could just update the data in the background, right?
e.g. with a systemd timer https://wiki.archlinux.org/title/Systemd/Timers
and while it's updating you either refuse to serve responses, or just serve outdated data if that's fine
Just curious but how long have you been trying to solve this problem? Iโve seen you ask about how to go about this few months ago and your still figuring it out?
btw shouldn't excel handle 5000 rows just fine?
just attach an API to excel ๐
and I'm only half joking
Yeah I asked something about database and sqlite couple of months back, but I had to focus on rest of the project, so now I am getting to database.
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.
i have been download postgresql and this happening. Please help me!
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
ok, well that was unexpected, i didnt think it would be this easy but i got it.
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
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?
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?
anyone know sqlite?
What is your question
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}.')
my internet is not that good, so thats why it took so long to reply
i did await self.conn
Hmm, then try it and see
ok just want to make sure
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?
Await the connect
You cant use await out side async functions so use loop.run_until_complete
it may work, not sure about this function
yeah
await aiosqlite.connect("db's/moderation.db")
why are you running create_all on before request?
you do realize that that is going to run everytime a request is made basically
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
O thought it's to create the database
you only need to create it once
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
before_request runs before each request, not the first
But it is before_first_request(). Let me check that
ok i see my bad. but still
just run it while the app is not in production to create the database
sqlalchemy will still connect to it
even if it exists
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)
not sure about asyncpg, but for psycopg:
col_details = []
try:
cur = conn.cursor()
cur.execute(sql.SQL("SELECT column_name, udt_name, character_maximum_length, column_default FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name LIKE {}").format(sql.Literal(table_str)))
col_details = cur.fetchall()
cur.close()
except psycopg2.Error as e:
print(e)
return col_details```
(oh, but also: sql.Literal() is one of the things I'm meant to fix... )
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
when does this error occur? when you run create_all?
what database are you using?
MySQL
Someone told my to not follow NF1, because it's not must
but im not sure about that
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.
yes, oc. but the problem is NF1. I can't have 2 objects on same cell
So โ๏ธ
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.
um...
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.
Yes
Good luck mate.
Normally it's used in a async with statement
alright
so do u think this will work?
Don't think insert or replace is valid
SQLite does have INSERT OR REPLACE INTO ...
Oh didn't know that, thanks ๐
normalize it to like 3rd or fourth form
yes, but how
all the NF requiring not having more then one object in a cell
you split the table to confine with the normal forms
you can use relationships to keep the data linked
ill think on somth
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?
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()
I mean, what if I have to close a database after a signal to end the execution of code is sent
Connection objects and other resources already define __del__ so that they will be closed in case of a termination.
oh
Additionally, the OS will free up all the resources you've taken
ty
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
@quiet birch https://stackoverflow.com/a/14693445 ?
anyone got a good resource for designing a datbase schema
Any good guide on normal forms will help you achieve the basic principles. But there is no universal designing guide. What may seem good for one project might not be good for another. Best is to understand your problem your working on and then work around that.
pymongo.errors.ServerSelectionTimeoutError: Does anyone know this error?
^ I had changed ip for some reason and that is the reason
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)
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
There is a built in UserEditForm class in django, use that it will automatically do the validation
I HAVE MAKE COSTOM USER MODEL
THE BUG IS IN clean_username method
You are filtering and excluding
yes this query i want
so .exists() will return false
hmm
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.
Mongo DB is the best for py?
thats a very broad question
generally the language has nothing to due with the suitability of a database
Can you elaborate?
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
b) What is the limit of sqlite in terms of size of a project?
depends on the application, but you're limited to being single threaded with heavy locks so performance degrades significantly when you get larger tables or need concurrency
But then can't we create more files?
Great input, thanks. Any suggestions for DB that is a good choice in terms of scalability? Financial data.
For each topic a file
@ripe rapids (sorry for ping, had to)
generally Postgres and / or Scylla (Cassandra) is a pretty good choice depending on scale and amount of writes needed
I will have a look into those. It would be a good amount of writes needed .
Hello guys, how to use SQLite in memory, any good article with example to teach me how, thx
literally the same as normal sqlite except you connect to :memory: rather than a file path
Oke noted ๐
How to update json file with python ?
what do you mean?
I want to update json file, opened with open() ?
I have some data in json and I want to add something in it
convert it to a dictionary and then update that, once done, just dump the dictionary to a json file
But when I update, updated data is not reflected in the file
Means open file, get dict, close file, update dict, then open file in write mode and write the dict and close file ??
i guess, use the json library in python
Yes I used it
don't just write the dict, convert it to json first, but i guess you know that
bro, you write to the file you want to change no? so it's overwritten with the updated data
sure
had to what.
asyncpg returning json data as string, how can i avoid that and return dict instead ??
probably use a custom Record factory / Record class
more details ?
not sure thats goona help me
so after configuring that, i can return json as dict using fetch ?
res = await conn.fetchval('SELECT $1::json', data)
they used ::json extra
thats just explicitly say the type to postgres
so i was searching for yours
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
uh
My issue is with regular fetch and how it is translated into the json using standard python utility.
you will likely want to use $n::json though when inserting data
yes
if you set a custom encoding and decoder asyncpg will use that when inserting and fetching the data before it's returned to you
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
so all i need to do is to set that codec and it'll return json and no str ?
it should do yes
alright thanks imma try it then
why dont u just search my name
spaces.
u cn still search it
ah k
