#databases
1 messages · Page 138 of 1
for the first question, does every guild have a pre-existing default value for the theme or something in the database?
i wanted to do it if
there is no value in the database for that guild
which if im right would give None back
then id be animals in this case
Yeah, if you try finding a document which doesn't match it'd give you None back
if themes is None:
self.slap_gifs = self.gifs["animals"]["slap"]
oh huh I'm a little confused by your first query. You seem to be passing the arguments that are usually used with an update into a find_one
do you want to insert if the guild doesn't have existing data?
would that matter?
cause if it doesn't have existing data it would just have the default theme right
but you'd be setting the default theme only in your code
i guess that is your choice really - but that query is still incorrect
you pass the second argument (the $set part) only for updates
I mean if it's better to set the default theme
in the database
im all ears
also how would I fix it then
the query? just remove that second dictionary
the way it works is that in an update query - the first dictionary acts as a filter (to specify which documents to update) and the second one actually tells how to modify the data
(i'll brb, a little busy)
Yh dw
By removing the second dictionary it wouldnt store the value for themes in the database tho right
hi I have a question
if double precision PSQL type has a 15 digits precision, then as long as my number is less than 16 digits, it WILL NOT be approximated? ie 1234567.8901
it wouldn't; it won't now either
because you can't insert/update data with a find query
a find only retrieves data
you'll have to write another query, to update/insert as well
can you explain again what exactly you want done there? do you need to retrieve the data, or is just updating enough?
They do the command
Set theme "theme"
The theme they set, if valid
Needs to be put in the database with the guild
And then in the slap command
It retrieves that data
And tells the slap command from what category to take
The gifs from
right I get it now
btw careful with sending multiple messages, it can trip @delicate field s spam filters lmao
Ahh noticed
so anyways, in your first command you don't really need to retrieve the data at all.
you want to insert data if that guild id doesn't exist yet in the database, and if it does exist you want to update it
insert + update = upsert, does exactly ^
so the query should be something like
db_themes.update_one({"guild_id": ctx.guild.id}, {"$set": {"Theme": theme}}, upsert=True)
notice again, the first dictionary tells the db which document to update, and the second dictionary tells the db to set that document's Theme key to whatever the theme was
upsert=True does the insert-if-not-exists, else-update logic
yeah i dragged that out a lot haha
but that query logic, once you understand it is quite helpful
Yeah i heard motor is better since it's async
tl;dr is using pymongo is blocking, motor is an async mongodb driver lib
ehh I wouldn't call it better, it can be kinda slow
but at least it won't block
In what situations would it be better
in this situation I'd say it is better, because it won't block your bot
rn your bot will be frozen for the nanoseconds that it is interacting with the db
it won't feel like much now, but it can add up quickly and make your bot laggy
using motor will keep the db interactions asynchronous too
Is it a big change from setting up?
I have a question. If you were making a client that had some sort of "log in" or any sort of data that needed to be trasnferred to a database, how would you do this? Would you put the database on an API and have it request the API? or would you use a server of some sort?
Someone told me that sqlite3's stocking in json... Is it true?
what do you mean by "sqlite stocking"?
afaik you can't store json in sqlite
No, sqlite is stocking in json
Someone told me that
Is it truz
True
oh no i don't think it does
Ok thx
one minute, i can try verifying
https://www.sqlite.org/fileformat.html ehh i can't figure out what the format is but by reading that i think i can safely say it isn't json
Ok thx
I recall that Sqlite started to add json colum data helpers, but that's certainly not about file format
interesting, hadn't heard of that before
how do I use row_number() to get someone's position in an ordered table? I've tried everything i can think of and read every documentation but i can't find the right syntax nor do i understand how to use it at all, and everything i've tried has just returned an error.
What database?
Also keep in mind that window functions need a window to work with
i have never used window functions before
the most complex thing i've ever done is order a query by experience in descending order
either way context is i need someone's exact position in the leaderboards when ordered by experience, the table is called g{id} (guild specific tables) and users are saved by discord id
id INTEGER PRIMARY KEY, experience INTEGER, level INTEGER (table structure excluding unrelated fields)
🤔 so something like
SELECT id, experence, level, ROW_NUMBER() OVER (ORDER BY experience DESC) FROM tablename
```?
i'll try it, thanks a lot
why do you have tables per guild though?
What exactly is the row number used for again?
it's a discord bot and users can earn experience and coins for chat activity, but i didn't want coins to overlap servers
#databases message has some reasons on why you wouldn't do something like that
to display the current user's position on the leaderboard
the leaderboard is sorted by experience in descending order
you could have all the users' data in a single table, and add a guild ID column to it as well?
(edited the query I sent to order by desc)
that was my original idea but my friend (who is a more experienced developer) proceeded to be very offended by this and called me a dumbass
then told me to do it with guild-specific tables instead
Sounds bad, insulting beginners is a red flag
but either way the guild table structure is pretty cemented and if i must make a change i would edit the creation command to add an extra field/delete any other one etc
not necessarily a beginner but it's what we do we tease each other a lot and stuff
if you plan on changing, i'd say the sooner the better
either way i severely doubt he had any bad intentions
what was their reasoning to use guild specific tables?
he insisted that records should have ids that don't ever overlap, so having multiple records with the same user ID = huge no no
i suggested adding a third ID column that's fully unique but he still didn't rlly think that was a good plan
either way you make some solid points but i still feel this is one of the more logical solutions to overlapping balances and guild specific cosmetics etc
and i also have access to a MySQL database so a few extra tables wouldn't be the end of the world (i think/hope dearly)
if you store guild ID + user ID, it remains unique though (a user's data is specific to a guild, and if they're in another guild with your bot, that guild's ID will help you uniquely identify the user's data for that guild)
you mean like id = {userid},{guildid} or something like that?
i don't know about how this affects performance, but I feel like querying would be much more intuitive this way. But that's just my opinion.
personally haven't had any issues with query's actually
not really, the table would have a guild_id column along with everything else you have
Ids in table names is a total horror and indicates a design failure
i made a bunch of functions + i use the discord API so whenever i need to retrieve data i just set table to f"g{ctx.guild.id}" which has worked just fine for me
yeah that made sense to me as well but idk
also i guess
rethinking your database structure might be a good idea
probably but i wouldn't have a clue what to do with it and even then i already have months of work built upon this structure from day one so i feel like it's a bit late for a structural reform
very first thing i did on this project was set up a script that automatically adds new users and guilds to the database as a unique table (guilds as unique, users don't have their own tables)
if you guys had to estimate, how big an issue do you think my current database structure will be in the future?
I can't really say, I'm not knowledgeable enough on the topic, but I have a feeling managing the database will be a nightmare as your bot grows
- I can't really imagine how you'd be doing joins and other fun stuff
joins let you combine different tables together in a query, to consolidate different inter-related data
if that made any sense
it didn't but then again i'm not the brightest
i don't see what i'd really need that for though
say you have this table with the users' experience and everything. you could have another table that stores, say, inventories.
with a JOIN you could, for example, get both the experience and the inventory together in a single query
oh i have the users' inventories in the same table
so far a guild table houses users' experience, currency, level, id, aswell as cosmetics such as their profile colours, a name prefix, etc
haha dump them all in together, avoid the need to join huh
haha yeah
i feel making a seperate table would just be unnecessary anyway
doesn't really have any added value
you're potentially making millions of tables here with guild-specific tables
and you have a problem with making 1 extra table? :p
honestly if this bot reaches a million guilds then god is already on my side anyway
not necessarily but i just don't see what the point would be
Maybe someone else here could better explain the benefits of doing joins
but i don't think your current database structure is the best way to do things out there
it's probably not but i wouldn't know what other solution to use, plus if it doesn't pose any severe issues i'd hate to mess with it right now as nearly all features are built on this structure
if i ever do a remake i'll make sure to revise the database structure though
fair enough
oh also i tried the row_number() solution you guys sent me and i got a syntax error
i was wondering how to get the row number of a specific record id aswell btw
since i only need to get the leaderboard position of the current user so
Ignoring exception in command leaderboard:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/Users/macbook/Desktop/Projects/Discord BOT/main.py", line 1613, in run
pos = db.get_leaderboards_pos(ctx.author.id, f'g{ctx.guild.id}')
File "/Users/macbook/Desktop/Projects/Discord BOT/database/functions.py", line 217, in get_leaderboards_pos
c.execute(f"SELECT id, experience, level, ROW_NUMBER() OVER (ORDER BY experience) FROM {table}")
sqlite3.OperationalError: near "(": syntax error
ohh that explains it yeah
i use sqlite for development but my friend is gonna be hosting it once it's released and he has access to a mysql database which he will connect it to afterwards
Oh, in that case you'd want to make sure that every query you write matches the SQL standard which is accepted by all databases (and not specific to one)
will do
Sorry I gotta go now - this is what I could find for the row_number function on sqlite https://www.sqlitetutorial.net/sqlite-window-functions/sqlite-row_number/
thanks a lot, have a good one
hey @glad spoke, our filter got mistriggered. Can you post the question without the invite domain name?
I am retyping it now
thanks :3
Hey. I am currently trying to delete a discord invite from my SQL database when the invite is deleted. The error is with the SQL and nothing to do with discord.py so this channel is appropriate imo.
Here is the error:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '://an invite link' at line 1
the code I use to do this:
url = "https://d1sc0rd.gg/{}".format(invite.code)
print(url)
query = "DELETE FROM invites WHERE url=" + url
cursor.execute(query)
db.commit()
I don't actually have it as d1sc0rd. I just wrote it like that to avoid a filter. :D
any help is greatly appreciated
@glad spoke You should never format SQL queries like that. If someone types https://discord.gg/'';DROP TABLE invites; (not exactly this, but something similar), you're in big trouble.
You should use formatting tools built into your database adapter:
cursor.execute("DELETE FROM invites WHERE url=?", url)
As a side note, if you're using discord.py, you should be using an asynchronous database adapter (aiosqlite for sqlite3, asyncpg for Postgres)
oh, cool. I'll have to look into that for sure
I imagine it'd work better when the bot is on more servers
because when you're making a blocking call (like input(), time.sleep(42) or cursor.execute(...)), the entire event loop gets clogged.
On a small scale it doesn't matter much, though.
So this is how you should always format queries. It does the right thing (wraps the string into quotes and escapes all special characters).
thanks for your help
however, one last thing.
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
I got this error when running the script you provided me with. I'm not sure why though.
@glad spoke Can you show the code?
sure thing
You can use https://paste.pydis.com not to trigger the filter
Ah, should I be using VALUES (%s) instead? I'm familiar with that
It should be
cursor.execute("DELETE FROM invites WHERE url=%s", url)
or
cursor.execute("DELETE FROM invites WHERE url=%(url)", {"url": url})
it sucks that different syntax variants are supported depending on the adapter
if anyone might find this useful, thought i'd post it here: https://github.com/AtakamaLLC/notanorm . it's easier-to-use (for me) than hitting the db directly, protects against sql injections, etc.
Why is this solution written in a subquery way ? Is there any difference in performance when it can be written in lesser lines?
There are few more where the query was written in a similar fashion
where did you find the other solutions?
It was in github
Well, if you want to see the performance of each, you should just create a very big table and check the query time 🙂
I'm doing a coursera course for a free trial and to check my query i saw this solution in github
But i don't get why it is written that way?
I have no idea myself. Doesn't make much sense to me.
They are pretty much doing the same thing. The other solution is just selecting from the subquery
There will be no performance difference
Yes, the results are same
the way you wrote it seems most obvious to me.
Although the last one also gets the count -- why?
But i was just confused whats the point of extending no. Of lines as im a beginner
You can always add EXPLAIN to the front of the query if you want to see how the query runs
Ooo will it run on the coursera platform?
The last one is on a different table so it’s probably a different question they are attempting for
ah right
I don’t know, never heard of what that is
Yeah, i just attached that image because that query can also be written without using the subquery way
But to answer your question there’s no difference. If this is some sort of competition people sometimes change their solution so it doesn’t look “copied”
@proven arrow random question: can you recommend any resources on relational algebra?
I had some links and books on my laptop which is broke currently and waiting for a new one. If I can find them again I will share them.
alright, thanks 👍
i could be wrong on this and i'm not a security expert, but can't people input things like url; DROP TABLE invites; # whatever the SQL comment symbol is ?
to kind of insert another command between the actual one
That's exactly what formatting in conn.execute is for. It escapes all special characters.
oh it does? i had no idea
Yep. Imagine making sure to escape every value correctly.
I wish there was a way to warn people on using f-strings or other formatting forms in queries.
f strings also fuck up?
my whole database functions file uses c.execute(f'query with dumb var or whatever') lol
Whenever you have user input or unsanitised data coming from external sources like an api you should always use parameterised queries.
true
i don't think i have any user-input sql queries but i'll be sure to check before release
in case i do, how do i fix this?
Well Sql injection is one benefit. Prepared statements also have other benefits, like it can give you better performance.
In your Sql query string the value, would be replaced by a placeholder. The actual value to be sent to the database should be supplied separately. What placeholder you need, and how you pass the values differ between some libraries that don’t follow the python db-api or however they implement it, but you should find examples in their documentation.
alright thanks
do you recommend asking someone to do some basic pentesting on shit like this?
nothing too big just checking for input vulnerabilities etc
cause i've found another issue where display names (which are saved in the database) can cause errors
One more doubt,
where col Like '%.99%'
Why this condition works? Shouldn't a wildcard only work for text strings? @Lufthansa-Pilot#0623
if you put multiple backslashes in your display name it returns an error for invalid tokens etc
I have used triple quotes and store the query in a var.. Never got any issues @rose
Ignore if it's out of context
Hello! How would i make a database that stores users balances in discord.py?
please ping me if you know!
@nocturne lance
It matches characters and those are valid characters
dm me, i'll show u how i did it
yes?
dm me
The column contains float type values. '%' works on numbers, floats as well. In programming dont we use wildcards only on strings to match values?
How is it still working on floats?
Some databases allow it
anyone here use pg_dump before?
hey i got a problem
need help with its not big of deal
pyfrom flask_wtf import FlaskForm
its unable to import
any help ?
nvm ish working
I have some static config data stored in yaml and I've created a class to correspond. Is this considered bad practice or even, the wrong approach?
My reason for doing so is to be able to easily access the variables with simple dot notation. There will be many instances across many scripts that these variables would need to be used.
What I do know is this is a rather static approach in that it relies on me to maintain the class should anything change in the yaml configuration. Is there any alternative that is more dynamic?
docker:
socket_unix: null
socket_tcp:
ip_address: 0.0.0.0
port: 2375
cert_path: null
containers:
mongodb:
name: mongodb_bot
image: mongo
redis:
name: bot_redis
image: redis
class docker:
socket_unix = config['docker']['socket_unix']
class socket_tcp:
ip_address = config['docker']['socket_tcp']['ip_address']
port = config['docker']['socket_tcp']['port']
cert_path = config['docker']['socket_tcp']['cert_path']
class containers:
class mongodb:
name = config['docker']['containers']['mongodb']['name']
image = config['docker']['containers']['mongodb']['image']
class redis:
name = config['docker']['containers']['redis']['name']
image = config['docker']['containers']['redis']['image']
Now accessible using dot notation...
print(docker.socket_unix)
I've tried doing my script a little differently:
cursor.execute("DELETE FROM invites WHERE code=" + str(invite.code))
however I get this error:
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'q8e5Bvwq' in 'where clause'
q8e5Bvwq is the invite.code (a discord invite code which is no longer valid because I deleted it)
any help is hugely appreciated! :3
I wouldn’t say it’s bad practice. I’ve seen some popular frameworks do a similar thing.
Alright, it's still super clunky in that it relies on my to maintain the class should anything change in the yaml file
is there a more dynamic approach?
I believe you were already told to use parameterised query previously by another user. If you did you wouldn’t have this issue.
Your error is being raised because your value of the invite code was not passed to the query as a string literal, so it’s treated as a column.
Yeah that’s the only thing, with the approach you mentioned. You would have to recache the values.
I'm not so worried about the values, i'm more worried about new keys/entries to the config
But how often would those values change?
New keys would be added with code change no?
But my OCD tells me things should be more dynamic than that
I meant, new keys added in the yaml config. I'm trying to find a solution to have that dynamically updated in the class itself
Would probably require a whole seperate class dedicated to creating that I'd guess?
Your over complicating it
Complicating it to make it less complicated later yes
I'm looking for some futureporoofing I guess
You add new keys to the config when you change some piece of code or add new feature or something. So your going to have to write the code so it reads those keys anyways.
Let's say I add a new property/key to the yaml, I'd like that to be reflected in the class automatically. Therefore I only need to deal with one yaml file and nothing else
Ah true
Forget what I said
The class is not magically going to understand what the keys in the config file mean
Yeah I guess, if python doesn't know it's there then there's no point lol
Has to be defined somewhere I guess
Yeah, my OCD of living in a perfect magic world haha
Cheers
Anyone have recomendations for taking a SQLite DB and uploading to google sheets? I cam stuck at taking my Select and parsing through the contecnt of fetchall
You can try export it as a csv and then import into sheets
Some database browsers have a built in option to do this
I was using g spread so it could connect via sheets api and would still want to use that
It's simple to append new info just taking my info and creating a new list with variables is confusing me
So what format does the api require your data to be in
What exactly is confusing?
SQLite will return rows as tuples
Each element of the tuple is a column in the row
variable = [dictName['Name']]
How the g spread api appends info from a dict
So to take the info from a tuple and convert into a dict
is confusing me
Either loop through it and make your own list of dictionaries where each dict is a row.
Another way would be to use the sqlite Row class or make your own row_factory with sqlite.
The second option would return you the data in a dictionary format when you fetch it from the database. https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.row_factory Either you make your own row_factory or use the already made Sqlite.Row class for this. The code in the link gives examples.
how do I delte duplicate rows in psql?
Yeah I tried with that but it didn’t work sadly
@burnt turret so I haven't looked into the motor things yet but just wanted to see if everything worked at first with pymongo. Anyways for some reason it sometimes mixes the themes together so lets say id set it to theme animals, it would still use the previous theme yet for either all gifs. or it would use the previous theme for the slap command for example but the hug command would be updated to the current theme. idk it seems kind of weird to me lmao
let's say I have some data coming over an API like
{"author": "foo", "books": [{"title": "bar"}, ..]}
i'd store it in two tables authors and books with a foreign key to author-id. so far so good, that works.
Now, both authors and books have IDs - over the API you can UPDATE the whole entry meaning you'd send something like:
{"author": "foo", "books": [{"title": "foobar"}, ..]}
how would I go about properly updating that one book title that changed? or how would something like that usually be done?
Do you think mongoDB triggers would be a good solution to this problem?
#help-croissant message
Or would there be a way to do it in Python/Mongo?
Hi guys! new to the community here. I have questions specifically about python pandas that no one I know seems to be able to answer. Is anyone willing to take a jab at my question? 
Define "duplicate rows"
nvm
@burnt turret What should I do to get the primary key?
show your code and explain the question again please
@tasks.loop(seconds=30.0)
async def checker(self):
connection = mysql.connector.connect(
host='localhost',
user='root',
password='open1234',
database='discordbot',
use_unicode=True,
charset='utf8',
port=3306
)
connection.autocommit = True
cursor = connection.cursor(buffered=True)
cursor.execute("DELETE FROM muted WHERE clock <= %(time)s", { "time": datetime.now() })
cursor.close()
connection.close()
I am trying to get their user_id before I delete them from the table then set the user_id to a variable so I can then remove their muted role aswell as remove them from the database of muted users @burnt turret
just googled it and apparently MySQL doesn't have an equivalent to the RETURNING clause I'd said about earlier
you'll need two queries, first to SELECT and then to DELETE
So the first would be similar to the second but its purpose would be to look for those in the db and get the user_id and the second would remove them from the db correct?
yep
also, two other things
you're using mysql.connector which is blocking - use aiomysql
Maybe later
the other thing is that opening a new connection each time is wasteful, you should be making a single connection for your bot and using it everywhere
you can make one on the bot starting up and assign it to a bot variable, which would then be accessible across all files
Aren't variables reset per function? Also once I select any piece of data with an expired date how will I get the primary key?
Bot variables are tied to your instance of commands.Bot
So I would define it with my command_prefix n shit?
sure, like
bot.db = mysql.connector.connect(...
``` after you do ```bot = commands.Bot(command_prefix=...)```
this would then be accessible everywhere as bot.db, and in cogs that'd be self.bot.db
Alright, I will do that tomorrow, anyhow how do i fetch the primary key of a piece of data?
I don't understand - don't you know what the primary key of the table will be?
so can't you just model your query to just get that column
Would it look something like this?
SELECT user_id FROM muted WHERE clock <= %(time)s
yeah
Then I fetch the data and do my magic to remove the role right?
I guess so
So does this work?
@tasks.loop(seconds=30.0)
async def checker(self):
connection = mysql.connector.connect(
host='localhost',
user='root',
password='open1234',
database='discordbot',
use_unicode=True,
charset='utf8',
port=3306
)
connection.autocommit = True
cursor = connection.cursor(buffered=True)
cursor.execute("SELECT user_id FROM muted WHERE clock <= %(time)s", { "time": datetime.now() })
member = cursor.fetchall()
muted = get_role(815735188045692969)
await int(member).remove_roles(muted)
cursor.execute("DELETE FROM muted WHERE clock <= %(time)s", { "time": datetime.now() })
cursor.close()
connection.close()
the database part should be fine I think
not so much about the dpy part - member will now be a list
Ah shit
- you'll need to get that user's
discord.Memberobject (guild.get_member possibly) first, before callingremove_roleson it
get_role(815735188045692969) this cannot be used alone like this, it's a method
I don't remember of what exactly, most probably...guild I'd guess. Or bot
It is guild
Also how do I convert it not into a list?
thing is at any time you may be getting more than one users who meet that condition right (more than one user's mute may be expiring then)
you will want it to be a list
the better solution would be to loop over the list it gives you and do the role removing for each member
So will I want to do something like this?
for members in member:
guild.get_member(members)
I am running out of names 
assign the list that the db gives you to members and then do for member in members
Would this work?
member = cursor.fetchall()
for members in member:
guild.get_member(members)
muted = guild.get_role(815735188045692969)
await members.remove_roles(muted)
the indentation looks off
hello guys
also please do this those variable names hurt me 😔
I know python basics
Okay lol
@burnt turret Since I don't have any args how am I suppose to get guild? I don't have ctx or anything
In the function I only passed in self
No I mean in your database
if I were you, I'd store the guild ID of each user as well
Oh yeah I only have 2 things, mute time and user_id
if the same person is muted in two guilds using your bot, there'd be no way of knowing which guild to unmute in if you don't store the guild ID as well
The bot is only for one server
oh in that case you can just hard code your guild ID in
use guild = bot.get_guild(your guild id)
would I do self.client?
yeah - whenever I say bot. i just mean "your instance of commands.Bot" - if you call it client do that
so yes, self.client.get_guild(...
It said NoneType has not attribute to get_role
that's because the bot's cache hasn't filled yet, because the loop is running before the bot is ready
so get_guild returned None
So will it work just fine?
it should
if you've got the IDs right
you could have the loop wait for the bot to be ready by adding a before_loop
https://discordpy.readthedocs.io/en/latest/ext/tasks/index.html there's an example titled "Waiting until the bot is ready"
What would I put in said before_loop
refer the example I linked, it gives you exactly what you need
The bot isn't removing anything from the db etc. anymore
how are you testing this?
Muting the person for 1 minute and waiting for them to be unmuted
huh. can you show the code again
@tasks.loop(seconds=30.0)
async def checker(self):
connection = mysql.connector.connect(
host='localhost',
user='root',
password='open1234',
database='discordbot',
use_unicode=True,
charset='utf8',
port=3306
)
connection.autocommit = True
cursor = connection.cursor(buffered=True)
guild = self.client.get_guild(814695010603368488)
cursor.execute("SELECT user_id FROM muted WHERE clock <= %(time)s", { "time": datetime.now() })
members = cursor.fetchall()
for member in members:
guild.get_member(member)
muted = guild.get_role(815735188045692969)
await member.remove_roles(muted)
cursor.execute("DELETE FROM muted WHERE clock <= %(time)s", { "time": datetime.now() })
cursor.close()
connection.close()
muted = guild.get_role(815735188045692969)
await member.remove_roles(muted)
``` these two lines need to be indented under the for loop
also, the datetime.now() will be slightly different between both queries
I'd assign it to a variable or something at the start and then use that variable in both queries
Alright, also it is saying get_member is undefined, how will I define it?
Wait no I read it wrong, it says NoneType has no attribute to get_member
that's the same thing as I'd said earlier about the get_guild returning None
sorry I gotta go now though :/
Ah alright, thank you for your help :)
@command()
async def slap(self, ctx: Context, member : Member ):
data = db_themes.find_one({"guild_id": ctx.guild.id})
if data is None:
slap_gifs = self.gifs["anime"]["slap"]
else:
slap_gifs = self.gifs[data["Theme"]]["slap"]
if "used_slaps" not in dir(self) or not self.used_slaps:
self.used_slaps = copy.copy(slap_gifs)
emb = Embed(title=f" **{ctx.author.name}** slaps **{member.name}**...", color=0x3498db)
bla = random.choice(self.used_slaps)
self.used_slaps.remove(bla)
emb.set_image(url=bla)
If I set the theme to for example humans, it still uses the previous set theme until I reload the cog. Sometimes it even uses gifs of 2 of the 3 themes I have when both of them are not the theme I set it to
@set.command(name="theme")
@has_permissions(administrator=True)
async def set_theme(self,ctx: Context, theme: str):
if theme not in self.gifs.keys():
await ctx.send("this theme doesn't exist")
if theme in self.gifs.keys():
self.slap_gifs = self.gifs[theme]["slap"]
db_themes.update_one({"guild_id": ctx.guild.id}, {"$set": {"Theme": theme}}, upsert=True)
data = db_themes.find_one({"guild_id": ctx.guild.id})
if data is data:
await ctx.send("theme has been set to " + theme)
this it the set theme command to set a theme
wondering what I did wrong and how to fix this
@commands.has_permissions(administrator=True)
no need
I use that
from discord import *
from discord.ext.commands import *
the commands
work
just
so I set the theme to humans which should get a gif out of the humans category
in my json file
but it still gets them out of the animals category
when I reload the cog it works good again but everytime I set a theme sometimes it works sometimes it doesn't
Can I take another database and expand on its attributes? How?
sql = "DELETE FROM invites WHERE code = %s"
cursor.execute(sql, invite.code)
code is just a discord invite code.
For some reason this gives me the error:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1
I've tried several things to fix this but have made no real progress
any help is greatly appreciated :D
pretty sure mysql connector expects the second argument to be a tuple
so,
cursor.execute(sql, (invite.code,))
``` (the comma at the end makes it a single element tuple)
also if you're using this for a discord bot, mysql connector is blocking https://discordpy.readthedocs.io/en/latest/faq.html#what-does-blocking-mean
you should be using aiomysql
@burnt turret do u know why my database isn't fully functional though?
and changed it to motor btw
@command()
async def slap(self, ctx: Context, member : Member ):
data = await db_themes.find_one({"guild_id": ctx.guild.id})
if data is None:
slap_gifs = self.gifs["anime"]["slap"]
else:
slap_gifs = self.gifs[data["Theme"]]["slap"]
if "used_slaps" not in dir(self) or not self.used_slaps:
self.used_slaps = copy.copy(slap_gifs)
emb = Embed(title=f" **{ctx.author.name}** slaps **{member.name}**...", color=0x3498db)
bla = random.choice(self.used_slaps)
self.used_slaps.remove(bla)
emb.set_image(url=bla)
the command that retrieves the info from the database
@set.command(name="theme")
@has_permissions(administrator=True)
async def set_theme(self,ctx: Context, theme: str):
if theme not in self.gifs.keys():
await ctx.send("this theme doesn't exist")
if theme in self.gifs.keys():
self.slap_gifs = self.gifs[theme]["slap"]
await db_themes.update_one({"guild_id": ctx.guild.id}, {"$set": {"Theme": theme}}, upsert=True)
await ctx.send("theme has been set to " + theme)
the command that writes to the database
and sets the theme
when I execute the set theme command multiple times it doesn't change the theme of the gives anymore eventho if I print data["Theme"] it says the theme changed
so it changes it the first time you use the command only?
Yeah mainly
what does it send in the channel? that it has changed the theme?
because I don't see anything wrong with that query
Yeah it does
I made a command
That shows what theme the server currently has
And it says
It's the theme I set
so you can be sure that the database is getting updated
the issue must be in the first command then
So im thinking the problem is with the slap command
Getting the new theme gifs from the json file
if "used_slaps" not in dir(self) or not self.used_slaps: i don't understand this condition
aren't they both doing the same thing
okay wait not really
It's like
You have multiple gifs
In the dictionary right
And i want it to not use the same gif twice
Till it went through all gifs
Of that theme
Couldve done this with a for loop too probably but I couldnt figure that one out
what might be wrong with this code ? ```<?php
// Prepare variables for database connection
require_once "connectconfig.php";
// Connect to your database
$conn = new mysqli("$host", "$db_user", "$db_password", "$db_name");
$conn=mysqli_connect("$host", "$db_user", "$db_password", "$db_name");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
//$variableINT = $variable;
//$variableINT = "elomelo";
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$sql = "SELECT * FROM datatable ORDER BY minid DESC LIMIT 1;";
$result = mysqli_query($conn,$sql);
$data = mysqli_fetch_array($result,MYSQLI_NUM);
//$data = mysqli_fetch_row($sql);
//if ($conn->query($sql) == TRUE) {
//echo "Jakis numerek2";
// } else {
// echo "Error: " . $sql . "<br>" . $conn->error;
//}
$conn->close();
// Prepare the SQL statement
echo "temp 1 = ";
echo $data[0];
echo "temp 2 = ";
echo $data[1];
// Execute SQL statement
?>
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in retrivedatauser.php on line 28
temp 1 = temp 2 =
@burnt turret so yeah it basically only works if u reload the cog / restart the bot
sorry, I'm busy right now, maybe someone else can answer
Just threw one out for postgresql
@white steppe This is a Python server. If you want to get help with PHP, you might ask in an off-topic channel or find a server dedicated to PHP.
@set.command(name="theme")
@has_permissions(administrator=True)
async def set_theme(self,ctx: Context, theme: str):
if theme not in self.gifs.keys():
await ctx.send("this theme doesn't exist")
if theme in self.gifs.keys():
await db_themes.update_one({"guild_id": ctx.guild.id}, {"$set": {"Theme": theme}}, upsert=True)
await ctx.send("theme has been set to " + theme)
have this command to set a theme ^^
@command()
async def slap(self, ctx: Context, member : Member ):
data = await db_themes.find_one({"guild_id": ctx.guild.id})
if data is None:
slap_gifs = self.gifs["anime"]["slap"]
else:
slap_gifs = self.gifs[data["Theme"]]["slap"]
to retrieve theme from database and make it get the slaps from that theme from the json file.
but it only sets the gifs to that theme when I reload or in rare occasions it updates it.
How do I fix this
Your query is failing and returning false hence why it gets the Boolean. Have some error handling to see why it fails.
Also that’s php so maybe wrong place to ask
You can format the time with python using strftime method. The format to be inserted needs to be
YYYY-MM-DD HH:MI:SS
Hey,I'm relatively new to coding, and for the last few months I've been experimenting with swift and using firebase for a backend for a messaging application. I've just completed it, and now want to create the same app, but using a sql-database, using mysql as the rdbms. I've been doing some research into it and have begun learning sql. But it also seems that I will need to learn another language e.g php or python to do so.My question is, if i were to learn python is it possible to code my frontend in swift whilst coding the backend in both sql and python?Sorry if its a stupid question.Thanks for your time
Hello, my python bot is saying it cannot connect to my SQL database despite me establishing a connection.
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: 10038 An operation was attempted on something that is not a socke
is that the full error?
Yup
oh what was it?
okay so
yours was the themes not updating problem right?
it does update the themes
but
this part
my theory is that it emptys the current theme's list
first
so it first uses all gifs in there
cause it basically makes a new list with the non uses gifs
used*
oh 
so now I need to find a way to fix that 😂
I really have no idea why it will not connect to the db
maybe ask in pygen or in a help channel #❓|how-to-get-help
were you whom I'd told earlier about how it is super wasteful to make a new connection everytime?
Yes, and I am doing that now
googling the error seems to suggest this happens because of very large queries and such
It is querrying like a very small piece of info
but i'm gonna guess that your program made more connections than the server could handle. unlikely, but that's the only reason i can think of.
ok
So what should I do to fix this?
what i'd said earlier - rewrite your code to use a single connection/single connection pool
That is what I am trying to do
Sure you can. In fact I have a similar setup for some of my mobile apps, for the backend.
I added
client.db = mysql.connector.connect(host='localhost', user='root', password='open1234', database='discordbot', use_unicode=True, charset='utf8', port=3306)
to the beginning of my code
heyo Lufthansa-Pilot, what do you think is causing this?
if you're rewriting it now would also be a good time to move to an async driver like aiomysql I'd say
I am not rewriting just changing it's format
Probably with how they setup their connection
#databases message this is an example, would it be possible for it to error just because of too many connections?
I created a pool and now it says NoneType has no attribute to cursor
Probably not, because too many connections has its own specific error which would say that
But you should avoid making connections like that in a loop anyways.
Why is it returning None?
What is none?
I have
cursor = self.client.db.cursor(buffered=True)
What is db defined as?
client.db = mysql.connector.connect(host='localhost', user='root', password='open1234', database='discordbot', use_unicode=True, charset='utf8', port=3306, pool_name="discord_pool", pool_size=4)
Are you even running that statement. Also I think you should stop troubleshooting that and first switch to a async library because it looks like your making a discord bot. So no point continuing with your current module because either it’ll cause more issues down the line/ or you will have to replace it later on.
It is a bit of a deadline situation, I am going to do it like this and once released I will roll out some updates to switch to the async SQL version.
Hey guys I have a string column in a table set to unique and allow Nulls
I enter informatino like this:
direct_cursor.execute(
f"INSERT INTO Material (mat_nr, etat_nr, bezeichnung, verbrauchsmat, notiz)"
f"VALUES ({mat_nr},{etat_nr},'{bez}','{verb}','{note}')")
Alright then in that case you need to first get a connection from the pool which you are not doing currently. You can read more here with examples https://dev.mysql.com/doc/connector-python/en/connector-python-connection-pooling.html
problem with that is that it doesn't work bc it requires the ' ' quotations around etat_nr (which is the column I'm talking about) since it's string
but if I put them in there and the field is left empty it's gonna literally write "Null" in there because it regards Null as a string
is there a more elegant solution to this than to catch it with an if clause and have two seperate commands one for when it's empty and one for when it contains a string
There's another problem with your statement
there is?
Your statement is vulnerable to SQL injection if any of the variables are gathered from user input
hey, I was wondering if I could make this postgresql query more performant / easier to read:
SELECT level, xp FROM dc_user WHERE (level >= (SELECT level FROM dc_user WHERE user_id = $1) AND xp > (SELECT xp FROM dc_user WHERE user_id = $1)) OR level > (SELECT level FROM dc_user WHERE user_id = $1) ORDER BY level DESC, xp DESC```
In database management systems (DBMS), a prepared statement or parameterized statement is a feature used to execute the same or similar database statements repeatedly with high efficiency. Typically used with SQL statements such as queries or updates, the prepared statement takes the form of a template into which certain constant values are subs...
Where are you seeing null ? @polar osprey
If it’s in the database then null is not an actual value. It just says null to show you there is nothing there.
Yeah that's what I'm talking about. I want to leave the field blank.
is [Null] = "" ?
no it's not I just double checked. Would have surprised me too
look at this screenshot
I want [NULL] to be in there and not ""
because the column is set to unique which would mean I could only have one blank ("") field in there. What I want though is to allow nothing to be there but if something is there it has to be unique
Yeah that’s not null then what you have there
Then In your query you can leave out specifying that column for the insert
but I think zombee has the solution for me
That’s not a solution, it’s how you should be writing your query to avoid security issues
thanks a lot. It's been a while and I totally forgot about this. I even prepared two cursors in my code one for direct and one for prepared.
I'm pretty sure I can solve my problem with this
@proven arrow I'm pretty sure it will solve both
if it doesn't I'll rephrase my question because I don't think you understood what my problem was
but I sincerely thank you for trying to help me
sorry I did a bad job of explaining what I wanted
You need 2 queries. The inner query selects the MAX value of that column, whos result you can use as a filter in the where clause of the outer query.
SELECT * FROM table WHERE delka = (SELECT MAX(delka) FROM table)
How do I correctly delete a collection in mongodb? Im using
db_guilds._id = (f'{guild.id}') # insertion
#some code
db_guilds.remove({_id: ObjectId(f'{guild.id}')})``` but I get the problem that _id isnt defined and i dont know why since I saw a lot of examples on the internet with exactly this soultion. Does anyone know how to fix this?
I need some help with my script: https://stackoverflow.com/q/66499396/15073127
I trying to get saperate strings from a columm in a table. If I output them without anything they are looking like this:
('https://google.com',)
('http://example.com',)
('http://example.com,http://...
tk.Button(Entersubj, text = "Submit", command = lambda: SubjectstoDB(subjectentered,subname)).grid(row = 7, column = 0)
File "C:\Users\vivaa\Desktop\aplicacion\masterappf.py", line 226, in SubjectstoDB
c.execute("SELECT student_id FROM student WHERE student_name = ?",(subname.get(),))
AttributeError: 'str' object has no attribute 'get'``` What does this error mean?
Thanks!
File "C:\Users\vivaa\Desktop\aplicacion\masterappf.py", line 319, in Displaygrades
c.execute("SELECT subject_id FROM StudentSubJunc WHERE student_id = ?",(studid[0],))
sqlite3.OperationalError: no such column: subject_id```
conn.execute("""CREATE TABLE IF NOT EXISTS StudentSubJunc(
student_id INTEGER,
subject_id TEXT,
FOREIGN KEY(student_id) REFERENCES student(student_id)
FOREIGN KEY(subject_id) REFERENCES subjects(subject_id)
)""")```
Huh??????????
hello so I have this method that is suposed to search in my data base if a student exists or doesn't but it always gives me that the student doesn't exist even tho i have it on my table "etudiant " I tried every other way possible but it always give me that the student doesn't exist (am using pyqt5 I enter the string "matricule " that is sepcial for each student from a qlineedit )
if anyone can help me please
you don't need to manually quote the argument (don't put single quotes around the %s)
in your cursor.execute, the second argument needs to be a tuple (i.e it should be like (matricule,) with the comma at the end)
😭😭😭😭😭 oh my god it worked THANK U
what does the () control for sql datatypes? Like varchar(45), smallint(10), etc.
how to install mysql for windows10 64-bit?
For int types it makes no difference to the value stored in database as the size is still the same. The only time it might change something is if you use something like zerofill for the column (but even that is just for how the number is displayed).
As for varchar the variable length is used for enforcing constraints on that column. So if you were storing for example a name, you could set it to varchar(100) so you can’t enter more characters than that.
for anyone using asyncpg in a controller / service / repository project, how are you handling transactions? do you create connection object in service layer and pass it to the repositories?
tried to make @transactional like Spring that sets connection to ContextVar in background (with repositories pulling from this ContextVar), but that was closest I could get to something like Spring where transaction management is handled by a java proxy
In python having repository as pattern seems uncommon to me, typically they are integrated with the models that are integrated with the db/transaction layers as well
For example with sqlalchemy the session, which handles transactions and units of work for all models is either in a context var or passed around
I'm unfamiliar with the tools one would use with async tho
yup that's what I'd do when using an ORM. Trying to experiment without an ORM for a side project, so am using asyncpg and that's why I introduced the repository layer to compensate. Allows me to mock out the SQL calls pretty easy
but end result I got was something like this in repository layer:
async def read_account_by_hashed_api_key(self, transaction: Connection, hashed_api_key: str) -> Optional[Account]:
record = await transaction.fetchrow("select * from account where hashed_api_key = $1", hashed_api_key)
if not record:
return None
return Account(**record)
and this in service layer:
async def get_labeled_account(self, api_key: str) -> Optional[Account]:
hashed_api_key = get_hash(api_key)
async with self.connection_factory.new_connection() as connection:
return await self.account_repository.read_account_by_hashed_api_key(connection, hashed_api_key)
connection_factory is injected into the service, and there is new_connection and new_transaction available. lot of this would probably be simplified with ORM, but trying to see where raw SQL takes me
In that case having a repository manager that combines transactions with the repository would potentially be useful
It might also be useful to use the low level sqlalchemy apis
alright I'll check out those APIs, have only used ORM side of sqlalchemy so will be new experience. thanks for the help
it is possible to use a python function with a php variable in a php script?
While technically it sounds possible, the performance costs is potentially unfathomable, why even do that?
For context, the typical way php is executed is a bad spot for in addition starting python with it
I've been dealing with a specific problem for several hours and haven't found a solution yet. I'm sitting at a script that takes a name and various urls (through an input field, so they are separated from each other by a comma) and puts them into a database. I would like a 2nd column to be created for each url, which can be either online or offline. I want the urls to be separated from each other first with the explode () function and then always to have a single column. I have a Python script that checks whether a url is online or offline.
If I can use the python function in the php script I could avoid doing that
Depending on the setup /requirements I'd either just write everything in python or port the online check to php, most likely by Google, stackoverflow and ensuring my name ain't on the result
The problem is that my entire site is built on php. The uptime checker in php doesn't work as reliably as the one in python. But since I need an accurate checker, it has to be written in python. If you have the time, I would give you a detailed description of my problem
In that case, how about making the python function into a Web service /background process and either just letting it write to the database or using it as an Web api
That would actually be the best solution. I've already tried to split the individual urls somehow. I just haven't understood how I can check every single url whether it's online or offline. I also have no idea how to make this clear in the database for each url. I would then like to pull the individual urls from the database with php and see immediately whether they are online.
I trying to get separate strings from a columm in a table. If I output them without anything they are looking like this:
('https://google.com',)
('http://example.com',)
('http://example.com,http://...
If you want per URL data, don't store multiple URL in a single field, one dB row per URL and you can solve it
And if it absolutely has to be a single field, use json or something else that is absolutely clearly specified
That make sense, but I want to use only one input field for the urls. I cant use js for my project.
So split it up before you store it, in the database
Based on what you present so far im strongly under the impression that you should just use php all the way, if you struggle with comma/whitespace separated strings, the quality of the uptime checks doesn't seem like a overly relevant factor and figuring one language is already going to be enough
🤔 i think RDB would be simpler
a table is easier to visualize imo
- it's likely they'd have already seen stuff like excel spreadsheets
Greetings, I'm working with MSSQL with fronted as React.js. I have several foreign keys inside tables whose data needs to be reflected on the front-end.
Example:
Table 1: Customer_id (PK), Name, Phone, Email, company_id (FK)
Table 2: company_id (PK), Company_Name
Now when I'm fetching data for table 1, I need Company_Name instead of company_id. How can I achieve such a result at the front-end?
Hi, quite a specific problem i have.
Im currently using mongodb for a flask app and was working fine on my local ip.
However, i tried deploying my app on pythonanywhere and pymongo is throwing this error:
pymongo.errors.ServerSelectionTimeoutError: No replica set members found yet, Timeout: 30s, Topology Description:
Anyone come across this before ?
You need to make a join on table 2
Depends exactly what your trying to each. The theory of databases? Or something else? In any case it’s important to teach or get the point across the advantages/disadvantages of a relational db over a non relational, and when to use one or the other.
yes, I figured that a bit later. Tested it and it's working just how I wanted. Thanks!
Then you need to decide what solution is the best for your app i.e. Nosql or Sql db
But relational databases will be a good starting point to introduce to databases
They cover most of the use cases someone will need. They can learn about nosql later on when a relational db is not good enough to solve their problem, if that’s ever the case.
Typically no sql needs specific use cases to shine
For a lot of the classical use cases sql / rmdbs are in a very good spot
I use sqlite3 to make a discord game and I have a guild id and then a user id, how can I select coins from the table players if the guild id is gid and user id is usrid?
you've almost written the query out there haha
cursor.execute("SELECT coins FROM players WHERE guild_id = ? AND user_id = ?", (gid, usrid))
also, look into aiosqlite - sqlite is blocking https://discordpy.readthedocs.io/en/latest/faq.html#what-does-blocking-mean
anyone here using mongosh?
im making a website on django and im having some issues understanding how to implement a database
do i have to create a class for each table on the models.py file? even if im using a cloud based mysql database?
yes, those are typically how you access your data, in addition there is typically migration files there so that a database can be updated to the most recent database schema without trouble
thank you!
when I try to "import mysql.connector" into my python VSC, I get an error that says "unresolved import 'mysql'
Even though I did 'pip install mysql-connector-python'
In my mac terminal prompt
I have python 3.8.3
Does anyone know why?
Okay, so I did what https://dba.stackexchange.com/questions/183672/unable-to-import-mysql-connector-in-a-python-venv-session asked me to do , but I got 'RuntimeError: PyPI's XMLRPC API is currently disabled due to unmanageable load and will be deprecated in the near future'
Did the folks who manage the PyPI endpoint system shut it down for hosting?
the search endpoint has been turned off, if that is what you mean. Use the search on the website.
Im basically trying to setup a mysql database with a python socket.
the 'import' statement is causing the hurdle
it's common to install into one python, and try to run a different one, and it can't be found.
@hoary sun try python -m pip install instead of pip install
I'm doing this on VSCode
you're trying to connect to MySQL with raw sockets????
with python sockets
you mean a MySQL py lib connector or like sockets
MySQL py lib connector
okay coolio
and...
are you using a virtual environment as of right now?
VSCode
okay, but that doesnt answer the question 
i don't know what a virtual environment is lol
send a SS of your folder quickly
click the icon in the top left corner
where it lists all your files and directory
send take a ss then send it
something like this, but with VSC
No idea why i dont have VSC installed but whatever
take a ss of that section
So I'm like at the beginning
**Python Certification Training: https://www.edureka.co/data-science-python-certification-course **
This Edureka video on 'Python Database Connection', you will learn how to establish a connection between Python and MySQL DB and perform CRUD operations on it.
Following are the topics discussed:
Introduction to Python & MySQL
Integrated Connecti...
Start at 6:21
He's running this on Pycharm
👏 Send a SS of your folder directory like i sent
I would ask in #editors-ides how to install packages for VSC
oh ok
do you have a open console in VSC
if you do python -V what does it display
3.8.3
cool
right
firstly, you'll want to open the folder area as a folder with VSC rather than a file
it'll make our lives easier
okay...
Do i need to save my file prior to this?
once the folder is open a new terminal
im doing this on a mac btw
thats fine
kk...
first thing we're going todo is make a virtual environment (you should probably get into the habit of doing this process because it'll stop these issues)
okay
you mean terminal?
Please say terminal from now, as I have an SQL console and a Debug console, so it confuses me.
aight
it did not
what did it say?
okay
i think mac has ls atleast
next
did it list a set of files and folders?
yup
did you see my-venv in there
yes
cool, you now have a virtual environment, this is like a mini interpreter that is completely blank of any 3rd party modules
now we need to activate it
run
source my-venv/bin/active
I got No such file or directory
perfect
This is what it's saying ( as it is)
now you're essentially in the virtual environment
so what ever python command you run now will be run from that environment
now you should be able to install the mysql module again
pip install <insert mysql connector name thing here>
look at your tutorial for that
oh
any1 could help me out with spliting dataframes in Help-gallium?
how do I make r2p2 not defined as None without putting flight before invisibility
Hey @flint osprey!
Uh-oh! It looks like your message got zapped by our spam filter. We currently don't allow .txt attachments, so here are some tips to help you travel safely:
• If you attempted to send a message longer than 2000 characters, try shortening your message to fit within the character limit or use a pasting service (see below)
• If you tried to show someone your code, you can use codeblocks
(run !code-blocks in #bot-commands for more information) or use a pasting service like:
hey guys I need help with sql
is it easier for me to learn SQL or just use like a txt file xd
Im using sqlite3, how I can make a command to set a variable and use it in an event? I want to make it so if the bot goes offline the server owner doesn’t need to set everything again
do you want to delete that entire document or just that array that's associated to the key blocked?
Like , if there multiple ids in there , and i want to pull out one guild id from the array
and delete it;s contant
That array only
Like 0 array contains guild_id , i want to find and delete that 0 array
you'd have to use an update with the $unset operator used for that field
that'll remove that blocked: array[] field
oh wait that's a nested array?
Yeah , array inside array
collection.update(
{ _id: id },
{ $pull: { 'contact.phone': { number: '+1786543589455' } } }
);
Got this on stack
why did you make it an array inside an array?
that would work if it was dicts inside the array
no it would be Array[Object]
where each object is like
{
guild-id:
reason:
}
newuser = {"id": ctx.guild.id, "channel": channel.id,'blocked' : []} # Initial
await info.update_one({"id": ctx.guild.id},{"$addToSet": {"blocked": [guild_id,reason]}})
# On blocking
instead of the list, why aren't you passing a dict to addToSet?
i guess you could work with it being an array too
here instead of using dot notation with a key (the key object) you could....arrayFilters and indices
await info.update_one({"id": ctx.guild.id},{"$addToSet": {"blocked":{f"{guild_id}":reason}}})
eh i didn't mean like that
like this
now the key is arbitrary
Lemme try
a .txt file works, a more adaptive solution is using an SQL db tho
Didn't work
How to remove numbering :thonk:
await info.update_one(
{"id": ctx.guild.id},
{"$addToSet": {
"blocked": {
"guild": f"{guild_id}",
"reason" : reason
}
}})
this is of that form
it's an array with object elements
So it is right ?
yeah
How to pull now ?
let me think one second
collection.update(
{ "id": ctx.guild.id },
{ $pull: { 'blocked.guild': { guild: 'id here' } } }
);
Something like this ?
let me refer the docs one sec
Kk
Im using sqlite3, how I can make a command to set a variable and use it in an event? I want to make it so if the bot goes offline the server owner doesn’t need to set everything again
🤔 what happens if you do {$pull: {'blocked.guild': id here}}?
Uhh idk pull , saw on stack
your query would imply a more nested structure
try it and tell me what happens, i haven't used pull a lot myself either
But if pull fails to work , it gives no error smh , just pass it
i think that happens with every query
it'll just say UPDATE count: 0/DELETE count: 0
what if it has multiple array
Smh i need to find the that guild id and remove it from the blocked array
🤔
pymongo.errors.WriteError: Cannot use the part (guild) of (blocked.guild) to traverse the element ({blocked: [ { guild: "815992843326980106", reason: "test" } ]}), full error: {'index': 0, 'code': 28, 'errmsg': 'Cannot use the part (guild) of (blocked.guild) to traverse the element ({blocked: [ { guild: "815992843326980106", reason: "test" } ]})'}
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1340, in invoke
await ctx.invoked_subcommand.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: WriteError: Cannot use the part (guild) of (blocked.guild) to traverse the element ({blocked: [ { guild: "815992843326980106", reason: "test" } ]}), full error: {'index': 0, 'code': 28, 'errmsg': 'Cannot use the part (guild) of (blocked.guild) to traverse the element ({blocked: [ { guild: "815992843326980106", reason: "test" } ]})'}
await info.update_one({ "id": ctx.guild.id },{"$pull": {'blocked.guild': { "guild" : f'{guild_id}'}}})
Using this
try what I said
this would work if it was more nested
like
blocked: [
{
guild: {guild: id here}
}
]
``` but that's pointless
await info.update_one({ "id": ctx.guild.id },{"$pull": {'blocked.guild': f"{guild_id}"
ik some } and ) are missing
You mean this ?
tell me if it works
are you sure you're storing the guild IDs as strings and not ints?
Yes
see this
right 👌
await info.update_one({ "id": ctx.guild.id },{"$pull": {'blocked.guild': f'{guild_id}'}})
``` Didn't work
was there an error?
pymongo.errors.WriteError: Cannot use the part (guild) of (blocked.guild) to traverse the element ({blocked: [ { guild: "815992843326980106", reason: "test" } ]}), full error: {'index': 0, 'code': 28, 'errmsg': 'Cannot use the part (guild) of (blocked.guild) to traverse the element ({blocked: [ { guild: "815992843326980106", reason: "test" } ]})'}
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1340, in invoke
await ctx.invoked_subcommand.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: WriteError: Cannot use the part (guild) of (blocked.guild) to traverse the element ({blocked: [ { guild: "815992843326980106", reason: "test" } ]}), full error: {'index': 0, 'code': 28, 'errmsg': 'Cannot use the part (guild) of (blocked.guild) to traverse the element ({blocked: [ { guild: "815992843326980106", reason: "test" } ]})'}
^
that error looks the same as the last one. are you sure you reloaded the cog etc?
Yes
but we aren't using the part guild of blocked.guild
we're using part guild of blocked
let me see if i can model the query some other way
Kk , ping me
@torn sphinx try
await info.update_one({ "id": ctx.guild.id }, {"$pull": {'blocked.guild': {"$eq": f"{guild_id}"}}})
kk wait
Guess what
it worked
kek
raise WriteError(error.get("errmsg"), error.get("code"), error)
pymongo.errors.WriteError: unknown top level operator: $eq, full error: {'index': 0, 'code': 2, 'errmsg': 'unknown top level operator: $eq'}
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1340, in invoke
await ctx.invoked_subcommand.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: WriteError: unknown top level operator: $eq, full error: {'index': 0, 'code': 2, 'errmsg': 'unknown top level operator: $eq'}
Another error
😭
I wonder what eq is
equal to
ohh kk
Any idea why my SQLite setup installation got stuck in 0%?
hi
do you know how to save emojis in a db? I can save them but when I try a select mysql (mariadb) returns everything and does not care of the exact emoji type
How are you trying to store the emojis? Also what collations are you using for the database and table?
fixed it by switching from utf8 to utf8mb4_bin which recognizes different emojis
Yeah mb4 should do it
Anyone online have experience intergrating MongoEngine with Discord.py?
Hi this is my db.py file for SQLAlchemy (not flask related)
engine = create_engine('mysql://root@localhost:3306/hyplex')
Base = declarative_base()
Session = sessionmaker(bind=engine, autocommit=False, autoflush=False)
db_session = Session()
meta = MetaData()
class DB:
@staticmethod
def create_all():
Base.metadata.create_all(engine)
def __init__(self):
self.session = db_session
All my models share the same Base which I import from this file
When I try to do DB.create_all(), no table is created on the db
hi
I'm trying to use mysql database, using aiomysql (built-in pymysql). The problem is that it rejects the connection
First I tried on my device, then on a hosting and it keeps rejecting
When I try with 127.0.0.1, it is the same even with the server activated.
async def pool_sql():
return await aiomysql.create_pool(host='127.0.0.1', port='3306',
user='root', password='-----',
db='mysql', autocommit=True
)
Anyone knows how to do a fetch on postgres where if you have a column with datetime objects, you only fetch the rows where current time and the time there has 12 hours difference
what
The port needs to be a int value not a string like you have
so the whole problem is because the port must be int and not str? lol.
So I'm pretty new to databases. I want to use a sqlite database for my discord bot, but i'm not sure where to start. I've looked at a few tutorials, but they just type out the code without explaining what it does. I need to know how to do it myself. What should I do to get started?
(ping me)
Well the main/ or most important thing is to be aware of SQL which is the query language that allows you to interact with and manipulate your database. You should have or try to get some basic understanding of this first. Then when it comes to use the database with python it’s pretty straightforward, because you’ll just be writing sql queries through the library function.
oh wait sql has its own language?
I will learn that first then let you guys know if i need anything else. Thanks!
Yes but it’s quite high level and readable, so it’s easy to pick up. There are some links pinned in this channel or you can view the Khan academy link for intro https://www.khanacademy.org/computing/computer-programming/sql
epic!
Im trying to mark the user disabled and store the date they leave so their information can be deleted in the future if they don't return in a set time (15 days) The issue im having is its not setting in my database and im not sure why.
database function's code:
db = sqlite3.connect('main.sqlite')
c = db.cursor()
sql = ("UPDATE economy_profiles SET active = ? and removal = ? WHERE guild_id = ? and user_id = ?")
val = (status, removal, guild, member)
c.execute(sql, val)
db.commit()
c.close()
db.close()
event code:
its storing in the database with the correct guild and user, but its changing 'True' to 0 instead of 'False' and not inserting FinalTime
does anyone know how to solve this?
DB = sqlite3.connect('D:\\Python\\Scripts\\DiscordBots\\IceBot\\UsersIceEmpire.db')
c = DB.cursor()
c.execute(f"SELECT WarnsInfo FROM UsersDiscordIceEmpire WHERE ID = '{member.id}';")
UserWarnsInfo = c.fetchone()
print(UserWarnsInfo) #This returns -> ('None',)
UpdatedUserWarnsInfo = UserWarnsInfo + (reason,)
print(UpdatedUserWarnsInfo) #This returns -> ('None', 'Prueba')
c.execute(f"UPDATE UsersDiscordIceEmpire SET WarnsInfo = '{UpdatedUserWarnsInfo}' WHERE ID = '{member.id}';")
DB.commit()
DB.close()
the error
Ignoring exception in command warn:
Traceback (most recent call last):
File "C:\Users\Jose Manuel\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "d:\Python\Scripts\DiscordBots\IceBot\ModCmds.py", line 43, in warn
c.execute(f"UPDATE UsersDiscordIceEmpire SET WarnsInfo = '{UpdatedUserWarnsInfo}' WHERE ID = '{member.id}';")
sqlite3.OperationalError: near "None": syntax error
c.execute("UPDATE UsersDiscordIceEmpire SET WarnsInfo = ? WHERE ID = ?", (UpdatedUserWarnsInfo, member.id,))
try that? from what i've heard its not recommended to use f-strings in queries.
.
i think the problem isn't there but thx
how come your {} are in single quotes?
and why do you have ; at the end? just out of curiousity
because i though they needed to be like that
this as far as i know is the preferred method
yeah i know but the other is easiest 😆
i thought so too until it didn't work anymore and i came asking 😂
who can learn me every single step in effective data storage (python & csv files) 😋
hi, I would like to know if the following code is fine and will not have problems when it is used
async def get_guild_prefix(self):
pool = await pool_sql_create()
async with pool.acquire() as section:
async with section.cursor() as temp:
prefix = await temp.execute('SELECT prefix FROM prefix WHERE gulldid = %s', [self])
if prefix == 0:
await temp.execute('INSERT INTO prefix(guildid, prefix) VALUES(%s, %s)', [self, '?'])
temp.close()
pool.close()
await pool.wait_closed()
return '?'
temp.close()
pool.close()
await pool.wait_closed()
prefix = await temp.fetchone()
return prefix[0]
My question is because waiting for pool.wait_closed() I would delay, idk.
@brave bridge From the other day when you asked. The SQL and relational theory book by Date, should cover it in good amount of detail that’s needed. Here is a pdf link: http://index-of.co.uk/Database/SQL Database/SQL and Relational Theory How to Write Accurate SQL Code(True pdf)by C.J.Date(pradyutvam2)[o'reily].pdf
Or take a look at codds publication who kind of first bought it around https://dl.acm.org/doi/10.1145/362384.362685
Ive not read those but I asked some dba’s and that’s what they gave. Although they did say it’s mostly overkill, and not something they consider on daily basis.
Thanks a lot, I'll check those out. But please don't post pirated links here 👀
help
mah fam
What do you mean?
You can start with some introduction into SQL
ok
@proven arrow help me
my question is whether it is convenient to close the pool after using it, pool.close() and pool.wait_closed
Knowing that it will be used many times
I don't know how it works, I am not a newbie but also a professional in sql (no).
@proven arrow So I don't close the pool? From what I see it acquires an open pool, or thread to perform the operations. What I have in mind is not close when collecting the guild prefix, but if at the end of a command
To keep it simple, opening database connections can be expensive. So the purpose of a pool is to minimise the cost of opening these connections. A pool keeps a collection of database connections. When your app needs a connection it takes one from the pool and returns it back to the pool, when it’s done using it. This way you avoid the connect/disconnect complexity.
So if you are going to close the pool each time, you defeat the purpose of the pool in the first place.
Wait, I am understanding.
What database should I get started with?
@proven arrow I was able to understand, but then taking connections from the pool would you be using a thread?
No, the library will have an acquire and release method to manage the connections, or use a context manager. And I’m going sleep now so don’t expect a response.
I started learning SQL, from what I understand on this tutorial SQL is like a database over network am I correct? https://realpython.com/python-sql-libraries/
Thanks for the help!
What sql python lib is the easiest?
It's not really the matter of a library. There are different database management systems (RDBMS) -- SQLite, Postgres, MySQL and some others; and Python simply has libraries to interact with them.
The most simple and barebones relational database is SQLite.
Hello, i'm trying to print with prettytable from asyncpg and i'm trying to use from_db_cursor(), the example said i need to pass in a cursor but it throws an error. This is what i have now ```py
pg_con = await asyncpg.connect(database="x", user="x", password="x")
async with pg_con.transaction():
cursor = pg_con.cursor("SELECT * FROM eco")
table = from_db_cursor(cursor)
print(table)
and the traceback
Traceback (most recent call last):
File "C:\Users\SMKN3-01\Desktop\jeyy's\codes\python\tests.py", line 27, in <module>
asyncio.get_event_loop().run_until_complete(main())
File "C:\Users\SMKN3-01\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:\Users\SMKN3-01\Desktop\jeyy's\codes\python\tests.py", line 13, in main
table = from_db_cursor(cursor)
File "C:\Users\SMKN3-01\AppData\Local\Programs\Python\Python38-32\lib\site-packages\prettytable\prettytable.py", line 1825, in from_db_cursor
if cursor.description:
AttributeError: 'CursorFactory' object has no attribute 'description'
Thx, I am currently learning SQL, I am very confused, can you help me out a bit?
Like for example
Where am I supposed to run SELECT * from tanle name and other commands?
Is this a good tutorial? https://youtu.be/p3qvj9hO_Bo
In this video we will cover everything you need to know about SQL in only 60 minutes. We will cover what SQL is, why SQL is important, what SQL is used for, the syntax of SQL, and multiple examples of SQL. SQL is the standard language for interacting with and manipulating data in a relational database system, and is one of the most important con...
Thanks
Is this a good tutorial? https://youtu.be/p3qvj9hO_Bo
In this video we will cover everything you need to know about SQL in only 60 minutes. We will cover what SQL is, why SQL is important, what SQL is used for, the syntax of SQL, and multiple examples of SQL. SQL is the standard language for interacting with and manipulating data in a relational database system, and is one of the most important con...
@commands.Cog.listener()
async def on_member_join(member):
db = sqlite3.connect("main.sqlite")
cursor = db.cursor()
cursor.execute(f"SELECT channel_id FROM main WHERE guild_id = {member.guild.id}")
result = cursor.fetchone()
if result is None:
return
else:
cursor.execute(f"SELECT msg FROM main WHERE guild_id = {member.guild.id}")
result1 = cursor.fetchone()
members = len(list(member.guild.members))
mention = member.mention
user = member.name
guild = member.guild
embed = discord.Embed(title="ChizLanks", description=str(result1[0]).format(members=members, mention=mention, guild=guild))
channel = client.get_channel(id=int(result[0]))
await channel.send(embed=embed)
This should get the Channel's ID and the msg's content from the DB and then send it to a channel
it's not working tho without any errors
any ideas why?
how are you testing this?
you'll need the members intents enabled for this event to work - there's a message pinned in #discord-bots explaining intents
Well I did enable members intents
still not working
and also what do you mean by how am I testing that?
how are you saying that it is not working?
what goes wrong/which part doesn't work as expected
Well everything is fine as it comes to errors, the event itself was not working, Like no message was showing up etc. etc. It seems I needed to add a self that's why, now it's working fine.
Anyways thank u!
Yep, all* methods in a class take self as the first parameter
@solid tundra Side note: you shouldn't use f-strings or other built-in interpolation to format SQL queries. Here it likely won't cause any issues, but it's very easy to let SQL injection into your system. If you do
cursor.execute(f"SELECT user_id FROM users WHERE username = '{username}'")
and a user named '; DROP TABLE users; -- joins your server, you're in trouble. You should use the database adapter's interpolation:
cursor.execute(f"SELECT user_id FROM users WHERE username = ?", (username,))
I see, so f-strings are bad for SQL?
#databases message this message explains why
For any interpolation of external things, such as query parameters when constructing a URL (which is often overlooked). The two main issues are:
- You can get the interpolation wrong (e.g. you need to quote and escape things correctly)
- You can introduce a risk of injection (where a user inputs malicious data)
@solid tundra ^ and the above link as well
You could argue that you're only including values with a few select possibilities (e.g. just ints). But you can overlook a possibility of it not being true; and Python is dynamically typed, so there isn't really any guarantee that there isn't a bug that will put a string in there.
No, SQL is the programming language to query databases.
It depends on the database you are or will be using. What do you want to learn? What operating system are you using? What database (MySQL, PostgreSQL, SQLite) is mentioned in the course or tutorial?
In what case would you want to use hidden columns in an sqlite db? @ me when responding please.
Mostly to avoid breaking your application when you want to make changes to a table. They don’t appear in select * queries, and it’s not necessary to specify the invisible column name when inserting. So if your code currently relies on the above then the invisible column won’t be seen by the existing code, and will save you any errors. However the new column can still be referenced by new applications/code.
Help pls
DB = sqlite3.connect('D:\\Python\\Scripts\\DiscordBots\\IceBot\\UsersIceEmpire.db')
c = DB.cursor()
c.execute("SELECT XP FROM UsersDiscordIceEmpire WHERE ID = ?", (member.id,))
UserXP = c.fetchone()
this is the error returned
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Jose Manuel\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "d:\Python\Scripts\DiscordBots\IceBot\main.py", line 173, in ON_MESSAGE
UserXp = float(UserXP[0])
TypeError: 'NoneType' object is not subscriptable
I've tried to print the value returned from the DB and returns None, don't know why.
If it returns none then there was no match found in the database with those values
yeah i know that, but is the code correct?
Hello! How I can store the guild_limit as int?
You need to handle the case where no value is returned from the database, to avoid an error.
Add an if check or try catch
What is that?
yes but first I want the code to work
I told you why it doesn’t work, and how to handle it. What more do you want?
how to solve it, because it should work, I have opened the DB and the values are there
and it should work
but doesn't
and why do you want me to do an error handling system is the DB simply doesn't works
Database works for everyone else?
what do you mean?
cross check your query and make sure it is right (are you passing in the right data type? etc)
there's no reason for that query to return None otherwise if you're so sure there are matching rows in the database🤷♂️
how can i make the member.id query to be an str value? because the problem could me there, but the value of the variable is int? how could i change it?
DB = sqlite3.connect('D:\\Python\\Scripts\\DiscordBots\\IceBot\\UsersIceEmpire.db')
c = DB.cursor()
c.execute("SELECT XP FROM UsersDiscordIceEmpire WHERE ID = ?", (member.id,))
UserXP = c.fetchone()
your id column is set to a text type?
to pass it in as a string, just cast member.id to str with the str() function - str(member.id)
like this?
DB = sqlite3.connect('D:\\Python\\Scripts\\DiscordBots\\IceBot\\UsersIceEmpire.db')
c = DB.cursor()
c.execute("SELECT XP FROM UsersDiscordIceEmpire WHERE ID = ?", (str(member.id),))
UserXP = c.fetchone()
you can try it and see
ok
ok know works perfectly, last question, is any why I can add into a field of the DB text, I mean to the existing text
eg:
DB = sqlite3.connect('D:\\Python\\Scripts\\DiscordBots\\IceBot\\UsersIceEmpire.db')
c = DB.cursor()
c.execute("SELECT Text FROM UsersDiscordIceEmpire WHERE ID = ?", (str(member.id),))
UserText = c.fetchone()
#Let's say that printing that returned a tuple with the text ('text1')
Then could i do something to add the new text ('text2'), to the tuple with the texts, then the tuple to be ('text1', 'text2')
because i am trying that in one way but returns error
you can only do that if your table has more columns already
but i wan t to add the text to the same column
like if it was
UserTexts
|text1|
|text1, text2|
|text1|
|text1, text2, text3|
i don't really understand, maybe someone else can answer
I mean like if the space in the column was a list with diferent texts
that's a single column?
You can use the concat operators || to add strings together. https://www.sqlitetutorial.net/sqlite-string-functions/sqlite-concat/
there is a number of libraries to generate those - a typical suggestion for applications is not to ever generate passwords
this returns error
c.execute(f"SELECT WarnsInfo || {reason} FROM UsersDiscordIceEmpire WHERE ID = ?", (str(member.id,)))
isn't correct, right?
Which DB do you use?
Well it looks like SQLite to me
Okay, I think that using {reason} is not secure solution but maybe '{reason}' should work
I mean if the reason was some reason' FROM UsersDiscordIceEmpire; DROP TABLE UsersDiscordIceEmpire;-- then it still wouldn't be secure
Yep, but it should work 🙂
I don't see how reason variable is created so... It can be or can't be a security issue
Why are you using concat in select? What are you trying to do?