#databases

1 messages · Page 147 of 1

flint imp
#

you will need to await your queries and the find() method will need to be passed to list to_list(length=...) or you can iterate thru it

Otherwise its all the same

jaunty galleon
#

Yeah I'll stay with that even tho it's for discord bot

proven arrow
#

Theoretically yes.

#

You would want atomic values, and if the list can be divided then it would break this.

burnt turret
#

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)

jaunty galleon
#

nd sure if you can why not

burnt turret
#

Yes motor is just the async driver for mongodb

#

All the same concepts

jaunty galleon
#
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??

burnt turret
inland imp
#

Glad I make my own class to handle MongoClient, switching to motor will be easier sueur

torn sphinx
#

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
burnt turret
#

Guild_id is an indefined variable there

torn sphinx
#

wdym

jaunty galleon
#

So just addin await ?

burnt turret
#

mostly yeah

burnt turret
# torn sphinx wdym

exactly what I said pithink

        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
torn sphinx
#

oops

#

so just quotes needed?

burnt turret
#

dunno, try it and see

torn sphinx
#

k

jaunty galleon
#

How can I edit existing data?
for example:
collection:
doc_a:
{"guild_id": "267624335836053506", "blah": 0}
How do I add two for 0?

torn sphinx
#

update

#

its in the link in your dm

jaunty galleon
#

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)``` ?
torn sphinx
#

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

jaunty galleon
#

Wait so what code will work?

burnt turret
burnt turret
#

you can update without having to first retrieve the old value

finite lynx
jaunty galleon
#

I can't quite understand it tho

burnt turret
#

where is this example from?
i'd made a mistake, what you'd want is $inc

proven arrow
burnt turret
#

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

inland imp
#

MongoDB doc is 👌

jaunty galleon
#
collection:
  doc_a
    "guild_id": {"267624335836053506": {"480404983372709908": "wallet": 0, "bank": 0 }}

await collection.update("$inc": "267624335836053506.480404983372709908.wallet": +2)

#

Like that?

burnt turret
burnt turret
jaunty galleon
jaunty galleon
burnt turret
#

because I see some ID again as a key, which i'd already advised against earlier

torn sphinx
#

{
"Guild_id": "<type>",
"prefix": "<type2>"
}
what should be the tip of each one

#

is it something like... int, string etc...??

inland imp
#

int64 and string

#

ids are too big to be stored as int32

torn sphinx
#

ok

inland imp
#

But types are automatic when you insert data with pymongo

jaunty galleon
#
collection:
  doc_a
    {"guild_id": "267624335836053506", economy: {"480404983372709908": "wallet": 0, "bank": 0 }}
#

There

#

how can I increase or decrease the 480404983372709908 wallet ?

burnt turret
#

now the key in the economy dict is a user ID?

jaunty galleon
#

Yeah

burnt turret
#

how do you plan on adding more users there?

jaunty galleon
#

Get the economy dict and update it?

burnt turret
#

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 }

jaunty galleon
#

Oh yeah

#

sorry

burnt turret
#

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 :/

jaunty galleon
#

oh ok

torn sphinx
#
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 ._.
jaunty galleon
#

It should

#

Can anyone help me understand how to update a doc?

inland imp
#

collection.update(query, data) where:

  • query works as a filter
  • data is the data to change
jaunty galleon
#

to change? how can I say what change?

inland imp
#

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

jaunty galleon
#

But I don't know exactly the content I am changing

inland imp
#

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

jaunty galleon
#

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?

burnt turret
#

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

jaunty galleon
#
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:

inland imp
#

query is just a filter, it doen't tell the key to modify

jaunty galleon
#

data = {"$set": {"wallet": +2}}

inland imp
#

In your case query is a little more complex due to how you structured the document

jaunty galleon
#

Is there a better way?

inland imp
#

There are multiple ways

jaunty galleon
#

such as?

#

Maybe the key as guild id and economy as value?

jolly surge
#

You guys are using pycharm or idle?

jaunty galleon
#

{"267624335836053506": "economy": {members and that}}

jaunty galleon
inland imp
#

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

torn sphinx
inland imp
#

You could also make a single wallet which is an array

#

Like this

{
    user_id: 201674460393242624
    wallet: [
        {guild_id: 267624335836053506, 'bank': 0, 'money': 0}
    ]
}
burnt turret
#

back when i used to use mongodb and had an economy setup, i had a single "users" collection

burnt turret
#

i'd just make another document if the same user was in multiple guilds

inland imp
#

That's the third solution I was thinking about

inland imp
burnt turret
#

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

jaunty galleon
#

I think I'll go with collection per guild

burnt turret
#

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)

jaunty galleon
#

I wish there was a server in discord for MongoDB

inland imp
#

MongoDB arrays are amazing though

#

There's a lot of handy operators to handle them

burnt turret
#

yeah, there are some useful operators

#

array filters are rather convenient

jaunty galleon
#

How can I make a collection per-guild? Just do CLUSTER["FirstDatabase"][ctx.guild.id]

#

Or does it have to string?

burnt turret
#

although i just told you that collection per guild isn't the best idea pithink

jaunty galleon
#

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}']

burnt turret
#

as I said, i don't think you can make an unlimited number of collections per database

jaunty galleon
#

Oh

burnt turret
#

you can have an unlimited number of documents per collection

jaunty galleon
#

So is there a better idea?

burnt turret
#

i'd probably just go with the data model i had suggested earlier

jaunty galleon
burnt turret
#

how so?

jaunty galleon
#

Or maybe I'll just make a multi-server economy system and that's it

inland imp
#

guild_id is a key, not a specific value (like user_id)

#

.find_one({'user_id': ctx.author.id, 'guild_id': ctx.guild.id})

jaunty galleon
#

Yeah I'll do a multi-server one

#

probably gonna hit me doing warning system

burnt turret
#

why? 🤔

jaunty galleon
#

I am trying to di it tho

#
{"480404983372709908": {"wallet": 0, "bank": 0}, "267624335836053506": 267624335836053506}```
#

