#databases

1 messages · Page 150 of 1

stray moss
#

UGHH

slow sand
#

You can use ODM, it will care about field names

iron drift
#

Help me please, here is the code:

@client.command(aliases = ['balance', 'cash'])
async def bal(ctx, member: discord.Member = None):
    if member is None:
        await ctx.send(embed = discord.Embed(
            description = f"""Баланс пользователя **{ctx.author}** составляет **{cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[0]} 🍃**"""
        ))
        
    else:
        await ctx.send(embed = discord.Embed(
            description = f"""Баланс пользователя **{member}** составляет **{cursor.execute('SELECT cash FROM users WHERE id = {}'.format(member.id)).fetchone()[0]} 🍃**"""
        ))```

**Error:**
```Command raised an exception: TypeError: 'NoneType' object is not subscriptable```
brave bridge
#

Try not to put so much stuff into an f-string. Extract the expression into a variable.

iron drift
brave bridge
dense barn
#
@welcome.command()
@commands.has_permissions(manage_channels=True)
@commands.guild_only()
async def channel(ctx):
    db = sqlite3.connect(db_path)
    cursor = db.cursor()
    cursor.execute("SELECT channel_id FROM welcome WHERE guild_id = ?",(ctx.guild.id,))
    result = cursor.fetchone()
    print (result)
    if result is None:
        sql = ("INSERT INTO welcome(guild_id, channel_id) VALUES(?,?)",(ctx.guild.id, ctx.channel.id))
        await ctx.send(f"Welcome channel has been set to {ctx.channel.mention}")
    elif result is not None:
        sql = ("UPDATE welcome SET channel_id = ? WHERE guild_id = ?",(ctx.channel.id, ctx.guild.id))
        await ctx.send(f"Welcome channel has been updated to {ctx.channel.mention}")
    cursor.execute(sql)
    db.commit()
    cursor.close()
    db.close()
Ignoring exception in command welcome channel:
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\25dch\github\pythonbot\CalcyBot.py", line 1161, in channel
    cursor.execute(sql)
TypeError: argument 1 must be str, not tuple
```any idea why im getting that error?
#

nvm fixed it

radiant elbow
#

you can do cursor.execute(*sql) instead

teal hare
#

can anyone give me a source where i can learn SQL?

#

i wanna use postgreSQL for my bot

spiral knoll
#

Whats up folks

#

would you recommend mysql with python for web development or another dbms? if so which library would you use to go with it

#

for regular db, user info, content etc

#

small to mid size deployment

#

management (backups, compatability) and security in mind

wise goblet
#

free, fun, interactive

crimson valley
#

is there an async postgresql driver? im using psycopg2 for now but it is not async

burnt turret
#

!pypi asyncpg

delicate fieldBOT
burnt turret
#

@crimson valley

crimson valley
#

thanks

autumn rune
#

I need help with this postgres, im trying to check if a value is in a row called id (which is a bigint). And if so, do something.

IF EXISTS (SELECT 1 FROM spotify_api.access_tokens WHERE id = 579646098704957460) THEN
    raise notice 'yes'
end IF;

but this doesnt seem to work

digital pecan
#

Hi

#

Can anyone tell me

#

How to make database

#

How to edit it with sql

#

And is ms access good for database

spiral knoll
#

what is your use case

dusk junco
#

How can I store pdf files submited through POST form into a PostgreSQL database? And what data type should I use? BYTE?

proven arrow
#

Some people would argue that you should not store such files in the database, and instead just store the path to the file which points to its location on a standard filesystem. Which path you take, and what data type to use depends on what you will do with these files, and so without knowing more about your requirements its hard to give a definitive answer. But you can read this to help you decide, as it goes through the different options, https://wiki.postgresql.org/wiki/BinaryFilesInDB

dusk junco
grim zephyr
#
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'username'@'localhost' (using password: YES)
#

how to fix this error

#

what kind of error is this

cedar pagoda
#
    @setroster_command.error
    async def setroster_command_error(self, ctx, exc):
        if isinstance(exc, ChannelNotFound):
            await ctx.send("Channel not found.")

        elif isinstance(exc, IntegrityError):
            await ctx.send("You are not able to have more than one active roster.")

        else:
            raise
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IntegrityError: UNIQUE constraint failed: roster.GuildID

On IntegrityError the bot should Send the message instead of raising the error. Does somebody knows why the error is raising?

grim zephyr
#

mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'username'@'localhost' (using password: YES)

#

how to fix this error

burnt turret
final trench
#

Can anyone tell me if this is a valid join in PostgreSQL? What type of join would it be considered as?

final trench
#

How do you know it's an inner join if it doesn't specify that?

#

sorry if that's a stupid question

half forum
#

How do I fix?

proven arrow
final trench
#

ah, TIL! Thank you!

half forum
#

nvm

iron drift
#
@client.command(aliases = ['deposit'])
async def dep(ctx, amount = None):
    if amount is None:
        embed = discord.Embed(
            description = f"**{ctx.author.mention} введите сумму которую хотите перевести в банк**"
        )
        await ctx.send(embed = embed)
    elif amount == 'all':
        cursor.execute(f"UPDATE users SET cash = {0} WHERE id = {ctx.author.id}")
        conn.commit()
        cursor.execute(f"UPDATE users SET bank = bank + {int(amount)} WHERE id = {ctx.author.id}")
        conn.commit()
        embed = discord.Embed(
            description = f"Вы успешно перевели себе на банк все коины"
        )
        await ctx.send(embed = embed)
    elif amount < 1:
        embed = discord.Embed(
            description = f"**{ctx.author.mention} введите сумму больше 0**"
        )
        await ctx.send(embed = embed)
    else:
        cursor.execute(f"UPDATE users SET cash = cash - {int(amount)} WHERE id = {ctx.author.id}")
        conn.commit()
        cursor.execute(f"UPDATE users SET bank = bank + {int(amount)} WHERe id = {ctx.author.id}")
        conn.commit()
        embed = discord.Embed(
            description = f"Вы успешно перевели себе на банк {amount} коинов"
        )
        await ctx.send(embed = embed)```

**help, the problem is in the line where there is "all"**

**Error:**
```Command raised an exception: ValueError: invalid literal for int() with base 10: 'all'```
grim zephyr
#

how to add information to mysql database

#

?

prisma girder
#

Why int(amount) when amount is equal to "all"?

prisma girder
#
INSERT INTO tbl_name (col1, col2) VALUES (15, 2);
grim zephyr
#

where will i put my database name in which i want to put information

jaunty galleon
#

Instead of tbl_name

grim zephyr
#

ok

prisma girder
grim zephyr
#

and is it possible to store files in database

prisma girder
grim zephyr
#

@prisma girderwhy??

prisma girder
jaunty galleon
grim zephyr
#

@prisma girdercan you tell me how to store a python file in a sql database

prisma girder
grim zephyr
#

i am supposed to download which module to do this

#

@prisma girder

prisma girder
grim zephyr
#

@prisma girderyep

#

@prisma girderand which module should i use to give those commands

#

shall i use mysql connector

prisma girder
#

!pypi mysql-connector-python

delicate fieldBOT
grim zephyr
#

!pypi SQLAlchemy

delicate fieldBOT
grim zephyr
#

@prisma girderwhich one is better

brazen charm
#

they're not the same thing

#

SQLAlchemy is an ORM

#

internally it uses mysql connector

#

for MYSQL

prisma girder
#

If you don’t have any experience with SQL you shouldn’t use SQLAlchemy and ORMs

grim zephyr
#

what is orm

brazen charm
#

but it also has several other connectors it uses for other databases

prisma girder
grim zephyr
#

which module is best for database management

waxen halo
#

@prisma girder Can you use sqlalchemy ORM with some kind of async or asyncpg implementation?

waxen halo
#

Oh super cool

brazen charm
#

in your connection uri

#

you need to do postgresql+asyncpg://... though

#

so you tell it the driver to use

waxen halo
#

I have been exploring SQlalchemy ORM, because before I used psycopg2

#

I feel the ORM part is kind of nice, and I'm also exploring async so I was wondering if it can be used together

#

Since if I understand correcly, the methods used within async code have to be async as well for it to have any benefit

brazen charm
#

yeah

prisma girder
brazen charm
#

with alchemy pretty much everything stays the same except the engine and sessions really. all the query generation stays the same

#

just have a lil read through the asyncio docs and it shows pretty much the only changes needed

waxen halo
waxen halo
#
Traceback (most recent call last):
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 719, in call_soon
    self._check_closed()
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 508, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed```
waxen halo
#

it's over 2k chars

#

can't dump here

brazen charm
#

yeah the error is because asyncio.run abruptly closes and removes tasks when it's done

#

rather than asyncio.run

#

use

#
loop = asyncio.get_event_loop()
loop.run_until_complete(foo())```
#

that way asyncpg can do its cleanup etc...

waxen halo
#

Perfect!

#

That worked 😄

jaunty galleon
delicate fieldBOT
#

Pasting large amounts of code

If your code is too long to fit in a codeblock in discord, you can paste your code here:
https://paste.pydis.com/

After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.

waxen halo
#

Thank you 🙂

torn sphinx
#

I've got a list of flattened lists, wanting to create a list of dicts so that the row header elements correspond to each row element. What would be the method that involves the least labour?

[
  [
    "ID", 
    "Name", 
    "Description", 
    "Price", 
    "Stock", 
    "Keywords", 
    "Category 1", 
    "Category 2", 
    "Category 3", 
    "Image URL"
  ], 
  [
    "001", 
    "Eggs", 
    "2 eggs fried in a pan", 
    "1.00", 
    "Unlimited", 
    "eggs, breakfast", 
    "Poultry", 
    "Breakfast", 
    "Protein", 
    ""
  ], 
  [
    "002", 
    "Bacon", 
    "Crispy bacon fried in a pan", 
    "1.00", 
    "Unlimited", 
    "bacon, breakfast", 
    "Protein", 
    "Breakfast", 
    "Protein", 
    ""
  ], 
  [
    "003", 
    "Sausages", 
    "Pan fried beef sausages", 
    "2.00", 
    "Unlimited", 
    "sausages, breakfast", 
    "Protein", 
    "Breakfast", 
    "Protein", 
    ""
  ], 
  [
    "004", 
    "Donut", 
    "Sweet donut", 
    "0.50", 
    "Unlimited", 
    "donut, sweet", 
    "Sweets", 
    "Deserts", 
    "", 
    ""
  ]
]
#

I've got a list of flattened lists, wanting to create a list of dicts so that the row header elements correspond to each row element. What would be the method that involves the least labour?

torn sphinx
#

is db.commit necessary for every command? (MySQL)

#

can i just add it at the end of my code?

torn sphinx
#

@torn sphinx Please stick to channel topics. This is the databases topical channel, you can try out bot commands in #bot-commands

cyan yacht
#
with db.cursor() as cursor:
    # do something
    db.commit()

Is this how a transaction looks like?

hexed estuary
torn sphinx
jaunty galleon
#

In PostgreSQL, how can I organize the table by id?(I made a column named id which is serial, but when I update one value in the first row it pushes it down)

hexed estuary
jaunty galleon
#

I'll check it out, thanks

#

Mhm, there isn't really an answer there, the solution was to make what I already did with an id...

jaunty galleon
remote plinth
#

you mean auto increment?

proven arrow
# jaunty galleon I still don't know how to fix that?

Rows in a relational db are not sorted. If you want them ordered, then the only way to do is by using the order by statement in your query. If you don’t specify an order by, then the database can return it however it likes.

jaunty galleon
#

Do I need to do that every time I update?

#

ORDER BY id ASC

proven arrow
#

I just told you database doesn’t store ordered rows.

jaunty galleon
#

I have an id row(serial)

iron drift
#
@client.command()
async def setS(ctx, statusarg = None):
    if statusarg is None:
        embed = discord.Embed(
            description = f"**{ctx.author.mention} введите текст!**"
        )
        await ctx.send(embed = embed)
    else:
        cursor.execute(f"UPDATE users SET status = status + {statusarg} WHERE id = {ctx.author.id}")
        conn.commit()
        await ctx.send("Успешно!")```
