#databases

1 messages · Page 83 of 1

maiden sonnet
#

( @rich trout I got it working (not optimized for now but it works), indeed you hinted me in the right direction. All I needed to do actually was replace the line trivia["category"] = { "category": raw_category } in the controller with del trivia["category"] . That way Marshmallow does not try to immediately create the object for category, and later I can attach it normally)

#

Now, will try to clean it up 🙂

rich trout
#

oh, nice

#

👍

pearl heath
#

@maiden sonnet wait until you have to access dict of dicts of dicts lol

maiden sonnet
#

ahaha, I hope by then I will handle it more robustly 😉

pearl heath
#

lots of [0][0][0]

maiden sonnet
#

I'd love to write more functional code in Python

#

I have worked more with JS lately where that seems a tad easier

valid turtle
#

Hi, is there a kinda local DB. I know I clould start a MYSQL DB or something but is there a way to use a DB which start with my py script and quits with it and saves itself on exit

gleaming quest
#

Try SQLite (sqlite3) or XAMPP program with local MySQL database

#

Did I help? 😃

coarse coral
#

Hello! I am working on my first database, and I have a question. You add a product on the back-end panel. You input product name, price, description and digital codes you wish to wish to sell. This should then send this information to the database. When a user purchases a product from you, it should take digital code from the database, and update the record.

My question: Do I create 1 table with all this information ? and for the digital codes set the format as JSON so I could have multiple digital codes in the one row ? Here is an example of what I am thinking:
Tables Products:

p_name | p_desc | p_price | p_payment_method | d_codes
Fifa 12  etc       4.55     paypal            ['232', '34344', '343434']

So my thinking was when the user purchases on the front-end, it queries the database, takes the first code '232' and delivers that to the buyer, removes from database, so the codes left would be the last two ?

or am I thinking about this wrong ?

#

or should I do something like this:

Table Products
p_name | p_desc | p_payment | foreign_key

Table Codes
foreign_key | codes

charred fractal
#

something like this:

Table Products
p_name | p_desc | p_payment | foreign_key

Table Codes
foreign_key | codes

#

Something like that @coarse coral

coarse coral
#

Yeah I was leaning more to that approach. I have one last question - am I right in thinking I should only assign one code per row right ? would my db not fill up pretty quickly if I have hundreds of rows ?

charred fractal
#

I can't answer that, but it could possibly.

coarse coral
#

okay thanks, I will wait and see if anyone else has any other advise before proceeding

charred fractal
#

Alright.

quick geyser
#

hey so im making a username check for a database,
and no matter what, even if the username is stored in the database its immeditaly going to else statement

print("Please enter your username.")
    USERN = input("USERNAME: ")
    cur.execute(f"""SELECT USERNAME from users where USERNAME='{USERN}'""")
    myresult = cur.fetchone()
    print(myresult)
    wildcard = myresult
    if USERN == wildcard:
        print("yes")
    else:
        print("That username is not in the system.")

any ideas? please tag me when you respond, ill be away from the pc for a while

#

yes it logs into the database at the first part of the code

#

the codes output with my iunput

Do you already have an account? yes
Please enter your username.
USERNAME: bob
('bob',)
That username is not in the system.
#

it literally finds the username but still says no haha

#

im sure im doing something so simple wrong haha

stark ferry
#

@quick geyser have you called type() on both USERN and wildcard?

#

if they are both strings, they should be identical

#

could it be that the database returns 'bob' instead of bob?

quick geyser
#

Thats what im thinking

#

Im not sure how to fix that

stark ferry
#

you can strip the variable

#

im sorry, i meant do replace

#