Is this the format

burnt turret
#

for what?

#

i'd already told you not to keep IDs as keys

jaunty galleon
#

You way

burnt turret
#

this isn't my way?

jaunty galleon
#

But

#

You said something that do Users collection

#

and have the guild there

#

with the user

burnt turret
#

yeah i did

burnt turret
#

yes, what are the keys i have set there?

jaunty galleon
#

oh

burnt turret
#

yes

#

the key is "user_id", and it's value being the actual ID

jaunty galleon
#

But where will I put the wallet and bank?

burnt turret
#

just add more keys?

jaunty galleon
#

{"user_id": 480404983372709908, "guild_id": 267624335836053506, "wallet": 0, "bank": 0}

#

Like that?

burnt turret
#

sure, that can work

jaunty galleon
#

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?

#

?

inland imp
#

The operator you're looking for is $inc

jaunty galleon
#

query = {"user_id": 480404983372709908, "guild_id": 267624335836053506}
data = {'$inc': {'wallet': +2}}
collection.update(query, data)

#

?

inland imp
#

Without the +

#

But yes

jaunty galleon
#

And if I want to remove 2?

inland imp
#

-2

jaunty galleon
#

oh ok

#

It's time to make a new bot

#

yay

dense barn
#

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
plucky wave
#

I want to get into discord.py databases?

  • I need a brief description of what a database does?
pure sleet
charred arch
#
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

jaunty galleon
#

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?

jaunty galleon
#

Really?

quick bloom
#

Yeah, should work fine

jaunty galleon
#

Oh wow

#

And it will automatically update right?

quick bloom
#

It'll update it whenever python reaches that line yes

jaunty galleon
#

Ok

quick bloom
# jaunty galleon Ok

Although I'm not sure on $inc since it depends on your specific case. If it doesn't work change it to $set

jaunty galleon
#

anand said to use inc

#

I'll try it

quick bloom
#

It depends on the way you're using it I guess. It should work but if not, just try $set

jaunty galleon
#
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 ?
delicate fieldBOT
#

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:

https://paste.pythondiscord.com

safe hare
#