**I want to make a command to change the status in the profile in the database, but something goes wrong**
hazy smelt
#

What's the error

iron drift
# hazy smelt What's the error

when I decide to use this command, I write 2 argument and it gives an error: ret = await coro(*args, **kwargs) File "C:\Users\Администратор\Desktop\MyServerBot\index.py", line 188, in setS cursor.execute(f"UPDATE users SET status = status + {statusarg} WHERE id = {ctx.author.id}") sqlite3.OperationalError: no such column: test

burnt turret
#

probably the best move here would be to just not use f-strings for SQL queries

dense barn
#

how do i get database ping/latency?

jaunty galleon
#

And than simple math

dense barn
#

ah

jaunty galleon
#
start = time.time()
#Do some db action
end = time.time()
ended_in_ms = round((end - start) * 1000)```
dense barn
#

ah tq

uneven lava
#

Hi i'm using sqlalchemy, and I'm trying to make "conditional relationship" like this :

class GuildConfiguration(Base):
    __tablename__ = "guild_configuration"
    id: int = Column('id', ForeignKey('guild.id'), primary_key=True)

    prefix: str = Column('prefix', String, nullable=False, default="!")
    commands_config: = relationship(
        'FeatureConfiguration', lazy='selectin',
        primaryjoin="and_(GuildConfiguration.id == foreign(FeatureConfiguration.guild_id), FeatureConfiguration.feature_type == 'command')"
    )
    modules_config: = relationship(
        'FeatureConfiguration', lazy='selectin',
        primaryjoin="and_(GuildConfiguration.id == foreign(FeatureConfiguration.guild_id), FeatureConfiguration.feature_type == 'module')"
    )

    guild = relationship('Guild', back_populates='configuration', lazy='selectin', uselist=False)


class FeatureConfiguration(Base):
    __tablename__ = "feature_configuration"
    id = Column('id', Integer, primary_key=True, autoincrement=True)
    guild_id = Column("guild_id", ForeignKey('guild_configuration.id'))

    feature_type: str = Column('feature_type', String, nullable=False)  # command | module

But I get this warning :

sys:1: SAWarning: relationship 'GuildConfiguration.modules_config' will copy column guild_configuration.id to column feature_configuration.guild_id, which conflicts with relationship(s): 'GuildConfiguration.commands_config' (copies guild_configuration.id to feature_configuration.guild_id). If this is not the intention, consider if these relationships should be linked with back_populates, or if viewonly=True should be applied to one or more if they are read-only. For the less common case that foreign key constraints are partially overlapping, the orm.foreign() annotation can be used to isolate the columns that should be written towards.   The 'overlaps' parameter may be used to remove this warning.

If anyone can help me ?

torn sphinx
#

osmeone help

jade swan
#

How do I get a specific data from an Array in MongoDB?
I have this code :

def get_user_data(guild_id: int, user_id: int):
    results = users.find({"_id": guild_id})
    for x in results:
        if x["_id"] == guild_id:
            result = x
        else:
            continue

    for user in result["members"]:
        print(user)

That's supposed to get the data of a specific user, and this user is in an array as shown in the image. when I print the data I get :

{'843273420799737876': {'items': [], 'balance': 0}}
{'510736807999307786': {'items': [], 'balance': 0}}
{'771829098137255976': {'items': [], 'balance': 0}}
{'797947530104668190': {'items': [], 'balance': 0}}
None
high geyser
#

I am planning on creating a search engine with python, have been doing research for a while. I am planning to scrape the web with broad crawlers in parallel (using scrapy), and I am planning to use django for the api (rest framework). Django haystack works well with full-text searches, I would have to tokenize and optimize the crawled data before storing it in my database (haystack document with template). However, django doesn't have native support for nosql. Obviously I cannot use relational databases for writing chunks of data and keeping it fresh at the same time.

What are my options?
Any suggestions?

hasty bronze
#

Not sure if this is a good place to ask this question. I've been dabbling with Discord for some months and still mostly can't understand how it works, but... I'm trying to form the question from a more narrow perspective this time.

Background: I have an old and complicated database application, though it's just personal information with a few thousand records in two primary tables (of books and authors). There's one complete version of the system in dBase and a separate Web-based front-end using PERL/CGI (under JavaScript). (It actually started on a typewriter, but one time I even programmed it with PL/C, a version of PL/I.) I'd like to port the entire thing to Python, possibly as a smartphone app. I've taken a number of Python classes, including a cumbersome SQL interface class and a fairly complicated version of Asteroids (in an unusual browser-resident subset of event-driven Python).

However the easy approach for me in my declining years is to just start with working code for a similar purpose and massage it to my twisted new purposes. (That's how the PERL got into the system, starting with a little personal contacts database.) Does that ring any bells? Someone have advice to point me at the right place to find a suitable Python source to start with?

torn sphinx
#

Is there a way to store a discord embed in a database?

burnt turret
#

You'd store the contents of the embed

torn sphinx
#

And how do I do that

#

And also how can I convert the stored contents of thr embed back to the embed whenever it is called sometimes in tbe future

burnt turret
#

You'd store all the fields of the embeds?

#

The title, description, url etc are all plain strings that can be stored

jaunty galleon
#

Make some columns, like title, description And more if you'd like. When someone creates an embed, insert values to these columns. When someone wants to call it, fetch them

#

Damn you anand you are fast

torn sphinx
#

Oh I see

torn sphinx
inner sentinel
#

Mongodb vs postgresql?

jaunty galleon
#

PostgreSQL probably

#

But it depends what's easier for you

true lily
#

what is recommended database for economy bot other than sqlite

raw saffron
civic prawn
#

@burnt turret I mean as in I dont need to select blah from black where blah and instead do something like bot.pool.dosomething

burnt turret
#

Oh right

#

You could maybe write a few functions that do queries that you use often I'd assume

#

But if you don't like writing SQL queries, you could check out using an ORM

#

!pypi orm

delicate fieldBOT
burnt turret
#

This is a pretty good async one

civic prawn
#

hmmm ok ok

#

so is this just like a module that turns my database into objects that then can do stuff

burnt turret
#

it lets you interact with your database with an object-oriented interface (I hope that definition is right)

civic prawn
#

oooh yes yes i think ill use an ORM

burnt turret
#

So you'd be writing classes and calling methods, and the ORM will convert it into SQL for you

civic prawn
#

I made an API for mongo that does the same and it was a real time saver

#

hmmm ok ok i understand now

#

thanks anand

brazen charm
#

I would recommend just using sqlachlemy now

#

ORM is pretty redundant now

stuck pebble
#

Hi anyone knows how to connect database in pycharm?

brazen charm
#

you mean with the developer console setup?

#

e.g. this

stuck pebble
#

who me?

brazen charm
#

yes

stuck pebble
#

yes

#

wait i will show

brazen charm
#

what database r u trying to connect to, it's pretty simple

stuck pebble
#

after adding my id and pass, I apply and run it. But shows error after running

brazen charm
#

is the mysql server actually running?

stuck pebble
#

Yes

brazen charm
#

looks like your login details are incorrect

#

do you have a working database URL with all the auth in it that you use to connect normally?

stuck pebble
#

but I am using the credential since long it can't incorrect

stuck pebble
#

Can I create new user name and password here only somehow?

brazen charm
upper basin
#

is it possible to store a list of string in an sqlite database

brazen charm
brazen charm
#

you can try make a new user and pass Ig

stuck pebble
#

Ok thanks

autumn epoch
#

How would I select a integer from a postgres database which is the highest in the column?

torn sphinx
#

if @@autocommit is 1 that means its enabled right

#

mysql connector python

burnt steppe
#

This is my entrypoint.sh for a docker based container. It runs everytime docker-compose is started so even the alembic migrations run everytime even if there is no change in the DB Schema. Is there any way to automate it by making revisions run only when there is a change and ignore the statement otherwise ?

pip install requests
alembic revision --autogenerate
alembic upgrade head
uvicorn app.server:app --reload --host 0.0.0.0 --port 5000
wise goblet
#

plus, since you already used SQLite, you can make easy transition

#

the language is more or less the same, just with some additional things

somber grove
#

Hello, I have an issue while importing a csv to a table in postgresql database using DBeaver. While the imported csv's columns appear correctly the data wouldn't al all. I get an empty table. Could anyone please help me fix this?

autumn epoch
burnt turret
#

yes

jaunty galleon
#

I use it for economy, it's easy, very easy

trim lintel
#

So i am creating multiple payment methods for my site. Paypal, card, adyen, stripe. A concern i have is how to create the tables for this? Should i make one table for all gateways, or multiple tables for each specific gateway.

#

@jaunty galleon

proven arrow
# trim lintel So i am creating multiple payment methods for my site. Paypal, card, adyen, stri...

Depends on what data you want to store in those tables.
If for each payment gateway you find that you need to store a lot of unique fields then, the multiple table inheritence method would work better. For this you can have a transactions table where you store the fields shared across all gateways, and then individual tables for each payment gateway, and each gateway table would have a foreign key for transaction_id, that references the transactions table.
If you find that the fields you store are common across all methods, then you can store in a single table for all payment gateways.

It is possible to do with both approaches. I have worked on applications that have had multiple gateways using both approaches. And there is nothing wrong with either approach. Its just a matter of choosing the one that suits your data best.

proven arrow
#

@trim lintel And for one recent project we did it with polymorphic structure, so it works better with the ORM we use. Note the payable type/id.

transactions table:
- id
- payable_type
- payable_id
- amount

buynet_payments:
- id 
- columns unique for buynet

paypal_payments:
- id 
- columns unique for paypal

mobile_payments:
- id 
- columns unique for mobile
torn sphinx
#

Can someone show me how to manually start a postgresql databse

pure sleet
#

does anyone know the exception sqlalchemy raises when you try to insert duplicate data in a database where the field has to be unique?

brazen charm
#

It'll likely be a programming error

#

ProgrammingError wraps all exception raised by the driver. The driver is what will handle a unqiue constraint failure from the db in all likelyhood

#

easiest way is to test

pure sleet
pliant cliff
#

I... I don't know anymore 😭

#

createdb: error: could not connect to database template1: FATAL: password authentication failed for user "User"
This keeps happening... but I'm unsure as to why

PostgreSQL

#

Everytime... This is the 4th time in the past 2 years that I'm trying to learn this again... and I always get stuck at setting up postgreSQL...
Every... single... time 😟

brazen charm
#

Im assuming you made a new user then?

pliant cliff
#

I don't know how...

#

All I did was do the setup from the wizard...
And then tried to use createdb

brazen charm
#

try do

#

createdb -d postgres -U postgres

#

actually scrap the -d bit

#

createdb -U postgres test i think it is

pliant cliff
#

I didn't get an error. And it seemingly accepted the password

brazen charm
#

by default the only user on the DB is postgres

#

and by default it doesnt have a password

#

you should now have a database called test made

pliant cliff
#

so what was the password the wizard required about then?

brazen charm
#

what password wizard?

pliant cliff
#

When I ran the installation wizard for postgres, one of the things it asked for was a password... along with the port, and the locale

brazen charm
#

oh

#

normally thats for the pgadmin tool by default

pliant cliff
#

:o

brazen charm
#

I havent setup postgres via the wizard for a while so i cant say for certain

pliant cliff
#

I see... Thank you CF8

#

I really wish There was something about that online (

brazen charm
#

aha

#

dw

#

postgres is a big beast, takes a while to get to grips with it

pliant cliff
#

Alright. Thanks again

jaunty galleon
#

The hardest part for me was to connect to PostgreSQL locally(stupid me had to go to elephant sql) but it's really all about SQL syntax

forest fiber
#

Hey guys should i use mongo db?

#

@jaunty galleon got some DB recommendations?

jaunty galleon
#

Well, if you used JSON until now, MognoDB is probably the best option to get started with, as it's uses JSON format.

pliant cliff
#
python manage.py migrate
  File "D:\Programs\code\Python\webdev\everycheese\.venv\lib\site-packages\psycopg2\__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: fe_sendauth: no password supplied

Now what is this about 😟

brazen charm
#

what does you code look like?

#

I'd recommend just telling it to connect via a url rather than the builder tool psycopg2 gives

pliant cliff
#

Trying to learn Django 😟

brazen charm
#

ohh

#

well if this is for django

#

I would recommend setting docker up for this

pliant cliff
#

Using Wedge of Django as a guide...

brazen charm
#

or using sqlite db backend for now till you want to deploy

#

then use docker

pliant cliff
#

I'm just tryna learn the basics...
THe book makes it seem so easy...
but now I'm just running into error after error >(

brazen charm
#

once you install docker it makes life much easier with this sorta thing

pliant cliff
#

Right...

#

It's not like the book is old or anything...
so why is all this so different...

torn sphinx
#

heyyy im new to pymongo and i need bit help with this

#

i need to know how to match the _id and then read out the cash and bank

flint imp
torn sphinx
#

penezenka_cislo= collection.find_one({"cash": []})

flint imp
#

You got some examples here

torn sphinx
#

i just need to know how to read numbers

jaunty galleon
torn sphinx
#

like {"cash": what here?}

jaunty galleon
#

So:

data = COLLECTION.find_one({"_id": 506874709607186432})
print(data['cash'])

Data would be:

{"_id": 506874709607186432, "cash": 150, "banka": 350}```
torn sphinx
#

