#databases
1 messages Ā· Page 128 of 1
Are you asking about the functions you'd use to add data to the db?
no, I want to be able to hash the passwords
Can anyone guide me the right direction.
Whats the best way to structure a database where I have several tables like so: "desk_phones", "printers", "desktops", "Fax" and have a table that called "devices" that accepts any of those tables.
Would it be called many to many relationships, or is that not how it works? I can't seem to wrap my head around the documents for postgesql or sqlalchemy.
In python when you have a an object you can nest a different class object reference in it. I know it doesn't likely translate or work like that with databases, but I could sure use some help understand how something like that is done.
seems like a bad datastructure
you would have one table called devices
and another table called devicetypes or what ever
where you can specify the type and its corresponding id there rationalising the data
you then in your devices table would have a device_id column which is a foreign key to devicetypes acting as a linking table
making a one to many relationship and keeping the data normalised with minimum duplication
I think I understand.
I'll have to keep looking into this. It's rather difficult to visualize the structure.
Thank you.
if the s13 will equal the modoul is will not be able to acsess the database
I need some help with my JSON
I have a very large data set, but I need it to make it fit my structure
I want it to look like this:
{"intents": [
{"tag": "greeting",
"patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up"],
"responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?"],
"context_set": ""
},```
but the data set looks like this
the file was too big to upload
what can I do so I can make it fit the first structure?
I really only what the question and answer part of the json
i am sure we have to add variable (x) to the database every time the user acces it
we have to convert all the variables to a string
in a mongodb database how do I find something when something else is something
like find xp when user = user
I am used to sqlite so idk how
collection.find({"user":<user>}) will return that user's document ( i.e dictionary with all of that user's data)
Then you can just use the keys to access the data
ok
thank you
@burnt turret I have this ping_cmm = {"_id":author_id} collectionfound = collection.find(ping_cmm) print(collectionfound) but it returns an object
<pymongo.cursor.Cursor object at 0x7f088e6a8f10>
find returns a cursor
You can loop over it with a for loop
While looping, each item will be a dict
In my example I used find_one, because
- it directly returns the dict
- I assume one user will have only one dict in your collection
Find_one will return the first document which matches the filter case
ok does that still return a cursor if its directly a dict
You use find when you're expecting more values
Also, use motor
Pymongo is blocking
motor?
One sec
is that like async pymongo
Yep
ok
I made a gist explaining how to migrate from pymongo to motor -
https://gist.github.com/anand2312/840aeb3e98c3d7dbb3db8b757c1a7ace
Except for adding awaits
You'd just need to import it and change your MongoClient to motor's equivalent
Then add awaits before queries
ok yeah I only have that so far so it will be easy
so import motor
@burnt turret what is motors eqivilent
I looked at the docs and tried motorclient but it didnt work
ok thanks
Oh one more thing, while using motor you'll await basically every query except find (you'll await find_one though)
This is because find here also returns a cursor, and it doesn't do an IO operation (doesn't ask the db for data) until you try accessing values from the cursor
So there are two ways to use it really;
Option 1
data = collection.find() #note the lack of await
async for doc in data:
# note the async for
do you put async in getting the db
async def mongotest(ctx):
mongo_url = "mongodb+srv://fireballbot:discord@fireball-discord-bot.5qsqj.mongodb.net/discorddatabase?retryWrites=true&w=majority"
cluster = motor.motor_asyncio.AsyncIOMotorClient(mongo_url)
db = cluster["discorddatabase"]
collection = db["main"]
author_id = ctx.author.id
guild_id = ctx.guild.id
ping_cmm = {"_id":author_id}
collectionfound = await collection.find_one(ping_cmm)
print(collectionfound)
await collection.update_one({"_id":author_id},{"$set":{"Level":1}})```
Option 2:
Using motor's to_list method
You will need to await this though
data = await collection.find().to_list(length=10)
it returns {'_id': 449756487510654988, 'GuildID': 742942218687479828, 'XP': 0, 'Level': 1}
Isn't that what you expect?
yes
but I have one question how do I get the part inside one of them
like the 0 in xp
or the 1 in level
That stuff (getting DB and collections) are handled lazily by mongo, you don't need to await it
By lazily I mean it actually gets/makes the collection only once you do some operation on it
ok
It is a dictionary, so you'd use the key to get it, in your case
collectionfound["XP"]
You could also use
!d dict.get
get(key[, default])```
Return the value for *key* if *key* is in the dictionary, else *default*. If *default* is not given, it defaults to `None`, so that this method never raises a [`KeyError`](exceptions.html#KeyError "KeyError").
ok thank you
how 1000 calls per 60 seconds can or will affect my postgresql database?
It won't run into problems, if that's what you're asking
Databases can handle much more
yeah thats what I meant, thanks!
by asking it?
so I just switched my bot over to mongoDB and now everything is rly slow
anyone know how I can speed it up?
I'm using motor.motor_asyncio.AsyncIOMotorClient
So I want to learn how to make a user wishlist for products. Like how to design table. Which I use relationship?
Hallo, i have asked question ^^
right, i only know postgres. though
Yeah but I think is the same for all
Any recommended books for database design?
I don't think it is
Can I use mongodb functions without worrying about them blocking?
how do I get the rank of someone in a collection in mongodb
Theyre asking about type of relationship, like one-to-one, many-to-one etc
Those concepts are similar across databases if I understand correctly
Pymongo is blocking, if you want it to be async use motor
#databases message @torn sphinx read messages from here, I explain the basics of switching from pymongo to motor
How is your data structured?
So you want the person with highest XP to be rank 1?
yes
One sec, lemme refer something
Off the top of my mind you could pass {"$sort":{"XP":1}} into find()
ok
ok
And then do you remember how to get list from cursor of find()?
no I never actually did that I jsut changed it to findone
I'd told you two methods to get the values from a result of find
ill go look one sec
Right, you don't want to sort in a find-one you want to use find
#databases message this with length = 1
Will give you [{actual data of user with highest XP}]
ok
It'll be in a list, so you'd have to do result[0] to get the dict inside
I will do 5 but also
Yeah that'll give you 5 highest
how do I get the rank of the specific user like even if they are something super high like 100
š¤ lemme think that through
I can't think of a way to make the database do the work in that case š¤ the easy way seems like sorting by descending order and then looping through all of them to find which number the specific user is at
ok
I am getting an error on the top 5 rank
rankfound = await collection.find({"$sort":{"XP":1}}).to_list(length=5)
pymongo.errors.OperationFailure: unknown top level operator: $sort, full error: {'operationTime': Timestamp(1610739908, 1), 'ok': 0.0, 'errmsg': 'unknown top level operator: $sort', 'code': 2, 'codeName': 'BadValue', '$clusterTime': {'clusterTime': Timestamp(1610739908, 1), 'signature': {'hash': b'\xfb\xd9\x9bf\x80Q\x8f\x07Xq\x86M\xa0\xef\x0b~;4\x97\xd5', 'keyId': 6916547034908459011}}}
You need to pass two dicts
The first dict is what you'd call a filter
It filters through the data to match
In your case you dont really want to match anything
So, pass {}
Ty, does motor still access my db I made in mongo?
And then the sorting doctor second
where should I put it in there
Pymongo, motor are all just different methods to access the same underlying database
rankfound = await collection.find({"$sort":{}{"XP":1}}).to_list(length=5)
tysm
rankfound = await collection.find({}, {"$sort":{"XP":1}}).to_list(length=5)
oh ok
As you can see we passed in two dicts, first filter -empty dict because we want to get everything, second is telling how to sort
im still getting an error I think its different though
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1471, in _retryable_read
return func(session, server, sock_info, slave_ok)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1360, in _cmd
return server.run_operation_with_response(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pymongo/server.py", line 135, in run_operation_with_response
_check_command_response(first, sock_info.max_wire_version)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pymongo/helpers.py", line 160, in _check_command_response
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: Unsupported projection option: $sort: { XP: 1 }, full error: {'operationTime': Timestamp(1610740138, 1), 'ok': 0.0, 'errmsg': 'Unsupported projection option: $sort: { XP: 1 }', 'code': 2, 'codeName': 'BadValue', '$clusterTime': {'clusterTime': Timestamp(1610740138, 1), 'signature': {'hash': b'\xf1\x17\xfe\xd1)\xc9\\\xb9\x97-%\xf861\xff\xf7k\xb6\xc3\xc2', 'keyId': 6916547034908459011}}}```
rankfound = await collection.find({}, {"$sort":{"XP":1}}).to_list(length=5)
I'm on mobile reading that is difficult
Also we want to sort descending, -1 but that's not the issue
I think this is the main part
Unsupported projection option: $sort: { XP: 1 }, full error: {'operationTime': Timestamp(1610740138, 1),
Lemme think one sec
What's the difference between motor and pymongo besides motor being asynchronous
Modify the query actually, to do
cursor = collection.find()
cursor.sort("XP":-1)
data = await cursor.to_list(length=5) @sinful condor
That is the primary difference, what answer are you looking for really?
Syntax wise they're basically the same
Just adding awaits before queries
Probably should have asked if there's any difference at all
Internally motor is just running pymongo functions on greenlets
@burnt turret TypeError: if no direction is specified, key_or_list must be an instance of list
Is this the correct syntaxpy cluster = motor.motor_tornado.MotorClient("mongodb+srv://<name>:<password>@<db>.pq3r5.mongodb.net/<dbname>?retryWrites=true&w=majority")
on this line cursor.sort({"XP":-1}) do I need to add a second one
instead of -1 try pymongo.DESCENDING
in there
you'd have to import pymongo for that constant though oof
like "XP":pymongo.Descending
Yep
ok
still invalid syntax cursor.sort("XP":pymongo.DESCENDING)
That's great!
so how should I do the single user rank again
is there a way to save rank as something like there is in sql
How would you save in SQL?
one sec
this was my rank in sql select userguild, xp, level, (select 1 + count(*) from level as p2 where p2.xp > p1.xp) as user_rank from level p1 where userguild=? order by xp desc
Someone can help?
Right SQL makes those queries so much simpler
There's no easy way to do it with mongo, you'd have to mess around with some aggregation pipelines I tjink
yeah it was even simpler before I had to go into an earlier version
it was rank as userrank
instead of that complicated thing
ok ill try to work it out later
@burnt turret thanks for helping me my aiosqlite database broke when my bot got too big for sqlite a couple of days ago
You would have a many to many relation.
A user can have many products. A product can belong to many users.
users
- id (PK)
- email
products
- id (PK)
- name
user_products
- id (PK)
- user_id (FK references users table)
- product_id (FK references products table)
- UNIQUE (user_id, product_id)
You dont need the surrogate key id in the user_products table but i like to have it.
Yeah I understand. Ok thanks you, let me try this hold on
Ok done this now how to make query?
For like say I want to get all wishlist items for user equal something?
If you want data from both sides.
SELECT u.email, p.name FROM user_products up
JOIN users u ON up.user_id = u.id
JOIN products p ON up.product_id = p.id
WHERE up.user_id = some_id;
If you just want details of the products then you dont need to join on the users table.

Is there some library to support asnyc postgres ORM?
I know that there's asyncpg, but that doesn't have ORM
I've only just started playing with it, but Tortoise ORM is essentially an async implementation of the Django ORM, and it uses whatever async engine you want under the hood (e.g. asyncpg): https://tortoise-orm.readthedocs.io/en/latest/
I wouldn't classify it as "production ready" though
yeah, I was just looking through the FAQ of asyncpg and looks like SQLAlchemy had an update to 1.4 which added the possibility of the use of an async engine, last time I checked this wasn't there so I didn't know, but thanks for the suggestion
In mongodb does anyone know a way where I can get the get the rank of a something in a whole collection based on one thing
Ping me if you know how
Building my database is taking way too long with the method I'm using.
I'm building a database with comment and replies (reddit dataset), problem is, the dataset is just comments. To get around this, I iterate through the dataset and add each comment with the relevant data to the database. Since there is a commentID and authorID, I can match those to get Comment-Reply data (to train a chatbot with). What I'm having an issue with is finding the parent comment, I use the mongodb "find_one" method. I do this each iteration that I add a comment to check if the comment I'm adding has a parent comment that it's replying to. As I add more and more, I believe the "find_one" method iterates through the entire database to find the match, as I add more and more comments this takes longer and longer each iteration and it's just not working.. Is there any way I can speed this up??
Wow. I removed the function that finds the parent comment just to see the time difference.
With Parent Comment Function
10k Comments | 30 Seconds
Without Parent Comment Function
330k Comments | 20 Seconds
I think I left a comment about your question the other day
Hmm maybe not. The other question was about sqlite
Yeah
I switched to MongoDB and figured out how to use that to see if it was any better
I don't know anything about mongodb or no-sql in general, sorry
I ended up going to sleep, and just got back to work and did some testing to further figure out the issue, so I posted a more I guess "accurately" detailed explanation
In a relational database the solution is to index the column so the lookups are fast
Index the column so lookups are fast?
Yeah I mentioned it the other day
yeah just went back to look at what you said
My sqlite3 table was constructed like so
Cursor.execute("CREATE TABLE IF NOT EXISTS RedditComments(ParentID TEXT PRIMARY KEY, Parent TEXT, CommentID TEXT, Comment TEXT, Subreddit TEXT, Score INT, Unix INT)")```
And the lookup function was like this
```py
def FindParent(ParentID):
try:
Cursor.execute(f"SELECT Comment FROM RedditComments WHERE CommentID = '{ParentID}' LIMIT 1")
Result = Cursor.fetchone()
if Result != None:
return Result[0]
else: return False
except Sql.Error as Error:
print("FindParent", Error)
return False
It should be significantly faster if the ID column has an index or is the primary key. If it already was and was still slow, then I'm not sure how to speed it up.
Should the CommentID be the primary key, not the ParentID ?
Is there a specific reason you make the parent the primary key?
No, there wasn't a specific reason, I don't believe it matters? I just had it organized that way
Yes it absolutely matters
Oh
I'm very new to sqlite and databases in general, this is kinda my introduction so I'm not too certain with what I'm doing
Not only from a performance perspective but also from a database design perspective
Oh
The primary key is meant to signify the column that uniquely identifies the row
The PK column automatically gets a unique index created for it.
Oh, and because I'm finding where the commentID = "whatever", it'll be faster if the CommentID is the PK column?
Yes
Ohhhh, okay, that makes sense, though I'm still confused about table IDs, I don't have one defined in my table, unless that would be the PK column..?
But also consider that many comments can have the same parent, so using a parent as the unique identifier is wrong.
Ohh, that makes a lot more sense
The PK is what is commonly referred to as the table's ID
Oh okay, but since I'm iterating through the table every iteration I add a comment, it'll still get exponentially slower even if I do that, right?
Even if I have a properly setup PK?
Why would you iterate through the table every time a comment is added?
To see if that comment I'm adding has a parent comment, so to see if that comment is replying to another one, I then pair them
The way I'm doing this is just structurally flawed I believe, but I don't know how else I could do it
But if it has a parent ID, don't you already know it has a parent comment?
Hmm... I guess if it has a parentID, I could then find the matching commentIDs in the dataset itself rather then trying to match it in the database each iteration..?
Does the dataset provide the parent ID for you already?
It's a screenshot so it's hard for me to tell if it has the parent ID
It has parentID
and "id" which is the ID of the comment
RowCount += 1
Row = Json.loads(Row)
CommentID = Row['id']
Comment = Functions.FormatData(Row["body"])
ParentID = Row["parent_id"].split("_")[1]
#Parent = Functions.FindParent(ParentID)
Parent = "None"
Subreddit = Row["subreddit"]
Score = Row["score"]
Unix = Row["created_utc"]
Time = Functions.UnixConvert(Unix)```
That's the information I'm taking
Insert all the comments into your database. Once they're all inserted you can quickly get the parent comment for any given comment just using the WHERE clause like you already were.
So, insert all of them as I was, make sure the CommentID is the PK, after it's finished inserting, loop through the database(?) and if a comment has a parent, remove the parent and add the parents comment body to the parent portion of the current comment to form a comment-reply dataset?????
I thiiiinkkk???
Yes, you could do that
However, from a pure db-perspective, that is bad design
Since you're de-normalising the data by adding the parent comment to the child comment.
I don't know what else you need the database for
Ohh I see what you mean.. though if I'm making a chatbot with this, wouldn't I need to lookup the parent comment each time? I'm not at the portion of making a chatbot and I'm still teaching myself about it, but I think having straight up comment-reply structure would make that a lot easier to train?
But it may be better to keep that merged text content separate from the original tables
I don't know anything about training, so I can't comment on what kind of data structure would be best.
The problem I see with merging the content into the table is that the rest of data becomes inaccurate.
Like the author, date, etc
Since now the text is that of multiple comments
You'd somehow need to reconcile that
Oh
Yeah I'd need to have parent score and time if I were to do something like that huh..
hmm
If you want training advice for this consider asking about it in #data-science-and-ml
Alright, thanks
š
If I'm updating the table's row to set the ChildComment's ParentComment, ParentScore, and ParentUnix to the ParentComment's details... the ParentID is the same as the CommentID, which is how I match them... but if I want to update the ChildComment to add the ParentComment's details.. how would I know which I'm using if I'm searching with the term "WHERE CommentID = {ParentID}" because, doesn't that apply to both the ChildComment and the ParentCommen..?
And if I'm doing this, how would I remove the ParentComment and keep the ChildComment..?
That's hellishly confusing but I hope I got my question across lol
@pure cypress
Sorry I didn't quite follow.
I'm off for the night anyway so hopefully someone else can help you out
Otherwise I'll try to respond tomorrow
Yeah it's alright, cya
what is difference between = and IS. In this query```sql
SELECT
company,
st,
st_copy
FROM meat_poultry_egg_inspect
WHERE st IS NULL -- if used = instead of IS this returns nothing (no error though)
ORDER BY st;
you can't use = to do a NULL comparison
more detailed answer
thanks
Elaborate
so i am working on a leveling bot for discord.
You have multiple databases? Or one database with multiple collections?
right
there alot of users so i dont want to manually reset all of there xps
that takes tons of times
Do all the documents have the same structure?
yes
what do u mean by the same structure
like are they in one database if so
yes,
Same key value pairs in each user's dict
So you'll use collection.update_many
for example bot has 20xp and dirk has 30xp
As our want to change everyone's XP, you want the query to match everyone
yes,
so
collection.update_many({}, {"$set":{"XP": 0}})
ok
This sets everyone's XP to zero
But that is assuming you have a key called XP
What do you mean?
the roles they got assigned when they leveled up
Obviously those won't be removed when you update the db
That's a dpy question now lmao
Loop through guild.members and remove roles
ok
If I want to build a calculator (for empirical formulas), but the equation is unknown (User input).
Is this the right way of going about with my data scheme?
def function_name(data_1, data_2):
Where one table is for the equation (id, data_1, data_2 ... data_nth) and my 2nd table is for the input parameters (relates back to the equation table but for the values).
Is there a better way to do this, instead of having a Table with 100 columns (unknown amount)
Anybody know the format that 0x454D424 would get stored in a database as? It's not a string or an INT, BLOB doesn't seem to work either
@torn sphinx you can convert it into INT
I'm trying to run a mysql insertion but the original data dumper didnt include a create table if not exists for me, all the data is in that format
can you show an example/sample of what you have?
INSERT INTO table (id, data) VALUES (6345, 0x454D424480) basically
there's no '' around the data so it's not a string and there's both letters and numbers so it's not a INT either
@torn sphinx what's the error?
Its probably because that value is out of the int size
Change it to bigint and will work.
it can't be an int with an x in it can it?
That's fine yes, it will just convert it to a decimal value for you.
Hmmmmm
ok thanks I'll take a look
@proven arrow I probably should have mentioned I wasnt posting the full length of the data, its over 1000 characters long
bigint max is 255
No it's not
@proven arrow I'm getting an out of range error when trying to insert the data
bigint
Im not even sure why this wasnt stored as a string in the first place
05:17:23 [ERR][SQL Editor Form]: Error Code: 1366. Incorrect string value

bigint didn't work?
nope, out of range apparently
how long is the hex value
over 1000 characters
a single one?
Then use a text type like I said?
Incorrect string value: '\x80\x00\x00\x00\xCB\xD5...'
thats what I get for TEXT column type
What encoding do you have for that column?
not sure, I think the table is latin-1
Try changing it to utf8mb4
I changed the column but the table has data that cant be changed to utf8
still doesnt work
seems I am able to insert the data in the goofy ($#W&($#(@#$(* looking char set with the latin-1 setting and TEXT type column but I dont think this is the right way
can someone help me with an apache starting up error?
Hi i have a discord bot and currently i'm developing an Ambiance Music bot for rpg sessions
So in that bot, Game Masters will be able to save their ambiance music with a keyword + optional changes like thumbnail, what bot will say when ambiance will played.
I'm asking should i open a new table for each person that will use the ambiance system?
Or should i just compress them to one table and check if it's gm's id all the time
Which way is the most efficient and sensable way?
Btw i'm using aiosqlite
one table, track user IDs
Hmm and if you would like to
Can you explain the logic behind this choice?
(I'm kinda new to databases)
you'd have one column with the user's id, and other columns for the other stuff, and then the next time someone uses the command you'd check your database for that user's ID
Making a new table everytime is a wasteful method I think
What all data would you be storing for a user?
Are you mistaking rows for columns
Actually rows are for the ambiance keys
oh sorry
rows*
Like they will type !insert-ambiance winter, youtube_link, other stuffs
Isn't it fixed how many other things will be there?
Uh nope, other things will be null and they are optional for the user
but first two things are not optional
Right, so what's your question exactly now?
So is it better to create a table for each person and insert the ambiance row to that table
Or just compress them to one
One table is better I think
And that's why I said, to identify who's ambience a row is representing you'd add a user-id column
So i'll do PRIMARY KEY(user_id, ambiance-name) yeah
š¤ a user could appear multiple times right?
Yeap, like actually more than 10 in average
And shouldn't multiple users be allowed to have the same ambiance?
Uh they can actually
Yeah so you don't want to pkey either of those columns
Yeah š
Hmm what NOT NULL does? Actually i can check it too
Also i would like to do commands like !take-profile Game Master's Name
As the name suggests a value in that column can never be Null (you can't leave that empty for any user)
So they will be able to use another person's ambiance list
Right so in that case in your query you'd do
SELECT .... WHERE user_id=other person's id
Yeap, okay so we are agreed in 1 table š
Got it
Thank you for the help ā¤ļø
I thought lufthansa-pilot was the only person that was looking here š
Lmao he is the reliable regular here, I pop in once in a while
he sure is
Hi, I want to update my mongodb document, but every time I try it, it says must be an int. How do I update with a string?
oh ok then idk
why does this not work guild_id = ctx.guild.id ping_cmm = {"_id":guild_id} collectionfound = await collection.find_one(ping_cmm) ping_cmmm = {"_id":guild_id, "Prefix":prefix} if collectionfound is None: await collection.insert_one(ping_cmmm) await ctx.send(f"Prefix has been set to ``{prefix}``", delete_after=10) else: await collection.update_one(ping_cmmm) await ctx.send(f"Prefix has been updated to ``{prefix}``", delete_after=10)
it doesnt change the prefix
Because the syntax for an update and an insert is different @sinful condor
If you'll see, an insert needs just one dict, and it'll insert that dict into the db
An update needs two dicts, one dict is to filter through the db and specify which document to update, and the second dict contains the new data that we want to use
What you should be doing is an upsert
When you pass upsert=True to update, it'll
up_date a document if it matches,
but if nothing matches, it will in_sert
Can you show the code?
You might be trying to $inc with a string, when you should be using $set, but I'm guessing without seeing code
Ah, alright š
oh yeah I forgot about that
@burnt turret I dont really understand how to do upsert but I made it so it was 2 if collectionfound is None: await collection.insert_one(ping_cmmm) await ctx.send(f"Prefix has been set to ``{prefix}``", delete_after=10) else: ping_cmmmm = {"_id":guild_id},{"$set":{"Prefix":prefix}} await collection.update_one(ping_cmmmm) await ctx.send(f"Prefix has been updated to ``{prefix}``", delete_after=10) but it still doesnt work
Now ping_cmmm is a tuple
And you're passing that tuple into the query
You need to be passing the two dicts inside the tuple, you can do that with
await collection.update_one(*ping_cmmmm)
ok
@burnt turret this still doesnt work collectionfound = await collection.find_one(ping_cmm) ping_cmmm = {"_id":guild_id, "Prefix":prefix} if collectionfound is None: await collection.insert_one(ping_cmmm) await ctx.send(f"Prefix has been set to ``{prefix}``", delete_after=10) else: ping_cmmmm = {"_id":guild_id},{"$set":{"Prefix":prefix}} await collection.update_one(*ping_cmmmm) await ctx.send(f"Prefix has been updated to ``{prefix}``", delete_after=10)
What happens?
nothin
no error but it just doesnt change it
it runs the update code but nothing happens
I'm on mobile so this code is tough to read, could you just screenshot this and send an image please?
idk just is, it doesnt hurt it
Are you sure?
It gives you message output?
like update = update_one
update = await collection.update_one...
print(update.matched_count, update.modified_count)
its not printing anything
Also I see you're making an instance of motorclient every command - that's unnecessary
In your main bot file, after you do
bot = commands.Bot(commands_prefix=...)
you should do
bot.db = motor.motor_asyncio.AsyncIOMotorClient(url)
This attaches that MotorClient instance to a variable on your bot
So wherever you need the db you can do bot.db (in a cog that's self.bot.db)
Huh that means that code isn't even being run
Alright nice, read what I said earlier though
ok
Making an instance everytime is unnecessary, you attach it to a bot variable
Anybody good with pandas here? I cannot seem to find a way to return the index number of a row. I need to do this because I need to find the # of rows of the column and I believe that becuase of the way it was written, getting the last index will not work
So I am trying to get the index value of a specific row, but there appears to be no way to do this with pandas
It looks like I can easily use .loc to enter and index number and get the value at that index, but there's really no way to do the reverse?
Figured out the solution to this problem. However, we not have an even bigger problem - for some reason pandas is truncating rows and ignoring some values in the same row. I am not sure why this is.
How should I check if an item exists in a table before inserting? Or should I just insert and error handle for primary key duplication? (postgres)
Or should I just insert and error handle for primary key duplication
yes
Bob: is there an item foo in the database?
Alice: is there an item foo in the database?
Postgres: No, Bob, there's no such item.
Postgres: No, Alice, there's no such item.
Bob: ah, alright, I'll insert a foo
Alice: ah, alright, I'll insert a foo
Postgres: sorry Alice, there's already a foo
Alice: WTF?
someone go to #help-candy
you can use ON CONFLICT DO NOTHING
so for example:
INSERT INTO things (name, age) VALUES ('super', 207) ON CONFLICT DO NOTHING;
or use a different expression after ON CONFLICT if you want to update instead, etc.
alright
someone go to #help-candy
Thanks!
What am i doing wrong here cursor.execute(f"""INSERT INTO users VALUES({dis},{user_id}, '{name}', NULL, NULL;""")
not sure if thats the issue but this query should be in the format
INSERT INTO users (dis,user_id,name) VALUES (%s,%s,%s)
something along those lines
oh so i cant pass values using f strings?
you can you just need to add this part (dis,user_id,name) with the name of the desired columns
cursor.execute(f"""INSERT INTO users (dis,user_id,name) VALUES({dis},{user_id}, '{name}'""")
you are trying to insert 4 parameters but you are passing 5 values
Theres 5
oh my bad
Dis
id
name
date
phone
no clue are you sure the columns are named correctly they need to match your database
Yeah i have pgadmin up as well to verify and run SELECT * FROM users to see if the information has been added
Even ran the statement in pgadmin to make sure it works.
!paste
Heres the full context of the function https://paste.pythondiscord.com/dekedipira.py
no idea sorry
Yes. I was adding the information but i wasnt saving it.
Went back and change the if statement so now it reads like this ```py
cursor.execute(f"SELECT * FROM users WHERE id ={user_id}")
data = cursor.fetchall()
if data != None:
print(data)
await ctx.send(data)
else:
cursor.execute(f"""INSERT INTO users (dis,id,start_date,phone,name)
VALUES({dis},{user_id}, NULL, NULL, '{name}');""")
connection.commit()
await ctx.send('You have been added')
Changed it to fetchone because thats the right one to use im pretty sure
you can, but it isn't recommended. The first pinned message in this channel has an explanation for that, check that out.
im having difficulties adding a table with two foreign keys in sqlalchemy
No, how to open it?
idk depends how you download it
for me i use a program to start it, but for your might be you have to do from command line or however you downloaded it
and i click "full installl"
Hey @pastel oyster!
It looks like you tried to attach file type(s) that we do not allow (.msi). We currently allow the following file types: .3gp, .3g2, .avi, .bmp, .gif, .h264, .jpg, .jpeg, .mkv, .mov, .mp4, .mpeg, .mpg, .png, .tiff, .wmv, .psd, .ai, .aep, .xcf, .mp3, .wav, .ogg, .webm, .webp, .flac, .afdesign, .m4a, .csv.
Feel free to ask in #community-meta if you think this is a mistake.
i use full install
hmm well i have never used that method to install, but you can try searching from mysql applications folder and it maybe has some program to start it
maybe its called "MySQL server"
type in mysql in search bar, and show me what appears?
in your windows search bar
anyone wants to help with sqlalchemy
what is that?
what's the problem?
try the option "MySQL shell" or then "Mysql 8.0 command line client"
that's a shell
im trying to record msges sent between two users using a table called messages it has 2 foreign keys referring to both users, problem is i cant get it to work sqlalchemy is giving me errors and im not getting this relationship and backref setup i tried a lot of online solutions nothings working
you need server
@pastel oyster now try running from python
can you show the models?
not work
@pastel oyster try this https://dev.mysql.com/doc/refman/8.0/en/windows-start-command-line.html
`class Messages(db.Model,Base):
tablename = 'message'
message_id = db.Column(db.Integer,primary_key=True)
sender_id = db.Column(db.Integer,ForeignKey('user.user_id'))
receiver_id = db.Column(db.Integer,ForeignKey('user.user_id'))
class User(db.Model,Base):
tablename = 'user'
user_id = db.Column(db.Integer, primary_key=True)`
ok
i removed the extras to make it clear
any Tracebacks
Could not determine join condition between parent/child tables on relationship Messages.sender - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.
3 votes and 3 comments so far on Reddit
tried those solutions
it's for flask right?
yes
ok. What is that "Base" in your model
just a generic configuration for the database i think
ok
remove the Base and try the reddit method
ok
I made a very simple quotation database which allows me to sort by name (its a quote database for me and my friends). I was wondering if there was a way to set the values from False to true easier? Let me know if there is a way (The red is there to cover up some of the active quotes)
Helo, so i have these queries for my shop.
select * from products;
select * from brands where id = 1
select * from brands where id = 2
The first query is getting products then the second two is getting the maker of this product.
It is working, but the issue is that its making the last 2 queries in a for loop so if i have 100 product, then it makes 100 queries, or if 1000 products then 1000 queries. How to avoid this?
Nice, N+1 spot š
You can eager load them which is basically loading them in advance, to avoid this kind of thing.
For example,
select * from brands where id in (1,2)
When you have more products you just pass in the brand IDs which you already have from the select products query.
ohhhh yeah yeah i see makes sense this, was more easier than i assumed š
i thought maybe i had to write some more complex stuff but ok
Yeah that is it, if you want more info on it just Google N+1 problem. It's a common thing people overlook especially when using orms.
Thanks. So for what im doing its okay since im not accepting any args but i would be leaving myself open to SQL injection if i was accepting user input
yep
Someone sent me aiopyql so ima rewrite my check and see how I do with just the api document and GitHub code. Wish me luck.
40 minutes before i figured out ssl='required' is needed for this to work. I hate api sometimes.
how to increment by 1 in MySQL?
await con.execute("""
CREATE TABLE profiles(
id bigint (50) PRIMARY KEY,
name varchar (3000),
age varchar (3000),
location varchar (3000),
dob varchar (3000),
height varchar (3000),
looking varchar (3000),
hobbies varchar (3000),
bio varchar (3000),
time bigint (50)
)
""")```
any idea why this code gives this
```Py
Traceback (most recent call last):
File "abc.py", line 51, in <module>
loop.run_until_complete(db_connection())
File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "abc.py", line 16, in db_connection
await con.execute("""
File "/home/ubuntu/.local/lib/python3.8/site-packages/asyncpg/connection.py", line 295, in execute
return await self._protocol.query(query, timeout)
File "asyncpg/protocol/protocol.pyx", line 316, in query
asyncpg.exceptions.PostgresSyntaxError: syntax error at or near "("```
since PG doesn't tell you which line the syntax error occurred on, try removing all columns except for the first and running it, see if you get an error. if not, add the next column and try again. eventually you will find which column definition is causing the syntax error. my hunch is that the space between varchar and (3000) is not allowed, but i'm not 100% sure without testing and i'm not at my computer at the moment
oh okay thanks lemme try
same goes for the space after bigint and (50)
I remoced them all and still same issue
then I tried removing all columns and try see which column has the problem
await con.execute("""
CREATE TABLE profiles(
id bigint(50) PRIMARY KEY
""")
this was my code
and it still gives the error so probably the issue is on line 1
it's giving a syntax error on just that? maybe remove the (50) and rely on the implicit size of the bigint type
hat about varchar, I need values for those
For varchar it's fine
lemme try
yes, varchar has no implicit size so you always have to specify it
whats the default length of bigint
cause user IDs can have like 20-25 characters I think
ahh ye it worked after I removed the length from bigint
You can see the ranges here https://www.postgresql.org/docs/13/datatype-numeric.html
thanks!
also how do we fetch stuffs
cause it gives me connection object has no attribute fetchall
postgres bigints are 8 byte signed so bigint(19) would be the max anyway https://www.postgresql.org/docs/9.1/datatype-numeric.html
think if its 19 then in some cases it might give errors cause some user ids have 20+ digits
still use bigint for the row PK, but a second column storing the user ID as a string
seriously anyone designing a system with larger than 64-bit user IDs are being ridiculous
oh then i'll have to convert it every time
ye like my user id is 513085098174709779
you can see that's a big ass number
in some cases its like 20-25 characters
also how do we fetch results
like in MySQL or SQLite we did cursor.fetchone() but that doesn't seem to work in postgres
and we don't even have a cursor object here
you could use the numeric type which i think under the hood just stores the value as a string but lets you treat it like a numeric type. it's as slow as storing it as a string tho.
think I can just store it as a string that's less hassle
depends on which python pg client lib you're using
im using asyncpg
there's probably some good answers on stackoverflow
I tried looking but cant find anything
how about the docs?
Bigint is fine
You won't exceed that for discord IDs
you sure?
idk how much postgres is different from MySQL but in MySQL discord user ID exceeded bigint in some cases
Well as far as I know all IDs are same length and under that range.
if we're talking Discord then yeah, they use Twitter snowflake for 64-bit IDs
Integrate your service with Discord ā whether it's a bot or a game or whatever your wildest imagination can come up with.
oh I see then
abc = 12345
efg = 638287238
await con.execute("""
INSERT INTO profiles VALUES ("{}", "tah", "23", "hhfh", "jfjfj2", "jfjfjfj", "jfjfjf", "jfjfjf", "jfjfjf", "jfjdjf", "jfjfjfj", "{}")""".format(abc, efg))
hm apparently the queries in postgres is a bit different from MySQL or SQLite I think
like that's giving me this
Traceback (most recent call last):
File "abc.py", line 31, in <module>
loop.run_until_complete(db_connection())
File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "abc.py", line 19, in db_connection
toup = await con.execute("""
File "/home/ubuntu/.local/lib/python3.8/site-packages/asyncpg/connection.py", line 295, in execute
return await self._protocol.query(query, timeout)
File "asyncpg/protocol/protocol.pyx", line 316, in query
asyncpg.exceptions.UndefinedColumnError: column "12345" does not exist
are we supposed to like mention which column something goes in? cause we didn't need to do that in MySQL or sqlite
The only time you don't specify columns is when you are inserting into every column.
ohh
Any idea how we fetch a specific amounts of rows in asyncpg
What would be the best way (preferably free) to set up a database for a discord bot?
Use sqlite?
There are some sites that give mysql hosting for free but I wouldn't recommend to rely on them
is there anything that would work for microsoft access?
I don't think so
I don't think you can use python/jvs to write on microsoft access db but can't be too sure
Maybe google it?
would MongoDB work?
Ye but mongodb's free tier is quiet slow
Queries will take up to 5 seconds to happen
Sometimes a bit longer
the commands wont be used very often. around once a week so that should be fine
Ye then ig you can use that
Are you gonna store a lot of data?
Cause if not then just use json. Json works perfectly fine if taken care properly
Currently the bot is in multiple servers and one of them has 1400+ members. And i need to store data for each of them
Only 1400?
Lmao one of my bot runs in a server with 141k users and json still works perfectly fine there
ah ok lmao
Unless you're handling way too many queries at a time you can stick with json
Just make sure to keep a backup from time to time
ok thanks
@slender rose I am using mongodb free and the queries maybe take .1 seconds it is lighting fast
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "movies_pkey" i have a table that i already inserted two rows, then i tried to insert again, with 10 rows of value. it gives me this error. i dont want to drop the table everytime i update it, how can i set it in the insert?
i googled it and i found this from psycopg2.errors import UniqueViolation
but i am not sure, how to place it
cursor.execute("""INSERT INTO movies (imdb_id, imdb_id is my primary key of the table movie
#help-honey someone please go there
what's the correct way for inserting date time in sql
With Postgres you can directly insert a datetime in ISO format, as a string
i think its the same with mysql where u encase it in a quotes. cant remember if the delimiter of - is neccessary
Hey @true kelp!
It looks like you tried to attach file type(s) that we do not allow (.zip). We currently allow the following file types: .3gp, .3g2, .avi, .bmp, .gif, .h264, .jpg, .jpeg, .mkv, .mov, .mp4, .mpeg, .mpg, .png, .tiff, .wmv, .psd, .ai, .aep, .xcf, .mp3, .wav, .ogg, .webm, .webp, .flac, .afdesign, .m4a, .csv.
Feel free to ask in #community-meta if you think this is a mistake.
can i private dm somebody
does someone know how can i use json with heroku? (i use heroku to deploy my bot)
or even mongoDB
Hey I hv an doubt
Did I use and manage sqlite database online ?
Can anyone help me with json stuff here
i installed vim but i keep getting that it's not reconized as an internal or external command
i looked for sollutions online and it told me to look in system 32 for it however even after i searched i could not find it there
it's there onto:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Vim 8.2
but those are fastclicker(snelkopelingen)
and not the actually path itself
adding it in the command prompt gives the same error
how am i suppose to solve this?
It will explain about:
How to install vim editor on windows ?
im following this vid
C:\WINDOWS\system32>c:
C:\WINDOWS\system32>..
'..' is not recognized as an internal or external command,
operable program or batch file.
C:\WINDOWS\system32>cd..
C:\Windows>cd "Program Files (x86)"
Het systeem kan het opgegeven pad niet vinden.
C:\Windows>```
š
Just getting into python, are guis hard to create if I wanted to start with basic IF this then that programs, to automate things with a click.
there are a lot of gui builders out there
if u use those its ez
What about diy for a beginner?
might take some time but, it do able
yes
U r lucky
thats fine i hate win too
Okay I like you :b
Look
Those commands that you're using are unix native if you want to move in win terminal it is different
I don't remember exactly what you have to use but write "help" and it will display the documentation
My recomendation is to use git bash terminal
It's a little bit better
Next
Try v -v
Or vim -v
Or vim --version
To check if vim was well installed
then how can i run the program?
Let me see the documentation and then I will come back :b
Well If you're live vim (Like me)
no i mean i installed it and it runs
You can use VS Code with vim extension if you need it asap
C:\Windows>vim --version
'vim' is not recognized as an internal or external command,
operable program or batch file.
and yet
it's fine š i posted the screenshot for clarification
no no im glad u are trying to help
true we are made to be loved
Is it win7 or win10?
win10
home
yes 64 bit
Got it
Well
I found something
Uninstall vim and remove the dir, then reinstall it and check the VimX file
can do
Vim Text Editor is an open source free text editor for Windows. Using a bare-bones text editor like Vim has its own benefits. You get to write the code from scratch as there are no templates. It deepens your knowledge about a programming language and makes sure you understand each bit of the code.
I found this guide
And this is the oficial page so
I think that it better to download this installer
yep
it works
i had it set to partly install
Step 2: Now open the setup file and follow the on-screen instructions. In the step where the setup asks you for the installation type, select Full and hit next.
when i followed that one
it worked
@heavy spear thanks so much ā¤ļø
Ohhhhhh cool XD
I'll be lucky :b
I don't know if you have your .vim
https://github.com/AndresMpa/My-NVIM-configuration if you need one you can use this :b
anyone know how input variables in python into SQL flask statements
def something(username):
con.execute('SELECT * FROM store Where Shopname = :name', {'name':username})
would give a syntax error, not quite sure how to input the values from 'username' into the sql statement
would anyone know how to link this with a excel database?
there's a module you can use to deal with excel
openpyxl
you can find it on pypi
any solution????
defaulting to user installation because normal site-packages is not writable
I think it means you already installed the module
why it is showing this
this line
it never happened before but after this i am unable to use any of the other modules
Hi i'm using aiosqlite and i'm trying to update something to my database but i want to raise error if that thing doesn't exist in there
But UPDATE method doesn't raise error
But UPDATE returned an aiosqlite object, can i use it and take the information that i want from there?
@bleak crown the execute function on update will return a cursor object. This will have a rowcount attribute that tells you how many rows were affected by the update.
You can check if it's 0, which would mean it didn't update anything
Yeah, thank you ā¤ļø
Ask your question, someone will answer if they can
How can I migrate a Postgres database hosted on my localhost to a Docker image? My goal is, of course, that another collaborator can access the database in its remote machine
@dusk junco You case use the official Postgres docker image, which works very well
OK, so I would just add that image into the container I'm running is that it?
you can use pg_dump and pg_restore to transfer you data
a docker container is based on a particular image
I've done this simple docker-compose but I don't know if it will work
version: '3'
services:
postgres:
image: docker_image
container_name: python:3.7.9-stretch
ports:
- "5432:5432"
network_mode: host
environment:
- LC_ALL=C.UTF-8
- POSTGRES_DB=elevation
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=
- POSTGRES_PORT=5432
you can understand a container as being an "instance" of an image
Right I see
if you wanted to use the official PG docker image, your image field should be something like postgres:latest
The PostgreSQL object-relational database system provides reliability and data integrity.
if you have other questions about it don't hesitate to ping me!
database guyes hello XD
facing a weird problem with mongo
what does this mean and how to fix it?
show the code that is raising this
can someone help me
how can i update a colum in mysql.connecotr
sql_query = (f"UPDATE hikari_clients SET plan = {arg1} WHERE client_id = {client_id}")
If you are inserting values into a table (assume using SQL, I'm very new to this), and you have defined a field to have a default value, how do you "skip" having to write NULL in that field if you don't have the value yet. An example is that you have 3 fields and the last field is called bird_example and contains BLOBs. I don't have the BLOB yet, but want to insert data for the first two fields, id and bird_name. I tried to just write "INSERT INTO birds VALUES (1, 'Hummingbird', NULL [I want to skip this blob] ) ;".
EDIT: I found out that you can specify which columns you want to input values for, so I just skipped the BLOB field :D.
Hi, does anyone now how environment variable work in python? I have a .env file with all the relevant information (e.g USER=postgres) and in my main python file I have imported dotenv. If I don't use environment variables I can connect to my database and insert data but when I use environment variables I get the following error:
psycopg2.errors.UndefinedTable: relation "my_table" does not exist
My envrionment variables look like this:
host = os.getenv('HOST'),
dbname = os.getenv('DBNAME'),
user = os.getenv('USER'),
password = os.getenv('PASSWORD'),
port = os.getenv('PORT')
Any help would be amazing!
Just to check, you are loading the env variables?
I want to load them but I am doing something wrong as they dont appear to be read by the code, like I said when I don't use env variables the data is inserted into the db but when I do use them it says the table does not exist
What I mean is, do you have this before trying to access env variables?
from dotenv import load_dotenv
load_dotenv()
I have the first line but not the load_dotenv() where about does that need to go?
ah yeah i see, just under it as you said
await self.bot.db_pool.fetch("SELECT note_id, raw_note FROM notes WHERE user_id=$1 AND note_id IN ($2::array)", ctx.author.id, note_id)
the variable note_id is a python list of integers, how would i use the sql IN operator here? this is asyncpg + postgresql (ping me on response please)
you're directly trying to insert the discord.Member object by the looks of it
insert only the user's ID
moreover, the code you just showed doesn't show the related code for the error
@burnt turret I've fixed it! Should have read it more carefully i have now made python convert it and not let mysql trying to do it.
Hello their! I have this code:
# Fetch all member name / id
guild = self.client.get_guild(Guild.id)
guild = await guild.chunk()
for member in guild:
print(member)
self.usersdb_cursor.execute(
'INSERT OR IGNORE INTO user(user_id, user_name, user_discriminator, user_creation) VALUES(?, ?, ?, ?)', (member.id, member.name, member.discriminator, member.created_at))
self.logger.log('information', 'Fetch all data from users.')
But it doesn't right anything in my db but, when i print(member) i list all user. Have you got idea?
Is that SQLite?
In any case, you may just need to call commit() on the connection
Depends on how you set it all up, though.
(Commit at the end, at least, that is -- in your case you could do it either after each execute inside the loop, or just when the loop is done and all the inserts were executed.)
Yay! šŗ
@bitter yoke Do you why it doing that:
[2021-01-18 21:33:53] Error: 'NoneType' object has no attribute 'execute'
Code:
class DB:
def __init__(self, client):
self.client = client
self.logger = Logger()
self.usersdb = None
self.usersdb_cursor = None
async def initialization(self):
try:
# Connection to database.
self.usersdb = sqlite3.connect('src\\db\\users.db')
self.usersdb_cursor = self.usersdb.cursor()
self.logger.log('sucess', 'Connected on db.')
# Create table
self.usersdb_cursor.execute(
'CREATE TABLE IF NOT EXISTS user(user_id INTEGER NOT NULL UNIQUE, user_name STRING NOT NULL, user_discriminator STRING NOT NULL, user_creation)')
self.usersdb_cursor.execute(
'CREATE TABLE IF NOT EXISTS infraction(id INTEGER NOT NULL, user_name STRING NOT NULL, user_id INTEGER NOT NULL, moderator_name STRING NOT NULL, moderator_id INTEGER NOT NULL, reason STRING, infranction_number INTEGER NOT NULL)')
self.logger.log(
'sucess', 'Table creation was successfull create in usersdb.')
# Fetch all member name / id
guild = self.client.get_guild(Guild.id)
guild = await guild.chunk()
for member in guild:
self.usersdb_cursor.execute(
'INSERT OR IGNORE INTO user(user_id, user_name, user_discriminator, user_creation) VALUES(?, ?, ?, ?)', (member.id, member.name, member.discriminator, member.created_at))
self.usersdb.commit()
self.logger.log('information', 'Fetch all data from users.')
except Exception as e:
self.logger.log('error', e)
nope but i can take a guess in a sec if you haven't found it
I haven't found it ^^'
so you know which line it's complaining about though, yeah?
so when it complains that it can't find "execute" it's gonna be complaining about a line where you try to use something called execute
and I see... I think that narrows it down to 1 line in that code right?
Yes
Im kinda stupid but what is this type of variable called?
var = {
code
}
It's say that i can execute with none :(
@golden warren so when it says it can't find "execute" in NoneType it's saying that self.usersdb_cursor is None
so it's trying to do
None.execute()
and that doesn'twork
Yes.
@autumn epoch is "code" a bunch of this: "That", you: "me", potato: "potato" in that example?
Yes
that's a dictionary
Ok thank you
Its answered in the faq, https://magicstack.github.io/asyncpg/current/faq.html#why-do-i-get-postgressyntaxerror-when-using-expression-in-1
In your case would be like:
await self.bot.db_pool.fetch("SELECT note_id, raw_note FROM notes WHERE user_id=$1 AND note_id = any($2::your_col_type[])", ctx.author.id, note_id_list)
At begin, it started but when a user join discord server i think that connection is non @bitter yoke
'NoneType' object has no attribute 'execute
Also how would I have a variable in a dictionary. Like Name: Name
so we have
im looking for a very fast database which can store a huge amout of links,products and access it. Is mysql good enough?
Yes
thanks alot ā¤ļø
Like this:
new_player = {
"The players name": {
"xp": 1,
"level": 0
}
}
without knowing your goal
The left side of the : is the 'key' and the right side is the 'value'
so "width": 5
Yes
and "height": 8
but
"Jimmy":
isn't the same as "width" and "height" if you see -- it's not the name of a thing about jimmy
so "Jimmy": ....
"what is the value of my width" and "what is the value of my height" make sense
but let's not talk about "what is the value of my jimmy"
but you can do what you're talking about, and it's pretty hard to say whether it's right or wrong without knowing what you're making this dictionary to do
Its to store information about the player, xp, level, inventory
So the program can access it later
And also it is saved to a json file
that makes sense, but to decide what the keys are
is this one big json file of all players, or a file-per-player?
One big one for all the players
and when you load the file, do you know what kind of python data you want from it? a list of players? or, I'm sensing, maybe you want a big dictionary where you can say all_players['jimmy'] and get jimmy's data?
Well using the discord.py I am going to wait till someone chats, checks if their name is on the file and then change the stats
Considering json is not for mutable data storage you should try and avoid it for what you are trying to do.
Would be better to use a relational db
Really, then what should I use?
It can very quickly become a pain to manage, and querying the data would be much easier with sql as well. And since you mention discord.py JSON would also be blocking whenever you read/write to it.
Well you may not notice it since if your testing, with only a few users.
you could make it work with a json file but you will not be happy with it, because you want the real-time updates
right
Wdym it is getting real time updates
right but getting real-time updates from multiple users into a single disk file is not a problem you want to solve by hand
However since discord code would be async, each time you write/read from the file your bot would become unresponsive for how long it takes to read/write from the file. As i said you might not notice it now but when you have many users at once you will.
So what would work?
and i've done that kind of thing for something I knew would never have more than 2-3 users
but it still kinda sucked
@autumn epoch can you re-state the problem you're really trying to solve? I think I may have lost the point here
Yes
thx
So basically when a new player joins, it would add on to the file, but I need the players name, which is in a variable
so let me fake it here:
Ok
so here are two things, in variables:
In [12]: new_player = "another player"
In [13]: print(new_player)
another player
In [14]: x = 5
In [15]: print(x)
5
and if i want them in a dictionary I can do
In [17]: player = {
...: "name": new_player,
...: "size": x
...: }
In [18]: player
Out[18]: {'name': 'another player', 'size': 5}
so the fact that they're variables isn't a big deal, like where I said 'x' I could've said '5'
same thing
so that's the syntax for using variables to set things in a dictionary
Ok thank you
Thx
Need help with high data @ me pls
any tips on choosing databases? Ive got report data to store and the different types have different fields. Should i go with non relational or relational. I mean i could shove it all into a json like format with mongo but is having definitive structure better?
Depends on what your requirements are. Different databases are good at different things, so define your requirements first and then go from there.
yeah im prob gonna have to ask tmr
oh right, I should've checked there first. Thank you!
ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs```
can i get help with this? im trying to use my mongodb and i keep getting this error, ive installed pymongo[srv] and imported dns, but still getting the same error.
somehow, rebooting the bot fixed it..??? i dont know how since i was reloading and trying to load it again
My column type is set as SERIAL, but in the query I should be doing $2::INT[] I assume?
If I have a bunch of dates in my postgres database, how can i get back all the rows in which the day-month is the same as today's day-month? the year can be different, but i want the rows with the same day and month. (i'm not sure if i've explained the question well enough, i can try rephrasing)
Yeah that would be right
It has an extract function you can use. You would have to extract the day, and compare it with today's day. And same for month.
For example, for date in the where you can have WHERE extract(day FROM your_date_col) = extract(day FROM current_date);
Oh, thank you so much!
@proven arrow what is the difference between this extract function and date_part?