Guys iam using flask and sqlalchemy, and i want to do a join with 3 diferents tablas, does someone know how to do it?

burnt turret
#

You use set to change the value

burnt turret
#

The error tells you what's wrong

#

{a, b} is a set
{a: b} is a dict

jaunty galleon
#

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)

quick bloom
#

But yeah use insert_one

jaunty galleon
#

god works on mysterious ways

#

jk you are a god

quick bloom
jaunty galleon
distant edge
#

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.

north ridge
#
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?

torn sphinx
#
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": "//",
}

jaunty galleon
#

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": , }

jaunty galleon
torn sphinx
#

u can see the ";" ...

jaunty galleon
#

Can mongodb store None?

proven arrow
#

it can but you need to escape it. The following will work, CREATE TABLE [{player.id}LOG] (ELOchange float, curELO float, lognote VARCHAR(99))

proven arrow
north ridge
#

Btw, is this your new account? What happened to your old account? @proven arrow

proven arrow
#

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.

proven arrow
#

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.

north ridge
#

Then I will make one table for the logs 😄

torn sphinx
#

how can i delete a whole doc if i only know a value in it

#

in mongo

#

no need i got it

frozen fossil
#

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?

stark nest
#

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.

hexed estuary
jaunty galleon
#

{"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?

burnt turret
torn sphinx
#

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?

elder vessel
#

how to use AIOSQLITE

torn sphinx
elder vessel
#

IK how to use SQLITE

#

but IDK how to use AIOSQLITE

torn sphinx
#

pls can you tell me

#

its same

elder vessel
#

yea IK everyone says that

#

but i didn't get where to put await

torn sphinx
#

leave that

elder vessel
torn sphinx
#

how would you store many text in one column

torn sphinx
elder vessel
#

a column for book etc. etc

torn sphinx
#

i was thinking that only! thanks for confirming me

elder vessel
torn sphinx
#

yah

elder vessel
#

like no. of items per user

torn sphinx
#

umm, so one table for every user

#

?

elder vessel
#

nooo

#

i table for inventory

torn sphinx
#

ok ok

#

i understood

elder vessel
#

a column named user and etc ,etc

torn sphinx
#

yes, tysm

elder vessel
#

NVm

#

can you DM me a code of basic data storing ,updating and retrieving @torn sphinx

#

not much advance level just basic

torn sphinx
#

of a aiosqlite? i am making it for my discord bot

elder vessel
#

like how to store data in a table in aiosqlite and etc etc

torn sphinx
#

ohh

#

wait

jaunty galleon
#

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?
burnt turret
#

I'm on mobile, I couldn't read the first one

jaunty galleon
#

Even tho Muted_role is not there yet?

burnt turret
#

Yep

#

update queries update documents , and adding a new key to a document is also just updating it so yeah

jaunty galleon
#

{"guild_id": 267624335836053506, "Muted_role": 822884733645488188}
How will I remove the Muted_role ?

jaunty galleon
#

?

warm rain
#

How do I reset postgre's main acc password

calm prawn
#

Mongo db vs Postgresql
Which is better and what should I use for my bot?

brazen charm
#

PostgreSQL pretty much beats mongo in just about everything other than simplicity of scaling clusters and beginner friendlyness

bitter bone
#

@warm rain,

lol

brazen charm
#

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

warm rain
brazen charm
bitter bone
#

is written by CF8

warm rain
calm prawn
#

thanks

flint wadi
#

Any tips for deciding which relationship loading techniques to use, lazy/eager/no load?(should a beginner worry about such things?)

cyan yacht
#

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?

brazen charm
burnt turret
proven arrow
burnt turret
#

ah I'm not sure then

cyan yacht
# burnt turret 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

proven arrow
#

It can’t be done in single query

cyan yacht
#

Aah ok

proven arrow
#

Either you select first, or do something like create a temp table

#

Both would mean extra queries still

cyan yacht
#

Yeah I understand

flint wadi
#

Thank you ハーリさん (CF8) | ᓇᘏᗢ and Lufthansa-Pilot.

keen moat
#

i have problems with my flask code

pure sleet
keen moat
#

I fix it

jaunty galleon
#

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?
raven trail
#

Has anyone used MotorEngine?

#

I'm trying it out and running into troubles, was hoping someone with experience could help get me rolling

burnt turret
#

COLLECTION.update_one(query, {"$inc": {"wallet": 1}}) will increase the wallet value by 1

#

to decrement, just increment but with a negative value

jaunty galleon
#
query = ({"member_id": 480404983372709908})
data = {"$inc": {"wallet": 2}}
COLLECTION.update_one(query, data)```
burnt turret
#

yes

#

the parentheses around the query dict does nothing btw

jaunty galleon
#

anand do you know I made a warning system with MongoDB and on my first try it worked perfectly?

burnt turret
#

nice work

jaunty galleon
#

Very nice helping

#

Really couldn't do it with out you

raven trail
#

Anand I wanted to say you are awesome

#

thanks for all you do

jaunty galleon
#

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.

lilac shard
#

This is sh8t im stuck my computer goes BootMGR is compressed

#

Idk how to fix it

raven trail
jaunty galleon
#

That is very odd

torn sphinx
#

how can i make an inventory system through aiosqlite?

#

and buying and selling and a shop tooo

#

anyone????

proven arrow
#

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.

torn sphinx
#

ok

torn sphinx
proven arrow
#

huh

#

what is 8buy riflr and apple?

torn sphinx
#

leave it, i am making a discord bot

proven arrow
#

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.

torn sphinx
#

main q is how to check how much amount to deduct?

proven arrow
# torn sphinx 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.

torn sphinx
#

Tysm

torn sphinx
proven arrow
torn sphinx
proven arrow
#

Which database?

torn sphinx
#

sqlite

proven arrow
#

Use Text, but no difference for SQLite

torn sphinx
#

ok

proven arrow
#

It uses a dynamic type, so you could put random letters there and it would work

torn sphinx
#

in text?

proven arrow
#

What in text

torn sphinx
#

leave it

#

btw tysm, enjoy playing GTA V😋

proven arrow
#

😂

primal burrow
#

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 :/:

harsh pulsar
#

@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

primal burrow
#

We're all beginners, so it's trial and error.

harsh pulsar
#

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

primal burrow
#

Yep, you're 100% right.

harsh pulsar
#

i guess windows is allergic to the space at the end of the filename

#

ask your classmate to fix it

primal burrow
#

Also, what did you mean binary databases?

harsh pulsar
#

something that isn't a "text" file

primal burrow
#

(Just trying to get a grip of it, been playing around with SQLAlchemy)

harsh pulsar
#

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

primal burrow
#

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.

harsh pulsar
#

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

primal burrow
#

yeah, that was the idea! but I really appreciate you for the input mate!

#

It's all a learning process.

pulsar stag
#

has anyone migrated a sqlite db to pg before?

#

how painful was the processs

harsh pulsar
#

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

pulsar stag
#

the db data is being migrated to pg behidn that api, i already recreated the schema

#

just need to transfer data

harsh pulsar
#

im enough of a hack that i'd just do it in python

pulsar stag
#

thats what ive been thinking

harsh pulsar
#

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

pulsar stag
#

write migration scripts in python

harsh pulsar
#

but you run into all the problems of csv

pulsar stag
#

its not too much data

harsh pulsar
#

so again that'd only work with basic data types

pulsar stag
#

its manageable

harsh pulsar
#

yeah then just do it in python

torn sphinx
#

how can i get all the column's data from a database of sqlite?

pulsar stag
#

SELECT name FROM Table;

torn sphinx
pulsar stag
#

that will get all data from a certain column in a given table

torn sphinx
#

thnx

harsh pulsar
#

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

torn sphinx
harsh pulsar
#

@torn sphinx this was meant for @pulsar stag

pulsar stag
#

ya soemthing like that is what ill do, i have some slight data normalizations to do as well

#

so this will be nice

harsh pulsar
#

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

pulsar stag
#

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#

harsh pulsar
#

psycopg2 is a decent library

#

you wont have issues with it

#

all mature python database libraries loosely follow the same spec

torn sphinx
#

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?

pulsar stag
polar pelican
#

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?

harsh pulsar
#

treat it like a password

brave bridge
#

well, if you hash it it's pretty useless

#

🙂

polar pelican
# harsh pulsar 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.

polar pelican
harsh pulsar
#

@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

polar pelican
harsh pulsar
#

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.

#

security always has 1000 edge cases

polar pelican
#

okay thanks!

harsh pulsar
#

so as long as your database encoding is utf-8, casting to TEXT will "just work"

frozen fossil
harsh pulsar
#

im not sure what you mean

frozen fossil
#

well in my example i only want the value z as TEXT and not cast the other values

harsh pulsar
#
SELECT x, y, cast(z as TEXT) z FROM location
#

this is just basic sql

#

nothing special here

frozen fossil
#

oh well

#

i thought just having cast(z as TEXT) was enough

#

instead of another z behind

harsh pulsar
#

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

frozen fossil
#

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 ^^

harsh pulsar
#

then the blob probably isn't utf-8 text?

#

what kind of struct? in python, or another language?

frozen fossil
#

well its rust

torn sphinx
#

why don't people just use text files instead of databases?

frozen fossil
harsh pulsar
#

you have to read it, modify it in memory, then overwrite it

thick bolt
#

Is it advisable to run a MongoDB server on a Raspi?

#

I'm using a Raspi 3B+ to run a discord bot

jaunty galleon
#
        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?
proven arrow
#

Can you try assigning the execute function to a cursor variable, and then do print(cursor.rowcount)

surreal flame
#

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

proven arrow
#

@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.
surreal flame
#

@proven arrow Sounds great

surreal flame
proven arrow
#

I would have thought you already had a database at this point?

surreal flame
#

I have PostgreSQL

#

but can i use it in this case

proven arrow
#

Yes

surreal flame
#

Okay and the first method is more efficient i guess?

#

cant imagine how to code that

proven arrow
surreal flame
#

I mean to store previous versions of content

#

like you have on facebook

proven arrow
#

How is the revisions used? How often will you access them?

surreal flame
#

never

#

its like on discord.

#

edited ^

proven arrow
surreal flame
#

and i want to store previous version of content

proven arrow
#

So the user never sees it?

surreal flame
#

i'll present it probably in the future when i will do some analytics or someting

surreal flame
proven arrow
#

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.

surreal flame
#

ah ok

proven arrow
# surreal flame 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 ...

jaunty galleon
#
{"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?

torn sphinx
#

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
thick bolt
#

Why do you want to remove it?

torn sphinx
#

i use User_id instead

thick bolt
#

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

torn sphinx
#

is there a way to remove it?.. everything is working fine but i dont need it

thick bolt
#

I mean, remove it as you would any other data field

#

I'm pretty sure it's the same command

torn sphinx
#

i will try

thick bolt
#

I have a bit of a stupid question

#

How can I safely shut down the MongoDB server process

torn sphinx
#

idk i started pymongo yesterday

still sandal
#

Hey, i got this error from MOngoDB and have no ide ahow to resolve it properly, i can give code if needed

thick bolt
#

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

quiet mountain
#

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

torn sphinx
torn sphinx
harsh pulsar
#

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']

cold mural
#

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.

jaunty galleon
#

Wdym?

#

Like doing random.randint and rn.randint?

#

@cold mural

cold mural
#

It is postgres

#

by default

#

When I installed PSQL. It was there by default

wise goblet
#

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

jaunty galleon
jaunty galleon
#

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
dapper root
#

How important codd rules is in DBMS?

jaunty galleon
#

insert_one needs to be awaited using motor?

burnt turret
#

it does

proven arrow
jaunty galleon
#
        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
burnt turret
#

I think here the constraint is your own connection speed

burnt turret
#

Are you running a mongodb instance locally?

jaunty galleon
#

I am connecting with MongoDB

#

I use motor with the cluster link

burnt turret
#

Mongodb is the database

#

Where is the database you're connecting to running?

jaunty galleon
#

Something called atlas I think

#

if that is what you mean

burnt turret
#

Is it on your own machine, or is it mongodb atlas i.e on the cloud

burnt turret
#

yes, so (i'm guessing here) your internet connection might be the culprit here

jaunty galleon
#

Oh ok, I use VSCode and not host yet

burnt turret
#

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)

jaunty galleon
#

might be the problem

#

Rocket league gives me 990ms so probably my internet

jolly vale
#
Hi all```
proven arrow
#

Not needed, but it was to see if the database was affected or not to debug

quartz sleet
spring copper
#

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)