ohhh

#

so like the _id read all the data

forest fiber
#

Sqlite or postgre? which is easier

jaunty galleon
forest fiber
jaunty galleon
#

It's record object return is amazing

brazen charm
#

sqlite is the easiest to setup

#

but postgres has much more power behind it and advantages in the long run

forest fiber
#

I’ll go with Postgre

#

will try

jaunty galleon
#

Yes nice, at first you'll be frustrated, but than everything will go as planned

forest fiber
torn sphinx
#

and how do i add to it?

#

like add not overwrite

jaunty galleon
#

I think set works for that as well

#

query = {"_id": 506874709607186432}
data = {"$set": {"new_key": "hi"}}
await COLLECTION.update_one(query, data)

torn sphinx
#

but like the set sets a new number?

#

or not

jaunty galleon
#

Wdym? You want to increase the number?

torn sphinx
#

yeah

#

i need to take the old one and + the number i want right?

jaunty galleon
#

No, there is $inc way

#

query = {"_id": 506874709607186432}
data = {"$inc": {"cash": 2}}
await COLLECTION.update_one(query, data)

torn sphinx
#

ohhhh

#

thats nice

#

and btw idk why but i somehow broke if i dont have a data to user, it should make one)

jaunty galleon
#

Wdym?

torn sphinx
#
        jmeno = str(ctx.guild.id)
        collection = db[jmeno]
        if not member:
            member=ctx.author

        data = collection.find_one({"_id": member.id})
        penezenka_cislo= (data["cash"])
        banka_cislo= (data["banka"])

        if data is None:
            id = member.id
            post = {"_id": id, "cash": 150, "banka": 350}
            collection.insert_one(post)
            await ctx.channel.send('Právě sis vytvořil ůčet, hůrá!')
#

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'NoneType' object is not subscriptable

jaunty galleon
#

What line raises the error?

torn sphinx
#

penezenka_cislo= (data["cash"])

#

this

#

w8

jaunty galleon
#

Well, if it is None, you are doing None['cash']

torn sphinx
#

yeah

#

im stupid xDDD

jaunty galleon
#

!e

print(None['cash'])```
jaunty galleon
torn sphinx
#

forgot that the cash data wasnt existing

#

and it cant get to the part when it makes the data

jaunty galleon
#

Well no, it couldn't find a doc with that member id

torn sphinx
#

no its ok

#
    @commands.command()
    async def penize(self,ctx, member: discord.member = None):
        jmeno = str(ctx.guild.id)
        collection = db[jmeno]
        if not member:
            member=ctx.author

        data = collection.find_one({"_id": member.id})

        if data is None:
            id = member.id
            post = {"_id": id, "cash": 150, "banka": 350}
            collection.insert_one(post)
            await ctx.channel.send('Právě sis vytvořil ůčet, hůrá!')
        else:
            penezenka_cislo = (data["cash"])
            banka_cislo = (data["banka"])
            embed = discord.Embed(title=f"Peníze pana {member}")
            embed.add_field(name="Peněženka", value=f"{penezenka_cislo}")
            embed.add_field(name="Peněženka", value=f"{banka_cislo}")
            await ctx.send(embed=embed)
#

my problem was the cask and bank

#

but this works just fine

#

thx so much <333

jade swan
#

I'm using MongoDB and I have this code :

def get_user_data(guild_id: int, user_id: int):
    results = users.find({"_id": guild_id})
    for x in results:
        if x["_id"] == guild_id:
            result = x["members"]
        else:
            continue

    data = result
    return data

That should get a certain user from the list members. When i print the whole data i get this :

[{'843273420799737876': {'items': [], 'balance': 0}}, {'510736807999307786': {'items': [], 'balance': 0}}, {'771829098137255976': {'items': [], 'balance': 0}}, {'797947530104668190': {'items': [], 'balance': 0}}]

But what I want it to print is only the specific user's data. I tried doing result[str(user_id)] and result[{str(user_id)}] but none of that worked. Can someone help me? And also I get that error :

TypeError: list indices must be integers or slices, not str 
hexed estuary
#

perhaps you need .find({"_id": str(guild_id)})?

jade swan
#

i will change this

jade swan
#

the guild data is already gotten

hexed estuary
#

ah, I see

#

hmm, can't you use MongoDB for filtering users out of guilds, too? anyway, you seem to be comparing to guild_id

jade swan
#

should i do

for x in data:
  if x.startswith(str(user_id)):
    user_data = x

?

hexed estuary
#

instead of getting the key str(user_id) from the dict

jade swan
#

the guilds' members are saved in the users collection

#

excuse me if im mistaking tho, i'm pretty new to mongo 😅

#

@hexed estuary

hexed estuary
#

so members is a list of dicts

#

oh

#

that's a very weird organisation you have here

#

the only way you can get the data of the user is by iterating over that entire list, searching for the right ID

#

consider instead making members itself a dict, so that looking up the data for a user is as simple as members[user_id]

burnt turret
#

The array is actually fine, if you'd make the user ID a value and not a key

#

Then mongodb would be able to directly query for it

#

The problem now is that you've kept the member ID as the key, so mongodb can't directly query for it and you need to iterate over the entire thing

#

I'm talking about members being an array, and each member's dictionary being something like

member_id : <id>,
items: ...,
balance: ...
dim sluice
#

Can someone help? I am using tinydb and have this error:

    update()
  File "E:\.Code\Python\SmallerProjects\Project14.py", line 13, in update
    db.update({'age': 26}, User.name == 'Tim')
NameError: name 'User' is not defined```
This is the code:
```py
from tinydb import TinyDB, Query
from time import sleep

db = TinyDB("database.json")

def insert():
    db.insert({'name': 'Tim', 'age': 26})

def clearDataBase():
    db.truncate()

def update():
    db.update({'age': 26}, User.name == 'Tim')

insert()
update()

print(db.all())

answered = False
while answered == False:
    answer = input("Do you want to clear the database?\n\nType 'yes' if you wnat to, else type 'no'")
    answer = answer.lower()
    if answer == "yes":
        clearDataBase()
        answered = True
    elif answer == "no":
        print("The database has no been cleared!")
        sleep(2)
        answered = True 
    else:
        print("You put in an invalid answer, you will be asked again.")
        sleep(1)
jaunty galleon
#

Where dod you define User?

dim sluice
#

I don't know what to define it as

#

nvm

#

1 sec lemme try fixing

jaunty galleon
#

Well you can't just do User.name without defining User

candid owl
#

def update():
    db.update({'age': 26}, User.name == 'Tim')

what args does db.update take?

#

you trying to treat dot notation like structs?

#

I mean technically possible to implement (I'm still ironing out the globals() details) but... you've still gotta implement it if you want to use it

dim sluice
#
db = TinyDB("\database.json")

What do I put so that the file is created in the same folder that the program is in?

torn sphinx
#

i wouldnt need db.commit everytime i update values in my database if i have autocommit enabled right

burnt turret
#

That's what autocommit does yes

alpine moon
#

im new to the sql stuff, why is this a syntax error? ```py
self.sqlcommand(f"""
INSERT INTO VALUES accounts (
{self.email},
{self.password},
{self.username},
{self.creation_time}
);
""")