wildcard=wildcard.replace('"'",'')

#

though stripping whitespace is not a bad idea

quick geyser
#

Hmmm lemme try that

stark ferry
#

Also, looks like your result is a tuple

#

and your input is a string

quick geyser
#

Tuple?

stark ferry
#

like a list but inmutable

#

exampleTuple=(1,3,4)

#

exampleTuple[2]

quick geyser
#

Oh gotcha

stark ferry
#

so you gotta get the fist element since you used fetchone

quick geyser
#

Should i str the result

stark ferry
#

wildcard=myresult[0]

quick geyser
#

Wildcard = str(myresult)

#

Oh ignore me

#

Ok😂

stark ferry
#

you also could do

#
     print('yes')

#

that would chec k the tuple for USERN

quick geyser
#

that just did it

#

your a beautiful person

#

xD

#
    USERN = input("USERNAME: ")                         
    cur.execute(f"""SELECT USERNAME from users where USE
    myresult = cur.fetchone()                           
    print(myresult)                                                
    if USERN in myresult:                               
        print("yes")                                    
    else:                                               
        print("That username is not in the system.")    
                                                        
#

works now

#
  print("Please enter your username.")                  
  USERN = input("USERNAME: ")                           
  cur.execute(f"""SELECT USERNAME from users where USERN
  myresult = cur.fetchone()                             
  print(myresult)                                       
  wildcard = myresult[0]                                
  wildcard = wildcard.replace("''", '')                 
  if USERN == wildcard:                                 
      print("yes")                                      
  else:                                                 
      print("That username is not in the system.")      
``` also works
#

dont even need the wildcard.replace

#

yea i got an error of tuple earlier haha

#

sweet thanks, i appricate ya

#

this is the error now if i purposly miss type a username

#
argument of type 'NoneType' is not iterable
#
    print("Please enter your username.")                
    USERN = input("USERNAME: ")                         
    cur.execute(f"""SELECT USERNAME from users where USE
    myresult = cur.fetchone()                           
    print(myresult)                                                
    if USERN in myresult:                               
        print("yes")                                    
    else:                                               
        print("That username is not in the system.")    
``` ysing this code
quick geyser
#

im guessing if the SELECT USERNAME from users where USERN comes back nonetype, its htorwing an error. any idea?

charred fractal
#

What SQL are you using?

quick geyser
#

mysql workbench

#

im just wondering if theres a check to see if it returns Null then it would reask for usernae

charred fractal
#

I would suggest doing if(len(myresult)) == 0: print("no") else: print("yes")

quick geyser
#

object of type 'NoneType' has no len()

rich trout
#

.fetchone() either returns a single result, or None

quick geyser
#

i got it to work i think

rich trout
#

👍

quick geyser
#
    USERN = input("USERNAME: ")
    PASSW = input("PASSWORD: ")
    cur.execute(f"""SELECT USERNAME from users where USERNAME='{USERN}'""")
    myresult = cur.fetchone()
    print(myresult)
    cur.execute(f"""SELECT PASSWORD from users where PASSWORD='{PASSW}'""")
    myresult2 = cur.fetchone()
    print(myresult2)
#    myresult = myresult[0]
#    wildcard = wildcard.replace("''", '')
    if myresult is None:
        print("That username and/or password is not in the system.")
        login()
    elif myresult2 is None:
        print("That username and/or password is not in the system.")
        login()
    myresult = myresult[0]
    myresult2 = myresult2[0]
    if myresult in USERN and myresult2 in PASSW:
        print("pass")
    else:
        print("That username and/or password is not in the system.")
        print("Make sure")
        login()
#

i know i can make it smaller in teh future, but just getting it to work is one thing for me

rich trout
#

yeah, that's how you use it 👍

#

I would suggest looking into parameterization as well though

quick geyser
#

will do! this is gonna be an app here soon once i figure out a few more things xd

#

got a long way to go

#

looooooolk

rich trout
#

good luck!

north niche
#

What is best async ORM for Python (PostgreSQL)?

fringe tiger
#

That's a hard question ping me if you find answer @north niche

echo beacon
#

Hello guys, I am trying to use the same model in two different models... the model is s Link and stores additional data about a link/web URL and I am trying to use it in multiple models (using Flask and SQLAlchemy). The problem is I don't know how to do it because the Link model binds the relationship with a hardcoded name in the code: links = db.relationship('Link', backref='project', lazy=True) and project_id = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False). First of all, is my approach reasonable or is there a better way to do it? If not, what should I do to make it work?
Thank you in advance 🙂

fringe tiger
#

@north niche so from a bit of googling these 3 stand out
https://github.com/python-gino/gino
https://github.com/encode/orm
https://github.com/tortoise/tortoise-orm

Problem with Gino is that for now it supports only asyncpg dialect.
Problem with encode/orm is that it seems it's the youngest and not developed a lot in terms of releases/commits/contributors (all 3 suffer from that but I think it's most prevalent here).
Not sure about Tortoise, can't see any problem that stands out right away from readme.

#

So my guess is that tortoise-orm would be the best overall, if someone can double check those 3 repos and give opinon that would be great

north niche
#

@fringe tiger I like too Tortoise, but only one problem: I wanna use pg arrays. Do you know how to use this?

fringe tiger
#

I didn't use it so I don't know

fringe tiger
#

I tried using it , looks good ... but can't figure out how to do checks/constraints with tortoise-orm model

#

I'm thinking of overwritting init, doing function checks, and then super() the model

#

hm ye seems to work

quick geyser
#

is a WHERE type allowed in f"""INSERT INTO users(LIKES) VALUES (%s), WHERE USERNAME (%s)"""?

#

if so, how would that properly be written

fringe tiger
#

no, logically it doesn't make sense. Use update then.

quick geyser
#

i just changed it over

#

didnt fully understand insert haha

#

okay so if it updates the number to 3, is there a way i can make it just add onto 3 seperated by a comma instead of replacing 3 to the new number?

#

basically cell has 3,4,5,6,7
instead of cell = 3
then cell =4...

#

likes = F"""UPDATE users SET LIKES = {test} WHERE USERNAME = '{USERN}'"""
cnx.commit()

#

maybe join would work?

fringe tiger
#

I'm not sure what you're asking but yes that query seems valid, just don't hardcode the string use the placeholders %s

echo beacon
#

Anybody here knows SQLAlchemy?

#

How can I create one to many relationship without mentioning the binding model in both models

#

I mean I have 3, lets say page, book and magazine. I want to use pageModel in bookModel and MagazineModel, but I don't know hot to achieve that, documentation and tutorials use properties like backref to "bind" the related model back to the originating model... if that makes any sense

wicked fog
#

@echo beacon me

#

Let's say your setup is books and pages

#

With the relationship being many pages to one book

#

You have a column in the pages table which foreign keys out to the id column of the books table

#

Your sqlalchemy python objects are Book() and Page() respectively

#

In the definition of Book you have

#

pages = relationship("Page", primaryjoin="Page.book_id==Book.id", backref="book")

#

So then in python book_object.pages gets you an iterable of associated Page objects

#

And page_object.book gets you the single Book object

#

@echo beacon ^

echo beacon
#

@wicked fog thank you for the answer, but it isn't exactly what I'm looking for

#

Imagine the scenario you've described but with one more model, lets say a Magazine (similar to Book, but not the same so I would like to have it separately and also it would be best to use Page model in both Book and Magazine)

cerulean pendant
#

@echo beacon you can just omit the backref and it works fine

#

in sqlalchemy you can declare the relationship on any direction -- many -> one or one -> many -- and with or without the backref

echo beacon
#

So pages = db.relationship('Page') should work, right?

cerulean pendant
#

if you have the foreignkeys in place, yes

echo beacon
#

The code ran without errors... but I cannot find a column named books in the table

#

Is that how it shall work?

cerulean pendant
#

that depends on "the code"

echo beacon
#

All it does so far is initialise the db with the declared models

#

And the model has declared its row like mentioned above

wicked fog
#

you'd need your column declared as well

cerulean pendant
#

maybe you could show an executable example of your code

#

maybe

#

it depends

wicked fog
#

probably something like book_id = Column(Integer, ForeignKey("books.id"))

#

or something along those lines

#

then you can have book = relationship(....)

echo beacon
#

So I am not actually having books and pages, but rather Project model which contains a collection of links (URLs):class Project(db.Model): id = db.Column(db.Integer, primary_key=True) ... many entities links = db.relationship('Link')

#

Where Link is also a model declared in other file

#

And then there is Profile model which should also contain collection of links (URLs), so I want to reuse the Link model

#

Does that make sense to you guys?

cerulean pendant
#

@echo beacon it seems you want a many to many relationship then

echo beacon
#

@cerulean pendant isn't many to many relationship a relationship between 2 models (1. with 2. and 2. with 1. with variable count)? I am trying to have 3 models... 2 models in relationship with the 3rd.

wicked fog
#

ok, three objects, Projects, Links, and Profiles

#

let's cover all the bases

#

@echo beacon

#
  1. Can a single Project be associated with one Link, or multiple Links (multiple I think)?
#
  1. Can a single Project be associated with just one Profile, or multiple Profiles?
#
  1. can a single Link be associated with just one Project, or multiple projects?
#
  1. can a single Link be associated with just one Profile, or multiple profiles?
#
  1. can a single Profile be associated with just one Project, or multiple projects?
#
  1. can a single Profile be associated with just one Link, or multiple Links?
#

@echo beacon answer me those ^ questions and that will tell us what kind of relationships we are working with

echo beacon
#

@wicked fog sorry for late response and thank you for patience ... so there are Profiles, each contains Projects and Links, and each Project contains its own Links

wicked fog
#
  1. A Project contains multiple Links
  2. A Project can be associated with multiple profiles
  3. A Link is associated with no more than one project
  4. A Link is associated with no more than one profile
  5. A profile can contain multiple links
  6. a profile can contain multiple projects
    @echo beacon that accurate then?
echo beacon
#

I would say point n. 2 shall be A project can be part of multiple profiles (many-to-many)

#

But other points fit my needs

gentle sparrow
#

Anyone know why I'm getting this error?

pure cypress
#

Did you go to the URL in the error?

#

It should explain it

#

Wrong channel by the way. If you have further questions about that ask in #tools-and-devops

gentle sparrow
#

Ok thanks

#

And let me try that

echo beacon
#

@wicked fog are you able to help given that info?

wicked fog
#

@echo beacon yes

#

ok

#

here we go

#

let's start with an easy one

#

a project can contain many links; a link is only part of one project

#

in the links table you have a project_id column which foreign keys out to the primary id of your project table

#

then in sqlalchemy as part of the Link object definition you have

#
class Link():
    __tablename__="links"
    project_id = Column(Integer, ForeignKey("projects.id")
    project = relationship("Project", backref="links")
#

we can do something similar with Profiles, I'm guessing a Link is associated with the person who added it

#

you'll have a user_id column in your links table as well

#
class Link():
    __tablename__="links"

    project_id = Column(Integer, ForeignKey("projects.id"))
    project = relationship("Project", backref="links")

    user_id = Column(Integer, ForeignKey("users.id"))
    user = relationship("User", backref="links")
#

that's two of your three relationships taken care of

#

now for the tricky one

#

profiles and projects are many to many

#

that means you need another table

#

let's call this table ProfileProjectRels ("profile project relationships")

#

and it would be set up in sqlalchemy like so:

#
class ProfileProjectRel():
    __tablename__="profileprojectrelationships"
    
    user_id=Column(Integer, ForeignKey("users.id"))
    user = relationship("User", backref="projectrels")

    project_id=Column(Integer, ForeignKey("projects.id"))
    project = relationship("Project", backref="userrels")
#

basically, this table is a listing of user/project ID pairs

#

now, in Users

#
class User():

    __tablename__="users"

    @property
    def projects(self):
        return [x.project for x in self.projectrels]
#

Similarly, your projects class

#
class Project():

    __tablename__="projects"

    @property
    def users(self):
        return [x.user for x in self.userrels]
#

@echo beacon ^ there you go

#

also I just realized I sort of turned Profile into User somwhere along the line

echo beacon
#

@wicked fog I get this error: sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'link.project_id' could not find table 'projects' with which to generate a foreign key to target column 'id'

#

Maybe there is a small mistake but I just blindly rewrote the code

#

I changed some stuff and it executed

#

Changed plurals like ForeignKey("projects.id") to ForeignKey("project.id")

wicked fog
#

@echo beacon you'll have to rename stuff to match your own table/column names of cours

#

note that in ForeignKey("tablename.columname") you're using the name of the table in the db

#

whereas for relationship("Thing") you're using the name of the Python class

echo beacon
#

Makes sense!

#

Thank you a LOT @wicked fog, you saved me hours days of searching, I wouldn't be able to do this kind of DB setup myself. You're a real lifesaver, I really appreciate your help ❤️.

wicked fog
#

@echo beacon yeah sqlalchemy is a lifesaver but it has a steep learning curve

echo beacon
#

One more question @wicked fog when I want to populate the tables, how to go about that? Do I need to instantiate profileProjectRels object and add Profile and Project to it?

wicked fog
#

@echo beacon so at the very beginning of your program you'll have a SQLalchemy session object created

#

so to do an insert

#

let's say a new user signs up

#

you would do something like this

#
new_user = User(username="captainmeta4", hashed_password="apoehfaaerq")
session.add(new_user)
session.commit()
#

again, adapt to your own table/column names

echo beacon
#

And how would I add a project?

#
session.add(new_user)
session.commit()

Does not work

wicked fog
#

@echo beacon you'd create a new ProfileProjectRel object

#

wait

#

now

#

no

#

you'd add a project as a project

echo beacon
#
profile = Profile()
profile.displayName = "petak5"

project = Project()
project.title = 'Hello, World!'

link = Link()
link.title = 'Google'

project.links.append(link)
profile.projects.append(project)

db.session.add(profile)
db.session.commit()
#

I did this but no data is added to db

#

But when I add only the project object, it gets into the DB

#

But when I add only the project object, it gets into the DB

#
profile = Profile()
profile.displayName = "petak5"

project = Project()
project.title = 'Hello, World!'

link = Link()
link.title = 'Google'

project.links.append(link)
profile.projects.append(project)

db.session.add(profile)
db.session.commit()
wicked fog
#

@echo beacon do you have a project_id column in your links table?

#

to add the link to the project, you'd set link_object.project_id=x

echo beacon
#

But the id is not calculated yet

#

When I run this code, only profiles table gets populated

torn sphinx
#

hey guys

#

could anybody help me with pandas?

hasty hinge
#

Hello, I'm actually using MySQL (with the AioMySQL module in Python) for my Discord bot, I have a file with all functions defined, so I just have to execute them when I need them, but right now I think I could improve the file, because I see I'm creating a new connection for every time I execute a function, for example, if I execute 3 functions on 1 second, 3 connections would be created, how could I improve this code?
https://paste.pythondiscord.com/luwemubuzu.py

#

I'm relatively new with AioMySQL, so sorry if this is a dumb question.

slim shard
#

Hey! You can create a pool of connections. In connection pooling, after a connection is created, it is placed in the pool and it is used again.

pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306,
                                           user='root', password='',
                                           db='mysql', loop=loop, autocommit=False)
def my_query():
    with (yield from pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("SELECT ...")
            ...
#

In your file you used connections pool wrongly, because in each function you are creating new pool and connections.

hasty hinge
#

Okay, thanks!

slim shard
#

You're welcome

quick geyser
#

when i update a value with a list it inputs a number
3 for example
then when i select the value and append another number it adds quotes around the orginal and adds the second
'3', 23 for example,

["'3', 23", 42]
``` it gets bad, how can i remove those quotes from the value
shell ocean
#

that's a single value.

#

show your code

#

also, seems like wrong channel...?

quick geyser
#

Its pulled from a database so I figured right channel

steady epoch
#
  File "C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 728, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: near "IF": syntax error```
#
    async def add_clan(self, ctx,user:discord.User, clan_tag):
        """Command is used to register a user to the database"""
        fixed_tag = utils.correct_tag(clan_tag)
        clan = await self.coc_client.get_clan(fixed_tag)
        db  = sqlite3.connect("bot_db.sqlite")
        db.execute("INSERT IF NOT EXISTS INTO register (clan_tag,discord_id) VALUES(?,?)",(clan,user))
        db.commit()
        await ctx.send(f"{clan.name} linked to {user.mention} ")```
#

getting error

rich trout
#

sqlite doesn't support INSERT IF NOT EXISTS

#

You'll need to reword it like INSERT OR REPLACE, or like this:
INSERT INTO memos(id,text) WHERE NOT EXISTS(SELECT 1 FROM memos WHERE id = 5);

steady epoch
#

@rich trout i want to tell user too if user is not registered

rich trout
#

does your current one do that?

#

You could also do INSERT OR IGNORE, which should return either a new rowid or nothing, but let me check

steady epoch
#

and i wnt to notify user if he/she is not registered already

rich trout
#

There

#

!e ```py
import sqlite3
db = sqlite3.connect(":memory:")
c = db.cursor()
c.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
print(c.execute("INSERT OR IGNORE INTO users VALUES (1, 'bob')").lastrowid)
print(c.execute("INSERT OR IGNORE INTO users VALUES (1, 'bob')").lastrowid)

delicate fieldBOT
#

@rich trout :white_check_mark: Your eval job has completed with return code 0.

001 | 1
002 | 1
rich trout
#

bleh

steady epoch
#

will it send to the same table which i wnt

rich trout
#

!e ```py
import sqlite3
db = sqlite3.connect(":memory:")
c = db.cursor()
c.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
print(c.execute("INSERT INTO users VALUES (1, 'bob')"))
print(c.execute("INSERT INTO users VALUES (1, 'bob')"))

delicate fieldBOT
#

@rich trout :x: Your eval job has completed with return code 1.

001 | <sqlite3.Cursor object at 0x7f80571512d0>
002 | Traceback (most recent call last):
003 |   File "<string>", line 6, in <module>
004 | sqlite3.IntegrityError: UNIQUE constraint failed: users.id
rich trout
#

Could use that for it

#

provided you use a UNIQUE column

steady epoch
#

hmm

#

what should i write query?

rich trout
#

Use a try and just INSERT normally

#

If you have a UNIQUE column--like discord id might be in your case--then it will throw an error you can catch

#

Or you could just SELECT to check first, which might be faster if you anticipate it failing lots of the time

steady epoch
#

hmmmm u meant except right?

rich trout
#

yeah

steady epoch
#

what is the better option in your opinion

rich trout
#

If it's a command you expect users to run manually, just try and catch the exception

steady epoch
#

hmmmmmm

#

try:

#

if user is not none
then execute

#

except:

rich trout
#
try:
  c.execute("INSERT...")
except sqlite3.IntegrityError:
  # already exists
steady epoch
#

hmmm

rich trout
#

You'll want to do CREATE TABLE clan_tags_etc (discord_id INTEGER UNIQUE, etc...)

steady epoch
#

i have table already

rich trout
#

since this is clearly a discord bot, you can just do a SELECT from to check if it exists before inserting, so long as you don't await anything in between the two

steady epoch
#

i am awaiting

#

oops

#

@rich trout

#

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: InterfaceError: Error binding parameter 0 - probably unsupported type

#

got this error

rich trout
#

Did you mix up the order of the id and tag? that could cause that

steady epoch
#

!add_clan 384158530623635456 #P99CRYU2

#

i used this

#

@rich trout

rich trout
#

What's your insert/select statement look like?

steady epoch
#
    async def add_clan(self, ctx,user:discord.User, clan_tag):
        """Command is used to register a user to the database"""
        fixed_tag = utils.correct_tag(clan_tag)
        clan = await self.coc_client.get_clan(fixed_tag)
        db  = sqlite3.connect("bot_db.sqlite")
        try:
            db.execute("INSERT  INTO register (discord_id,clan_tag) VALUES(?,?)",(user,clan))
            db.commit()
            await ctx.send(f"{clan.name} linked to {user.mention} ")
        except sqlite3.IntegrityError:
            await ctx.send(f"{user} is already registered")```
rich trout
#

You're passing in a user object, probably want to be passing in the id from it

steady epoch
#

what should i write @rich trout

rich trout
#

in your (user, clan) you're passing in a discord Member and a string

#

it needs an integer (the user's id) and a string

steady epoch
#
    db = sqlite3.connect("bot_db.sqlite")
    cursor = db.cursor()
    cursor.execute('''
         CREATE TABLE IF NOT EXISTS register(
         clan_tag text NOT NULL, 
         player_tag text NOT NULL,
         discord_id integer NOT NULL,
         guild_id integer NOT NULL,
         PRIMARY KEY(discord_id)
         )
    ''')```
#

i have made this table

rich trout
#

hm

#

You'll need an SQLIte unique clause

#

UNQIUE(discord, guild)

#

at the end, so that sqlite throws an error if the same user on the same server tries to set another tag

#

and then you catch it and tell them it's already set

steady epoch
#

actually i am new to db

#

where should i put unique

#

i have made primary key discord id to keep it differentiated

rich trout
#

at the bottom, like primary key is

#

youll need a comma like the rest of the columns too

#

Here's an example:

steady epoch
#

ooo

rich trout
#
CREATE TABLE shapes(
    shape_id INTEGER PRIMARY KEY,
    background_color TEXT,
    foreground_color TEXT,
    UNIQUE(background_color,foreground_color)
);
steady epoch
#

ok

#

@rich trout will this solve my problem

#

???

rich trout
#

yeah

steady epoch
#

ok

#

@rich trout same error

rich trout
#

Is your insert statement still db.execute("INSERT INTO register (discord_id,clan_tag) VALUES(?,?)",(user,clan))?

#

Shouldn't be (user, clan)--that'll turn into something like ("Bob#122", "clan_test"), which doesn't work cause you can't turn "Bob#122" into a number

steady epoch
#

ok

#

same error @rich trout

rich trout
#

Hm

steady epoch
#

!add_clan 384158530623635456 P99CRYU2

#

this i am using

rich trout
#

So you'll notice that the table is this:

#
         clan_tag text NOT NULL, 
         player_tag text NOT NULL,
         discord_id integer NOT NULL,
         guild_id integer NOT NULL,
#

But you're only passing in two things

#

probably need to pass all of them, and in order

steady epoch
#

but in query 2 things are mentioned only

rich trout
#

true

steady epoch
#

then y this erro

rich trout
#

What's your insert look like now?

steady epoch
#
    @commands.command(name="add_clan")
    async def add_clan(self, ctx,user:discord.User, clan_tag):
        """Command is used to register a user to the database"""
        fixed_tag = utils.correct_tag(clan_tag)
        clan = await self.coc_client.get_clan(fixed_tag)
        db  = sqlite3.connect("bot_db.sqlite")
        try:
            db.execute("INSERT  INTO register (discord_id,clan_tag) VALUES(?,?)",(user.id,clan))
            db.commit()
            await ctx.send(f"{clan.name} linked to {user.mention} ")
        except sqlite3.IntegrityError:
            await ctx.send(f"{user} is already registered")```
rich trout
#

Well, here you're passing in clan, not clan_tag. Does clan have an id?

steady epoch
#

yep

rich trout
#

Then you should probably pass the id, not the clan itself

steady epoch
#

i am checking through get_clan method

rich trout
#

In your db.execute() line you're passing in the clan object from your coc client

#

you should pass in a tag or an id there

#

depending on how you did it originally

steady epoch
#

i am passing tag only

#

get_clan make sure it should be correct

rich trout
#

Well, the error you got and the code you shared above indicates you're passing the clan object--not the clan tag

#

yes, which you store the clan object in clan, and then pass clan, instead of clan_tag to the insert command

steady epoch
#

hmm means i need to do clan.tag?

#

@rich trout my bot executed but its giving i am registered already

rich trout
#

Hm. Try printing the error message. IntegrityError might be used for if you're short values on an insert. But otherwise it should be working

steady epoch
#

ok

rich trout
#

--and by printing the error message I mean just adding a raise to the end of that except block so it prints again

steady epoch
#

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IntegrityError: NOT NULL constraint failed: register.player_tag

#

@rich trout

rich trout
#

Yep, as I thought

#

your CREATE TABLE tells sqlite that you have to provide a player tag, and a clan tag, and a discord_id, and a guild_id

#

but you're only giving it two of the four

steady epoch
#

i think i need to remove not null

rich trout
#

that would work too

steady epoch
#

i want to add player id nxt to their discord id

#

when player uses !add_player command

#

@rich trout

rich trout
#

Sure, if you want

#

just have to add it to the command and insert like the other bit

steady epoch
#

but for this command how can i pass player_tag

rich trout
#

Same way as you pass clan_tag I assume?

steady epoch
#

if i will dont give player id will it throw error @rich trout

rich trout
#

For each item there, either the command needs to require it and you need to provide it to sqlite in the insert statement, or it needs to be nullable (without NOT NULL)

#

I've also noticed you've got guild_id as NOT NULL, but aren't passing it in

steady epoch
#

i think i need to remove not null from every thing except discord id @rich trout

rich trout
#

Probably a good idea

torn sphinx
#

Guys could someone help me with manipulating a database

steady epoch
#
    async def th(self,ctx , clan_tag):
        db = sqlite3.connect("bot_db.sqlite")
        if clan_tag is None:
            stmt = db.Cursor()
            stmt.execute(f"SELECT clan_tag from register WHERE discord_id = {ctx.author.id}")
            clan = stmt.fetchone()
        else:
            fixed_tag= utils.correct_tag(clan_tag)
            clan = await self.coc_client.get_clan(fixed_tag)
            await ctx.trigger_typing() 
        th = {13: [], 12: [],11: [], 10: [], 9: [], 8: [], 7: [], 6: [],5: [] ,4: [],3:[],2:[],1:[]}
        emoji=''


        async for player in clan.get_detailed_members():
            th[player.town_hall].append(player.name)
        for i,j in th.items():
            if not j:
                pass
            else:
                emoji += f'{(os.getenv(str(i)))} {len(j)}\n'
                embed = discord.Embed(colour=discord.Colour.blue(),title=f'{clan.name} {clan.tag}',description=f'**TOWNHALL COMPOSITION**\n{emoji} \n TOTAL MEMBERS : {clan.member_count}/50')
                embed.set_thumbnail(url=clan.badge.url)
                embed.timestamp = datetime.datetime.utcnow()
        await ctx.send(embed=embed)```
#

@rich trout its not fetching one data which i inserted

rich trout
#

did you forget a commit()?

steady epoch
#

nopw

#

nope

#

my register command worked

#

but this clan tag has not been fetched

#

@rich trout

rich trout
#

I don't see anything wrong with it. Are you sure it's showing up in the database properly?

#

I bet you didn't insert a guild_id and it's back to throwing an integrityerror

steady epoch
rich trout
#

ok

#

Oh, I see it

#

clan = stmt.fetchone()

#

Are you missing a await self.coc_client.get_clan(fixed_tag)?

steady epoch
#

i dont need to check that again as it register clan after checking only

rich trout
#

Right, but right now clan is a string, returned from sqlite

#

and then you immediately go after clan.get_detailed_members, as if it was a Clan Object

steady epoch
#

so what i correct

#

@urban goblet
discord.ext.commands.errors.MissingRequiredArgument: clan_tag is a required argument that is missing.

#

this error is coming

#

actually bot is not selecting anything

rich trout
#

Ah, you want clan_tag=None then, if you want it to be optional on your def line

#

otherwise it defaults to required

steady epoch
#

ooo

#

@rich trout
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'sqlite3.Connection' object has no attribute 'Cursor'

rich trout
#

Cursor -> cursor

steady epoch
#

ok

fluid frigate
#

alright I'm about to pull my hair out with how clumsy SQLAlchemy is. Maybe someone here can help me.

Effectively, I have two tables, a "Users" table, which contains one row for every user in the system, and a "Warnings" table, which has a one-to-many relationship with the Users table, where each user could have any number of warnings related to them. It's fairly straightforward, but here's the data model:

class UserTable(_Base):
    __tablename__ = 'Users'

    User_Id = Column('User_Id', Integer, primary_key=True, nullable=False)
    Discord_Id = Column('Discord_Id', Integer, unique=True, nullable=False)


class WarningTable(_Base):
    __tablename__ = 'Warnings'

    Warning_Id = Column('Warning_Id', Integer, primary_key=True, nullable=False)
    User_Id = Column('User_Id', Integer, ForeignKey('Users'), nullable=False)
    Warning_Stamp = Column('Warning_Stamp', DateTime, nullable=False)

What I want to do, is query for the Discord_Id, and get all the warnings associated to that user. Here's the SQL statement I want to achieve.

SELECT Warnings.* 
FROM Users
LEFT OUTER JOIN Warnings ON Users.User_Id == Warnings.User_Id
WHERE Users.Discord_Id = <provided id>

I've been wracking my head for over a day on this and trying different things, and the best I've gotten seems to be this statement:

warning_rows = session.query(WarningTable). \
               select_from(UserTable). \
               outerjoin(UserTable.User_Id). \
               filter(UserTable.Discord_Id == discord_id)

But when I try to execute this, I just get an error back saying AttributeError: entity.
For context, among other things, I'm referencing the SQLAlchemy's documentation example, shown here: https://docs.sqlalchemy.org/en/13/orm/query.html?highlight=query#sqlalchemy.orm.query.Query.select_from

Any help would be greatly appreciated at this point, as I'm about this close to just saying fuck it and trying to manually shove text() sql down alchemy's throat.

steady epoch
#

@rich trout
Traceback (most recent call last):
File "C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 728, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'tuple' object has no attribute 'upper'

#
    async def th(self,ctx , clan_tag=None):
        db = sqlite3.connect("bot_db.sqlite")
        if clan_tag is None:
            stmt = db.cursor()
            stmt.execute(f"SELECT clan_tag from register WHERE discord_id = {ctx.author.id}")
            clan_tag= stmt.fetchone()
            fixed_tag= utils.correct_tag(clan_tag)
            clan = await self.coc_client.get_clan(fixed_tag)
        else:
            fixed_tag= utils.correct_tag(clan_tag)
            clan = await self.coc_client.get_clan(fixed_tag)
            await ctx.trigger_typing() 
        th = {13: [], 12: [],11: [], 10: [], 9: [], 8: [], 7: [], 6: [],5: [] ,4: [],3:[],2:[],1:[]}
        emoji=''


        async for player in clan.get_detailed_members():
            th[player.town_hall].append(player.name)
        for i,j in th.items():
            if not j:
                pass
            else:
                emoji += f'{(os.getenv(str(i)))} {len(j)}\n'
                embed = discord.Embed(colour=discord.Colour.blue(),title=f'{clan.name} {clan.tag}',description=f'**TOWNHALL COMPOSITION**\n{emoji} \n TOTAL MEMBERS : {clan.member_count}/50')
                embed.set_thumbnail(url=clan.badge.url)
                embed.timestamp = datetime.datetime.utcnow()
        await ctx.send(embed=embed)```
rich trout
fluid frigate
#

it does that a lot .-.

rich trout
#

I would venture a guess that clan_tag= stmt.fetchone() is giving you (clan_tag,), and you need to use [0] to get it out

steady epoch
#

what should i write clan_tag,[0] where

rich trout
#

fetchone()[0]

steady epoch
#

ok

#

@rich trout ty broooooooo it worked

rich trout
#

Scytail, what sql is it emitting right now?

fluid frigate
#

i don't know, i can't get it to run. whenever I try to get that statement to execute, it just throws an error about attributes

#

i think it's blowing up when it's trying to build the SQL to begin with

#

I've even tried sticking relationship references in my tables to see if I could somehow impact the statement that way:

steady epoch
#

@rich trout if i register different clan with same discord id and i do fetchall will it show all?

fluid frigate
#
class UserTable(_Base):
    __tablename__ = 'Users'

    User_Id = Column('User_Id', Integer, primary_key=True, nullable=False)
    Discord_Id = Column('Discord_Id', Integer, unique=True, nullable=False)
    Warnings = relationship("WarningTable", back_populates="User")


class WarningTable(_Base):
    __tablename__ = 'Warnings'

    Warning_Id = Column('Warning_Id', Integer, primary_key=True, nullable=False)
    User_Id = Column('User_Id', Integer, ForeignKey('Users.User_Id'), nullable=False)
    Warning_Stamp = Column('Warning_Stamp', DateTime, nullable=False)
    User = relationship("UserTable", back_populates="Warnings")
rich trout
#

It should, provided you're on a different guild, because you made them unique

fluid frigate
#

^

rich trout
#

Ah, those relationships are important

fluid frigate
#

yeah, but i still have the same problem

rich trout
#

What's the full traceback?

fluid frigate
#

it's a mess, but hold on

rich trout
#

👍 I read messes for a living

fluid frigate
#
Traceback (most recent call last):
  File "C:\Users\Ben\Documents\Git\Manageable\venv\lib\site-packages\discord\ext\commands\core.py", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Ben\Documents\Git\Manageable\Code\Cogs\ModToolsCog.py", line 39, in warn
    self._remove_outdated_warnings(target_member)
  File "C:\Users\Ben\Documents\Git\Manageable\Code\Cogs\ModToolsCog.py", line 63, in _remove_outdated_warnings
    user_warnings = DataAccess.lookup_warnings_by_discord_id(target_member.id)
  File "C:\Users\Ben\Documents\Git\Manageable\Code\Base\Decorator.py", line 83, in __call__
    return self.run(*args, **kwargs)
  File "C:\Users\Ben\Documents\Git\Manageable\Code\Data\DataAccess.py", line 23, in run
    return_data = super(DatabaseMethod, self).run(*args, **kwargs)  # Execute the method
  File "C:\Users\Ben\Documents\Git\Manageable\Code\Base\Decorator.py", line 93, in run
    return self.decorated(*args, **kwargs)
  File "C:\Users\Ben\Documents\Git\Manageable\Code\Data\DataAccess.py", line 46, in lookup_warnings_by_discord_id
    warning_rows = session.query(WarningTable). \
  File "C:\Users\Ben\Documents\Git\Manageable\venv\lib\site-packages\sqlalchemy\orm\query.py", line 2312, in outerjoin
    return self._join(
  File "<string>", line 2, in _join
  File "C:\Users\Ben\Documents\Git\Manageable\venv\lib\site-packages\sqlalchemy\orm\base.py", line 220, in generate
    fn(self, *args[1:], **kw)
  File "C:\Users\Ben\Documents\Git\Manageable\venv\lib\site-packages\sqlalchemy\orm\query.py", line 2426, in _join
    right = onclause.property.entity
  File "C:\Users\Ben\Documents\Git\Manageable\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 977, in __getattr__
    return self._fallback_getattr(key)
  File "C:\Users\Ben\Documents\Git\Manageable\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 951, in _fallback_getattr
    raise AttributeError(key)
AttributeError: entity
#
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Ben\Documents\Git\Manageable\venv\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Ben\Documents\Git\Manageable\venv\lib\site-packages\discord\ext\commands\core.py", line 728, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Ben\Documents\Git\Manageable\venv\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: entity
#

enjoy hahaha

#

in case it matters, the "decorator" stuff basically just wraps my sql methods in some simple code to handle sql sessions, doesn't impact the queries or anything

rich trout
#

ew gross

#

lol

fluid frigate
#

don't hate it actually works p well

rich trout
#

I see, I was right

fluid frigate
#

completely abstracts away session management, and my discord bot doesn't have to care about databases at all now, which cleans up the data layer from the business layer

rich trout
#

Something's not being passed to the join properly

fluid frigate
#

yeah that's what i thought

#

but i cannot for the life of me figure out what's going on

#

especially since I'm mirroring sqlalchemy's code example so closely

rich trout
#

yeah

#

let me just..

#

oh

#

wow

#

wth

fluid frigate
#

did we miss something obvious

rich trout
#

Idk

#

I blindly copied the example and reproduced your error

fluid frigate
#

hooray for broken documentation \o/

rich trout
#

found it

#

it's a tiny irritating impossibly easy to miss detail that reminds me of exactly why I don't like sqlalchemy's docs

#

It also seriously needs a better error message

#

let me get my phone I'm reporting a bug

#

Ok, the detail: you must join on the relationship not the column

fluid frigate
#

yeah "AttributeError: entity" means literally nothing lol

#

oh what

#

w h a t

#

how was i supposed to know that lmao

rich trout
#

IKR

#
q = session.query(Owner).select_from(Dog).\
    join(Dog.owner).\
    filter(Dog.name == 'Spark')

works

#
q = session.query(Owner).select_from(Dog).\
    join(Dog.owner_id).\
    filter(Dog.name == 'Spark')
#

AttributeError: Entity

fluid frigate
#

ahhhhh it works

#

thanks so much

#

i would never have figured that out haha

rich trout
#

oh good, it's already in their issue tracker

#

last..touched..dec...19..2019

fluid frigate
#

;-;

rich trout
#

Hm. Well, at least there's attention to it

#

glances at a particular, related error message from ponyorm that happens when you screw up capitalization during a query

#

anyway

#

glad it's identified, and I'll keep that in the back of my mind if someone else runs into it

fluid frigate
#

yeah i'll have to remember that too, thank you so much

#

so many of my code pieces just clicked into place with that bug gone you have no idea lol

rich trout
#

:P

thorn gale
#

Hello, is this the right place to ask a question about a sqlalchemy query?

rich trout
#

yep

thorn gale
#

So I have this query :

db.session.query(UserCatalogs).filter(UserCatalogs.id == catalog_id).first().child_cat

Which gives me :

gChild('14', '786','1'), CatalogChild('15', '786','1'), CatalogChild('16', '785','1'), CatalogChild('17', '784','1')]```

But I would like to somehow recover the numbers(761, 786 etc) inside the CatalogChild but I do not know how to and I guess I'm googling it wrong too. I mean I would basically like to do :

 ```db.session.query(UserCatalogs).filter(UserCatalogs.id == catalog_id).first().child_cat.item_id```

But ofc, it doesn't work
rich trout
#

Well, it's giving you a list of results

#

So you'd have to use a for loop, for example, to get them out

#
for child_cat in db.session.query(UserCatalogs).filter(UserCatalogs.id == catalog_id).first().child_cat:
  print(child_cat, child_cat.item_id)
thorn gale
#

Alright, thanks you so much ❤️

sonic mural
#

Using SQLAlchemy, what is the best practice for checking if an entry exists with a specific value. Would it just be a query.one() enclosed in a try-except block or is there a specific method for checking if an entry exists?

cerulean pendant
#

you can also use first() and check for None

cyan yew
#

getquery = "SELECT * FROM requests"
requests = cursor.execute(getquery)

cerulean pendant
#

or you can use exists() in a subquery

cyan yew
#

does that store all the rows in my table requests into a variable named requests

cerulean pendant
#

no.

cyan yew
#

(im new to this)

#

oh

#

how do I do it?

cerulean pendant
#

you still have to call one or more of cursor.fetchone/cursor.fetchall/cursor.fetchmany to actually get the data

cyan yew
#

oh ok

#

does fetchone get the latest

#

?

cerulean pendant
#

fetchone will get one record

#

the next

cyan yew
#

I did requests = cursor.execute(getquery).fetchall() and I got this error;

AttributeError: 'NoneType' object has no attribute 'fetchall'

cerulean pendant
#

yeah, you have to call it in the cursor

#

no method chaining

cyan yew
#

ok

cerulean pendant
#
cursor.execute(getquery)
data = cursor.fetchall()
cyan yew
#

ok thx

sonic mural
#

or you can use exists() in a subquery
@cerulean pendant
ty

#

Ok question numero dos

#

I'm storing an entry which could either be a a BigInteger or a unicode emoji.
My thoughts are to convert the unicode emoji to some integer value

#

However, I'm not too sure on how to do that

#

(In sqlalchemy on a postgres server)

cyan yew
#

('d', 'd', 'd', 'd', datetime.datetime(2020, 3, 17, 17, 2, 30), 1) if my cursor has this as a return, how do I access the first d, second d, so on

#
getquery = "SELECT * FROM requests"
cursor.execute(getquery)
rows = cursor.fetchall()
print(rows)
#

printing returned the d d dd stuff

#

I submited d d d d as a test

knotty parcel
#

With PostgreSQL how to store a list that is made up of all strings?

Currently got: cursor.execute("CREATE TABLE applications(guildid BIGINT, app VARCHAR(255), appid BIGINT, questions TEXT[], roleid BIGINT") but it throws an error ```syntax error at end of input
LINE 1: ... VARCHAR(255), appid BIGINT, questions TEXT[], roleid BIGINT

#

So store a list of strings

#

@cerulean pendant Pinged you as you seem online and who what you are doing

cerulean pendant
#

@knotty parcel you are missing a )

knotty parcel
#

Thanks

calm charm
#

does anyone know how to make it so that the each user has a different inventory, with a variety of things being inside the inventory.

#

with sqlite?

fresh zephyr
#

What is the item, is it just a string?

#

You don't really need sqlite for that

cerulean pendant
#

I mean, sqlite is pretty good

fresh zephyr
#

Yeah, it is

#

It might just be an overkill for him

frail hearth
#

@calm charm for PostgreSQL, i used this:

CREATE TABLE items (
    itemid bigserial NOT NULL,
    title text NULL,
    description text NULL,
    rarity int2 NULL,
    emoji text NULL,
    CONSTRAINT "items-primarykey" PRIMARY KEY (itemid)
);

CREATE TABLE useritems (
    userid int8 NOT NULL,
    gildid int8 NOT NULL,
    itemid int8 NOT NULL,
    CONSTRAINT useritems_unique UNIQUE (userid, gildid),
    CONSTRAINT useritems_fk FOREIGN KEY (itemid) REFERENCES items(itemid)
);

items is a table that holds all items.
useritems holds the items in peoples inventories

#

example data for items would be

 itemid |   title   |            description             | rarity | emoji
--------+-----------+------------------------------------+--------+-------
   0    | Test Item | This item Should Not Be Obtainable |      0 |   ⚠️    
#

data for useritems would be

       userid       |       gildid       | itemid
--------------------+--------------------+--------
 477034228366966785 | 647850202430046238 |      0 
#

hope this helps!

calm charm
#

@frail hearth do I have to manually insert an item with the details into the items table

#

and what is gildid?

#

like guiild id?

frail hearth
#

guildid is the I'd of the guild

#

Items have to be inserted, however you could probably make a command

calm charm
#

how would I retrieve what's in the inventory for let's say the ctx.author.id

#

how wuld I select from inventory where userid is userid in useritems

frail hearth
#

Unsure tbh, try looking up usage info for primary keys and foreign keys

charred fractal
#

user_item.

frail hearth
#

Gtg, apologies

calm charm
#

u use like INNER JOIN

#

but i dont understand what it's saying

alpine patio
#

hey

#

how to update a table by checking if something is new

charred fractal
#

Example?

alpine patio
#

a table with
id , name , email
.....
and let's say someone changed his name

#

I'm getting my data table as a json and then I want to check " does id = 11231 changed his name or not"

cerulean pendant
#

if you really want to check, you have to fetch the data from the database and compare

#

but usually what we do is just send a UPDATE command - if nothing changed then it will update to the same value and nothing happens

alpine patio
#

aaah I see

#

thank you

#
cursor = connection.cursor()
         sql_update_query = """Update students set type = %s where id = %s"""
         inputData = (type, id)
         cursor.execute(sql_update_query, inputData)
         connection.commit()
         print("Record Updated successfully ")

#

so i did something like that for 'type'

#

do i need to make another function for 'name' and 'mail' or is there an easier way

cerulean pendant
#

you can update all fields with a single query

alpine patio
#

did it like that

#

wish me luck

cerulean pendant
#

🍀

alpine patio
#

@cerulean pendant ayyy worked

torn sphinx
#

can someone help me with sql database pls?

delicate fieldBOT
#

Asking good questions will yield a much higher chance of a quick response:

• Don't ask to ask your question, just go ahead and tell us your problem.
• Don't ask if anyone is knowledgeable in some area, filtering serves no purpose.
• Try to solve the problem on your own first, we're not going to write code for you.
• Show us the code you've tried and any errors or unexpected results it's giving.
• Be patient while we're helping you.

You can find a much more detailed explanation on our website.

torn sphinx
#

can someone pls help me with this homework lool i got 15 mins to hand it in

#

lol

wicked fog
#

!rule 5

delicate fieldBOT
#

5. Do not provide or request help on projects that may break laws, breach terms of services, be considered malicious/inappropriate or be for graded coursework/exams.

silk ember
#

tell us your problem and we'll see if we can help

#

we can't do it for you though

calm charm
#

why does this sql CREATE_TABLE_SQL = """CREATE TABLE Inventories ( user_id INTEGER PRIMARY KEY item_id INTEGER PRIMARY KEY FOREIGN KEY(user_id) REFERENCES Users(user_id) FOREIGN KEY(item_id) REFERENCES Items(item_id) );""" get me sqlite3.OperationalError: near "item_id": syntax error

shell ocean
#

comma?

calm charm
#

Nope

#

commas dont make a difference

#

wait nvm got it

#

ok so I got it working

#

Now how do I enter an item with has an id of 0 and name of test and desc of test to the ctx.author.id

#

Like if command run, add 1 of the test item to the ctx.author.id

#

also how are we measuring how much of each item they have

frail hearth
#

Repetition of the same item

#

But with different item id'd

calm charm
#

Ok now how do I get lets say an item description, item name, itemid that associates with the userid

#

Also how do I put each of those into an individual variabpe

frail hearth
#

You'd have a variable for the items

calm charm
#

Let's take this to dms

frail hearth
#

Sure, I probably wont be able to help for the next 9 hours, as I have school. After I can try to make a mockup idea

calm charm
#

jfc. 9 hours?

#

that's too long

#

it's fine

#

ping me when you need to talk

#

cuz imma probably be asleep

#

or just dm me

gentle sparrow
#

Very Cliché question. I’m trying to make a social media like Twitter/Instagram and I want to use python/Django for some backend stuff. I don’t know anything about backend. Does anyone have any advice on where I should start ? I do know about frameworks and things like AWS but I don’t know how to use any of them.

#

If possible can anyone go into detail on what Instagram would specifically use Django for

solemn ridge
finite lynx
#

In SQLAlchemy if I would like to return all columns that contain a string I should do:
Something.query.filter_by(name='string').contains()?

azure drift
#

hi foizal

torn sphinx
#

hi doniyal

azure drift
#

Wald

torn sphinx
#

personally isted of using MYSQL use tkinter

#

insted*

azure drift
#

@calm charm sqlite3 to make data base doesnt work something is wrong its a problem many people have so use tkinter instead and os here is a video link

wicked fog
#

@finite lynx session.query(Object).filter(Object.variablename.contains("string")).all()

calm charm
#

But I don't have a register login db

hasty hinge
#

Hello, I'm having an error in my MySQL DB when I try to add a unicode emoji as text, could you help me?

#

I already set the encoding as utf8mb4 but the error keeps happening

shell geode
#

as far as I know, \xERe\as123\12312 is bytes, so you need to decode them to str

#

I might be wrong

serene pawn
#

SO im trying to write a script that reads SQL tables

upbeat lily
gentle sparrow
#

@solemn ridge Thx for the redirection

solemn ridge
#

np

finite lynx
#

@wicked fog Ty

#

the variable name would be for example a column in a table and the Object is the table?

#

Also why sometimes querying would be done like with session.query etc and other times with Sometable.query.filter_by etc?

wicked fog
#

@finite lynx yes Object is a python object which you've got mapped to a db table, and variablename is an object property which you've got mapped to a column in that table

#

query() is the general interface for selecting from a table

hasty hinge
silk ember
#

you can attach the pool to your bots instance

#

there's an example for it i think

#

i'd probably just do something like this though ```python
class Bot(commands.Bot):
def init(self):
super().init(command_prefix=prefix, description=description)
self.loop.create_task(self.initialize_database())

async def initialize_database(self):
    self.db = await aiomysql.create_pool(**credentials)
#

then you have access to your pool wherever you have access to your bot instance

strong compass
#

guys i have been stuck on this for 3 hours dont know wat to do

#
import discord
from discord.ext import commands, tasks
from itertools import cycle
import random
import os
import asyncio
from discord.utils import get
import datetime
import sqlite3


class Wallet(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command(pass_context=True)
    async def wallet(self, ctx):
        db = sqlite3.connect('Main.db')
        cursor = db.cursor()
        cursor.execute("SELECT user_id FROM wallet")
        result = cursor.fetchall()
        if result is None:
            cursor.execute((f"INSERT INTO wallet (user_id) VALUES ({ctx.message.author.id})"))
            db.commit()
            cursor.close()
            db.close()
        elif result is not None:
            cursor.execute((f"UPDATE wallet SET (user_id) = ({ctx.message.author.id})"))
            db.commit()
            cursor.close()
            db.close()

        member = ctx.author
        embed = discord.Embed(title=f'- Customer - {member.name} - Wallet -', description=None, colour=discord.Color.
                              blue())
        embed.add_field(name="Platinum Tokens", value="0")
        embed.add_field(name="Held Platinum Tokens", value="0")
        embed.set_thumbnail(url=f"{member.avatar_url}")
        embed.timestamp = datetime.datetime.utcnow()
        embed.set_footer(text='MafiosOS Management Team', icon_url="https://cdn.discordapp.com/attachments"
                                                                   "/658963133125230602/664277505858994201"
                                                                   "/Mafioso_Discord.gif")
        await ctx.send(embed=embed)


def setup(client):
    client.add_cog(Wallet(client))

everything works properly but it wont insert or update user_id on the database

slim shard
#

When you call fetchall() method, an empty list is returned when no rows are available.

#

Maybe if result: ... and elif not result: ... will work.

strong compass
#

still doesnt work

slim shard
#

Ahh, maybe if not result: ... and elif result: ...

#

Throws any exception?

strong compass
#

omg

#

thank you soo much

#

worked

slim shard
#

\o/

strong compass
#

@slim shard

#

it works

#

but

#

if another user do that command again it will take same row

#

and change id

slim shard
#

what does this function need to do?

strong compass
#

i just want it to add user id of users who use that command into a user_id

#

and keep storing them

calm charm
#

If I have a column in sqlite, how do I assign a variable to it

#

example I have a column named item_id how do I assign a variable so that i can do if item_id == 1:

#

also let's say I have like 15 rows, can I make it so that each there are going to be 5 rows per result, and the user has to type .list 2 to see the next 5. and .llist 3 to see the last 5.

steady epoch
#

db.execute("INSERT IF NOT EXIST INTO register (discord_id,clan_tag) VALUES(?,?)",(user.id,clan.tag))

#

i am using sqlite

#

error is coming it says its incorrect

#

if

finite lynx
#

Whem setting up Flask-Migrate, should I better do it in the database config file or my models file?

torn sphinx
#

I want to save the amount of messages users send monthly. Saving each individual message is overkill, so how should I go about creating a table with monthly entries? Thought about setting all dates to the 1st day of the month

cerulean pendant
#

I usually have a yearmonth field: 202002 202003 202004

torn sphinx
#

As a string/integer field?

cerulean pendant
#

yes - and it is sortable so makes it easy for my use case

#

I figured I don't need the day as I won't be using it anyway

solemn ridge
#

You can also use epochs

torn sphinx
#

I was gonna use the date field but I guess a string or int should work too. But which one is faster to process?

solemn ridge
#

epochs is just an int

#

4945432342

#

soemthing like that

solemn ridge
#

This is the epoch for rn

#

1584617552000

#

In my country

#

@calm charm I take it you have set up a model

#

?

calm charm
#

wat

solemn ridge
#

How are you connecting to your dtabase?

calm charm
#

through a connection?

solemn ridge
#

how then?

calm charm
#

._.?

solemn ridge
#

Send me the connection block

calm charm
#
connection = await aiosqlite.connect('currency.db')
        cur = await connection.cursor()
solemn ridge
#

Why are you using aiosqlite tbw?

calm charm
#

it's asynchronous

solemn ridge
#

so?

calm charm
#

stops blocking

solemn ridge
#

DB transactions cant get blocking

#

since you are using sqlite

calm charm
#

aiosqlite is very similar to sqlite3

#

it doesnt matter

#

u just add await to almost all commands

#

so

solemn ridge
#

I know how aiosqlite works

calm charm
#

Like i have the column item_id

solemn ridge
#

Anyways you need to setup a model for your table

calm charm
#

what is a model?

solemn ridge
#

What is the name of the table and the columns it has?

#

Let me explain.

cerulean pendant
#

I was gonna use the date field but I guess a string or int should work too. But which one is faster to process?
@torn sphinx which kind of processing you plan on doing?

torn sphinx
#

Adding to the entry's fields. Frequently

#

Like, I'm counting messages

cerulean pendant
#

then you won't process

calm charm
#

Name of table is Items columns are: item_id item_name item_description item_icon_emoji and name

cerulean pendant
#

you just run UPDATE tb SET msgcount = msgcount + 1 WHERE yearmonth = ?

solemn ridge
#

Basically. A table is like a class
Its attributes are its columns. Every row is a different instance, an object in other words, of class. If you want to create a new row, you can just create a new object and then commit that object to the database. If you want to update an object, you query all the rows using a filter of yourchoice, the thing you get is your object, you update the attributes of the object, you commit and it is updated

cerulean pendant
#

if the yearmonth column is indexed it doesn't matter which type you use, it will be the same speed @torn sphinx

torn sphinx
#

Alright, thanks

solemn ridge
#

So how are you currently adding values to your table

calm charm
#

but how do I create a python var for it?

#

through update or insert commands?

#

or manually through the editor

#

if it's a one time thing

solemn ridge
#

Please stop using the ? it is kinda irritating, I am not sure if you don't mean it like that

#

So

#

A model looks like that

torn sphinx
#

@calm charm you can use raw SQL like I did with my crappy bot I'm remaking. Here's an example


GET_POSTCOUNT_THREE_MONTHS = """SELECT COUNT(*) FROM messages 
WHERE guild_id = 107900245270110208 AND author = ? 
AND datetime(timestamp) > datetime('now', '-3 months')"""
async def get_postcount_three_months(connection, member):
    try:
        cursor = await connection.cursor()
        pctm = await cursor.execute(GET_POSTCOUNT_THREE_MONTHS, (member,))
        postcount = pctm.fetchone()[0]
        await cursor.close()
        return postcount
    except OperationalError as e:
        print(f"DB error when getting topping eligibles:\n{e}")
        await sleep(1)
        await get_postcount_three_months(connection, member)```
solemn ridge
#

dont.

calm charm
#

._.

torn sphinx
#

Better don't, yeah. ORMs are handy

calm charm
#

i dont think that's what i need

solemn ridge
#

Either way, using a Model is 100% times better practice

torn sphinx
#

Notice the try/except - aiosqlite tends to error a lot

solemn ridge
#

than insert, update

calm charm
#

I just wanna turn a column into a variable

solemn ridge
#

You can't

#

That easily

torn sphinx
#

You need to fetch that column from the database first

solemn ridge
#

Using insert, update

#

Is very unreliable

#

I take it you have one of these:

async with aiosqlite.connect(...) as db:
  ### stuff
db = await aiosqlite.connect(...)
calm charm
#

like i have a column named item_id. and I wanna run: ```py
if item_id == 1:
name = 'pill'

#

ye

#

2nd 1

solemn ridge
#

Ok

#

So your model is going to be this

calm charm
#

the fr3ak is a model

solemn ridge
#
class Item(db.Model):
  __tablename__ = 'Items'
  item_id = db.Column(db.Integer, primary_key=True)
  item_name = db.Column(db.String)
  item_description = db.Column(db.String)
  item_icon_emoji = db.Column(db.String)
  name = db.Column(db.String)
  def __init__(self, item_id: int, item_name: str, item_description: str, item_icon_emoji: str, name: str):
    self.item_id = item_id
    self.item_name = item_name
    self.item_description = item_description
    self.item_icon_emoji = item_icon_emoji
    self.name = name

This is your model.
If you want to append a new row to your database you can just do:

new_Item = Item(item_id='...', item_name='...', item_description='...', item_icon_emoji='...', name='...'
db.session.add(new_Item)
db.session.commit()

If you want to query the object, filter by say item_id and update something you can do:

  #Saying we have a variable called given_id which is the id of the item you want to update
  items = Item.query.all()
  for item in items:
    if item.item_id == given_id:
      item_you_want = item
  item = ('update the attributes you want here')
  db.session.add(item)
  db.session.commit()
#

This a complete base for what you want to do

calm charm
#

ok

#

ic

solemn ridge
#

?

calm charm
#

I see

solemn ridge
#

Oh

#

Does this cover your needs?

uneven elbow
#

Hello everyone! I'm new to this stuff and having problems hosting my django web app on pythonanywhere.com for the past 2 days. I keep getting Error running WSGI application. sqlalchemy.exc.DatabaseError: (sqlite3.DatabaseError) file is encrypted or is not a database. If someone could help me out, I would give sum that I can afford to you as thanks.

cerulean pendant
#

@uneven elbow It seems the sqlite file you're using is corrupted somehow

#

did you try reuploading it?

uneven elbow
#

I pulled from git, I will try to upload it manually again. Maybe my path is wrong?

cerulean pendant
#

no, the error is not related to path, but to a file that is not a database

#

when you get that error, it means sqlite found the file you're pointing it to

#

but just couldn't use it because it is not a database

#

@uneven elbow send the code

#

!paste

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.

cerulean pendant
#

we can take a look if there is something wrong in it

#

maybe a screenshot of your files in the file explorer will help pinpoint the issue

uneven elbow
cerulean pendant
#

is it a django site?

uneven elbow
#

yes

cerulean pendant
#

ok, then that could help too - the file that defines the database

cerulean pendant
#

ok, now the screenshot of your files in the file explorer - I'm looking for the db.sqlite3 file

uneven elbow
cerulean pendant
#

ok, try renaming the db.sqlite3 file to something else, say db.sqlite3.backup

#

django should regenerate the file

uneven elbow
#

and reload the site?

#

nothing happens

cerulean pendant
#

@uneven elbow no errors?

uneven elbow
#

same error log

#

2020-01-19 14:27:27,855: File "/home/MaxxelZ/mysite/main.py", line 32, in <module>
2020-01-19 14:27:27,856: db.create_all()

#

but why is this showing in error log. my django site doesn't have main.py

#

and the path is wrong aswell

#
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/MaxxelZ/mysite/mysite/settings.py'
## and your manage.py is is at '/home/MaxxelZ/mysite/manage.py'
path = '/home/MaxxelZ/django_site'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'django_site.settings'
#
## then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
cerulean pendant
#

the error log should change if you rename the file

#

if it didn't change, then you are not running the same code as you are modifying

#

because if the file is not there anymore, it should give a different error

uneven elbow
#

I will try to upload everything again

cerulean pendant
#

@uneven elbow I still think your db.sqlite3 file is somehow corrupted

#

renaming it should change the error log

#

it has to change

#

if it doesn't, we can't proceed because the log is not being updated or something

#

and that makes it very hard to troubleshoot

uneven elbow
#

it didn't change, so I will try to upload everything from begginging

cerulean pendant
#

there's no way that renaming the file won't change the log

#

it must give a different error

finite lynx
#

When filtering in sqlalchemy is it possible to have a or statement?

#

For example

.filter(username='string' or id='string')

lusty igloo
#

Is it bad if to make my code shorter overall I use f-strings in fetchrow method? To be exact I use it in place "SELECT {variable} FROM..." or is it a thing that won't really matter?

solemn ridge
#

it doesnt matter

#

it just makes your code more semantic

#

which is better

lusty igloo
#

Its more about
I have same function in 5 files but only 1 thing changes, that is that variable so I though
make 6th file containing Class which takes name of variable and has that function and just import it within those files

#

But thanks

upbeat lily
#

I'm pretty sure you shouldn't be using fstrings with databases at all. You should use paramaterisation, not manual interpolation

#

You should be doing this @lusty igloo

 query = 'SELECT * FROM foo WHERE bar = ? AND baz = ?'
 params = (a, b)
 cursor.execute(query, params)
solemn ridge
#

Oh I thought it was a string

#

I didnt see it was db related

lusty igloo
#

I don't intend to fetch all columns but single column from single row

#

so I have

(f"SELECT {variable} FROM foo WHERE bar = $1",other_var)
``` as the `variable` is the only thing that changes between those files
upbeat lily
#

I think as long as variable isn't anywhere close to the user or other I/O, it should be safe

lusty igloo
#

It is not

upbeat lily
#

I'd still look to avoid using them with SQL though, paramaterisation isn't too difficult

lusty igloo
#

its in other class so it looks like

def __init__(self):
  self.func = DBFetch("variable").func``` more a thing
torn sphinx
#
_bot.players.aggregate([{"$match":{"rating":"95"}}])
``` Im attempting to aggregate using pymongo but this returns an object
#

how would i obtain the data

#

sorry i have this disease called dumb fuck

#

i got it

finite lynx
#

If I would make a new non nullable column for my table with a default value, would all the objects in the table have the default value or would there be a error

uneven elbow
#

@cerulean pendant I somehow fixed it by uploading everything from beggining

cerulean pendant
#

@uneven elbow good!!

#

I knew there was something wrong with the file - it was corrupted!

#

@uneven elbow What I find strange is that the error message didn't change from "File corrupted" to "File not found" when you renamed it

#

but whatever

uneven elbow
#

But guess what

#

theres a new error when I try to make post request. CSRF verification failed. Request aborted. Posted about this problem on django discord and they couldn't see whats wrong

cerulean harbor
#

hey

languid flame
#

hello

#

I am a beginner

#

want a simple easy database that is online

#

Easy to acess

#

with low latency

calm charm
#

sqlite3

torn sphinx
#

I have this MT Dataset, what can be done with such dataset, as a Data scientist, what questions can be asked regarding this dataset, what can be the yields, what to analyze, etc.

So far I thought to make a classification on age, gender.

To find the reasons why patients undergo, surgery, allergy tests, etc. what else can be done with it, any suggestions, please?

Any suggestion is appreciated.

mild haven
#

should I use sqlite3 or pymysql

charred fractal
#

https://pastebin.com/Vxg1RJNY
I need help on making it send to each user that is in quiz to send to the user's dm
Updated: Error: ```Ignoring exception in command quiz:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\User\Desktop\FlippingFlamingo\cogs\quiz.py", line 93, in quiz
await message.author.send(f"{Question}")
AttributeError: 'int' object has no attribute 'send'

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

Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'int' object has no attribute 'send'

rain wagon
#

@charred fractal maybe you should use bound statements instead of concencation

#

just saying

charred fractal
#

What do you mean @rain wagon ?

torn sphinx
#

So I am approaching on migrating to a new db lib

#

Motor for MongoDB

#

What do I do to start?

#

I'm reading the docs right now

languid flame
#

Hi

#

I need a database for a discord bot

#

I will use AWS DB

#

SHould I use postreg or MYSQL

#

i will actually ise postreg

#

I have databse exp

#

None with python

torn sphinx
#

So I need a basic intro to using databases with discord.py. If you can, please redirect me to a video or a document that explains everything, thank you!

#

PS, I've already downloaded SQLite if that's any help.

#

nvm

torn sphinx
#

Does anyone know what this error means

#
pymongo.errors.OperationFailure: not authorized on feudalmod to execute command { find: "modlog", filter: { guild_id: 674386972898361354 }, limit: 1, singleBatch: true, lsid: { id: UUID("91aa7d43-8403-4463-b494-e97fce4e37aa") }, $clusterTime: { clusterTime: Timestamp(1584814269, 1), signature: { hash: BinData(0, B26D7DF11DC80B5F34CD42A2EA0E95361F9A5C87), keyId: 6762369017630949377 } }, $db: "feudalmod", $readPreference: { mode: "primaryPreferred" } }```
#

I looked it up and it basically means that you are missing permissions

#

But I have a url that i connect to but it allowed me to use it for a while then it started giving me this error

warped sequoia
bold light
#

How can I have a team and a player reference eachother, ie. I want to have a teamlist for the player that has a list of teams they are a part of and I also want to have a list of all the players that are on a team as part of the team class. I am trying to do this in Mongo. Ty in advance.

pure cypress
#

@finite lynx Which dialect are you using?

#

E.g. SQLite, Postgres, MySQL, etc

#

I am honestly not familiar with SQLAlchemy. Maybe it abstracts it away so the behaviour is consistent across all of them.

#

But there are dialects, if not all of them, that will update existing rows with the default value.

finite lynx
#

Ok, it is SQLite

finite lynx
#

Ty

copper echo
#
        query = "INSERT INTO guilds (guild_id, register_message, register_emoji, register_role) VALUES ($1, $2, $3, $4) " \
                "ON CONFLICT (guilds.guild_id) " \
                "DO UPDATE SET register_message = $2, register_emoji = $3, register_role = $4 WHERE guild_id = $1"
``` so this give me error
#

Command raised an exception: PostgresSyntaxError: syntax error at or near ")"

#

i i use ON CONFLICT (guild_id) instead of following i get Command raised an exception: AmbiguousColumnError: column reference "guild_id" is ambiguous

#

if you know how to help me please ping, because im out of ideas

finite lynx
#

Are primary_keys always be the same counting upwards or can you change that?

half gyro
#

@finite lynx primary keys don't have to be numbers at all

finite lynx
#

Ok

#

Ty

pure cypress
#

@copper echo I believe ON CONFLICT (guild_id) is the only way and you'd have to prepend the fields with the table name in the SET clause to avoid the ambiguity error.

#

And, for what it's worth, in the SET clause you can access values being inserted with excluded.field_name but I guess using $1 etc is fine.

copper echo
#

so i have to use excluded.guild_id instead of that?

#

im not the best in sql

pure cypress
#

For example, either guilds.guild_id = $1 or guilds.guild_id = excluded.guild_id

copper echo
#

oooh, so you think problem is in WHERE clause?

pure cypress
#

Yeah probably

copper echo
#

ill try later thank you!

#

also can i ping you if it wont be this?

pure cypress
#

It doesn't know if you want to be checking the old guild id or the new one

#

So it says it's ambiguous

copper echo
#

ye makes sense

pure cypress
#

Yeah you can ping me for that though if that doesn't work I'm out of ideas honestly 😅

finite lynx
#

Woild that be a good idea for making the if for example for users a random arrangement of letters and numbers and just saving the primary key to a different named column?

pure cypress
#

Sounds like you're thinking of a UUID

#

But why wouldn't a plain autoincremented integer suffice for a user ID?

celest zodiac
#

@pure cypress One scenario where you wouldn't use an autoincremented integer is if you have, for instance, a horizontally sharded database with a very high rate of inserts

#

If you use an ID column, you need to have one node as a source of truth for the value of that column universally

#

A UUID can be keyed to, say, the shard ID, and so is guaranteed to be unique

#

If you need that one node as a source of truth for anything, that means any insert operations are going to block until you get that ID from that node

#

With a UUID you don't need that, since the right kind of UUID can guarantee uniqueness across all the nodes

#

But if you just have one node, then an ID column is fine.

pure cypress
#

Neat. Makes sense. Wouldn't have considered that since I haven't ever operated on such a scale.

celest zodiac
#

I found out about this the other day when doing research about ID columns. I'm working on a project that's basically a personal wiki, and I considered exactly the same question myself. For what I'm doing, an autoincremented integer is fine because it isn't meant to scale.

finite lynx
#

@pure cypress Yeah UUID I guess, and so generating that for each user so there wouldn't be a possibility for a user to loop through all users

#

Can I for example generate it using the UUID model and them save it to a string field in my database?

#

But maybe I should find something less hardcore for this use case, because I don't think I need a 128bit id for my user

#

uuid.uuid4().hex[:8]

#

I could do this, when generating uuids like this should I better still make sure that there isn't a equal uuid that exists already?

reef hawk
#

are database an o1 lookup? E.g. search through a column for a certain ID with a lookup instead of scrolling through the entire column for that column

#

if not, is it possible to make it an o1 lookup

torn sphinx
#

heya, how do I make a list dataset in an SQL database?

languid flame
#

hello

#

with asuncpg

#

i used fetch

#

this is the response

#

[<Record name='dydy' age=10>]

#

how do I manipulate this to get the data in name

#

and the age

#

like is there x['name']

#

or something

rich trout
#

results[0].name

#

if use use .fetchone() it will give you the result rather than a list of results

#

@finite lynx The reason uuid4 is so many bits is precisely so you don't have to worry about checking for an equal uuid

#

Your database should have a uuid field--MYSQL and POSTGRES both support them, which will help you avoid the severe penalty of string keys.

#
To put these numbers into perspective, one's annual risk of being hit by a meteorite is estimated to be one chance in 17 billion, equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%.
#

Really the impact of using a uuid in your design is not that significant, so you really shouldn't worry about it if you've decided to use it. If you're going to have problems with it, you were already going to have problems and need to look at your problem domain much more closely.

#

@reef hawk This depends on what you mean by O(1). Realistically, an INTEGER PRIMARY KEY lookup is the absolute fastest you can expect from a database and most other software in general

#

This is because the rows of a particular table are of a fixed size, and thus if you know you want item, say, 52, then it will be at rowsize*52, and you can immediately grab and send it.

#

For other columns/types, once you or the db determines there should be something called an INDEX, you get O(logn) or better performance due to it having a preconstructed decision tree of what values go to what rows. It's only for unindex, unsorted columns that databases will do a scan, or for some kinds of comparisons. For example, it's easy to tell whether, say, the primary key is less than 10. That's rows 1-9, which are all contiguous and immediately found. Finding all rows that have a "type" of 6 (like in the case of a foreign key) can be fast with an index. All the values for 6 are together, so it knows it needs to give you rows, 1, 2, 6, 9, etc. If there is no index, though, it has to check every row by definition. But it's also very fast at this.

#

There are a few exceptions, one of which is STRING/VARCHAR columns. Because these kinds of columns have variable length and LOTS of data assigned, they can be very unwieldy to work with. You should avoid using them for querying. As an example, grouping a database's list of records based on first name is expensive. It needs to keep track of every (Variable length chunk of data) visited, and sort them, and then collate them into a result. It's faster than python doing the sorting because its got lots of internal optimizations, but if you're not trying to get a sorted-by-name listing then there are much better options.

languid flame
#

thanks

#

What should I use to host a PostregSQL database\

#

For a discord bot that will very frequently acess the database with asyncpg

#

I was planning on using AWS RDS

#

is there a better alternative?

grim lily
#

I have a Project class, User class and AssociationUserToProjectClass

class AssociationUserToProject(SqlAlchemyBase):
    __tablename__ = 'user_to_project'
    num = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
    member_id = sqlalchemy.Column(sqlalchemy.Integer, sqlalchemy.ForeignKey('users.id'))
    project_id = sqlalchemy.Column(sqlalchemy.Integer, sqlalchemy.ForeignKey('projects.id'))
    project_role = sqlalchemy.Column(sqlalchemy.String)

I want to project_role be accessible from User, but it's not, and i don't know to do it.
Can anyone help me,please?

brisk oar
#

in aiosqlite3 con.row_factory = aiosqlite3.Row, raise ```py
Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Python/smth/test.py", line 25, in db
self.bot.db = await data_base.Data(self.bot.state['js'], self.bot.state['db'])
File "C:\Python\smth\cogs\utils\data_base.py", line 9, in call
fn = obb.init(*args, **kwargs)
File "C:\Python\smth\cogs\utils\data_base.py", line 20, in init
self.con.row_factory = db.Row
ValueError: no active connection

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

Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: no active connection```

#
class AsyncMeta(type):
    async def __call__(cls, *args, **kwargs):
        obb = object.__new__(cls)
        fn = obb.__init__(*args, **kwargs)
        if inspect.isawaitable(fn):
            await fn
        return obb


class Data(metaclass=AsyncMeta):
    def __init__(self, js, file):
        self.file = file
        self.js = js
        self.con = db.connect(self.file, check_samethread=False)
        self.con.row_factory = db.Row```
finite lynx
#

@rich trout Would that still hold if I make it 10 characters long though? As for a users Id I don't that complex of an Id

rich trout
#

Less so, but there is a uuid type you should be using in the database

waxen iron
#

Or maybe someone knows another good async orm for mongo?

torn sphinx
#

How would one convert

    @welcome.command()
    async def channel(self, ctx, *, channel : discord.TextChannel):
        if ctx.message.author.guild_permissions.view_audit_log:
            db = sqlite3.connect('welcome.sqlite')
            cur = welcome.cursor()
            cur.execute(f"SELECT channel_id FROM welcome WHERE guild_id = {ctx.guild.id}")
            result = cur.fetchone()
            if result is None:
                sql = ("INSERT INTO welcome(guild_id, channel_id) VALUES (?, ?)")
                val = (ctx.guild.id, channel.id)
                await ctx.send(f"Channel has been set to {channel.mention}")
            elif result is not None:
                sql = ("UPDATE welcome SET channel_id = ? WHERE guild_id = ?")
                val = (channel.id, ctx.guild.id)
                await ctx.send(f"Channel has been updated to {channel.mention}")
            cur.execute(sql, val)
            db.commit()
            cur.close()
            db.close()

to aiosqlite3?

calm charm
#

ummm

#
    result = function()
sqlite3.OperationalError: database is locked```
#

wdym

random sandal
#

Hi, currently im learning ER Diagram, so i am a beginner and I need help.

#
  1. A Virus has a name, an ID, and a date of discovery.
  2. A Viral Disease has a name, an ID, and a date of discovery as well.
  3. A Viral Disease is caused by a Virus and you can assume that a Virus causes one Viral Disease.
  4. Viral Diseases are distinguished between being fatal and nonfatal.
  5. Viruses infect. Each citizen in this database has been infected. They, therefore, have at least one Viral Disease.
  6. A citizen has a name, a BSN, and an age.
#

Can someone say if i am doing right or wrong and how should i do point 4?

#

  1. Unfortunately, fatal Viruses have killed and are killing people. When that occurs, a time-of-death is noted.
#

anyone tips how i should do step 7 ?

random sandal
#

@pure cypress or @rich trout any tips for this new guy

rich trout
#

I’m out on the water ATM, but a time of death is related to a given person, and perhaps a cause of death

#

If that cannot be inferred by the tables you already have

#

Does your model handle multiple infections?

#

As described, it does not

#

If not, then you can probably assume that the disease the person had at their time of death was the disease that killed them

#

Therefore, TOD is only logically keyed to person

#

@random sandal if you can follow that logic the answer shouldn’t be too far away

reef hawk
#

@rich trout I'm usually using bigint primary key as my lookup

#

I guess that'd be the fastest way to retrieve it? for ex

SELECT col_1, col_2 FROM datatable WHERE p_key = $1 (value)
languid flame
#

Okay

#

Is AWS good for postrge hosting

#

I want cheap but fast hosting

#

what would ypu recommend

north niche
#

@languid flame DigitalOcean

languid flame
#

Thanks➡️

#

@north niche I am using windows

#

Is digital ocean also good for bots

#

I want to use only one platform

north niche
#

DigitalOcean offer VPS and Kubernetes for hosting @languid flame

languid flame
#

But its 20$ per month

#

any cheaper alternatives?

#

15$ for the database and 5$ for the droplet

north niche
#

You can host DB in droplet too @languid flame

random sandal
#

@rich trout How do I point 4 making a distinguish being fatal and non fatal?

rain abyss
#

Hi, so I made a database with sqlite but now I need something for production. I was planning on using mySQL, but I don't really understand how I would be able to interact with it externally. Do I need php? Can I use python? I've only just learned how to work with databases so if someone could help me with this I would greatly appreciate it

rich trout
#

You can use python--see mysql-connector for python

#

@reef hawk yep

rain abyss
#

I'm taking a look ay my-sql connector and it seems like a good choice for accessing the database on my local machine, but if I wanted to connect to it from outside of my network what would I do?

Would it be inefficient to create something like a flask server that then runs mysql connector commands? @rich trout

rich trout
#

You can in fact connect to a database on a remote machine. It's not usually the best of ideas because exposed databases are often targets (you'll see services like amazon have an "internal network" and an "external network" for these kinds of reasons)

rain abyss
#

Sorry I should clarify more. I have made an application that interacts with the database since the users aren't really code savvy, so at this point it would be discord bot > sql server. Say I decide to host it with a droplet, should I do discord bot > flask server on droplet > run sql commands?

rich trout
#

No need

#

Either you've got the msyql server on the same machine as the bot--at which point it's identical to local, even when accessing it as it is in fact on localhost for connections, or it's on a different machine in the same cloud, where you should look for the "private address" to set up a direct server connection for

#

I would suggest having them on the same machine--you're unlikely to need more and it's usually cheaper

rain abyss
#

So say I decide to have both on the same machine, if someone runs a command, I can just use mysql.connector to get that data? That would be incredibly simple

#

Because that's what I'm doing in testing right now with sqlite, I don't know why this didn't even cross my mind

#

I don't know why I thought I would have to over complicate things. Thanks for your help bast, really appreciate it

rich trout
#

👍

rain abyss
#

Hey @rich trout, another quick question. So I'm reading that it can take some time to open and close connections to the mysql database, so do you think I should open the connection when I start the discord bot, and then pass the connection object to my specific functions when I call them?

rich trout
#

That is one way to do it, but I would suggest something called a connection pool if there's a nice one provided

rain abyss
#

Great, I'll check that out

random sandal
#

@rich trout at point 4 do i need to make a new entity with fatal and non fatal as properties? is this the good way to slove that?

rich trout
#

is there some reason you can't just.. add a new property to the entity on the map?

vast parrot
#

Hello, i use pyrebase to store and push data to firebase.
But how can store only the value of the key?
Example in json:

{
  "name": "Morty",
  "age": 33,
  "hobby": "Skating",
  "wife": false
}

I want to store the hobby value how can i do it?

lofty summit
#

my_dic["hobby"]

#

nvm

gentle sparrow
#

Can anyone dumb down what migrations in Django are

#

like DUMB down to the core

void otter
#

migrations aren't really what they say they are

#

you would say it is used for migrating data?

#

it's used for preserving data when you change table definition

#

let's say you have some certain table which holds some data

#

and you edit, add or remove a column

#

it would pretty much break the db if data stays the same

#

migration is a way of fiddling with table without losing data

vast parrot
#

lol i solve that problem

gentle sparrow
#

I see

#

Thanks

#

So

#

Let's use a real life example

#

Usernames and passwords

#

Say you wanted to permanently ban a user

#

What you would do is delete their column right

#

@void otter How would that look like in a migrations and models file

#

What would i need to edit to delete that user

torn sphinx
#

So how would I make it so that there’s one big chunk (a guild) and inside that chunk, there are users. Each user would usually have a None for the amount of warns they have, but when they’re warned, it would give them a warn. How would I make this? Would this end up with me making a new table for each guild or something?

#

{ [“guild_id”]{ [“user_id”]{ [“warn”]{ [“mod (who warned”]{ [“date”]{ [“reason”]{ } } } } } }

#

Somewhat this?

#

But that looks like JSON to me :/

fallow ingot
#

How can i use a local mysql database? I am using right now an external online database, but these hosts rejects the connection if i start my flask project from like pythonanywhere. Is there a module for a local database or something like that?

calm charm
#

is there a command or like a system that automaticall backups your guys db Is there a way to automatically backup a database through discord.py or any python script

languid flame
#

hello

#

asyncpg.exceptions.TooManyConnectionsError: remaining connection slots are reserved for non-replication superuser connections

#

what is this error

#

Is it because my bot connects and disconnects a lot

toxic rune
#

I might be completely wrong here but, are you gracefully shutting down your bot? I had to deal with a similar problem where I was forcing it to shut down and it wouldn't release the connections so I had to restart the PostgreSQL service.

void otter
#

@gentle sparrow each database and each framework has it's own implementation of migration

#

only one i can remember of the top of my head if flask_migrate

#

a lot of people use libraries tied with their framework of choice

languid flame
#

oh no

#

I was just spam connecting

#

realised rather than reconnetc