proven arrow
#

And Sql would probably be easier for this as well, and probably cheaper in pricing compared to firebase unless your on the free tier.

torn sphinx
#
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?
  }
}
astral hearth
#

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?

proven arrow
astral hearth
#

@proven arrow Could async be used for something like this or is that sub optimal?

proven arrow
astral hearth
#

@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

fickle pecan
#

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

jagged cove
jaunty galleon
#

MongoDB(motor) is sooooooooo cool

iron drift
#

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```
hazy smelt
#

Your column count doesn't match the number of values you entered

brave bridge
# iron drift **Please help me how to fix this error?** ```Ignoring exception in on_ready Trac...

as hhhhhhhh said, check how many values the column has.

  1. 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

  1. 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>

jaunty galleon
#

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?

harsh pulsar
#

@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?

golden cave
#

Anyone here worked with SQL Views? If I wanted to alter the view do I have to recreate it?

jaunty galleon
burnt turret
#

try running mongodb locally

torn sphinx
#

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?

#

😅

jaunty galleon
burnt turret
#

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?

jaunty galleon
#

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

jolly vale
burnt turret
#

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

jaunty galleon
#

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?

#

?

burnt turret
#

what?

#

you'd just change the database URI

#

i don't understand what you mean by "transmits to the compass"

wise goblet
#