#

oooh wait

#

it has to be in quotes

jaunty galleon
#

And don't use f-strings, use the placeholder of your lib. See #databases message to know more.

alpine moon
#

thank you, i discovered this bug like a minute ago where there was a ' " ' in the password

iron drift
#

How do I do so that the bot translate the whole balance to the bank with SQLite?

dim sluice
#

How do i use this in TinyDB?

db.search(User.name.exists())
fringe sentinel
#

hi guys
question
how do you store Your data if You don't want to make "server" ?

torn sphinx
#

@fringe sentinel what kind of data you want to store

#

Is it user data or program data

fringe sentinel
#

user

torn sphinx
#

If its sensitive data like password then you can use cloud services to store your data. But if it's not sensitive then store in users local device

fringe sentinel
#

lets say i want to gather info to gui from table or whatever then user do something or change value and push back info to modify it

torn sphinx
#

use local database in this case if you don't want to save user data

fringe sentinel
#

first thoughts was to make "local" warehouse system

torn sphinx
#

Ya that will be fine in your case

#

heyy small question, Im using mongoDB, how do i decrase numbers?

#

what numbers ?

#

{"$inc": {"cash": cash}}- i know how to add but how do i decrase

hazy smelt
#

how to fix this
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '142.54.191.90:3306' (110 Connection timed out)

hazy smelt
torn sphinx
#

oh

#

like {"$inc": {"cash": -cash}}

hazy smelt
#

yus ig

#

havent used mongodb but the db i use, i just add negative amount to subtract

torn sphinx
burnt turret
#

yeah, you'd $inc a negative number to decrement

torn sphinx
#

oki imma try that one

#
        myquery = collection.find_one({"_id": ctx.author.id})
        vyber = {"$inc": {"cash": {kolik}}}
        vyber_banka = {"$inc": {"banka": {-kolik}}}
        collection.update_many(myquery, vyber, vyber_banka, upsert=True)
#

why this isnt working?

fair stag
#

bruh

torn sphinx
#

or like how tf i update more then 1 var?

#

im nobie xd

hazy smelt
#

comma

torn sphinx
#

pymongo

#

or just im trying to decrase the fricking number

#

idk how

worldly inlet
#

I started in programation

#

and im french

torn sphinx
#

hi

#

anyone here

#

im trying to get update statistics from azure data warehouse

#

is the modified date in sys tables enough

woeful jolt
#

Anyone knows ?

torn sphinx
#

show the query

spiral knoll
#

thats a column

#

not a table

#

oh, im wrong sorry mate

#

lets definetley check out the query

torn sphinx
#

Hello, anyone knows how i can check if a id exist in my MongoDB database that i then can store into a variable? for now the following code is just not working because after a reboot of the bot python thinks there is nothing in the variable and thats what i need to fix. any solutions?

#

heyyy try looking for it:

#
        if data is None:
            post = {somestuff here}
            collection.insert_one(post)
#
        data = collection.find_one({"_id": username})
torn sphinx
# torn sphinx .

is this correct?

    @bot.command(name="ban")
    @commands.has_permissions(ban_members=True)
    async def banuser(ctx, member: discord.Member, *, reason=None):
        userdata = {"_id": member.id, "Username": member.display_name}
        data = collection.find_one({"_id": member.id})
        
        if data is None:
            post = {userdata}
            collection.insert_one(post)

        print(member.id)
        if member.id not in userdata:
            print("Not in userdata")
            bannedusers.insert_one(userdata)

        elif member.id in bannedusers:
            await ctx.send(f"Trying to ban the User {member.display_name}...")
            try:
                await member.ban(reason=reason)
                await ctx.send("Banned this user Sucessfully")
            except:
                await ctx.send("error")
#

@torn sphinx

#

well should be fine, but why is there the if member.id not in?

#

or like whats the poin? Make a banned user database right?

#

yea

#

noow with the code the bot is just not responsing

#

okkk, well if you ban someone make for them the {"_id": member.id, "Username": member.display_name}, and if you unban them, delete the thing

#

that would be easier

#

hmm im not sure what exaclty you mean...

#

like make doc for them if they get banned

#

somewhat like this

#

then when they get unbanned, delete that

#

do you got a examplecode for that? I feel like my brain is dead now and i need to wait a bit to do better work again....

#

well i dont have that ://

#

im trying to make a economy sys and i have some problems too xD

#

oh okey...

#

collection.delete_one()

#

but there is simple delete one command

#

btw @jaunty galleon can u help me a bit?

jaunty galleon
#

If I'll know, sure

torn sphinx
#

idk what im doing wrong, Im using pymongo and im trying to overwrite 2 values at the same time

#
            vyber_banka = {"$inc": {"banka": kolik_banka}}
            vyber = {"$inc": {"cash": kolik_cash}}

            collection.update_one(data,vyber_banka)
            collection.update_one(data, vyber)
#

it overwrite just the first update

jaunty galleon
#

Weird

torn sphinx
#

yeah

#

and no errors

#

like tf?

jaunty galleon
#

Send data

torn sphinx
#

i tried the second to not overwrite the _id

wheat python
# torn sphinx is this correct? ```py @bot.command(name="ban") @commands.has_permission...
@bot.command(name="ban")
    if ctx and member ctx.author.guild_permissions.ban_members:
    async def banuser(ctx, member: discord.Member, *, reason=None):
        userdata = {"_id": member.id, "Username": member.display_name}
        data = collection.find_one({"_id": member.id})
        
        if data is None:
            post = {userdata}
            collection.insert_one(post)

        print(member.id)
        if member.id not in userdata:
            print("Not in userdata")
            bannedusers.insert_one(userdata)

        elif member.id in bannedusers:
            await ctx.send(f"Trying to ban the User {member.display_name}...")
            try:
                await member.ban(reason=reason)
                await ctx.send("Banned this user Sucessfully")
            except:
                await ctx.send("error")

My idea

torn sphinx
#

still not changed anything

celest rain
#

heyo! just asking, how can i make it so the bot does:

Format: Discord ID | IGN | End Date | <ping>
('311383238373277697', 'NoNameWrath', '2021-06-15') @hard phoenix
('244212458670129152', 'Foetation', '2021-08-14') @forest forge
('789896054048686100', 'Wh1teLightning', '2021-06-23') @gleaming igloo
('444009219499229184', 'Robominer116', '2021-05-26') @open ocean
('448121879161602048', 'MindBlow42', '2022-05-18') @winter parrot```
but rn the bot does this:

```python
Format: Discord ID | IGN | End Date | <ping>
('311383238373277697', 'NoNameWrath', '2021-06-15') @hard phoenix
('244212458670129152', 'Foetation', '2021-08-14') @hard phoenix
('789896054048686100', 'Wh1teLightning', '2021-06-23') @hard phoenix
('444009219499229184', 'Robominer116', '2021-05-26') @hard phoenix
('448121879161602048', 'MindBlow42', '2022-05-18') @hard phoenix```

(if u dont see the difference, its basically, the numbers in discord ID should match with the <ping> so it would ping the right person) 

this is sqlite3 btw
sand trench
#

Hi guys. I need a bit of nudging to implement a feature. So, I'd like to check if a database exist in my flask app when I start the app by running python app.py. If it does not I'd like to create the database as well as create a default user with a password on the fly when the database is created. How do I go about this?

upper basin
#
        await self.bot.primedb.execute("create table if not exists userstrikes (caseid int autoincrement, guildid int, userid int, reason str, dateissued str, reminders int)")
#

hey im getting a syntax error near autoincremennt, can someone point it to me?

#

sqlite3.OperationalError: near "autoincrement": syntax error

#

wait to be able to use autoincrement in sql i need to have the field be a primary key?

iron drift
#
elif amount == 'all':
            cursor.execute(f"UPDATE users SET cash = {0} WHERE id = {ctx.author.id}")
            cursor.execute(f"""UPDATE users SET bank = bank + {cursor.execute(f"SELECT cash FROM users WHERE id = {ctx.author.id}").fetchone()[0]} WHERE id = {ctx.author.id}""")
            conn.commit()```
**how to make the bot select the entire user's balance and send this balance to the bank?**
jaunty galleon
#

SELECT * FROM users WHERE id = the id

jaunty galleon
quaint gyro
#

TypeError: index 'xp' cannot be applied to Cursor instances

silver summit
#

so....
maybe it sound horrible but....
I got a discord bot that I run on a free server which has 208 mb ram and....
I run SQLite db in it. how can I make it be fast?

#

it is kinda very slow

hazy smelt
#

Why does my MySQL database show connection timed out??

pearl hazel
#

Im new to databasing and need some help making a simple one can anyone help me?

opal dawn
#

How complicated is your form? If you can save form submissions as json, then you may not need a relational DB at all.

harsh pulsar
thick bolt
#

I'm using motor to interact with a mongodb database

#

What's the difference between each() and to_list()?

storm patrol
#

Hello there

thick bolt
#

o/

storm patrol
#

I have a problem

thick bolt
#

elaborate

storm patrol
#

I am trying to make a MySql database application with the Flask module. What happens is that Flask as such recognizes it, but when downloading and importing Flask MySQL it says that there is no module with that name.

slow sand
thick bolt
#

what is a callback?

slow sand
thick bolt
#

Thanks

keen gorge
#
SELECT * FROM reactionroles WHERE gid=%s AND cid=%s AND mid=%s AND reaction=%s OR role=%s

Does the or here works like just WHERE role=%s or is it like WHERE gid=%s AND cid=%s AND mid=%s AND role=%s?

jaunty galleon
#

You mean if it's like:

