#databases
1 messages · Page 147 of 1
Yeah I'll stay with that even tho it's for discord bot
Theoretically yes.
You would want atomic values, and if the list can be divided then it would break this.
I've written a gist on switching to motor from pymongo, I can link it to you if you want (when I turn my computer on)
,otor still uses MongoDB?
nd sure if you can why not
from discord.ext.commands.core import guild_only
from discord.invite import PartialInviteGuild
import pymongo
from pymongo import MongoClient
from pymongo import mongo_client
print(pymongo.version)
CLUSTER = MongoClient("m")
NAMES_DATABASE = CLUSTER["FirstDatabase"]["names_data"]
def tryout():
options_log = ['log-in', 'log in', 'login']
options_sign = ['sign-up', 'sign up', 'signup']
while True:
sign_or_log = input("Sign up or log in?")
if sign_or_log in options_sign:
name = input("What is your name? - ")
check_if_exists = NAMES_DATABASE.find_one({"name": name})
if check_if_exists:
print("This name already exists")
else:
skill = input("What is your skill - ")
ended_dct = ({"name": name, "skill": skill})
NAMES_DATABASE.insert(ended_dct)
elif sign_or_log in options_log:
name = input("What is your saved username? - ")
check_if_exists = NAMES_DATABASE.find_one({"name": name})
if check_if_exists:
skill = check_if_exists.get('skill')
name = check_if_exists.get('name')
print(f'Name: {name} \n Skill: {skill}')
else:
print("This name is not in the database.")
tryout()```
Did you see what I did? I am proud of myself
If you'd look at this a little, what would you change of it to async version??
Glad I make my own class to handle MongoClient, switching to motor will be easier 
would this work? :
@commands.Cog.listener()
async def on_guild_join(self, guild):
data = {
Guild_id:f"{guild.id}",
prefix:"//"
}
is_exist = db.find({Guild_id:f"{guild.id}"})
if is_exist:
return
else:
db.insert(data)
```db var is for the guilds collection
Guild_id is an indefined variable there
wdym
So just addin await ?
mostly yeah
exactly what I said 
data = {
Guild_id:f"{guild.id}",
prefix:"//"
}
is_exist = db.find({Guild_id:f"{guild.id}"})
``` you're using `prefix` and `Guild_id` there without any quotes, so python will think they are variables, and you never defined them anywhere
dunno, try it and see
k
How can I edit existing data?
for example:
collection:
doc_a:
{"guild_id": "267624335836053506", "blah": 0}
How do I add two for 0?
Oh right
guild_data = await collection.find_one({"guild_id": "267624335836053506"})
new_obj = guild_data
new_obj['blah'] += 2
await collection.update_one(guild_data, new_obj)``` ?
no need to make new_obj var
guild_data['blah']
oh, no you need it
ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs
error when i tried my code
Wait so what code will work?
Wait what?
the error tells you what to do? install the module
look into mongodb's $set operator
you can update without having to first retrieve the old value
Alright, thank you very much, so would I use relationships for that then?
There is this example
I can't quite understand it tho
where is this example from?
i'd made a mistake, what you'd want is $inc
Depends what you're working on, but generally yes (make the relationship and normalise it). However, there are some data modelling techniques which do allow arrays, and in such cases you can ignore the guidelines of how to do relational modelling correctly.
w3schools
right
i'd recommend looking at mongodb's own documentation, they give examples and a mongodb shell so that you can try the queries out there itself
MongoDB doc is 👌
collection:
doc_a
"guild_id": {"267624335836053506": {"480404983372709908": "wallet": 0, "bank": 0 }}
await collection.update("$inc": "267624335836053506.480404983372709908.wallet": +2)
Like that?
i did
nope, you should use it like $inc: {key to increase: value to increment}
can you explain how you've structured this data?
I don't understand how?
Wdym?
what are the keys and values in this example document you've shown
because I see some ID again as a key, which i'd already advised against earlier
Thank you man
{
"Guild_id": "<type>",
"prefix": "<type2>"
}
what should be the tip of each one
is it something like... int, string etc...??
ok
But types are automatic when you insert data with pymongo
oh forgotr {
collection:
doc_a
{"guild_id": "267624335836053506", economy: {"480404983372709908": "wallet": 0, "bank": 0 }}
There
how can I increase or decrease the 480404983372709908 wallet ?
now the key in the economy dict is a user ID?
Yeah
how do you plan on adding more users there?
Get the economy dict and update it?
you're not understanding what i'm asking
anyways "480404983372709908": "wallet": 0, "bank": 0 is wrong syntax, it's not possible
is it "480404983372709908": {"wallet": 0, "bank": 0 }
even then you run into the earlier issue that you'd had when you were doing something like "nir": "something"
because now it's impossible to query to find that user's details
i'm a little busy now, sorry :/
oh ok
import pymongo, dns
from pymongo import MongoClient
account = pymongo.MongoClient("url")
GuildsDB = account['Discord']['Guilds']
UsersDB = account['Discord']['Users']
data = {"Guild_id":"78888882125258541298654", "prefix":"//"}
GuildsDB.insert_one(data)
```will this work ._.
collection.update(query, data) where:
queryworks as a filterdatais the data to change
to change? how can I say what change?
Let's say a user wants to change their email.
His id is 1234 and the new email is NIR@gmail.com
from pymongo import MongoClient
connection = MongoClient(...)
collection = connection['database']['users']
query = {'id': 1234} # User with id 1234
data = {'$set': {'email': 'NIR@gmail.com'}} # Set the key field to NIR@gmail.com
collection.update(query, data)
There are a lot of different operators with MongoDB
But I don't know exactly the content I am changing
Using $set, you're overwriting the previous value
Knowing the previous value is unnecessary
Here, query works as a filter, I'm telling MongoDB that the document tI want to change is one that has 1234 as id
Then, data tells the modification to make.
I want to set ($set) the email key to NIR@gmail.com
Can't I just do something like:
{"guild_id": "267624335836053506", economy: {"480404983372709908": {"wallet": 0, "bank": 0}}}
full_dict = USERS.find_one({"guild_id": "267624335836053506"})
wallet = ful_dict['economy']['480404983372709908']['wallet'] += 5
bank = ful_dict['economy']['480404983372709908']['wallet'] += 5
and than dump it back?
but that's highly inefficient compared to just updating it in place in the db itself
you're doing 2 queries (possibly 3) which should be done with 1
collection:
doc_a:
{"guild_id": "267624335836053506", economy: {"480404983372709908": {'wallet': 0, 'bank':0}, "201674460393242624": {'wallet': 0, 'bank':0}}}```
```py
query = {'480404983372709908': {'wallet'}
data = {"$set": +2}
collection.update(query, data)
or:
query is just a filter, it doen't tell the key to modify
data = {"$set": {"wallet": +2}}
In your case query is a little more complex due to how you structured the document
Is there a better way?
There are multiple ways
You guys are using pycharm or idle?
{"267624335836053506": "economy": {members and that}}
I use VSCode but go to #editors-ides
The simplest way would be to have a collection for each guild
With the collection name as the guild id
Like this
But idk if it's the best solution
making wallet for each user is easier than wallet for each user in each server
You could also make a single wallet which is an array
Like this
{
user_id: 201674460393242624
wallet: [
{guild_id: 267624335836053506, 'bank': 0, 'money': 0}
]
}
back when i used to use mongodb and had an economy setup, i had a single "users" collection
the documents were similar to this,
{
user_id: ID,
guild_id: ID,
...
}
i'd just make another document if the same user was in multiple guilds
That's the third solution I was thinking about
Easier to handle than this
lets me query like collection.find_one({"user_id": ..., "guild_id": ...}), accessing data becomes easier this way than trying to query it out of an array
I think I'll go with collection per guild
eh, i don't think that's the best idea
although i don't have concrete arguments against it off the top of my head either 😦
i think there's an upper limit to the number of collections per database though (but not one for number of documents per collection i think)
I wish there was a server in discord for MongoDB
How can I make a collection per-guild? Just do CLUSTER["FirstDatabase"][ctx.guild.id]
Or does it have to string?
oh
_"guild_id" ?
I don't know
So how should I do it?
But really, can't I do something like:
guild_economy = CLUSTER['EconomySystem'][f'_{guild.id}']
as I said, i don't think you can make an unlimited number of collections per database
Oh
you can have an unlimited number of documents per collection
So is there a better idea?
i'd probably just go with the data model i had suggested earlier
That? It seems super-hard
how so?
How do I get the user matched to the guid? is .find_one({f'ctx.author.id': author.id, f'{ctx.guild.id}: ctx.guild.id} possible?
Or maybe I'll just make a multi-server economy system and that's it
guild_id is a key, not a specific value (like user_id)
.find_one({'user_id': ctx.author.id, 'guild_id': ctx.guild.id})
why? 🤔
too hard
I am trying to di it tho
{"480404983372709908": {"wallet": 0, "bank": 0}, "267624335836053506": 267624335836053506}```
Is this the format
You way
this isn't my way?
But
You said something that do Users collection
and have the guild there
with the user
yeah i did
.
user_id ?
yes, what are the keys i have set there?
But where will I put the wallet and bank?
just add more keys?
{"user_id": 480404983372709908, "guild_id": 267624335836053506, "wallet": 0, "bank": 0}
Like that?
sure, that can work
Ok
and the update thingy
still don't understand
query = {"user_id": 480404983372709908, "guild_id": 267624335836053506}
data = {'$set': {'wallet': +2}}
collectoin.update(query, data)
Like that?
?
The operator you're looking for is $inc
query = {"user_id": 480404983372709908, "guild_id": 267624335836053506}
data = {'$inc': {'wallet': +2}}
collection.update(query, data)
?
And if I want to remove 2?
-2
i keep getting this error.
Task exception was never retrieved
future: <Task finished name='Task-226' coro=<ConnectionState.chunk_guild() done, defined at C:\Python39\lib\site-packages\discord\state.py:830> exception=ConnectionResetError('Cannot write to closing transport')>
ConnectionResetError: Cannot write to closing transport
``` i think this is happening because i added db based commands, but before that i never saw this error
I want to get into discord.py databases?
- I need a brief description of what a database does?
in short, it's for storing persistent data
async def create_db_pool():
client.db = await asyncpg.create_pool(database = "Discord_ids", user = "postgres", password = "" , port = 5432)
print("connected to db")
client.loop.run_until_complete(create_db_pool())
code and error
i was hosting and got this error
Hi, I am using MongoDB for a discord bot. I want to make a prefix command. How can I update a current exsiting document?
I heard about $inc
Not sure how to do it here
query = {"guild_id": ctx.guild.id}
data = {'$inc': {'prefix': new_prefix}}
PREFIXES.update(query, data)
Will that work?
Honestly looks fine
Really?
Yeah, should work fine
It'll update it whenever python reaches that line yes
Ok
Although I'm not sure on $inc since it depends on your specific case. If it doesn't work change it to $set
It depends on the way you're using it I guess. It should work but if not, just try $set
loaded_prefixes = {}
def get_prefix(bot, message):
global loaded_prefixes
if str(message.guild.id) not in loaded_prefixes:
PREFIXES = CLUSTER['Multi-purpose']['Prefixes']
guild_prefix = PREFIXES.find_one({"guild_id": message.guild.id})
if guild_prefix:
prefix = guild_prefix['prefix']
else:
prefix = '!'
loaded_prefixes[str(message.guild.id)] = [prefix, bot.mention]
return loaded_prefixes[str(message.guild.id)]```
```py
@bot.command(aliases=['set-prefix'])
async def set_prefix(ctx, *, prefix=None):
PREFIXES = CLUSTER['Multi-purpose']['Prefixes']
guild_prefix_check = PREFIXES.find_one({"guild_id", ctx.guild.id})
if not guild_prefix_check:
data_to_insert = ({"guild_id": ctx.guild.id, 'prefix': prefix})
PREFIXES.insert(data_to_insert)
else:
query = ({"guild_id", ctx.guild.id})
data = {"$inc": {"prefix": prefix}}```
Looks ok to you ?
Hey @jaunty galleon!
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:
Should I try $set ?
Guys iam using flask and sqlalchemy, and i want to do a join with 3 diferents tablas, does someone know how to do it?
Inc is for "increment", I'd said that for your wallet or something
You use set to change the value
https://paste.pythondiscord.com/irefunixaw.sql Same error tho
ohh
query = ({"guild_id", ctx.guild.id})
I did this, is that the problem?
Yeah that's not the problem
ohh
I see it now
OMG OMG OMG OMG My first database actions workkk
anand is a god
And one more tiny thing, every time using .insert, it says:
c:\Users\Student\Desktop\discord_bot_databse\main.py:27: DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead.
PREFIXES.insert(data_to_insert)

Wanna see something cute? go to #ot0-fear-of-python
Also posted to #help-lemon.
Anybody have enough experience with PySpark to tell me where I'm going wrong here?
covid_positive = main_stream\
.withWatermark('event_time', '30 seconds')\
.where(main_stream.patient_status_code.isin(list(COVID_POSITIVE_CODES)))\
.groupBy('zip_code').count()
Getting a really odd error, considering that I've included a watermark.
pyspark.sql.utils.AnalysisException: Append output mode not supported when there are streaming aggregations on streaming DataFrames/DataSets without watermark;
Aggregate [zip_code#3], [zip_code#3, count(1) AS count#19L]
+- Filter patient_status_code#4 IN (2,5,6)
+- EventTimeWatermark event_time#5: timestamp, 30 seconds
Additional info:
main_stream, covid_positive: pyspark sql streaming DataFrames
event_time, zip_code: DataFrame Columns
COVID_POSITIVE_CODES: Macro to filter input for appropriate examples.
def ELOlog(player, curELO, ELOchange, logNote):
#gangCursor.execute(f"SELECT name FROM sqlite_master WHERE type='table' AND name='{player.id}LOG'")
localGangCursor = gangCursor.execute(f"SELECT count(name) FROM sqlite_master WHERE type='table' AND name='{player.id}LOG'")
if localGangCursor.fetchone()[0] == 1:
gangCursor.execute(f"INSERT INTO {player.id}LOG (ELOchange, curELO, lognote) VALUES (?,?,?)", (ELOchange, curELO, logNote))
else:
gangCursor.execute(f"CREATE TABLE {player.id}LOG (ELOchange float, curELO float, lognote VARCHAR(99))")
gangCursor.execute(f"INSERT INTO {player.id}LOG (ELOchange, curELO, lognote) VALUES (?,?,?)", (ELOchange, curELO, logNote))
connector.commit()
What am I doing wrong here?
use('Discord');
col = db.getCollection('Guilds');
id = col.find({"Guild_id": "551971555784978488"});
```how can i get "prefix" value from the guild_id
not py
.
the doc:
{
"Guild_id": 551971555784978488,
"prefix": "//",
}
Using MongoDB, is is possible to make something like that?:
{"guild_id": 267624335836053506, "Muted_role": muted_role_id, "Sugggestions_channel": suggestiones_channel_id}
And than remove channel command, that will make it be empty, so doing .get("Muted_role") can return None?
So it would be {"Muted_role": , }
not py?
Can mongodb store None?
table name cant start with a number
it can but you need to escape it. The following will work, CREATE TABLE [{player.id}LOG] (ELOchange float, curELO float, lognote VARCHAR(99))
It will probably be translated to Null
Thanks man! 😄
Btw, is this your new account? What happened to your old account? @proven arrow
Hmm not sure this is the only account
Also looks like your making a table per user for their logs, which is not such a good idea. Instead you should have a single table to store the logs.
Howcome?
It creates unneccessary spam. If you have 100 users thats 100 tables. 1000 users 1000 tables. What if the table design needs changing? That would mean you need to alter each of those tables. Aggregating and making joins becomes much more difficult too.
Aaaah ok
I see
Then I will make one table for the logs 😄
how can i delete a whole doc if i only know a value in it
in mongo
no need i got it
I have a query like "SELECT x, y, z FROM location the problem is that Z is a blob and not TEXT can i cast only that value into utf-8?
can someone help me with this please
im not sure how to go about it
does anyone knnow why im getting this error? v
UsageError: Cell magic %%read_sql not found.
Well, you don't have that cell magic then. Perhaps it's from an extension that you haven't loaded with %load_ext.
{"guild_id": 267624335836053506, "Muted_role": "None", "Suggestions_channel": "None"}
query = ({"guild_id": 267624335836053506})
data = {"$set": {"Muted_role": 822884733645488188}}
collection.update(query, data)
Is that ok? Is.there a better way of storing non-existing data?
I would just...not store those keys with a Null value in the first place (if it doesn't exist, the idea is clear either ways)
how can I make an inventory system that keeps track of what we have, my main ques is how can we store them with aiosqlite?
how to use AIOSQLITE
?
leave that
yea sure
how would you store many text in one column
just like an inventory system in sqlite?
Inplace of storing many text items in one column create a another table name inventory and add a different column for different item like
a column for book etc. etc
i was thinking that only! thanks for confirming me
as it also help in storing quantity
yah
like no. of items per user
a column named user and etc ,etc
yes, tysm
NVm
can you DM me a code of basic data storing ,updating and retrieving @torn sphinx
not much advance level just basic
ummm, like what
of a aiosqlite? i am making it for my discord bot
like how to store data in a table in aiosqlite and etc etc
Me Too
So should I do something like:
SERVER_SETTINGS = CLUSTER['Discord-bot']['Server-settings']
check_if_exist = SERVER_SETTINGS.find_one({"guild_id": 267624335836053506})
if check_if_exist:
muted_id = check_if_exist.get("Muted_role")
if muted_id:
role = ctx.guild.get_role(muted_id)```
Like that?
{"guild_id": 267624335836053506}```
And how can I update it, to add more keys?
query = {"guild_id": 267624335836053506}
data = {"$set": {"Muted_role": 822884733645488188}}
SERVER_SETTINGS.update_one(query, data)```
Like that?
I'm on mobile, I couldn't read the first one
This looks good
Even tho Muted_role is not there yet?
Yep
update queries update documents , and adding a new key to a document is also just updating it so yeah
{"guild_id": 267624335836053506, "Muted_role": 822884733645488188}
How will I remove the Muted_role ?
?
How do I reset postgre's main acc password
Mongo db vs Postgresql
Which is better and what should I use for my bot?
PostgreSQL pretty much beats mongo in just about everything other than simplicity of scaling clusters and beginner friendlyness
@warm rain,
lol
saying that about scaling though a single postgres instance can handle way more than a standard mongo instance
If its for a bot id recommend going for postgres because the drivers and async support in python is better
it's also way more suited to the relation discord data
what's funny about that homie
https://www.postgresqltutorial.com/postgresql-reset-password/ is probably what you need
In this tutorial, we will show you step by step how to reset forgotten password of postgres user in PostgreSQL.
is written by CF8
Thanks
Ah lol gotcha
thanks
Any tips for deciding which relationship loading techniques to use, lazy/eager/no load?(should a beginner worry about such things?)
Is it possible to delete rows from table and also return those values to the program, or should SELECT and DELETE be done separately for that?
generally you dont need to worry about that in most / all situations really
which database is this? you can add a RETURNING clause to the delete if it is postgres i think 🤔
You should only worry about them if you have performance issues. However, the most common is probably eager load, if your using orms so you can avoid N+1 problem
I'm using aiomysql
ah I'm not sure then
I'm pretty sure I can do a SELECT on the table to get the rows
Do a fetchall to turn them to a list
and the DELETE the rows based on the same condition as SELECT
Then return that list
I just wanted to see if it could be done in one query
It can’t be done in single query
Aah ok
Either you select first, or do something like create a temp table
Both would mean extra queries still
Yeah I understand
Thank you ハーリさん (CF8) | ᓇᘏᗢ and Lufthansa-Pilot.
i have problems with my flask code
no one is a mind reader who's going to automatically know what your issue is
I fix it
I need some help. I want to make economy system to my discord bot using MongoDB(pymongo), should I store the data:
{"member_id": 480404983372709908, "wallet": 0, "bank": 0}
or something else? If I would use this method, how will I increase and decrease the wallet and bank?(and get it's data)
query = ({"member_id": 480404983372709908})
data = {"wallet": 2}
COLLECTION.update_one(query, data)```
Will that increase the wallet by two?
Has anyone used MotorEngine?
I'm trying it out and running into troubles, was hoping someone with experience could help get me rolling
if you'd remember, i had already told you yesterday - you use $inc to increment values
COLLECTION.update_one(query, {"$inc": {"wallet": 1}}) will increase the wallet value by 1
to decrement, just increment but with a negative value
query = ({"member_id": 480404983372709908})
data = {"$inc": {"wallet": 2}}
COLLECTION.update_one(query, data)```
anand do you know I made a warning system with MongoDB and on my first try it worked perfectly?
nice work
Yeah he really is the best I have seen. Even tho I am trash at some stuff he always help, and while helping me helps so many others.
I'm trying to follow the docs on https://aiomotorengine.readthedocs.io/en/latest/ but when I try to pip install aiomotorengine module is not found and when I try to install via their git it errors on importing requirements
That is very odd
how can i make an inventory system through aiosqlite?
and buying and selling and a shop tooo
anyone????
Your question is not detailed enough. What kind of inventory system?
You should start by writing down exactly what your system will need to do, and then model tables around that.
Or look at some examples online of similar things for that domain.
ok
i was confused in the amount to deduct, that if we said 8buy riflr, then to deduct is 2000, and when we 8buy apple then to 50, how to manage what amount to deduct?
leave it, i am making a discord bot
If apple and rifles are products, then you need to have a table that stores the cost of these. You would then look up the cost and deduct from someones balance.
main q is how to check how much amount to deduct?
i was also thinking of that
As a simple example, something like this:
Given the below tables
create table items (id integer, name varchar(255), price integer);
insert into items values (1, 'Apple', 100), (2, 'Phone', 1000);
create table accounts (id integer, username varchar(255), balance integer);
insert into accounts values (1, 'Adam', 500), (2, 'Luca', 500);
We can deduct the balance.
update accounts set balance = balance - (select price from items where name = 'Apple') where username = 'Luca';
That's just to give you the idea of how the query can look to update the balance. The tables will need more detail.
Tysm
umm, cant we put TEXT instead a VARCHAR(N)?
You can
whats the differnce?
Which database?
sqlite
Use Text, but no difference for SQLite
ok
It uses a dynamic type, so you could put random letters there and it would work
in text?
What in text
😂
Might be more of a Github question but:
Having a bit of a hard time with Git, and co-operating with classmates using a database within the git, anyone that's got experience of that and has a quick fix? Here's the error message:
After doing git status:
After doing git resto --source=HEAD :/:
@primal burrow this is weird, is there a file name with a space at the end??
also why are people storing binary databases in a git repo...
sigh
We're all beginners, so it's trial and error.
maybe the space at the end of the filename is throwing off something in the windows git program
do you actually have a .git directory?
it sounds like the clone failed part-way through, and the .git directory was incompletely created
i guess windows is allergic to the space at the end of the filename
ask your classmate to fix it
Also, what did you mean binary databases?
something that isn't a "text" file
(Just trying to get a grip of it, been playing around with SQLAlchemy)
i.e. something you can't open in a text editor and see what it is
git is really bad at storing those kinds of files. it's fine for small projects and/or data that doesn't change very often, but git does a very poor job of compressing/deduplicating that kind of data and it can make your repository really big
plus git doesn't know how to generate readable diffs from it
What would you suggest instead? For storing the database for a minimalistic project prototype like this one?
It's basically just enabling you to log in, and some relational tables aswell.
i dont think the database should be stored as part of the source code at all...
but maybe in this case it's fine as a workaround for sharing easily w/ the team
yeah, that was the idea! but I really appreciate you for the input mate!
It's all a learning process.
i havent specifically done it, but the pain level will depend on how fancy the database is
"just tables" of basic data types wont be hard
migrating indexes, constraints, etc. might be harder
json1 -> jsonb could be hairy
not to mention you might have to modify whatever code you use to interact with it, and maybe even the sql queries
its probably easier to go from sqlite to postgres than from postgres to sqlite
ya the data layer is being ripped out and replaced with an api
the db data is being migrated to pg behidn that api, i already recreated the schema
just need to transfer data
im enough of a hack that i'd just do it in python
thats what ive been thinking
if it's a lot of data, you can export from sqlite as csv or something like that and import to pg with their loader
write migration scripts in python
but you run into all the problems of csv
its not too much data
so again that'd only work with basic data types
its manageable
yeah then just do it in python
how can i get all the column's data from a database of sqlite?
SELECT name FROM Table;
then i will get all the inserted data from a column?
that will get all data from a certain column in a given table
thnx
you can "stream" the data if you want
cur_sqlite = db_sqlite.execute('select * from table1')
for row in cur_sqlite:
query = 'insert into table1 values ({})'.format(
','.join('%s' for _ in range(len(row)))
)
db_postgres.execute(query, row)
you can even use tqdm for a nice progress meter
it is too complex for my dumb brain🤯
@torn sphinx this was meant for @pulsar stag
sorry, till also
ohh thats nice
ya soemthing like that is what ill do, i have some slight data normalizations to do as well
so this will be nice
you can also load it all at once then write again with executemany
or use pandas even
pandas can write a dataframe directly to a sql database
probably not enough data to need that tbh, the most annoying part will be using pg in python ive never done that beofr
e
my api is entity frameowrk in c#
psycopg2 is a decent library
you wont have issues with it
all mature python database libraries loosely follow the same spec
await client.db.execute("CREATE TABLE IF NOT EXISTS items (id int, name text, cost int)") await client.db.execute("INSERT INTO items (1, apple, 50)")
is anything i am doing wrong here?
pls can you help me?
tysm
if i'm going to run an application where users can input their API token for another service (and save that in a database), what would be the best/"private" way to encrypt this information?
treat it like a password
i assumed that, but i have only been using werkzeug.generate_password_hash() and werkzeug.check_password_hash() before, and those doesn't let me "read" the actual API tokens when i need to use them.
exactly. any ideas on what libraries, methods etc i should look into? (haven't done api key storing before)
I have no idea
@brave bridge true, if you actually need to use the api token within your application then you have to store it in plain text somewhere. or encrypt it at rest then decrypt in your application, which could reduce some ways of leaking your customers data
an attacker that has access to your machine without you knowing could be reading the decrypted passwords in memory of your application, but in that case you're kind of fucked no matter what
i need to use it, that's the thing. that's why i think i'm going to encrypt it using some kind of cipher. i just wanted to confirm that this was actually a valid method to do things
it's what password managers like keepassxc do
your passwords are stored encrypted on disk, and the password database is decrypted when you open the application and need to read a password, using a master passphrase, yubikey, etc.
but this might be more of a #cybersecurity question
security always has 1000 edge cases
bump
okay thanks!
what database? sqlite? you can cast(blobcolumn as TEXT)
https://www.sqlite.org/datatype3.html
BLOB values are converted into TEXT values by simply interpreting the binary BLOB context as a text string in the current database encoding.
so as long as your database encoding is utf-8, casting to TEXT will "just work"
https://www.sqlite.org/lang_expr.html#castexpr the actual authoritative doc is here:
To cast a BLOB value to TEXT, the sequence of bytes that make up the BLOB is interpreted as text encoded using the database encoding.
yeah i get how to use CAST but im not sure how i can only cast one value inside a select which gets multiple values
im not sure what you mean
well in my example i only want the value z as TEXT and not cast the other values
SELECT x, y, cast(z as TEXT) z FROM location
this is just basic sql
nothing special here
oh well
i thought just having cast(z as TEXT) was enough
instead of another z behind
the z at the end is an alias for the column
otherwise sqlite might give it a funky name like "cast(z as TEXT)" which usually you don't want
if you aren't using the column names then you don't need the alias
again, this is basic sql syntax
worth reviewing it
if i were to be really really pedantic writing this query id do it like this ```sql
SELECT
loc.x AS x,
loc.y AS y,
cast(loc.z AS TEXT) AS z
FROM
location AS loc
Yeah im doing the whole thing to cast all values into a struct anyways and it does not like casting a blob to a String which corrupts it ^^
then the blob probably isn't utf-8 text?
what kind of struct? in python, or another language?
well its rust
why don't people just use text files instead of databases?
yeah that might be correct since sqlx is also erroring trying to cast it
you usually can't modify a text file in the middle
you have to read it, modify it in memory, then overwrite it
Is it advisable to run a MongoDB server on a Raspi?
I'm using a Raspi 3B+ to run a discord bot
monog_db_start_time = time.time()
PREFIXS = CLUSTER['Multi-purpose']['Prefixes']
full_dct = PREFIXS.find_one({"guild_id": ctx.guild.id})
monog_db_end_time = time.time()
mongo_reponse = round((monog_db_end_time - monog_db_start_time) * 1000)```
Is tat a good way to detect ms of database response?
Can you try assigning the execute function to a cursor variable, and then do print(cursor.rowcount)
I have created models in django
class Post(models.Model):
user = models.ForeignKey(User,on_delete=models.DO_NOTHING)
title = models.CharField(max_length=250)
content = models.TextField()
date_published = models.DateTimeField(default=timezone.now())
is_deleted = models.BooleanField(default=False)
is_edited = models.BooleanField(default=False)
edited_versions = models.JSONField()
def __str__(self):
return self.content
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.DO_NOTHING)
user = models.ForeignKey(User,on_delete=models.DO_NOTHING)
content = models.TextField()
date_published = models.DateTimeField(default=timezone.now())
is_deleted = models.BooleanField(default=False)
is_edited = models.BooleanField(default=False)
def __str__(self):
return self.content
should i handle edited_versions in JSONField?
or maybe i should use another table?
wich option is better
@surreal flame Not sure how you plan to exactly do it with the json field?
But the common approaches for this kind of thing i can think of on top of my head are 2.
- The first is to add a start and end column to the posts table, and add every revision here. With this method, each time you add a new entry to the table, you would close the previous post version by adding a end date. And, so the most current post's end date will always be null.
- The second is to create a posts history table.
@proven arrow Sounds great
but wich database to use in this case?
I would have thought you already had a database at this point?
Yes
Okay and the first method is more efficient i guess?
cant imagine how to code that
Both can be as good. Depends how you plan to access the data.
How is the revisions used? How often will you access them?
I dont know what facebook is like, dont use it
and i want to store previous version of content
So the user never sees it?
i'll present it probably in the future when i will do some analytics or someting
yep
If your application **always ** needs the history, then use the start/end date method, and keep the records in a single table (make sure to add the proper indexes too).
The second method is good when you mostly want the current post, and only occasionally want the historical posts.
ah ok
Here is the official term for it, if you want to read more. https://en.m.wikipedia.org/wiki/Tuple-versioning
Tuple-versioning (also called point-in-time) is a mechanism used in a relational database management system to store past states of a relation. Normally, only the current state is captured.
Using tuple-versioning techniques, typically two values for time are stored along with each tuple: a start time and an end time. These two values indicate ...
{"guild_id": 267624335836053506}
``` I store every doc in my collection like that. I want to makeserver settings, to set muted role, logs channel, and a suggestiones channel. How can I add it a key and value?
```py
query = {"guild_id": 267624335836053506}
data = {"$set": {"Muted": 822884733645488188}}
COLLECTION.update_one(query, data)
Will that work? And if yeah, how can I remove a key with out knowing it's value?
how can i remove _id field (mongodb)
{
"_id": {
"$oid": "608fffa22685c09e3eabe5b8"
},
"User_id": 811592090072645600,
"wallet": 115,
"bank": 0,
"xp": 39,
"level": 1
}
```this is how docs looks like i want to remove the _id
Why do you want to remove it?
i use User_id instead
why don't you just add a key named User_id
I'm new to MongoDB so take this with a grain of salt, but I don't even know if you can remove it
you're definitely not supposed to
is there a way to remove it?.. everything is working fine but i dont need it
oh ok
I mean, remove it as you would any other data field
I'm pretty sure it's the same command
i will try
I have a bit of a stupid question
How can I safely shut down the MongoDB server process
idk i started pymongo yesterday
Hey, i got this error from MOngoDB and have no ide ahow to resolve it properly, i can give code if needed
I tried to run db.adminCommand({shutdown: 1}) and it returned this error: ```Error: error doing query: failed: network error while attempting to run command 'shutdown' on host '127.0.0.1:27017' :
DB.prototype.runCommand@src/mongo/shell/db.js:169:19
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
I switched to the admin database before running the command
and the shell I'm using to access the database isn't letting me type anything
what do I do
I have created a barchart using a dataset. However, I want to colour the bars for every category that the subjects fit but im not too sure how to do this
What is wrong here? (pandas)
you can't put a "slice" inside a list like that
it's not like in R where : is a real operator
just write df.loc[:2, 'Hobbyist'] or df.loc[0:2, 'Hobbyist']
One thing I wanna ask about PGSQL that how to change the default username postgres to something other. In internet, I can't understand what they are doing.
Nah I am asking that how to change the default username
It is postgres
by default
When I installed PSQL. It was there by default
create new user
and give rights for the created db
to the new user
then you could use the new user for connecting to particular db
while leaving postgres as just root user
#help-grapes MongoDB(pymongo) help please?
Thanks Got it
My database reponse takes 500 ms, how can I fix it?
mongo_db_start_time = time.time()
PREFIXS = CLUSTER['Multi-purpose']['Prefixes']
full_dct = PREFIXS.find_one({"guild_id": ctx.guild.id})
mongo_db_end_time = time.time()
mongo_response = round((mongo_db_end_time - mongo_db_start_time) * 1000)```
This is my code for the response
How important codd rules is in DBMS?
insert_one needs to be awaited using motor?
it does
Important. But you can break them, if you need to and makes sense to do so.
ok
mongo_db_start_time = time.time()
PREFIXS = CLUSTER['Multi-purpose']['Prefixes']
full_dct = await PREFIXS.find_one({"guild_id": ctx.guild.id})
mongo_db_end_time = time.time()
mongo_response = round((mongo_db_end_time - mongo_db_start_time) * 1000)```
I am testing the db response, and it doesn't seem to be ok. One second it shows me 767ms, and the other 76ms
I think here the constraint is your own connection speed
Wdym?
Are you running a mongodb instance locally?
Is it on your own machine, or is it mongodb atlas i.e on the cloud
yes, so (i'm guessing here) your internet connection might be the culprit here
Oh ok, I use VSCode and not host yet
sometime it's slow, giving you high latency (i don't know the technical terms here or how the data is transferred but this is my guess)
Hi all```
Not needed, but it was to see if the database was affected or not to debug
hi
Hi all, I want to populate a database with the user defined target prices; then I want to check the current price against the target prices at regular intervals and if appropriate notify the user. Atm I populate the target price on firebase (see screenshot below), however each time the data is overwritten. Please note the 'sign' key of the dictionary relates to more than the target price ('m') or less than ('l') the target price.
Do you know how best to add further target prices, without overwriting the previous data? Perhaps using an SQL table would be simpler?
Also see code next to red line in snippet for how I send data to firebase below.
Thanks in advance! (crypto currency price alerts app using kivy)
Make sure to update the deepest child
And Sql would probably be easier for this as well, and probably cheaper in pricing compared to firebase unless your on the free tier.
if user:
old = {
"wallet": before1,
"xp": before2
}
newdata = {
"$inc": {
"wallet": random.randint(20, 60),
"xp": random.randint(0, 80),
"life": -random.randint(0, 80) #is this going to work to decrease life?
}
}
i'm looking to store future dates and times in a record. what is the best way to have a "trigger" when these individuals records are now() without constantly running a query?
You’d have to have some sort of event, or background job. I’m doing similar thing now, and for this we just call a command in our app, that checks if the time has passed.
@proven arrow Could async be used for something like this or is that sub optimal?
If your app is async and you want to handle this stuff inside your app without blocking then use that.
However, the way i did it essentially runs a cron job on the server, every minute.
@proven arrow I will probably do a cron job as well just so I don't have a million background tasks running waiting for time to expire
hello everyone, i try to make sql alchemy query but i dont know how to do it , can someone help me (with voice call , i will share my screen ) .
currently my query looks like this, but i dont know how to make a join on another table
qry = db.session.query(func.sum(poap.amount).label("sum_payments_terms_amounts"),poap.no) qry = qry.group_by(poap.no)
i have 2 tables (pos, poaps)
in POS [no, cntno]
in POAPS [ no,amount]
how to make sql achemy request to print the cntno and the amount ?
this is the curent result :
can someone help pls?
i'm stuck
how'd u do the colour thing
MongoDB(motor) is sooooooooo cool
Please help me how to fix this error?
Traceback (most recent call last):
File "C:\Users\Администратор\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Администратор\Desktop\MainServerBot\bot.py", line 33, in on_ready
cursor.execute(f"INSERT INTO users VALUES ('{member.name}', {member.id}, '<@{member.id}>', 100, '[]', 0, 0)")
sqlite3.OperationalError: table users has 6 columns but 7 values were supplied```
Your column count doesn't match the number of values you entered
as hhhhhhhh said, check how many values the column has.
- it's always helpful to name the columns you're inserting into:
INSERT INTO users (name, id, handle, foo, bar, baz, boom) VALUES (...);
it makes it easier to read the queries, because it's clear what parameters you're passing.
So if you fill out the column names, the mistake will be clear
- You should never format queries like that. This is prone to SQL injection: if someone chooses
',1,'',1,'',0,0); DROP TABLE users; --as their username, you're in trouble.
You should use built-in tools to format queries, something like this:
cursor.execute("INSERT INTO users (id, first_name, last_name) VALUES (?, ?, ?)", (42, "Hello", "world"))
(read the documentation for your database adapter for more info on how to do that)
<#databases message>
Guys, I am using motor(MongoDB) and I em experiencing some issues with speed. It takes a lot of time for it to response. Anyway I can fix that?
@jaunty galleon is your mongo server slow? is your internet connection slow? are you executing big aggregation queries and/or fetching a lot of data?
Anyone here worked with SQL Views? If I wanted to alter the view do I have to recreate it?
The mongo server is slow yeah, my ijternet is fine, and I am only extracting one user data for economy using find_one
try running mongodb locally
hola
i wud like some help
i run a discord bot hosted on heroku cuz i cant spend money lol
so i have dictionaries in json files
and heroku wipes them every restart i do to update the bot
someone told me to use a database that shouldnt be local for that
but i have no experience so can somebody help me in guiding how to using a database that doesnt wipe for my discord bot?
😅
But a) I already made the code for the atlas
b) I want to host it
c) I want to see all collectiones
a) there would be no code changes other than changing the database URI when you're connecting
b) ? You can run mongodb yourself on your VPS
c) What makes you think you can't see collections?
I can see collectiones from the Atlas Website?
And it's just changing url?
Even if I am using motor? And how does this work exactly, how can I et up this local database
Hi @quartz sleet
?
not from the atlas site, because it won't be....atlas
do you mean you want a GUI to view the data?
that's what mongodb compass is
oh compass I downloaded that but had no idea what that is
And what will I change in my code in order to make local host that transmits to the compass?
?
what?
you'd just change the database URI
i don't understand what you mean by "transmits to the compass"
apperently compass is analog of PhpMyAdmin
To what url?
I need to download compass?
That?
that's just compass
compass isn't a database
compass is just a GUI to view the database
It's to view data
the first link has guides to install mongodb
the second one has guides for connecting to mongodb from
- compass
- python
the python examples probably use pymongo but it'll be the exact same thing in motor
oh actually the second link has an install guide as well
Ok
Should I choose MongoDB as service or MongoDB?
which link is that in?
huh, i don't remember picking anything like that 🤔
let me see
oh that's your choice
picking "as a service" will be more convenient
as i understand it, the db will start up automatically if you pick that option
that site explains it:
If you only installed the executables and did not install MongoDB as a Windows service, you must manually start the MongoDB instance.
Oh ok
so service it is
"C:\Program Files\MongoDB\Server\4.4\bin\mongo.exe"
Where should I put this? cmd? wat does it do?
it runs mongo.exe
i think that starts up the mongodb shell, in which you can directly interact with the database
you can add that to your PATH so that you can just run it with mongo as well
Wait
I have this
It opened automatically
Should I create a cluster?
And I didn't ran C:\Program Files\MongoDB\Server\4.4\bin\mongo.exe in the cmd yet
what happens when you just press the connect button?
it's been a long time since i've used mongodb, and then i didn't really ever use compass
This happend
i think by default it should connect you to your local database
yep so that's still a thing nice
basically you've connected to your local mongodb instance now
created database?
you can create a database if you want and try connecting to it via python
And how will I use it with host service?
you can install it on your VPS like you just did
there's an install guide for linux as well on the site i'd sent you earlier
I use something called sweplox if you know what that is
never heard of it
It is a fre host, there are paid versions that dyno, rythm and more bot uses it
And I think is it running on linux
So what url should I put in motor.AsyncIOMotorClient ?
i want to learn aiosqlite any docs ?
!pypi aiosqlite
There might be docs there
ohk
again; the link i'd sent has a tool that will help you form your database uri
So I am done with the download right?
it generally looks something like mongodb://username:password@host:port
yes
I am trying to followe the guide but it says username, and I can't understand how I am getting the username
I can see there is something says: HOST localhost:27017
How can I even set username and password? I have the username and password of the CLUSTER in the Atlas MongoDB website
if you haven't set a username and password, i believe the URI will be something like mongodb://localhost:27017/
Let me check
import motor.mototr_asyncio as motor
CLUSTER = motor.AsyncIOMotorClient("mongodb://localhost:27017/")
TEST_DATABASE = CLUSTER['Multi-purpose']['Prefixes']
@bot.command()
async def test(ctx):
await TEST_DATABASE.insert_one({"guild_id": "blah_test"})
``` I will try that
🤔
What's wrong?
you aren't making a cluster here though
the cluster would be a thing on atlas
here you're directly making a db
notice how in the compass screenshot you'd sent, it says "cluster: standalone"
So I will have to change some code with the url?
if you want your database to be called "Multi-purpose", you'd do
client = motor.AsyncIOMotorClient("mongodb://localhost:27017/")
db = client["Multi-purpose"]
``` and proceed as you had done earlier
I already opened a database named Multi-purpose and a collection there of Prefixes
right
Can't I keep it the same? CLUSTER is just a varname I think
I mean, I assign CLUSTER to a value
yeah, it'll work, it'll insert into the prefixes collection alright
print(player_id, ret) -> 404459661321043969 []
shouldn't it give me the contents of the 404459661321043969 id entry
fields :
fields = ['id int PRIMARY KEY',
'name text NOT NULL',
'deck blob NOT NULL',
'character text NOT NULL',
'wins int NOT NULL',
'losses int NOT NULL',
'elo int NOT NULL',
'new int NOT NULL',
'pickled text NOT NULL']
those variable names are misleading (for NIR)
Oh ok, so I'll call it client and db
You are right
(ret is the result from cursor.fetchall())
Instead of:
CLUSTER = motor.AsyncIOMotorClient("mongodb://localhost:27017/")
PREFIXES = CLUSTER['Multi-purpose']['Prefixes']
It'll be:
client = motor.AsyncIOMotorClient("mongodb://localhost:27017/")
db = client['Multi-purpose']
PREFIXES = db['Prefixes']``` Right?
yeah
fyi, using atlas caused 700, now sometimes I have 6 for it's response
latency? what are these numbers?
mongo_db_start_time = time.time()
PREFIXS = db['Prefixes']
full_dct = await PREFIXS.find_one({"guild_id": ctx.guild.id})
mongo_db_end_time = time.time()
mongo_response = round((mongo_db_end_time - mongo_db_start_time) * 1000)```
Yeah ms
Before that, mongo_response was 700, now it is sometimes 6
🤷♂️ pretty obvious the connection was the culprit then
Which statement used to access function of a package in main program?
Umm could u share a example, and I don’t think this is related to databases
Yeah, now I gotta figure out how to upload it or I don't even know to te host
In PL/SQL
line 1, in <module>
from PyQt5 import QtCore, QtGui, QtWidgets
ModuleNotFoundError: No module named 'PyQt5'
help
uhm, install pyqt5
Traceback (most recent call last):
File "C:\Users\Milda\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\Milda\Desktop\A.M.E\bot.py", line 44, in ping1
cluster = MongoClient(mongo_url)
File "C:\Users\Milda\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\mongo_client.py", line 639, in __init__
res = uri_parser.parse_uri(
File "C:\Users\Milda\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\uri_parser.py", line 428, in parse_uri
raise ConfigurationError('The "dnspython" module must be '
pymongo.errors.ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Milda\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Milda\Desktop\A.M.E\bot.py", line 39, in on_command_error
raise error
File "C:\Users\Milda\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Milda\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Milda\AppData\Local\Programs\Python\Python39\lib\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: ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs``` anyone know what caused this error?
here is my code: ```@client.command()
async def ping1(ctx):
mongo_url = "mongodb+srv://Milda:<password>@amebot.yj6hu.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
cluster = MongoClient(mongo_url)
db = cluster["Discord"]
collection = db["AmeBot"]
ping_cm = {"command":1}
collection.insert_one(ping_cm)
await ctx.channel.send("ping has been registred")```
Hey @lapis crane!
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:
Thank you so much man :)))
What is best datatype to store hex color in postgresql?
It is also using SQL lang, but it have much more stuff and is general it is better database, so start would be difficult.
No, database syntax is same, but it have more stuff
You can host this database on your pc
just connect via localhost
So anybody know?
well, i have the database set up, and have followed a basic turtorial about using it, so i know it works
sort of
Ok, so if you enter a function, it will be able to get two argument, bot and message
bot(You know what it is) and message is the place where the command invoked
def get_prefix(bot, message):
So now, you want to get the prefix, but you don't want to open the db every single time, so make a global variable named loaded_prefixes @lapis crane
i am very confused 😅
loaded_prefixes = {}
def get_prefix(bot, mesage):
You know what global variable is?
No
It is a variable you don't set in a function, and you can use it and change it after doing global variabe_name
st = "hi"
def foo():
print(st)
And you can't:
def foo():
st = "hi"
print(st)```
Yes
okay
?
loaded_prefixes - {}
def get_prefix(bot, message):
global loaded_prefixes
Now we have full access to it in order to edit.
what next? you want to check if the guild id is in loaded_prefixes
loaded_prefixes = {}
def get_prefix(bot: commands.Bot, message: discord.Message):
global loaded_prefixes
if str(message.guild.id) not in loaded_prefixes:
#Now because the id is not there, load the prefix from the database, and set it to a variable named prefix
#After getting prefix, insert the prefix to loaded_prefixes
return [loaded_prefixes[str(message.guild.id)]]```
And when I say insert I mean loaded_prefixes[str(message.guild.id)] == prefix
What didn't you understand?
i think i understand, we're making a check?
sort of, a check?
sort of when it comes to finding the prefix
should i put that in my code?
If you understand how, yes
i think i understand
how do we create the command, though?
or well
Is psyscop3 stable enough yet does anyone know? I would be using it in production btw.
The command, insert the prefix to the db, global loaded_prefixes and out it there as well
i'm a little confused
there?
like the command prefix? for example !prefix ?
in ""?
I want a DB with python support, which has fairly simple syntax,
like this?
No...
my bad, sorry
I don't really know what's best. I want to store user preferences for a discord bot - which would i use?
? 🤔
loaded_prefixes should be empty
oh
Not right now, but I want to develop it in such a way that it could be
You are using it so it won't opn the db every single time
Show me your current get_prefix function @lapis crane
You should async it btw if you are using motor
Is it free to use?
yeah
get_prefix function? you mean the prefix of the bot or the prefix i'm gonna be making in the command?
Thank you I'll look into it
I meant what I just helped you do
the function
Me?
I am helping Milda but you can ask me if you'd like
Where is the entire getting the prefi x from the db?
Add it to the code, and here is what you should do in the set-prefix command:
a) Get the current loaded_prefixes using global
b) dp loaded_prefixes[str(ctx.guild.id)] = prefix
c) update the database prefix
@velvet ridge What you need help with in the meantime?
could you explain making that a little more please? i'm pretty confused
Sure, you got any idea how to start? Send in #discord-bots what yo made this far/
do you mean like this? (this is another command using mongodb)
?
@jaunty galleon
Yeah but put the prefixes in the format you'd like
kinda like this right?
that is a bad idea
oh?
the get_prefix function is called every time a message is sent
you shouldn't be making a database connection in there
a database connection is an expensive operation
you should do it only once at your application startup and then use that connection throughout
how should i do that?
how do you think that would work?
did you understand the problem that i said here?
Ye, but i dont understand how to counter the problem
just connect to the database outside the function?
sorry i'm really new to creating databases and stuff, do you know a place where i can read about connecting to databases and such? i don't wanna bother you and NIR to much
mongodb's own documentation has some good tutorials for connecting to the database
thanks
in there there's a link to their free course
alright, thank you!
Oh anand I have a question
Using pymongo, how can I add a key and value to the auto_triggers value?
I tried:
data = {"$addFields": {f"auto_triggers.{trigger_name}": response.content}}
await SERVER_SETTINGS.update_one(query, data)```
And it doesn't work.
error:
https://paste.pythondiscord.com/wukaxebulo.sql
$addFields is a thing?
hey guys, I'm making a messaging webapp, I want to add the messaging functionality, how should I do this and is this is based on database only or javascript only?
wouldn't it just be a $set
Yeah but how tho
I want to add a key and value to the vaulue of auto_triggers
yeah, just change the addFields to set
i don't really see why you're using addFields there
i think that's a step in an aggregation pipeline or something, i don't remember
anyone?
wtffffff
I tried connecting to Lighsail database for like 3 hours with psycopg2
it worked fine the first time in pgAdmin
searched all over the place
all arguments the same
turned out I was using placeholder text in my function....
not the passed argument
how do I select data from two tables at once in SQL Server?
this is what I want to do: I have created the following tables:
IF OBJECT_ID('Students', 'U') IS NULL
CREATE TABLE Students (
StudentID varchar(8000),
FamilyID varchar(8000),
FirstName varchar(8000),
LastName varchar(8000),
Homeroom varchar(255),
StudentEmail varchar(8000),
KioskPersonalMessage varchar(8000),
KioskMessageExpiryDate datetime2(0),
);
IF OBJECT_ID('AttendanceLog', 'U') IS NULL
CREATE TABLE AttendanceLog (
LogTime datetime2(2),
StudentID varchar(8000),
ScanLocation varchar(8000),
ScanType tinyint,
/*
Scan Types:
0 - Sign In
1 - Sign Out
2 - Excused Absence
*/
);
and I want to select the data from the AttendanceLog table plus the FirstName and LastName from the Students table that corresponds to the StudentID of that row in the AttendanceLog table
My bot got really slow after i added db based commands. And i wonder whats causing it.
context manager?
A context manager is when you use stuff like
with or async with
!with
The with keyword triggers a context manager. Context managers automatically set up and take down data connections, or any other kind of object that implements the magic methods __enter__ and __exit__.
with open("test.txt", "r") as file:
do_things(file)
The above code automatically closes file when the with block exits, so you never have to manually do a file.close(). Most connection types, including file readers and database connections, support this.
For more information, read the official docs, watch Corey Schafer's context manager video, or see PEP 343.
That's a better explanationish
@jaunty galleon im not using compass, but I have my mongodb on localhost
and yes im using motor
If you read the aiosqlite docs you can see that you can use the library in a procedural way
Oh ok, you have a host where you can upload files right?
How did you upload whatever is needed for localhost?
i see
you know when your connecting to a database with motor right?
just change to url to something like mongodb://localhost:27016 iirc
i might be wrong
Oh wait you're using asqlite
ye
I have client = motor.AsyncIOMotorClient("mongodb://localhost:27017/")
Do I need to upload something tho?
you would need to make the collections and db or whatever
if you were writing to collections already you would need to make those cause they dont exist on the localhost
@thorn geode i also keep getting this error
ConnectionResetError: Cannot write to closing transport
I thought you meant aiosqlite, I'll ask danny about using it procedurally.
ohk
Are you running database code outside of the context manager?
nope
hmmm
Task exception was never retrieved
future: <Task finished name='Task-124' coro=<ConnectionState.chunk_guild() done, defined at C:\Python39\lib\site-packages\discord\state.py:830> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\discord\state.py", line 835, in chunk_guild
await self.chunker(guild.id, nonce=request.nonce)
File "C:\Python39\lib\site-packages\discord\state.py", line 1147, in chunker
await ws.request_chunks(guild_id, query=query, limit=limit, presences=presences, nonce=nonce)
File "C:\Python39\lib\site-packages\discord\gateway.py", line 654, in request_chunks
await self.send_as_json(payload)
File "C:\Python39\lib\site-packages\discord\gateway.py", line 591, in send_as_json
await self.send(utils.to_json(data))
File "C:\Python39\lib\site-packages\discord\gateway.py", line 587, in send
await self.socket.send_str(data)
File "C:\Python39\lib\site-packages\aiohttp\client_ws.py", line 150, in send_str
await self._writer.send(data, binary=False, compress=compress)
File "C:\Python39\lib\site-packages\aiohttp\http_websocket.py", line 687, in send
await self._send_frame(message, WSMsgType.TEXT, compress)
File "C:\Python39\lib\site-packages\aiohttp\http_websocket.py", line 598, in _send_frame
raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
``` heres the full error
I have collectiones I can see in compass, I created it there. My question is do I need to upload something to the host so it would recongnize where to connect to? Or it is automatically updated my compass data
ok
Are the collections you made on compass on your own computer or is this hosted on their website?
It is not on Atlas, it is CLUSTER: Standalone
I once did it on their web tho
@jaunty galleon go to the file where your mongodb is like here
then click on mongod, then click mongo
in the terminal type show dbs;
do you see the database youve made?
so what do you see when you run show dbs;
Where? Which one of 'em should I launch?
first run double click mongod to start the service then run mongo
it should open a terminal, once your there type in show dbs;
they'd installed mongodb as a service so mongod should be running
Multi-purpose is my database yeah
ah perfect, so this means all you need to do is change your mongodb url to the localhost thing and you will be automatically writing to this database
they've already done this
im not the best with databases so ¯_(ツ)_/¯
i think? because i guided them through it in the morning 😔
ive always had to start mongod manually
I am already using client = motor.AsyncIOMotorClient("mongodb://localhost:27017/")
yeah, you have to do that if you don't install it as a service
huh, then what were you asking firsty?
If I need to upload something to my host
you dont need to change anything
Where I want to host my bot
you just install mongodb on your VPS as well?
the same way you installed it on your computer
yep
or well not exactly the same, it'd depend on your VPS
I think it's os is linux
I recently integrated my mongodb with my ubuntu
it was easy
just sudo apt install mongo
make ur db and collections
and ur set
does it auto write?
My host accepts downloading packages if that's what you guys mean
if you try accessing a collection / db that doesn't exist, mongo will just make it on the go
neat i had no idea
databases arent my strong suit 
so all you would really need to do is sudo apt mongo or maybe mongodb then just run ur code normally
it seems like
seems like pterodactyl