apperently compass is analog of PhpMyAdmin

jaunty galleon
burnt turret
#

to the database URI for localhost?

#

i'd recommend you first download it

jaunty galleon
#

I need to download compass?

burnt turret
#

if you want a GUI to view it, sure

#

you only really need mongodb

jaunty galleon
burnt turret
#

that's just compass

#

compass isn't a database

#

compass is just a GUI to view the database

jaunty galleon
#

It's to view data

burnt turret
#

the first link has guides to install mongodb

#

the second one has guides for connecting to mongodb from

  1. compass
  2. 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

jaunty galleon
#

Ok

jaunty galleon
burnt turret
#

which link is that in?

jaunty galleon
#

The first

#

Microsoft download

burnt turret
#

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.
jaunty galleon
#

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?

burnt turret
#

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

jaunty galleon
#

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

burnt turret
#

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

jaunty galleon
#

This happend

burnt turret
#

yep so that's still a thing nice

#

basically you've connected to your local mongodb instance now

jaunty galleon
#

created database?

burnt turret
#

you can create a database if you want and try connecting to it via python

jaunty galleon
#

And how will I use it with host service?

burnt turret
#

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

jaunty galleon
#

I use something called sweplox if you know what that is

burnt turret
#

never heard of it

jaunty galleon
#

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 ?