if i == "hi" or "hii":```
#

In PostgreSQL, doing WHERE i = ($1) AND z = ($2) works, so it should be fine

#

Or did you mean something completly different?

keen gorge
#

Hmm, i dont quite no how to say what I mean, lemme google real quick

jaunty galleon
#
if i == "i" and i == "blah" or i == "iii":```?
keen gorge
#

Yea like that

jaunty galleon
#

So yeah, I am pretty sure after a or statement it'll try to detect a new True

#

It's hard to explain lol

keen gorge
#

yea ik haha

#

so I have to put gid=%s AND cid=%s AND mid=%s also after the or?

#

because I need to check that too for the role

jaunty galleon
#

I am pretty sure you do need, but not sure...

keen gorge
#

Ill just try it out

#

ty for helping CB_ginger_love

jaunty galleon
#

np

slate moth
#

@flat wind

blissful knot
#

I need help with postgresql in python. this query coordinate_select = "SELECT fun_id, c1x, c1y, c2x, c2y, c3x, c3y FROM graphics_items_sorted WHERE fun_id = %s AND device_type = %s;" device_id = 'CAR-TV-102T' device_type = 'track_vector'
returns nothing when I use it like this: coordinate = cursor.execute(coordinate_select, (device_id, device_type)) and I know the values I am passing are in the database. What am I doing wrong?

spiral knoll
#

how do you know it returns nothing

#

how do you display it

#

@blissful knot

blissful knot
#

@spiral knoll Well I get this error, and I was using a debugger. Traceback (most recent call last):
File "/home/carl/OneDrive/Chicago_database_experiments/database_query_test.py", line 19, in <module>
coordinate = cursor.execute("SELECT * FROM graphics_items_sorted WHERE fun_id=%s AND device_type=%s", (str(device_id), str(device_type))).fetchall()
AttributeError: 'NoneType' object has no attribute 'fetchall'

ivory parcel
#

Hi guys, anyone could help me with a SQLite problem?

#

please

torn sphinx
#

@ivory parcel I was for question on sqlite as well, encryption. I made some progress with sqlite, what would you like to ask?

ivory parcel
#

i created a DB with SQLite

#

that i have 4 columns

#

names, gender, agr and ID

#

i wanna insert the data column by column

#

i will send a pic of what i got

#

im using a PySimpleGUI interface to insert the values

#

do you know how can solve this problem?

harsh night
#

what is a blob in sqlite

spiral knoll
#

The method returns None. If a query was executed, the returned values can be retrieved using fetch*() methods.

#

have you tried your sql query in the console?

#

open your sql console and execute the query as you would in your python script (use the actual values instead of the placeholders) and see if you get the response you expected.

blissful knot
distant hazel
#

If I were to receive massive XML files from a vendor (why?!) and it was the only format they could send

#

What tools or processes would you use to get it to something more user friendly for process automation and reporting?

#

It’s nested XML data with 3-5 main branches that are basically tables

jade swan
#

I'm trying to update data of only one dictionary inside a list of dictionaries, but when i use this code it changes the data of the whole list to only the dictionary given

def add_money(guild_id: int, user_id: int):
    results = users.find({"_id": guild_id})

    amt = get_amt(guild_id)

    for x in results:
        result = x

    members = result["members"]

    for x in members:
        if x["user"]["id"] == user_id:
            users.update_one(result, {
                "$set": {
                    "members": {
                        "user": {
                            "id": user_id,
                            "balance": x["user"]["balance"]+amt,
                            "items": x["user"]["items"]
                        }
                    }
                }
            })

How do I update the value of a specific dictionary only?

high geyser
#

would you recommend using mongodb for storing trillions of documents?
My use case is to do broad web crawling in parallel and continuously store web pages.
I am using django, and I see some libraries giving good support for mongodb.

As for other nosql databases, the support isn't that great.

#

please ping me when help, thanks!
Also, please let me know of any alternatives if you suggest any.
Yes, I wouldn't have trillions of documents when I start off, but
in my use case, documents never stop growing.

Also, I need to periodically update crawled pages.

slow sand
loud crane
#
import pymongo
from pymongo import MongoClient

cluster = MongoClient("mongodb+srv://<user>:<pwd>@cluster0.yqqvw.mongodb.net/Discord?retryWrites=true&w=majority")
db = cluster["Discord"]
collection = db["prefix"]

post1 = {"_id": "1", "prefix": "!"}
collection.insert_one(post1)
delicate fieldBOT
#

Hey @loud 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