elder vessel
#

i want to learn aiosqlite any docs ?

jaunty galleon
#

!pypi aiosqlite

delicate fieldBOT
jaunty galleon
#

There might be docs there

elder vessel
#

ohk

jaunty galleon
burnt turret
jaunty galleon
#

So I am done with the download right?

burnt turret
#

it generally looks something like mongodb://username:password@host:port

burnt turret
jaunty galleon
#

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

jaunty galleon
burnt turret
#

if you haven't set a username and password, i believe the URI will be something like mongodb://localhost:27017/

jaunty galleon
#

Let me check

jaunty galleon
burnt turret
#

🤔

jaunty galleon
#

What's wrong?

burnt turret
#

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"

jaunty galleon
#

So I will have to change some code with the url?

burnt turret
#

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
jaunty galleon
#

I already opened a database named Multi-purpose and a collection there of Prefixes

burnt turret
#

right

jaunty galleon
#

Can't I keep it the same? CLUSTER is just a varname I think

#

I mean, I assign CLUSTER to a value

burnt turret
#

yeah, it'll work, it'll insert into the prefixes collection alright

haughty linden
#

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']
burnt turret
#

those variable names are misleading (for NIR)

jaunty galleon
#

You are right

haughty linden
#

(ret is the result from cursor.fetchall())

jaunty galleon
# burnt turret those variable names are misleading (for NIR)

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?
burnt turret
#

yeah

jaunty galleon
burnt turret
#

latency? what are these numbers?

jaunty galleon
#
        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

burnt turret
#

🤷‍♂️ pretty obvious the connection was the culprit then

dapper root
#

Which statement used to access function of a package in main program?

bitter bone
#

Umm could u share a example, and I don’t think this is related to databases

jaunty galleon
glass kraken
#

line 1, in <module>
from PyQt5 import QtCore, QtGui, QtWidgets
ModuleNotFoundError: No module named 'PyQt5'

#

help

glass kraken
#

i have done it

#

but when i open my gui file it says this

lapis crane
#
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")```
delicate fieldBOT
#

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:

https://paste.pythondiscord.com

lapis crane
#

another traceback, fixed the other one ^

#

nvm

torn sphinx
#

Thank you so much man :)))

lapis crane
#

How do i make a custom prefix command with mongodb?

#

i use mongodb

formal shell
#

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

formal shell
lapis crane
#

anyone experienced with mongodb willing to help?

#

if so, ping me pls

jaunty galleon
#

I can help

#

@lapis crane what do you have already?

lapis crane
jaunty galleon
#

ok, so you are familiar with command_prefix right

#

?

lapis crane
#

sort of

jaunty galleon
#

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

lapis crane
#

i am very confused 😅

jaunty galleon
#

loaded_prefixes = {}
def get_prefix(bot, mesage):

#

You know what global variable is?

lapis crane
#

No

jaunty galleon
#

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)```
lapis crane
#

ohh

#

global variable is like st = "hi" right?

jaunty galleon
#

Yes

lapis crane
#

okay

jaunty galleon
#
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)]]```
lapis crane
#

uhm

#

ah

jaunty galleon
#

What didn't you understand?

lapis crane
#

sort of, a check?

jaunty galleon
#

sort of when it comes to finding the prefix

lapis crane
#

should i put that in my code?

jaunty galleon
#

If you understand how, yes

lapis crane
#

how do we create the command, though?

#

or well

dark oxide
#

Is psyscop3 stable enough yet does anyone know? I would be using it in production btw.

jaunty galleon
lapis crane
#

there?

#

like the command prefix? for example !prefix ?

jaunty galleon
#

yeah set-prefix

#

Whatever you'd like to call it

lapis crane
#

in ""?

royal crane
#

I want a DB with python support, which has fairly simple syntax,

lapis crane
jaunty galleon
lapis crane
royal crane
#

I don't really know what's best. I want to store user preferences for a discord bot - which would i use?

lapis crane
jaunty galleon
lapis crane
royal crane
#

Not right now, but I want to develop it in such a way that it could be

jaunty galleon
#

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

royal crane
#

Is it free to use?

jaunty galleon
#

yeah

lapis crane
royal crane
#

Thank you I'll look into it

jaunty galleon
#

the function

#

Me?

lapis crane
jaunty galleon
#

I am helping Milda but you can ask me if you'd like

jaunty galleon
jaunty galleon
# lapis crane uh

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?

lapis crane
jaunty galleon
#

Sure, you got any idea how to start? Send in #discord-bots what yo made this far/

lapis crane
#

do you mean like this? (this is another command using mongodb)

jaunty galleon
#

Ok, so you are not a started, so I won't go ez on you

#

Yeah

#

ping me there

lapis crane
#

@jaunty galleon

jaunty galleon
#

Yeah but put the prefixes in the format you'd like

lapis crane
#

kinda like this right?

burnt turret
#

that is a bad idea

lapis crane
burnt turret
#

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

burnt turret
#

how do you think that would work?