loud crane
# loud crane ```py import pymongo from pymongo import MongoClient cluster = MongoClient("mon...
Traceback (most recent call last):
  File "D:\python projects\main.py", line 9, in <module>
    collection.insert_one(post1)
  File "D:\python projects\venv\lib\site-packages\pymongo\collection.py", line 698, in insert_one
    self._insert(document,
  File "D:\python projects\venv\lib\site-packages\pymongo\collection.py", line 613, in _insert
    return self._insert_one(
  File "D:\python projects\venv\lib\site-packages\pymongo\collection.py", line 602, in _insert_one
    self.__database.client._retryable_write(
  File "D:\python projects\venv\lib\site-packages\pymongo\mongo_client.py", line 1497, in _retryable_write
    with self._tmp_session(session) as s:
  File "C:\Program Files\Python39\lib\contextlib.py", line 117, in __enter__
    return next(self.gen)
  File "D:\python projects\venv\lib\site-packages\pymongo\mongo_client.py", line 1829, in _tmp_session
    s = self._ensure_session(session)
File "D:\python projects\venv\lib\site-packages\pymongo\mongo_client.py", line 1816, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "D:\python projects\venv\lib\site-packages\pymongo\mongo_client.py", line 1766, in __start_session
    server_session = self._get_server_session()
  File "D:\python projects\venv\lib\site-packages\pymongo\mongo_client.py", line 1802, in _get_server_session
    return self._topology.get_server_session()
  File "D:\python projects\venv\lib\site-packages\pymongo\topology.py", line 501, in get_server_session
    self._select_servers_loop(
  File "D:\python projects\venv\lib\site-packages\pymongo\topology.py", line 215, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError:
high geyser
#

I wouldn't start with trillions

#

but, I just wanted to know if mongodb is a smooth option long-term

#

or else, I can do something similar to discord- start with mongodb because we have excellent support for it in django (3rd party)

#

and then move onto something like cassandra when mongodb cannot scale

#

the main problem is locking, I guess?

#

@slow sand

#

I also found papers specifically advising not to use relational databases

#

whats your take on this?

slow sand
# high geyser whats your take on this?

Locks are not a problem, I'd say. If this is a problem - something should be changed in the schema design. About the relational vs nosql databases - this holywar will not end never 🙂
I'd suggest to start first of all. You will change your schema many times during the development process. It is impossible to predict everything in the early stage. Probably you will need more than one database. Something like document db for the metadata + append-only fast timeseries for the append logs. and etc.
I would choose mongodb for this. It s very flexible - will fit the changing requirements well.

high geyser
#

thanks for the response!

chilly nimbus
#

because i dont know any database languages, can I use Django's orm feature in the development of my discord bot, i mean, is it possible to use django to update and fetch data from my database?

#

ping me if you know

trim marten
high geyser
storm kayak
#

hello, i'm using django's ORM, I have a question, I tried to write a query into phpmyadmin, and it works like i've expected. However, when I'm trying to use Django ORM like x.objects.related_select('y'), Because my query join multiples tables, my request doesn't work and even if I try to use .raw(), the output doesn't work...; Is anyone can help me

real cliff
real cliff
blissful knot
pine geode
#

I'm trying to delete some duplicates in a postgres db using SQLAlchemy. I'm following the example here:
https://stackoverflow.com/a/63764238

I can't get mine to work. The difference is I have an additional filter.

 q = session.query(func.min(Price.id)).filter(
            Price.listing_id == Listing.id,
            Listing.page_id == Page.id,
            Page.scrape_id == self.id,
            Price.processing_level == 
            processing_level).group_by(Price.listing_id)
aliased = alias(q)
q2 = session.query(Price).filter(
            ~Price.id.in_(aliased))
print(q2.count())
#

The following works, but I feel like this isn't the right way to do it:

 q = session.query(func.min(Price.id)).filter(
            Price.listing_id == Listing.id,
            Listing.page_id == Page.id,
            Page.scrape_id == self.id,
            Price.processing_level ==   
            processing_level).group_by(Price.listing_id)

price_ids = q.all()
price_ids = [row[0] for row in price_ids]

q2 = session.query(Price).filter(
            ~Price.id.in_(price_ids))
print(q2.count())
#

Error message when I try to run the first example:

LINE 4: WHERE price.id NOT IN (SELECT min(price.id) AS min_1
                                      ^
chilly nimbus
#

I need to create a currency system for my discord bot, I dont want to learn any other languages for now, for now i just need something with which I can implement my currency system

#

so should I learn psycopg2 directly or postgres first

#

will I understand psycopg2 without learning postgres

burnt turret
#

you'd need to know SQL to be able to use postgres

#

psycopg2 is just a module for interacting with the postgres database

#

you'd be writing SQL queries anyways, so that would be what you should learn first

chilly nimbus
#

:<

#

do i have to learn sql then postgres and then psycopg?

#

but thats true i am not understanding anything

#

about postgres and psyopg2

#

doesnt mattter how hard i try

burnt turret
#

learning SQL is the only major part in there

chilly nimbus
burnt turret
#

using the modules are pretty easy

burnt turret
chilly nimbus
#

bro, its all making me sad

#

:<

chilly nimbus
#

😢🙂

worthy shard
#

DOes Python work with Fauna

proven arrow
molten echo
#

hi there

#

Anyone familiar with cx_oracle?

#

my issue: I need to be able to deploy that lib + what's needed for it to work (oracle instant client) on an AWS lambda, but the size is way tooo much (150+MB). Anyone knows how to make it lighter, or any alternative that is easier to set up without external files?
Please ping me if you answer, I might miss the answer otherwise.

inner ivy
#

whats a good database if i want it to simply save a guild id + a channel id and then have the option to delete the chanel id associated with a certain guild id

hexed estuary
#

if that's all you need, no reason not to use a simple relational database like sqlite

simple kelp
#

Bit of a specialised question, but does anyone know how I can use PyCharms "Language Injection" to syntax highlight SQL strings with aiosqlite? It appears to have built-in support by default for sqlite3, but not aiosqlite. Thanks in advance!

harsh pulsar
#

i should try configuring this myself

simple kelp
remote plinth
#

can someone explain how to use local host db in mongo

#

using motor driver*

#

that is the right channel

#

its dead tho hehe

#

@burnt turret can u help?

slow sand
remote plinth
#

idk if there is a different but i never used localhost

#

i want to know how..

slow sand
#

No difference at all from the application side. Just set the right url. The simplest way to run mongo is to use docker. Here there are a few examples: https://hub.docker.com/_/mongo/

remote plinth
#

i want the db in my host server and i cant access to a lot of things

smoky turtle
#

Hello, I'm not sure if this is the right spot for this, but I'm attempting to host a web-app online, and am using a MySQL database that's done through Google Cloud Platform's SQL. I have connected to it via Flask-SQLAlchemy, but I am having trouble actually creating the tables.

When running locally, I can just do as follows, but with my understanding, this code is not run when being deployed through Google App Engine.

if __name__ == "__main__":
    db.create_all()
    app.run(host='127.0.0.1', port=5000, debug=True)
    

The error that I'm getting through GCP is ProgrammingError: (1146, "Table 'file_info.file_indices' doesn't exist") so I'm assuming that the db.create_all() code hasn't run. The issue is, I'm not quite sure how I can run it, as it appears that I only need this to be run once in the Flask app.

Here is the dabase model:

class FileIndices(db.Model):
    _id = db.Column(db.Integer, primary_key=True)
    user = db.Column('user', db.String(80), unique=False, nullable=False)
    filename = db.Column('filename', db.String(200), unique=False, nullable=False)
    file_info = db.Column('file_info', db.String(500), unique=False, nullable=True)
    file_type = db.Column('file_type', db.String(20), unique=False, nullable=False)
    summary_generated = db.Column('summary_generated', db.Boolean, unique=False, nullable=False)
    transcript_generated = db.Column('transcript_generated', db.Boolean, unique=False, nullable=True)

    def __init__(self, user, filename, file_info, file_type, summary_generated, transcript_generated):
        self.user = user
        self.filename = filename
        self.file_info = file_info
        self.file_type = file_type
        self.summary_generated = summary_generated
        self.transcript_generated = transcript_generated

jaunty galleon
zenith scarab
#

hello how can i count items from a csv file (iris.csv) to plot it

proven arrow
zenith scarab
#

matplotlib

#

Ehre für den namen an der stelle

simple kelp
#

Quick question, is this formatting OK? (4 space indents, but 2 space indents for SQL string)

mortal scarab
#

@simple kelp match the indents all around ur code

#

SQL is indent independent ]

simple kelp
mortal scarab
#

yes

simple kelp
#

Apart from that, is the formatting ok?

#

this just looks a bit awkward to me

mortal scarab
#

it depends on how u like it

#

its ok

#

i have way large table creation methods

#

just live wit it lol

simple kelp
#

ok, thanks

#

(Also I must say that PyCharm's string language injection is godly)

#

I just wish the injection rules worked properly

torn sphinx
#

can you guys help me with how can I send data from an existing form website to SQL with python?
I am using mysql.connector to connect to the server all working properly.
the question is how to send data through

proven arrow
#

You may have to count it using something pandas. Not sure if you can do it with directly matplot but best to ask in #data-science-and-ml for better answer @zenith scarab

proven arrow
torn sphinx
proven arrow
torn sphinx
#

the python file is working

#

but how can I send data from the placeholder to sql?

#

I am sure it is super simple but I am new with python

proven arrow
#

I thought you were using some sort of framework for this.

torn sphinx
#

can I do it without frameworks?

proven arrow
#

The idea of how it should work is, your sever/app has an endpoint where it can receive data. Then you give this route to the HTML form through the action attribute and so whenever you submit the form your severs endpoint will receive whatever data was submitted through the form as a request.

torn sphinx
#

thanks

#

do you think better if I am using flask or django?

zenith scarab
proven arrow
torn sphinx
#

thanks

#

I might come back to you:)

bold ether
#

hi

#

iam new

#

in database

split ore
#

hey guys

#

can u tell me how to use the MySQL module?

split ore
#

ok

#

thanx

calm prawn
#

Can somone tell me how can I check what are the tables I have in my postgresql database

dusk junco
#

So, I have the following question:

I have a postgresql data volume (Docker) which is being shared with other developers in my team, but the thing is, whenever I push something into our repo, even tho the data volume is being shared, the DB tables are empty and the DB is not created at all, as it should be. With this in mind I have two questions, actually:

  1. How can the tables be already created inside my Docker image?
  2. How can the table data I have in the image running locally on my machine, be shared with the other developers?
dusk junco
calm prawn
#

okay thanks

proven arrow
dusk junco
#

I admit it's quite strange pushing volumes to git

proven arrow
#

So then make a seeder/script that populates the db.

dusk junco
#

The thing is: this "data" should be actual data from an API

proven arrow
#

Do you have any factories or faker libs that can easily create data for you? If so might make the process a little easier.

dusk junco
#

My app populates the DB with data retrieved from an API

#

Then, VueJS has a listener for making requests to the DB

proven arrow
#

So mock an api response then

#

And add it to db

dusk junco
vast flower
#

Anyone got experience with using mysql-connector-python for writing large amounts of data?

calm prawn
#

If i connect my database in @tasks.loop() will it stay connect after initial connect or do i have to connect it after some time.

I am using postgresql

#

Will I mean when my discord bot launches it will run the function under @task.loop()

#

which will then add self.connection object

rich ravine
#

hey everyone! Can somebody tell me some free course (MOOC) about postgreSQL? I´m trying to learn databases...

jaunty galleon
rich ravine
#

thanks!

jaunty galleon
#

Thank anand

torn sphinx
#
    async def limit_set(self, ctx, new_limit: int):
       try:
         info = antilimit.find_one(
            {'guild_id': ctx.guild.id}
         )
         blimit = info['ban_limit']
         blimit[0] = new_limit
         antilimit.update_one(
            {'guild_id': ctx.guild.id},
            {
                '$set': {
                    'ban_limit': blimit
                }
            }
         )

returns - TypeError: 'int' object does not support item assignment

anyone know a fix? also idk if this would go in #discord-bots

jaunty galleon
#

Send full error

torn sphinx
#

@torn sphinx

jaunty galleon
#

Yeah I didn't understand lol

burnt turret
jaunty galleon
#

Damn anand is here problem fixed lemonshake

torn sphinx
#

wdym

burnt turret
torn sphinx
#

It is me and saiv’s question

burnt turret
#

anyways, you can greatly simplify that function by using $inc

#

instead of $set

finite ice
burnt turret
#

ah right right

#

wait a minute i'm a little confused: what exactly do you want the function to do?

torn sphinx
#

to change the number that is said and put it in the db

burnt turret
#

you want to set the ban_limit in the database to new_limit, or do you want to increment the value by new_limit

#

so you just want to set it right? why are you even getting the old value from the db in the first place?

torn sphinx
#

yea

#

idk

burnt turret
torn sphinx
#

ok

burnt turret
#

and just do the update_one

#

and $set it to new_limit

torn sphinx
#

ok

#

We want the limit to save to the db, and the bot to see that that’s the limit

#

After said limit bans it would punish the member

burnt turret
#

yes, and the bot gets the number from the database right?

torn sphinx
#

Yes

burnt turret
#

so updating it in your database means your bot "sees" that new limit too?

#

i don't understand why you are retrieving the old data in this case, when you just need to update

torn sphinx
#

I belive we got it to work

#

yes

#

thank you

#

and I didn't realize it was trying to read old data

#

God mobile sucks

vague oak
#

guys

past current
#

Hello Guys, how would a follower/following table look a like? what would be the relation between the tables and the user?

wintry stream
torn sphinx
#

for "find_one", would it be only used for an array or does it not matter?

jaunty galleon
torn sphinx
#

so if im trying to get my program to find something from the database would using "find_one" work even if what its trying to find isn't an array and is just one thing ?

#

an array that would be added into the collection ^^

#

nvm

#

findOne() - if query matches, first document is returned, otherwise null.

find() - no matter the number of documents matched, a cursor is returned, never null.

jaunty galleon
#

find_one returns dict object for the first document it finds by the query

proven arrow
#

And, example queries would be:
Get number of followers for a user:

SELECT COUNT(*) FROM followers WHERE followed_id = ?

Get all users following someone:

SELECT follower_id FROM followers WHERE followed_id = ?
past current
zinc sage
#

Hey,

I'm working with a peewee sqlite database, and I've encountered some very weird behavior and I can't seem to figure out why or how this is happening.

So I have this function. It creates a new ListItem and stores it to the database.

class List(Model):
    # snip

    def append(self, value):
        new_item = ListItem(parent=self)
        new_item.set_value(value)
        new_item.save()
        print("Value from object:", new_item.value)
        print("Value from queried object: ", ListItem.select().where(ListItem.id == new_item.id).get().value)


>>>

Value from object: 1
Value from queried object:  <class 'peewee.CharField'>

the ListItem has a property value, when reading directly off it it works. But when I query the object and then try to read off it (after saving, notice new_item.save()) it has the peewee.CharField type as a value.

Does somebody know what is happening here? And can they explain :?? All help is appreciated

harsh pulsar
#

what is a ListItem? is that a peewee thing?

green raptor
#

hey guys i am stuck, can someone help me? Please ping me

**ERROR: **

Traceback (most recent call last):
  File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "W:\SupportBot\bot.py", line 104, in ticket
    await create_ticket_db(userid, issue)
  File "W:\SupportBot\bot.py", line 65, in create_ticket_db
    sql.execute("INSERT INTO SupportTickets (UserId, Issue) VALUES (?, ?)", (userid, issue))
sqlite3.IntegrityError: UNIQUE constraint failed: SupportTickets.UserId

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Jakob\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: IntegrityError: UNIQUE constraint failed: SupportTickets.UserId```

**CODE:**
`# CREATE TICKET IN DATABASE
async def create_ticket_db(userid, issue):
    # sql.execute("INSERT INTO SupportTickets (UserId, Issue) VALUES (" + str(userid) + "," + issue +")")
    sql.execute("INSERT INTO SupportTickets (UserId, Issue) VALUES (?, ?)", (userid, issue))
    conn.commit()`
proven arrow
#

And im not sure why you decided to add a unique constraint to the users field, as it would mean a user can't create more than one support ticket which is strange to see.

green raptor
# proven arrow Your error is trying to say, when you created your table you specified a `unique...

Error:

Traceback (most recent call last):
  File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "W:\SupportBot\bot.py", line 106, in ticket
    channelname = "ticket-" + str(await get_support_ticket_id(userid))
  File "W:\SupportBot\bot.py", line 54, in get_support_ticket_id
    sql.execute("SELECT TicketId FROM SupportTickets WHERE UserId = " + userid)
TypeError: can only concatenate str (not "int") to str

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Jakob\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: TypeError: can only concatenate str (not "int") to str```
#

there is no unique data doubled

proven arrow
green raptor
proven arrow
green raptor
#

it is writing, but sending error

proven arrow
#

Because your entering it twice lol like i already said

green raptor
#
async def create_ticket_db(userid, issue):
    sql.execute("INSERT INTO SupportTickets (UserId, Issue) VALUES (?, ?)", (str(userid), issue))
    conn.commit()```
proven arrow
#

That doesn't tell me anything of where the function is called or how

green raptor
# proven arrow That doesn't tell me anything of where the function is called or how
@bot.command()
async def ticket(ctx, *, issue = None):
    userid = ctx.message.author.id
    if ctx.author != bot.user:
        # CHECK IF TICKET IS EMPTY
        if issue == None:
            # ERROR - NO TICKET IS EMPTY
            await ctx.send("Der Inhalt deines Tickets sollte nicht leer sein: ticket <grund>")
            return
        # Create Support-Ticket
        await create_ticket_db(userid, issue)
        # CREATE SUPPORT-TICKET-CHANNEL
        channelname = "ticket-" + str(await get_support_ticket_id(userid))
        await ctx.guild.create_text_channel(channelname)
        # set channel permission
        # send embed ticket message
    else:
        return```
#

thats the code

torn sphinx
#

anyone want a nuke bot

proven arrow
#

@green raptor See the code and error it raises #bot-commands message
Theres only one reason why such an error can happen and ive explained it to you. So you'll have to figure out where its calling that code twice or where its inserting multiple times.

proven arrow
torn sphinx
#

how can I format the string so it looks like sql text?

#

its very hard to read if it doesnt use idfferent colors

#

its pycharm btw

#

Say could we use databases to come up with a specific algorithm

proven arrow
#

Use multi line strings if you want to make it more readable

proven arrow
#

Hes using a different IDE.

#

You can try looking at your IDE settings for similar thing

torn sphinx
#

so its possible but not for pycharm?

trim marten
#

pycharm can definitely do sql syntax highlighting

torn sphinx
#

does vs support it for free?

proven arrow
torn sphinx
#

I think I should switch to visual studio :o

#

its says yes

obtuse glacier
#
                db = sqlite3.connect('bury.sqlite')
                cursor = db.cursor()
                sql1 = f"SELECT ground FROM buried WHERE guild_id = {ctx.message.guild.id}"
                cursor.execute(sql1)
                deleted = random.choice(cursor.fetchall()[0])
                db.close()

the command for my discord bot breaks when i change the above SQL to the below SQL... why?

                db = sqlite3.connect('bury.sqlite')
                cursor = db.cursor()
                sql1 = f"SELECT ground FROM buried WHERE guild_id = ?"
                val = ctx.message.guild.id
                cursor.execute(sql1, val)
                deleted = random.choice(cursor.fetchall()[0])
                db.close()
torn sphinx
#

Hi is it okay to ask about openpyxl here?

inner ivy
#

how do i loop through users to reset their invites in a text based db file?

#

without using a for loop

#

bc that takes too long

rain plank
brave bridge
#

The slowest part is probably resetting an invite.

#

You can do several network calls concurrently

inner ivy
copper depot
#
  get_balance = f"SELECT balance FROM ID WHERE name=(%s)" #:(((((
  val = (token_user, )
  balance_getting = mycursor.execute(get_balance, val)
  balance = mycursor.fetchall()
  mydb.commit()

i have this code but the balance is only coming 0 even though it's 10
('d26783af0421f917a8aac69d59125231', '10')]

velvet flicker
#

Hello. How can i increment a value in a table ?
I tried this but it doesn't seem to work

 c.execute(f"SELECT warns FROM Moderation WHERE user_id = {user.id} AND guild_id = {ctx.guild.id}")
                warnings = c.fetchone()
                amount = warnings[0]
                c.execute(f"UPDATE Moderation SET warns = {amount} + 1 WHERE user_id = {user.id} AND guild_id = {ctx.guild.id}")
frigid glen
#

warns = warns + {amount} perhaps?

#

or if incrementing by 1

#

warns = warns + 1

#

remove the {amount} if just increment by 1

velvet flicker
#

ok that leaves me with this

 # c.execute(f"SELECT warns FROM Moderation WHERE user_id = {user.id} AND guild_id = {ctx.guild.id}")
                c.execute(f"UPDATE Moderation SET warns = warns + 1 WHERE user_id = {user.id} AND guild_id = {ctx.guild.id}")

Correct ?

#

Ok seems to be working. thanks mate

torn sphinx
#

this is my first try to design a database

#

any hints?

#

data base guys where are you :d

vernal glacier
#

is user_id in table XP the same as in user settings? if so then it should be a foreign key in XP as well

torn sphinx
#

yep its the same id

#

but the user profile needs to be independent from a server

velvet flicker
#

It keeps saying no such column

c.execute(f"INSERT INTO Moderation(user_name, user_id, guild_id, warns) VALUES ({user.display_name}, {user.id}, {ctx.guild.id}, 1)")

It thinks user.display_name is column idk why

quartz walrus
#

There's a better way to do that

torn sphinx
#

that's what god_of_chaos said

#

:D

quartz walrus
#

Smth like this
c.execute("INSERT INTO moderation(user_name, user_id, guild_id, warns) VALUES (?,?,?,?)" ,(user.display_name, user.id, ctx.guild.id, 1) )

#

Try this one

#

I just typed it roughly so tell me if you get an error

velvet flicker
#

Oh it worked thanks

#

but what was wrong with the other way ?

quartz walrus
#

There are some complications when using those expressions

#

If you want you can read the documentation

smoky tartan
#

this is my csv dataset anyone knows how can I remove entire lines? for example just remove the parameter of year and remove year from every single example

quartz walrus
smoky tartan
#

yu[

#

yup* sry

#

i forgot the word

quartz walrus
#

do you know how to do it then?

#

well, i hope ur using pandas
when u make a dataframe object, let's say df
you can say :
del df['year']

real cliff
real cliff
# torn sphinx this is my first try to design a database

I also see a lot of columns being strings. this should be avoided. If you have definite options for possible values for a column then either use ENUM or INT with enum mapped in application layer. even after proper indexing, lookup on INT will be way faster than that of strings

smoky tartan
quartz walrus
high geyser
smoky tartan
#

is there a way to remove all of these in one command?

harsh pulsar
# smoky tartan is there a way to remove all of these in one command?

if you know you only want to read certain column names, you can use the usecols option in read_csv https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html#pandas.read_csv

in your case, if you want to omit certain column names, you can put those column names in a list or set and use a function:

invalid_names = {'prestige', 'widowed', 'reg16'}

def is_valid_column(colname):
    return colname not in invalid_names

df = pd.read_csv('data.csv', usecols=is_valid_column)
#

also in the future please post your code as text, not as a screenshot

#

!code-block

delicate fieldBOT
#

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

harsh pulsar
harsh thistle
#

Why does python with MySql suck? its just too much of a headache ..
Are there any other options available to store my python application's data and retrieve when needed...(Except SQL please, i hate it already)

#

pls ping if u answer

burnt turret
#

What have you been having difficulty with?

harsh thistle
#

Its so unreliable, like using connecter i've trying inserting data, the script runs but it aint even inserting it

#

Code's completely fine

burnt turret
#

Never had any issues of that kind; are you sure you've been commiting the changes as well?

harsh thistle
#

and i always have to check my sql back to make sure everything's working

harsh thistle
harsh pulsar
#

that said, personally i much prefer using postgresql and its associated python libraries (psycopg2, aiopg, asyncpg). i think postgres is a better "default" choice for most projects.

burnt turret
#

Aiomysql not being actively maintained is a real bummer lemon_pensive

harsh thistle
harsh thistle
harsh pulsar
#

also - if you share the code that you are struggling with, someone here might be able to help.

harsh thistle
tribal cargo
#

Hello there, I created two Tables:

#

class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)
    usertype_id = Column(Integer, ForeignKey('usertype.id'))
    usertype = relationship("Usertype", back_populates="users")
    name = Column(String(20), nullable=False,unique=True)
    password = Column(String(60)) #wird vom Dekorator aus password.py genommen
    eMail = Column(String, unique=True, nullable=False)

    
# Parent für User --> ein Ustertype hat n User
class Usertype(Base):
    __tablename__ = 'usertype'

    id = Column(Integer, primary_key=True)
    type = Column(String)
    users = relationship("User")
#

The idea is, that each user has a usertype with different possibilities on the webapp.

#

I'm not sure how to add the foreinkey in my form to add the data. Do I just add a number from wich I know what usertype she represent or can I call the Usertype class direct?

torn sphinx
smoky tartan
#

is there a way to sort columns (like if my columns are now 1-2-3 and I want them to be 2-1-3 and is there a way to remove an unnamed column without naming it?

real cliff
torn sphinx
harsh pulsar
#

@tribal cargo in general you should state what library you are using, it looks like django but you shouldn't make people guess

frigid glen
harsh pulsar
torn sphinx
#

does mySQL support bools?

real cliff
# torn sphinx but sqlite doesnt support booleans

oh. Generally if you are using something like django then it will take care of it internally and replace boolean with smallest number. If you are using it raw then you don't really have option. Also, AFAIK, sqlite doesn't care about types that much anyway

harsh pulsar
torn sphinx
#

then I think I will stick to mysql with 0s and 1s

#

But I also guess that I dont have to care about file sizes since its a pretty small project and I have a heavy serv for it

#

nevertheless, its good to get used to efficient solution

modest needle
#

Hello I'm a beginner in SQL and I'm doing a lab in SQL injection. I'm wondering when you send a querry like this one : "SELECT * FROM T_Client WHERE utilisateur = « Harold » AND password = «1234»" does it return a boolean ? Because a way to bypass a login form is to state that '1'='1'. Or does WHERE have a special behaviour when putting a True statement?

upper basin
#

is there a way to have an if-else in sqlite? like i have a table with userid, character, numberofcharacters, and what im trying to do is if character not in the database, insert userid, character, numberofcharacter else numberofcharacters += 1 where userid and character = (random)

harsh pulsar
#

not all databases have a native boolean type, sometimes it's just 1 and 0

real cliff
harsh pulsar
#

in fact i think most databases don't even let you get a "return value" from AND and OR

harsh pulsar
real cliff
harsh pulsar
#

!e ```python
utilisateur = "a' OR 1=1;"
query = f"SELECT * FROM T_Client WHERE utilisateur='{utilisateur!s}'"
print(query)

delicate fieldBOT
#

@harsh pulsar :white_check_mark: Your eval job has completed with return code 0.

SELECT * FROM T_Client WHERE utilisateur='a' OR 1=1;'
harsh pulsar
#

sql injection works because people don't do input escaping properly, not because sql itself has some special behavior when you append OR to a query

modest needle
#

Thanks for the clarifications !

real cliff
#

yeah, but one has to be aware or be able to speculate what will be the actual query after injection

vernal glacier
real cliff
#

and hence using OR and -- at the end helps. -- in sql is comment, which helps you discard everything if any in the application code for that query

tribal cargo
upper basin
#

i've tried doing an on conflict update, but i get a ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint error

#

for context

#

insert into userchars (userid,charobtained,charcount) values (?,?,?) on conflict(userid,charobtained) do update set charcount = charcount + 1',(ctx.author.id,obtained,1)

#

create table if not exists userchars (userid int, charobtained text, charcount int)

#

is there something im missing?

#

im using (userid,charobtained) as a primary key

brazen charm
#

you havent actually marked it as a primary key or unique though

#

Although you may treat it like it is

#

SQL doesnt treat it like it is unless it's marked

#

create table if not exists userchars (userid int PRIMARY KEY, charobtained text UNIQUE, charcount int)
is what you would need to have to be able to use that upsert statement

upper basin
#

can i do create table if not exists userchars (userid int PRIMARY KEY, charobtained text UNIQUE, charcount int PRIMARY KEY(userid, charobtained)) instead?

brazen charm
#

Not really, that statement doesnt make much sense

upper basin
#

mb fixed

brazen charm
#

why do you want PRIMARY KEY(userid, charobtained) instead of just having the constraint next to the column

#

PRIMARY KEY marks that column as being the primary key

#

UNIQUE adds the unique constraint

upper basin
#

i don't think charobtained will be unique, like

#

one sec

#

this is what the table looks like, there are multiple same userid and multiple same charobtained

#

so i dont think either would qualify for a primary key/unique on their own? i could be wrong tho

harsh pulsar
#

i think the composite primary key makes perfect sense here

#

ah, never mind... i see the issue

#

you'd need a unique constraint over userid, charobtained, and charcount

#

actually yeah i think the primary key is what you want

#
CREATE TABLE userchars (
  userid int,
  charobtained text,
  charcount int,
  PRIMARY KEY (userid, charobtained)
);

INSERT INTO userchars (userid, charobtained, charcount)
  VALUES (1234, 'john', 1);
INSERT INTO userchars (userid, charobtained)
  VALUES (1234, 'john')
  ON CONFLICT DO UPDATE SET charcount=charcount+1;

is this not what you want?

smoky radish
#

A question that came up at work just now:

database version: MariaDB 10.3.29
say we have a table like this

key | val | overwriteable 
-------------------
1   | 5     | 1
2   | 8     | 0
4   | 9     | 1

Now the issue is, i want to do bulk inserts which should only replace certain rows.
say, if i do a insert like this

INSERT INTO table (key, val) VALUES (2, 9), (3, 6), (4, 5)

This exact query would fail due to a duplicate key
how would i write a query which can insert new rows (3, 6), overwrite rows that have overwriteable set to 1 (4, 5) and do nothing to rows where overwriteable is 0 (2, 8)

I imagine something like a conditional ON DUPLICATE KEY UPDATE, but i dont think thats possible.

Some more context: a row which is not overwritable would be quite rare, so one possiblitly i thought of is selecting the non-overwritable rows, then overwriting them anyways, and writing back the non-overwritable ones after to reset them to before the insert.

upper basin
harsh pulsar
#

@upper basin i did too actually, i probably misunderstood something in the sqlite docs

#

or maybe it needs some compiled extension

upper basin
#

well rip i think ill just try to do it within python itself

harsh pulsar
smoky radish
#

i did rewrite it as a IF instead, since thats easier to read IMO

#

ON DUPLICATE KEY UPDATE val= IF(overwritable = 1, Values(val), val);

inner sentinel
gaunt garden
#

Hey, im trying to store a raw dict inside of a sqlite database. I've tried making the data type blob but im getting the "Error binding parameter - of unsupported type"

#

Is there even a way to do this? or is there some type of work around i can do.

opal finch
#

Who know a PyMongo?
My code is not connecting to the database
I connected everything, but the data does not appear in the database

jaunty galleon
tough mist
#

Hello all,
I'm trying to set up a couple functions for a discord bot, all of which with the need to interact with the same database (some functions will add data to it, others use the data). As such, this means that I'll have asynchronous functions, running in parallel, and a high chance of them sending requests to the database at the same time.
As such, would you recommend using sqlite or mysql (I would go with sqlite, since it is given as simpler to use, but doesn't use a separate server, and I'm afraid I could lose data if two functions attempt to write to the same table simultaneously).
Any advice from those more experienced than me ?

hexed estuary
#

I don't think sqlite is vulnerable to problems with simultaneous writes; I think it would just block the second connection until the first finishes.

#

(that doesn't mean it's intended for concurrent access much, but at least it shouldn't be corrupted if you do)

proven arrow
#

It implements ACID so your good on that part.

brave bridge
harsh pulsar
#

It does support concurrent writes with WAL but not nearly as efficient

serene pivot
#

Hey! So I have a question. Basically, I have a table of names, and I want to insert name when my application runs. Here's an example, When a user has a name, say John, and I insert that name, I do not want to reinsert John when the application restarts. However, if John changed his name to Sam, and I recorded that, and he changed his name back to John while my application was not running, I would want to reinsert John back into the table. Creating a unique constraint on the name wouldn't do it. I'm hoping for a more efficient solution than selecting from the table and comparing the current name with the name that has the largest timestamp. (latest inserted name)

zenith iron
#

I TRIED EVERYTHING

#

still gets this

burnt turret
gaunt garden
#

I actually just stored it as a string and just used json.loads to load it back in

burnt turret
#

That's what I said :p

gaunt garden
#

so yeah pretty much the same 😂

calm prawn
#

If I try to delete a tag which is not present in a table what am I suppose to get
My command
"""DELETE FROM tags WHERE LOWER(TAGS) = %s"""

I am using postgresql

shy beacon
dusk junco
#

I have a question regarding Postgres using Heroku. I have a database hosted on the Heroku postgres add and I can establish a connection to it, but I can't seem to insert data into it or even run queries, just connect. DOes anyone know what can be the cause for it?

obsidian thunder
#

which database is best for python ?

pure sleet
obsidian thunder
pure sleet
obsidian thunder
pure sleet
obsidian thunder
obsidian thunder
proven arrow
# obsidian thunder whats best for webdev?

You will most likely want a relational database for that. If your app has some sort of data access layer with the right level of abstractions, then it will be easy to change the database you use later on, so you might want to keep that when your creating your project.

obsidian thunder
proven arrow
#

Exactly

#

And its quite common that people will use sqlite for local dev and on production they will switch to something else, with the same codebase

median swift
#

So I am using asyncpg. The following code outputs this. How can I loop over each record?

warnings = await self.client.conn.fetch("SELECT * FROM moderation WHERE type = $1 AND userid = $2 AND serverid = $3", "Warn", str(user.id), str(ctx.guild.id))
print(warnings)
[<Record id=2 type='Warn' reason=None userid='562901935267774480' serverid='12345678' date=None>, <Record id=3 type='Warn' reason='test' userid='562901935267774480' serverid='12345678' date=datetime.date(2021, 5, 21)>]
#

For example, If I wanted to print it to the terminal like this:

ID = 2, Reason = None
ID = 3, Reason = test
...
...
proven arrow
#

They are just objects, so you can loop over them as you would do with anything else and access that value for each of them

median swift
#

So like warnings[0] would output the first record?

proven arrow
#

Yes first record object, which would be <Record id=2 type='Warn' reason=None userid='562901935267774480' serverid='12345678' date=None>

median swift
#

ok that is interesting, could I use this ( warnings[0] ) to get reason?

#

actually wait I think I figured that out

#

thanks :))

upper basin
#

i need some help setting up a database. basically, i need to store userid, multiple objects the user has, and the amount of objects each user has. the only thing i can think of is a table with create table userinventory( userid int, object text, amount int, primary key(userid, object) . Is there a better/more efficient way of doing this?

proven arrow
upper basin
#

they can be given to multiple users

#

theres a set pool of objects, say [a,b,c,d], anyone can have any amount of these

proven arrow
#

Ok then what you have is almost correct. Except 2 things.

#

Do you already have a items table?

upper basin
#

nope

proven arrow
#

Oh

#

Ok then.
So you need to make an items table. In this you just store details of the item like id, name, price, ...
Then in your new user_inventory table you have 2 columns (user_id, item_id). Both of these are primary keys, and they both are foreign keys as well to their respective tables. This is basically how you achieve a many-to-many relationship which is what you need here.

upper basin
#

the item has no details, its just a name

proven arrow
#

And you don't need to store the amount because it will be redundant, and you should only store this when you have performance problems.
As the saying goes, A man with one watch always knows the time. A man with two watches is never sure.

proven arrow
keen lily
#

guys , how can I fix flask_sqlalchemy import ? if I have installed it but I could not import to my script.

#

pip show flask_sqlalchemy

#

Name: Flask-SQLAlchemy
Version: 2.5.1
Summary: Adds SQLAlchemy support to your Flask application.
Home-page: https://github.com/pallets/flask-sqlalchemy
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD-3-Clause
Location: c:\users\polcs\appdata\local\programs\python\python39\lib\site-packages
Requires: SQLAlchemy, Flask
Required-by:

upper basin
keen lily
#

It is weird cuz I can import it if I am in other directories except this one that I am working in

upper basin
#

in dict form itd look like {user: {"objects": {"A": 9, "B": 1, "C": 0}}}

proven arrow
#

If you want to count the number of objects a user has use the COUNT() function provided by the database. Why do you need to calculate the count yourself? Let the database do it. And this way you have a single source of truth. If you store the count seperatly you can never guarantee the value you store is always correct.

torn sphinx
#

Hey, I have this warnings system for my discord bot that I am creating. The warn command works fine it's just the warning command that I am having trouble with. I am trying to format the command so it looks cleaner but when I run the command the bot doesn't respond and it doesn't give me an error.

Code 👇
https://paste.pythondiscord.com/ilulimuluf.py
I am using MongoDB and PyMongo

torn sphinx
#

The bot doesn't respond

#

And I don't get an error

#

@jaunty galleon ☝️