burnt turret
lapis crane
burnt turret
#

just connect to the database outside the function?

lapis crane
burnt turret
#

mongodb's own documentation has some good tutorials for connecting to the database

lapis crane
burnt turret
lapis crane
#

thanks

burnt turret
#

in there there's a link to their free course

lapis crane
#

alright, thank you!

jaunty galleon
#

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
burnt turret
#

$addFields is a thing?

steel terrace
#

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?

burnt turret
#

wouldn't it just be a $set

jaunty galleon
#

I want to add a key and value to the vaulue of auto_triggers

burnt turret
#

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

swift wraith
#

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

untold quartz
#

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

dense barn
#

My bot got really slow after i added db based commands. And i wonder whats causing it.

#

context manager?

thorn geode
#

A context manager is when you use stuff like

with or async with

dense barn
#

ohh

#

so whats the problem then?

thorn geode
#

!with

delicate fieldBOT
#

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.

thorn geode
#

That's a better explanationish

dense barn
#

bruh

#

ik what this is

#

i get all the things

#

holy shit

#

hold o

#

on

civic prawn
#

@jaunty galleon im not using compass, but I have my mongodb on localhost

#

and yes im using motor

thorn geode
# dense barn bruh

If you read the aiosqlite docs you can see that you can use the library in a procedural way

jaunty galleon
#

How did you upload whatever is needed for localhost?

civic prawn
#

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

thorn geode
dense barn
#

ye

jaunty galleon
civic prawn
#

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

dense barn
#

@thorn geode i also keep getting this error
ConnectionResetError: Cannot write to closing transport

thorn geode
# dense barn ye

I thought you meant aiosqlite, I'll ask danny about using it procedurally.

dense barn
#

ohk

thorn geode
#

Are you running database code outside of the context manager?

dense barn
#

nope

thorn geode
#

hmmm

dense barn
#
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
thorn geode
jaunty galleon
thorn geode
#

It means your network is dying.

#

I need to go now, back I'll be back in a few hours

dense barn
#

ok

civic prawn
jaunty galleon
#

I once did it on their web tho

civic prawn
#

@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?

jaunty galleon
#

Let me search it one sec

civic prawn
#

so what do you see when you run show dbs;

jaunty galleon
#

Where? Which one of 'em should I launch?

civic prawn
#

first run double click mongod to start the service then run mongo

#

it should open a terminal, once your there type in show dbs;

burnt turret
#

they'd installed mongodb as a service so mongod should be running

jaunty galleon
#

Multi-purpose is my database yeah

civic prawn
#

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

burnt turret
#

they've already done this

civic prawn
burnt turret
#

i think? because i guided them through it in the morning 😔

civic prawn
#

ive always had to start mongod manually

jaunty galleon
#

I am already using client = motor.AsyncIOMotorClient("mongodb://localhost:27017/")

burnt turret
civic prawn
#

huh, then what were you asking firsty?

jaunty galleon
#

If I need to upload something to my host

civic prawn
#

you dont need to change anything

jaunty galleon
#

Where I want to host my bot

burnt turret
#

you just install mongodb on your VPS as well?

#

the same way you installed it on your computer

civic prawn
#

yep

burnt turret
#

or well not exactly the same, it'd depend on your VPS

jaunty galleon
#

I think it's os is linux

civic prawn
#

I recently integrated my mongodb with my ubuntu

#

it was easy

#

just sudo apt install mongo

#

make ur db and collections

#

and ur set

burnt turret
#

yep

#

you don't really need to make anything actually

civic prawn
#

does it auto write?

jaunty galleon
#

My host accepts downloading packages if that's what you guys mean

burnt turret
#

if you try accessing a collection / db that doesn't exist, mongo will just make it on the go

civic prawn
#

neat i had no idea

#

databases arent my strong suit sweat

#

so all you would really need to do is sudo apt mongo or maybe mongodb then just run ur code normally

#

it seems like

jaunty galleon
#

?

civic prawn
#

like this

jaunty galleon
burnt turret
#

can't you just SSH in?

civic prawn
#

seems like pterodactyl