#databases

1 messages · Page 65 of 1

willow sentinel
#

I erased the database and then ran if the user didnt have a account his part

torn sphinx
#

What sql query you used to create the db?

willow sentinel
#

Sqlite3

torn sphinx
#

Not the db type

willow sentinel
#

Oh all three numbers are integers

torn sphinx
#

I am gonna try to recreate your db

#

Like you know CREATE TABLE ...

willow sentinel
#

Yes

#

Create table if not exists usersmoney (Id INTEGER DEFAULT 1000, money DEFAULT INTEGER, streak INTEGER DEFAULT 0);

torn sphinx
#

BTW i see you are using a non-async(sqlite3) lib with an async lib(discordpy) i wouldn't do that if i were you

willow sentinel
#

Already been told that

#

Kinda too late?

torn sphinx
#

Never too late

willow sentinel
#

Ok then whaat do i use?

torn sphinx
#

I mean sqlite doesn't have async alternatives iirc but you can switch to MySQL or PostgreSQL

willow sentinel
#

Would i have to rewrite my code?

#

Hat much

#

Eh>

torn sphinx
#

Just a little bit worth it imo

#

BTW your table name doesn't match

#

In your screenshot you refer to it as usermoney and on the query its usersmoney

willow sentinel
#

I just typed it wrong on here probably

#

How would i make a MySQL in python

#

I’m looking at the website and it looks like you have too login?

#

To*

torn sphinx
#

You need to run the db seperately

#

And use aiomysql to communicate with it

willow sentinel
#

You see i cant do that

#

Is there an async sql that i can run inside of python

torn sphinx
#

@willow sentinel found the problem

#

id = currentid should be id = c.fetchone()

willow sentinel
#

That was what it used to be but then it would always create a new row no matter what

torn sphinx
#

try replacing id == None to id is None

#

(both)

willow sentinel
#

Currently i cant get. Aiosqlite to work so hold it

torn sphinx
#

You can only use await in async functions

#

Or you can use async.run

#

instead of await

willow sentinel
#

Now its telling me that dB isn’t defined

#

Nvm got it

#

I cant await this?

torn sphinx
#

@willow sentinel You can't use await while not in an async function, But you can use asyncio.run -> async.run(cursor.execute((...))

willow sentinel
#

Ok now I’m getting context manager object is not iterable

torn sphinx
#

@willow sentinel make an async def main(...

#

And call async.run on that

willow sentinel
#

Gosh databases are so frustrating

torn sphinx
#

ye

willow sentinel
#

Ok so now i have another syntax error.

torn sphinx
#

@willow sentinel make a main func

novel gust
#

stop making d.py commands in on_message

#

dont think you can hide that from me @willow sentinel

torn sphinx
#

He seems like hes still using the old version

novel gust
#

where?

#

also aiosqlite is bad

#

use asyncpg and pg

torn sphinx
#

he said he can't use postgre and mysql

novel gust
#

lmao why

#

cause he's using replit?

torn sphinx
#

idk

#

@novel gust any idea to store a dict(str:str) and a list of strings?

novel gust
#

a dict

#

of strings to strings

#

and a list of strings

#

ok focus on the first one

#

explain the purpose of this dict

torn sphinx
#

Holding addresses for non-numerical ids

willow sentinel
#

Do i need to await this?

torn sphinx
#

@willow sentinel just use a main func

willow sentinel
#

That’s what. I thought this was.

torn sphinx
#

@willow sentinel So this is in async def main()?

willow sentinel
#

I didnt know what a main func was so :/

torn sphinx
#

forget what i said

#

What are you trying to achieve?

willow sentinel
#

A database that uses I’d’s to determine the amount of ‘money’ someone has and then use it for gambling etc.

torn sphinx
#

And what are you trying to achieve in the async with... part?

willow sentinel
#

Not really anything i just want it to work

torn sphinx
#

Wdym by work?

willow sentinel
#

Function in a way that doesn’t cause any errors and keeps players data stored safely

torn sphinx
#

Drop the main and async with part

#

You just need to replace the sqlite parts

willow sentinel
#

How so?

torn sphinx
#

i mean replace conn.execute etc

willow sentinel
#

But i set con = aiosqlite.connect?

#

Conn

torn sphinx
#

async with aiosqlite.connect(...) as conn

willow sentinel
novel gust
#

stop being fucking ignorant about aiosqlite

#

and python in general

#

you need to replace async with with await

spiral burrow
#

Really

willow sentinel
#

Ive already done that and it just gives me syntax errors

novel gust
#

seen his other code?

#

kthxbye

spiral burrow
#

!tempmute 328678556899213322 1d take some time to cool off

delicate fieldBOT
#

:incoming_envelope: :ok_hand: muted @novel gust until Mon, 29 Apr 2019 22:10:01 GMT (take some time to cool off).

spiral burrow
#

Stop being shitty to other people

willow sentinel
#

He’s probably gonna yell at me in anther server instead lol. But thanks.

torn sphinx
#

@willow sentinel async with aiosqlite.connect(...) as conn:

fading river
#

Anybody know how the precision argument to a Float works in SQLAlchemy? The docs just say it's "the numeric precision for use in DDL CREATE TABLE", whatever that means

#

I just want to store values with a four digit significand with one digit after the radix, like 123.4

#

So if I can hint the underlying sqlite store to use fewer bytes I would like to do that.

torn sphinx
#

DECIMAL(p,s): Exact numerical, precision p, scale s. Example: decimal(5,2) is a number that has 3 digits before the decimal and 2 digits after the decimal - stackoverflow

fading river
#

Wouldn't that be with the SQLAlchemy Numeric type, not Float?

torn sphinx
#

True but i think precision would be the same

fading river
#

I have actually read the doc, it says Numeric is "A type for fixed precision numbers, such as NUMERIC or DECIMAL."

#

But I didn't want the overhead of translating into Python Decimal objects

glossy scroll
#

I've been invited to help out with a project using MariaDB and I'm pretty sure it should be asynchronous because ofdiscord.py as well as pretty consistent errors for example: 2019-04-28 22:38:50 21 [Warning] Aborted connection 21 to db: '<db>' user: '<user>' host: '<ip>' (Got an error reading communication packets)(I removed the db name, the user and the ip). I believe this comes from discord.py... but I'm honestly not sure
However there doesn't seem like there's many options that'll work. Can anyone point to a good MariaDB asynchronous library, or should I just convert over to something like Postgres with asyncpg or am I just incorrect about my assumption?

keen cedar
#

After helping LukeAbby, if you can, can someone lemme know how I can refactor this query? :,)

WITH pls_refactor_this AS 
    (WITH filtered_scores AS 
        (SELECT user_id, exp FROM scores WHERE guild_id = $1) 
     SELECT user_id, exp, ROW_NUMBER() OVER (ORDER BY exp DESC, user_id DESC) FROM filtered_scores) 
SELECT row_number FROM pls_refactor_this WHERE user_id = $2;```
#

Got it to this, can I refactor it any further?

WITH ordered_scores
AS (
    SELECT user_id ,ROW_NUMBER() OVER (
            ORDER BY exp DESC, user_id DESC
            )
    FROM scores
    WHERE guild_id = $1
    )
SELECT row_number
FROM ordered_scores
WHERE user_id = $2;```

Also, there won't be any side effects from this, right?
cunning ginkgo
#

How could I make an expiring database entry in postgresql? It's linked to a flask backend but that doesn't matter.

#

Like, it's deleted after x hours sorta thing

pure scroll
#

there is no such functionality, you need to cleanup your tables yourself. E.g have a cron job that would run delete query every X minutes

red osprey
#

Guys, I used SQLAlchemy to develop a simple web app connected to a local sqlite db while testing. It uses sessions (db.session.add, db.session.commit etc). My prod db is in google cSQL. I have docs showing how to connect using pymysql, but that seems to be create a connection, and then squirt SQL commands through the connection. Is there a way to use db.sessions with cSQL?

wind pelican
#

looks like sessions are part of the sqlalchemy library, so no not likely.
what part of the sessions do you like? the sql bindings you are using might have something similar

#

for example sqlite has a cursor that you can use to build transactions

#

oh looking in to it anything that uses pythons db api spec is likely to autocommit and so transactions dont reall work :/

inner lintel
#

Hello,
my question aboutredis-py
can i store list on it ?

my_list=[{"name": "James", "age": "12"}, {"name": "Jorden", "age": "15"}]
db.set("list", my_list)```
pure scroll
#

you need to serialise it, redis as any other storage is not able to store python object, you can only store serialised objects

red osprey
#

@wind pelican In my tests, using sqlite, it enabled me to create a generic upsert function that each of my models could use (where the unique_id is NOT the pk - dont ask, I didn't design the db...). Worked a charm, so was hoping to do a simple lift n shift to my cSQL db

#
def upsert(model, item, **kwargs):
    dt = datetime.now()
    #session = db.session
    instance = db.session.query(model).filter_by(**kwargs).first()
    if instance:
        # update
        print('Exists')
        itemmembers = [attr for attr in dir(item) if not (attr.startswith("_") or  attr.startswith("query") or  attr.startswith("metadat"))]
        instancemembers = [attr for attr in dir(instance) if not attr.startswith("__")]
        for elem in itemmembers:
            print(elem, getattr(item, elem), ' | ', getattr(instance, elem))
            # check each member in item
            if getattr(item, elem) is not None:
                # if the received instance contains a value for
                # that member, then update the value in the db
                setattr(instance, elem, getattr(item, elem))
        setattr(instance, 'updated_at', dt)
        db.session.commit()
        return "Exists: " + instance.unique_id
    else:
        # insert
        print('Update')
        instance = item
        setattr(instance, 'created_at', dt)
        setattr(instance, 'updated_at', dt)
        db.session.add(instance)
        db.session.commit()
        return "Insert: " + instance.unique_id```
#

Background: It's a system info logger. Clients send a JSON object of a list of infos. This web app receives the JSON, maps the keys to db fields, and then does an upsert

#

Otherwise, crafting an INSERT ON DUPLICATE UPDATE for each JSON type and class would be a ball-ache

#

Connection turned out to be really simple using cSQL proxy: ```python
from flask_sqlalchemy import SQLAlchemy

app.config['SQLALCHEMY_DATABASE_URI'] = (
'mysql+pymysql://{nam}:{pas}@127.0.0.1:3306/{dbn}').format (
nam=db_user,
pas=db_password,
dbn=db_name
)
db = SQLAlchemy(app)``` - I'll use a unix_socket once I move the app to GAE

knotty parcel
#

Can I get help?

#

With sqlite3

#

Ignoring exception in member_join
Traceback (most recent call last):
File "/home/runner/.local/lib/python3.6/site-packages/discord/client.py", line 255, in _run_event
await coro(*args, **kwargs)
File "/home/runner/cogs/welcome.py", line 32, in on_member_join
embed = discord.Embed(colour=discord.Colour(0xac6f8f), description=(result1[0]).format(members=members,mention=mention,user=user, guild=guild))
AttributeError: 'NoneType' object has no attribute 'format'

wary merlin
#

not related to sqlite

knotty parcel
#

Is there a solution?

wary merlin
#

u_u

knotty parcel
#

I tried debugging it

#

I get None

wary merlin
#

use aiosqlite plz

knotty parcel
#

aiosqlite?

wary merlin
#

sqlite is blocking

knotty parcel
#

How do I use it

wary merlin
#

also

#

return
print("None")

#

return stop the function

#

the print is never called

#
>>> def f():
    print("1")
    return
    print("2")

    
>>> f()
1
knotty parcel
#

I see thank you

wind pelican
#

@knotty parcel also you are using format strings to generate your sql query, do not do that. Its only advantage is that it increases the chances of someone breaking your queries/hacking your bot.
use the built in system for passing in arguments:

t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)```
#

there are more advanced options but the tldr is that you can pass your data in as actual data and sqlite will take care of sanitizing most of it

knotty parcel
#

okay
@torn sphinx ...

torn sphinx
#

the way I showed you worked I see what you did wrong from what I showed

#

description=(result1[0]) the () should not have been there

#

@knotty parcel

fluid nova
#

really not sure if this is the right place to ask this but i need help with something that lnolves a large amount of text data... json... or somethign else if recommended .. and flask/jinja

#

anyone available ?

drifting moth
#

hey guys

#

I need some assistance with running some SQL commands, what method is best to communicate these?

red osprey
#

@wind pelican I checked, and looking here - https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql - cSQL does support SQLAlchemy sessions. Turns out 1 of the columns in cSQL was slightly different to the corresponding column in my sqlite db (and my model class). Once I matched it across, it works. Just for ref, in case anyone asks in future. Means I can use that upsert function and have a really lean app

torn sphinx
#

if i want to make a json file with information about all the members in server. i need to make a json list for any member?

#

i wrote here becuse it about json

river barn
#

Don't post questions in two different channels please

fading river
#

I have a bunch of SQLAlchemy ORM-mapped classes in different files, but when it comes time to create the tables for them, is there a simple way to do that? Or do I have to hunt them down, import every single one, and do a metadata.create_all(engine) on them?

#

Actually I'm going to try having a single declarative base

#

Yep. That's how I should have done it.

#

oh god circular import I'm so bad at this

ionic pecan
#

@fading river i‘d recommend using alembic for this

quiet ermine
#

If its flask its probably best to use flask-migrate

#

Just to support multiple databases for debug and production

fading river
#

not flask, I'm polling IOT devices and feeding the results to dash

hardy grove
#

Hey There

#

Does Heroku Free Hosting Supports SqlLite3 Python

#

?

#

Will it Work

#

Just Ping me :D

river barn
#

sqlite3 cannot really be "hosted", it's just a file based db lol

#

so yeah it should work

#

but don't use heroku

fading river
#

@ionic pecan that seems to be a migration tool, would it help me out in this scenario?
So what I've got is a db.py that hosts the declarative base, which is then imported to the various files that have ORM mapped classes. But those files must be run (imported somewhere) in order to register the classes, meaning I can't have both the declarative base and the call to create the tables in the same file!

#

Because that's a textbook circular import

ionic pecan
#

Yeah

#

The idea is to drop the c all to create the tables from your regular code altogether and have alembic manage that

#

Because if you do that you will also get conflicts once you start changing your schema

patent glen
#

@hardy grove sqlite will not work on Heroku, it stores data in a file and Heroku deletes all files other than the ones that were originally deployed periodically

river barn
#

This is why you don't use heroku

hardy grove
#

then tell me a free hosting server ;-;

torn sphinx
#

How do you define a string on creating a table in psycopgs2, I suppose its STRING?

#

Or is it TEXT?

tropic zealot
#

TEXT

torn sphinx
#

And how do I insert a null value into it? do I just do ""?

#

@tropic zealot

tropic zealot
#

Yeah, that looks right.

torn sphinx
#

Sorry for tagging you btw

tropic zealot
#

All good.

torn sphinx
#
def create_server_if_not_exists(server_id: str):
    c.execute("SELECT COUNT(*) FROM Users WHERE UserID=%s", (str(server_id),))
    user_count = c.fetchone()[0]
    if user_count < 1:
        print("[Servers Table] Creating server with id " + str(server_id))
        c.execute("INSERT INTO Users VALUES (%s, %s, %s, %s, %s, %s)", (str(server_id), "", "", False, False))
        db.commit()```
#

Look good?

#

(I should use f strings but yeah)

#

@tropic zealot

#

Given that the 2&3 values are TEXT

tropic zealot
#

Try it out and see. I've only just begun learning SQL myself.

torn sphinx
#

oh ok

#

cheers

tropic zealot
#

That looks like it's inserting a server ID into a users table. Are you sure that's what you wanna do?

torn sphinx
#

Oh Cheers

#

Didn't notice that

#

😊

maiden halo
#

Not really sure what to do with this now... It works on other apps, just not this...

lilac spruce
#

so with dealing with sub/supertypes, are both the subtypes supposed to have the "same" primary key ?

fading river
#

@maiden halo Your project is probably in a different venv than the one you have the mysql package installed in

#

Can you go to one of the projects where you have mysql importing successfully, and run this script python import mysql print(mysql)

lilac spruce
#

this is the table set up i am looking at at the moment, but idk if its right

hardy grove
#

Wow

light zinc
#

Hey,
Questions about sqlalchemy
Can i add comment and doc for column at the same time? So I dont have to make variable or duplicate the comment?

price = db.Column(db.Integer, nullable=False, comment="Price is in cents.", doc="Price is in cents.")
#

And second question is how can I add comment on table?

maiden halo
fading river
#

So if you start the other script with python champs.py does it work?

maiden halo
#

I made a new conda env and it worked out, but is there a way to install these modules for all envs?

#

Also I'm running into a new problem now. Not a syntax error, but wondering how can I get just clean info from DB without the dictionary tags and \n's

#
cursor.execute("SELECT champion FROM all_champions")
all_champs=cursor.fetchall()
cursor.execute("SELECT matchup from all_matchups")
all_matchups=cursor.fetchall()
for champion1 in all_champs:
    for champion2 in all_champs:
        matchup=f"{champion1} vs {champion2}"
        print(matchup)```
#

('Aatrox\n',) vs ('Aatrox\n',)

#

How do I remove the ('from infront and \n,) from behind?

#

Do I need to regex? I feel like there's an easier and better way to do this.

light zinc
#

You can try something like:

re.sub(r"[\(\)\n',]", "",  champion1)

or maybe

re.sub(r"\W", "",  champion1)

Though if I can, I would fix how they are stored in db

glossy dock
#

how does one create a sqlachemy class that inherits from two classes that share a base? I'm getting a

sqlalchemy.exc.InvalidRequestError: Class <class '__main__.Consumable'> has multiple mapped bases: [<class 'Action.Action'>, <class 'Item.Item'>]

I essentially want to have the tables 'action' and 'item', but a class Consumable that would let me have the properties of both actions and items and then save to both tables

visual rune
#

Hey all, can anyone tell me whats wrong with this query ?

INSERT INTO prefixes (guild_id, prefix) VALUES (123456789, 'test') ON CONFLICT (guild_id) DO UPDATE SET prefix='test' WHERE guild_id = 123456789;

it returns this error column reference "guild_id" is ambiguous

#

oh and im using postgres

pure scroll
#

WHERE prefixes.guild_id = 123456789;

#

should work

visual rune
#

Yeah it works. Thanks

torn sphinx
#

databases are hard :c

hot sphinx
#

Hi all, I come from a JS background and the popular database is MongoDB for that. What is the equivalent of that for Python users? (What is the popular database I should study on)

ionic pecan
#

PostgreSQL

hot sphinx
ionic pecan
#

none that I‘m aware of, nope

#

the postgresql development group is a nonprofit afaik

hot sphinx
#

@ionic pecan Ahh I see. Thank you! 😃

ionic pecan
#

no prob

#

Feel free to ask here if you have any questions

hot sphinx
#

Thanks! @ionic pecan I do have another: Is MySQL and Oracle the same thing? Im a little confused if theres a oracle db and a separate mySQL db when I hear people talk about it

ionic pecan
#

I‘m pretty sure they are different

#

Oracle is some commercial database with AI in it and whatnot, mysql is open source

#

There‘s also MariaDB which is a fork of mysql

patent glen
#

the confusion is because Oracle, the company, bought MySQL a while back.

#

so it owns both of them now

tawny sail
#

hey guys

#

how do i make conditional insert in sqlalchemy-aio

#

like sql code would be (UPDATE guild_settings SET prefix=%s WHERE guild_id = %s', data)

tawny sail
#

nvm,i got it

torn sphinx
#
sql = "SELECT * FROM has_channel_scalit WHERE userID = %s"
val = (str(ctx.author.id))

That gives an error "near %s"

merry prism
#

How is it supposed to tell what %s is? @torn sphinx

#

Look at how you wrote it

tawny sail
#

@torn sphinx you have to provide a tuple at the end

#

for the actual value

#

it will look something like this

conn.execute(sql, (val,))```
merry prism
#

Yup.. Declaring a SQL statement, then declaring the data that goes into it doesn't make any sense, its Python, top to bottom

keen cedar
#

Okay so, I have a database table users that has columns user_id and exp.

Now, I have a dictionary, the keys are user ID's and the values are EXP.

I want to update the database table. If it contains the User ID already - it shall update the EXP if the new EXP from the dictionary is higher than the one in the database. If it doesn't contain the user ID - it should insert a new row, with the user ID and the experience from the dictionary.

What would the query look like? Don't want to loop through the list and querying each of the item of the dictionary separately

sleek mango
#

Hey, how can I update all the documents in a collection in mongodb ? I just wanna add an array to the end of a document

#

I was thinking maybe I could loop through all the documents using pymongo and update them

#

Is there any GUI which can do this job for me ?

neon turtle
#

i need info about dealing blob objects like documents and media files
like

#

inserting into a db
retrieving ...

#

the general way of how to do

neon turtle
#

hey anyone there
i just need a tutorial or a blog kinda thing
ping me if u have one to give me

faint prairie
#

you just treat them like any binary object, so open the file in binary mode, read it into memory, stuff it in the database

torn sphinx
#

Error:

Error adding user: current transaction is aborted, commands ignored until end of transaction block

Code:

try:
            with self.db.cursor() as cursor:
                sql = """ INSERT INTO players (user_id, joined, xp_points) VALUES (%s, %s, %s)"""
                cursor.execute(sql, (member.id, member.joined_at, 0))
            self.db.commit()
            print("Added user %s to database." % member.id)
        except Exception as e:
            print("Error adding user: %s" % e)

I am unable to INSERT INTO my players table. I have added quotes, backticks, and deleted them entirely. I have attempted to emulate this variable declaration from https://pynative.com/python-postgresql-insert-update-delete-table-data-to-perform-crud-operations/:

  postgres_insert_query = """ INSERT INTO mobile (ID, MODEL, PRICE) VALUES (%s,%s,%s)"""

Still no such luck. Ive been trying to figure this out for a couple hours, so any direction would be greatly appreciated!

#

When I did have the quotes, Id get this error:

Error adding user: syntax error at or near "'user_id'"
LINE 1: INSERT INTO players ('user_id', 'joined', 'xp_points') VALUE..
river barn
#

pretty sure you either don't need the commit() method with the context manager or it needs to be in the block

#

In the example you linked it doesn't use the with block so it's probably that

torn sphinx
#

I didnt even notice that! ._.
Ill try it and get back to ya!

torn sphinx
#
    def get_player(self, user_id):
        try:
            #with 
            cursor = self.db.cursor()
            sql = "SELECT 'user_id', 'joined', 'xp_points' FROM players WHERE 'user_id'=%s"
            cursor.execute(sql, (user_id,))
            result = cursor.fetchone()
            if not result:
                print("User does not exist: %s" % user_id)
            else:
                return result
        except Exception as e:
            print("Error looking up userid %s.\n%s" % (user_id, e))

    def add_all_users_to_db(self):
        for member in self.bot.get_all_members():
            self.add_user_to_db(member)

    def add_user_to_db(self, member):
        if self.get_player(member.id):
            return

        try:
            cursor = self.db.cursor() 
            sql = """ INSERT INTO player_users (user_id, joined, xp_points) VALUES (%s, %s, %s)"""
            cursor.execute(sql, (member.id, member.joined_at, 0))
            #self.db.commit()
            print("Added user %s to database." % member.id)
        except Exception as e:
            print("Error adding user: %s" % e)
#
Error looking up userid .
current transaction is aborted, commands ignored until end of transaction block
Error adding user: current transaction is aborted, commands ignored until end of transaction block
#

Stillk getting the same error >.>

river barn
#

seems weird

#

btw for a discord bot you should use asyncpg, not psycopg2

torn sphinx
#

I had no idea that existed 😮

river barn
torn sphinx
#

I appreciate it!

#

Im checking out the docs now

ornate isle
#

If you want to use asyncpg with SQLA core, I’ve been using Tom Christie’s new databases library: https://github.com/encode/databases

torn sphinx
#

After I refactored it to include asyncpg/asyncio, I keep getting an error saying RuntimeWarning: coroutine 'connect_database' was never awaited, but when I debug the function it connects with no issue. Its not until I run everything within my object that I get that error. @river barn

rustic yarrow
#

Anyone know what kind of issues you might run into on MongoDB compared to something like SQL?

formal oar
#

cursor.execute(sql, (member.id, member.joined_at, 0)) you're missing a , @river barn

river barn
#

?

formal oar
river barn
#

A) Shouldn't be needed because the , at the end serves only for defining tuples with just one item
B) I didn't post the code

formal oar
#

Oof pinged the wrong guy

torn sphinx
#

Hey thanks!

#

I didnt notice that!

formal oar
#

What do you mean serves only for defining tuples with just one item?

#

It won't work

river barn
#

You don't need it

#
In [29]: (1)                                                                                                                                       
Out[29]: 1

In [30]: (1,)                                                                                                                                      
Out[30]: (1,)

In [31]: (1,2)                                                                                                                                     
Out[31]: (1, 2)

In [32]: (1,2,)                                                                                                                                    
Out[32]: (1, 2)```
see this
#

But I think you are getting the query formatting wrong, does psycopg2 use %s formatting for parameters?

torn sphinx
#

Yes it does

#

not sure about asyncpg though

#

still reading those docs

river barn
#

asyncpg uses $index

#

aka something like execute("SELECT name FROM test WHERE id=$1 AND exp=$2", user.id, user.exp)

torn sphinx
#

That makes sense

#

Im still trying to figure out why I keep getting TypeError: A Future, a coroutine or an awaitable is required even though Im pretty sure Ive set all my coroutines and awaitables properly

ornate isle
#

@torn sphinx show your code. are you awaiting it properly?

#

if youre calling it in a method, the methods need to be async as well and be awaited

#

@rustic yarrow if your data is very structured and would be using relations (table of users, each user owns books from the book table, each book has an author from the author table, etc) then mongo is not good

#

in general mongo is easier to start with but then a mistake in the long run, except in pretty specific use cases where the data is not handled via schema very well and the read/write patterns are such that mongo is better with

rustic yarrow
#

So mongo is a mistake in long run because it becomes too difficult to manage relations?

ornate isle
#

mongo is bad with relations in general in comparison

#

SQL is geared for it

#

mongo is happier when your data is all nested and doesnt have to relate to other things. it can do it but it is not great

#

beyond that, there are just larger issues that result out of having no schema

#

maintainability etc. mongo is much more difficult to manage as you grow

rustic yarrow
#

I see that makes sense

torn sphinx
#

Couldnt post the whole code becasue character restrctions and felt it was wrong to take it out of context

ornate isle
#

you can use a pastebin or something next time but im looking at this. which methods are failing

torn sphinx
#
Exception has occurred: TypeError
An asyncio.Future, a coroutine or an awaitable is required
  File "C:\Program Files\Python36\Lib\asyncio\tasks.py", line 526, in ensure_future
    raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
  File "C:\Program Files\Python36\Lib\asyncio\base_events.py", line 452, in run_until_complete
    future = tasks.ensure_future(future, loop=self)
  File "C:\Users\Ryan\Desktop\WebDev\Python Projects\Misc\Bots\DiscordRpgBot\rpg_bot.py", line 128, in <module>
    loop.run_until_complete(RpgBot(BOT_TOKEN))
  File "C:\Program Files\Python36\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Program Files\Python36\Lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files\Python36\Lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
#

I forgot about pastebin lol

fringe tiger
#

Opening that file in IDE immediately spawns "Coroutine was not awaited" errors

ornate isle
#

haha

#

yeah @wild plank you are just giving asyncio a class

torn sphinx
#

lmao dem feels exactly

ornate isle
#

RpgBot() does not do anything on its own by instantiating it

#

oh yeah it does

#

but you still dont want to do it that way, also you dont want it to call a new event loop

#

you cant put async functions in init

#

__init__

#

put it in a method of the bot, and then await that method

fringe tiger
#

What IDE are you using? It should notify you of these errors

slate spire
#

you're trying to run the bot itself rather than calling run from it

#

you gave the RpgBot has a run method that runs the bot for you

ornate isle
#

@mighty dome his __init__ method has the db start doing stuff async

#

which isnt going to work

slate spire
#

it calls run_until_complete when initing it

#

and you wouldn't wrap the whole thing in run_until_complete either way

ornate isle
#

yeah

#

or call a new event loop

fringe tiger
#

Ye this file is just one big error 😄

ornate isle
#

haha yeah well i think this is a first attempt at async stuff and async scares people away

torn sphinx
#

Yeah I had

if __name__='__main__:
    bot = RpgBot(BOT_TOKEN)
     bot.run()

before I tried moving over to asyncpg, which I read needed asyncio

#

vscode @fringe tiger

#

lol it is my first time with async stuff

ornate isle
#

yeah once you go asyncio you have to make sure eveyrthing support it

#

dont sweat it, it's difficult in python

#

remove the call to new event loop, have the db stuff occur in a method, which you await()

fringe tiger
#

Aside from weird loop logic and few eyecandies, you're forgetting to call multiple await calls

ornate isle
#
@self.bot.event
async def on_member_join(member):
    await self.add_user_to_db(member)
#

notice the await

#

gotta do that on everything basically once you go async

torn sphinx
#

gotcha

fringe tiger
#

Yes, 5 of those. Basically for each call of async function you need to write await before the call.

ornate isle
#
async with self.db.cursor() as cursor:
    sql = """ INSERT INTO player_users (user_id, joined, xp_points) VALUES (%s, %s, %s)"""
    await cursor.execute(sql, (member.id, member.joined_at, 0))
#

etc

torn sphinx
#

Upon shutting down the bot, I still get this error RuntimeWarning: coroutine 'connect_database' was never awaited

fringe tiger
#

No need for that, just await what is needed... but good work I guess

#

Idk why your IDE didn't warn you but for python dev I would recommend PyCharm

torn sphinx
#

Yeah Im using vscode, not an IDE

#

That might explain it

#

Im running it through pycharm now

ornate isle
#

meh

#

i write 90% async and never touch pycharm so its not a must

fringe tiger
#

Welp if he had used it he would have saved himself a lot of time :P

ornate isle
#

i dont think so. this is his first async project and he didnt now he had to 'await' stuff at all. he knows the line says something hasnt been awaited, he posted it

#

just didnt know what to do about it

fringe tiger
#

Ah oki then. Idk I know the problem only from the last few msgs, I didn't go that far up.

ornate isle
#

@torn sphinx same issue -- the db function isnt being awaited in the end. not really probematic, but theres a bigger potential issue -- you are connecting and disconnecting from the db every time you make a query to it. im not sure how frequently this bot will be doing stuff but that may or may not be the behavior you want

torn sphinx
#

That db will be integral to the bot, since the bots function is to gamify discord a bit

ornate isle
#

if you are fine with it doing it this way but just want to have it shut up with the asyncio errors, then put the connect_database function inside the run loop

#

yeah i just mean its slower to have it connect and disconnect on every single request

#

but either way youll want to remove self.db = connect_database() and put it somewhere inside the run loop

#

i'm not familiar with how the discord bot works but i assume it has some sort of startup and shutdown handlers, yeah?

#

assuming on_ready is on setup, then i'd put the db connect await in there

torn sphinx
#

Yes it does

ornate isle
#

and then the await disconnect on the shutdown one

#

self.db = await asyncpg.connect() in the startup, await self.db.close() in shutdown handler, as long as theyre all inside the run looop

torn sphinx
#

I ended up doing this!

    def prepare_client(self):
        @self.bot.event
        async def on_ready():
            self.db = connect_database()
            await self.bot.change_presence(activity=Game(name=" DiscordRPG"))
            await self.add_all_users_to_db()
            await print("[*] Connected to Discord as: " + self.bot.user.name)
            await self.db.close()
#

Since connect_database already has the asyncpg.connect function in it

ornate isle
#

yes that works

#

well

#

did you remove the await con.close() from connect_database

torn sphinx
#

Yes

#
async def connect_database():
    connection = await asyncpg.connect(host=DB_HOST,
                                       port=DB_PORT,
                                       user=DB_USER,
                                       password=DB_PASS,
                                       database=DB_NAME
                                       )
    print('[*] Connected to database: %s' % DB_NAME)
    return connection
ornate isle
#

boom. okay thats what i was wondering

torn sphinx
#

😁

ornate isle
#

perfect

torn sphinx
#

I really appreciate the help!
Now to refactor the sql and I should be able to write to the db!

#

But im definitely not getting those errors anymore!

#

I have a feeling Ill be back plenty x)

fringe tiger
#

But you still closed it at the end of on_ready?

torn sphinx
#

For now, yeah

#

But this way Ill remember it when I put together the shutdown handler

fringe tiger
#

I'm not sure what type of db you are using but what I have done is use a botvar (bot variable) for db connection.

Basically in your init just do
self.bot.connection = await connect_database()

#

Then you can acces it simply with self.bot.connection

torn sphinx
#

PostgreSQL 10

#

When I tried adding is at a bot variable, it goes back to giving me RuntimeWarning: coroutine 'connect_database' was never awaited

fringe tiger
#

Yes

#

Dude

#

If it's an async function

#

You need to await

torn sphinx
#

right, this is what I had. I may have misunderstood what you were suggesting

def __init__(self, token):
        self.bot = Bot(client)
        self.bot.db = await connect_database()
        self.token = token
def prepare_client(self):
        @self.bot.event
        async def on_ready():
            await self.bot.db
            await self.bot.change_presence(activity=Game(name=" DiscordRPG"))
            await self.add_all_users_to_db()
            await print("[*] Connected to Discord as: " + self.bot.user.name)
            await self.db.close()

Problem is I get an invalid syntax since init is not an async function

#

and according to dkh, making init an async function doesnt work

fringe tiger
#

Ah yes I'm on mobile, though you can use init but forgot. Still the suggestion stands, just declare it a botvar somewhere else. That way, in future, if you use cogs it'll be easy to pass commection around.

torn sphinx
#

Now that makes sense! 😁
I didnt think about that! Thank you!

dawn pulsar
#

Is there anyway I can do some "SELECT * FROM table WHERE column1 IN my_list"

pure scroll
#

@dawn pulsar what do you mean by that and which database is that?

dawn pulsar
#

MariaDB

#

So I have a list of people

#

And I only want to select data where the column (say name) is people in that list

pure scroll
chilly hamlet
#

I'm looking for recommendations for an ORM-ish library, which doesn't do the whole "keep track of loaded/added objects and then flush in one go" thing, but just has get/save/delete operations which translate directly to select/insert/update/delete queries. Searching the web didn't turn out much, but I don't really know how such a library would be called. Anyone know if such a thing exists?

terse stump
#

not sure what you are asking about tbh. are you look for something simpler then SQLAlchemy?

#

Pony maybe?

chilly hamlet
#

I didn't know about Pony, it's an interesting concept. but I don't think it's what I'm looking for

#

I think I just want something really simple, where the objects that represent DB rows and the stuff that loads and saves them keep no connections between each other, sort of keeping the entire thing as stateless as possible

#

even if that means that I loose some higher level features like lazy loading

#

Something that sits in the middle between SqlAlchemy's core and ORM parts. Like core, that works with record classes in its selects ands updates

pure scroll
#

I think with sqlalchmeny you can just skip using session and use connection abstraction directly, that might do what you need

#

but anyways why don't you need those caching optimizations?

quiet ermine
#

I am having issues trying to migrate a db with flask-migrate

#

Even if I put the sqlalchemy.url to the right connection, it still shows the same

#

(owen is my username; I set it to access postgres so it's essentially doing postgresql+psycopg2:/owen:1234@127.0.0.1:5432/owen instead of postgresql+psycopg2://DB_USER:1234@127.0.0.1:5432/DB)

pure scroll
#

your credentials are valid, it just says that there is no database called owen

#

you can have more than one database on your database server

#

the last part of url is the name of database

quiet ermine
#

Yep

#

@pure scroll All the configs are right but it it is just doing owen all the time?

chilly hamlet
#

reading further, on the subject, I think the thing I want to try living without is the "unit of work pattern"

#

As to why, it's mostly desire to experiment doing things in a different way.

pure scroll
#

okay, well I'd say unit of work is quite a nice pattern

#

you can just set your session to autocommit I guess and that would to the trick for you

prime prism
#

How should I go about DateTime in sqlite?

chilly hamlet
#

you can just set your session to autocommit I guess and that would to the trick for you

I just re-read the documentation on autocommit and, like its name suggests, that's about transaction management. I don't see any relation to the unit-of-work stuff. Am I missing something?

pure scroll
#

unit of work is abstraction of a transaction in your code. It accumulates all the changes that you need to do and then runs it in one short transaction on the actual database layer. In the case of sqlalchemy this is what they call session object.
if you set it to autocommit=True it won't accumulate any changes but would rather directly execute it on the database.

chilly hamlet
#

So, you're saying that with autocommit=True assigning a value to an object's field will fire off an immediate UPDATE to the DB? I'll give it a try

pure scroll
#

well, not sure here, in the wost case you can do blackmagic and override __setattr__ method of your db models and make it call commit/flush on your connection/session

#

but I really don't see much of reasons to go for active record over unit of work

chilly hamlet
#

seems like with autocommit=True you just need to call flush() manually. so it really has no connection with unit of work

#

active record means that the record classes map themselves to the DB, and I don't want that either

chilly hamlet
#

I think I managed to achieve what I wanted with SQLAlchemy, by using the declarative stuff, but without a session, just SQLAlchemy Core stuff, and converting between record instances and dict(-like) objects. Time to see how much of a mess that is

umbral needle
#

Hello!

#

I am writing an app that interfaces with a url to pull json data from it. I then parse through that data to find the matching dictionary value for a zip code. I know that I am interfacing directly with a data layer and that my data layers need to be completely separate, but I'm wondering if I should keep that module separate and call from it as necessary or if that wouldn't matter.

#

url = "https://public.opendatasoft.com/api/records/1.0/search/?dataset=us-zip-code-latitude-and-longitude&q=co&lang=en&facet=state&facet=timezone&facet=dst&refine.state=CO"

url = "https://public.opendatasoft.com/api/records/1.0/search/?dataset=us-zip-code-latitude-and-longitude&q=co&lang=en&rows=5000&facet=state&facet=timezone&facet=dst&refine.state=CO"

json_content = requests.get(url).json()
content = json.dumps(json_content)
content = json.loads(content)
content = content["records"]

def find_zip_code(passed_zip, data):
return next(filter(lambda r: r['fields']['zip'] == passed_zip, content), None)

#

I'm using the requests library as well as the json library

merry prism
#

@umbral needle ```py on top of your code

#

And ``` on the bottom

#

For code blocks in Discord chat, makes it so much easier to read

#
url = "https://public.opendatasoft.com/api/records/1.0/search/?dataset=us-zip-code-latitude-and-longitude&q=co&lang=en&facet=state&facet=timezone&facet=dst&refine.state=CO"
## url = "https://public.opendatasoft.com/api/records/1.0/search/?dataset=us-zip-code-latitude-and-longitude&q=co&lang=en&rows=5000&facet=state&facet=timezone&facet=dst&refine.state=CO"
json_content = requests.get(url).json()
content = json.dumps(json_content)
content = json.loads(content)
content = content["records"]

def find_zip_code(passed_zip, data):
        return next(filter(lambda r: r['fields']['zip'] == passed_zip, content), None)
quiet ermine
#

Database migrations with flask-migrate are being tempermental

umbral needle
#

thank you!

ripe stratus
#

Anybody here familar with pymssql and _mssql?

ionic pecan
#

Don't ask to ask

ripe stratus
#

I'm not asking to ask, I'm asking if somebody here and not AFK has experience with pymssql. but I'll ask anyway.

https://paste.pythondiscord.com/afoyolocux.py

Forgive the bad white space and indentation, hastebin screwed it up, but it's right in the file.

I can do all the things with _mysql (part of pymssql), insert, delete, and return COUNTS. However, when I try to do a simple query for certain row (or even all of them) I'm getting a None back.

wide osprey
#

Anyone formiliar with PostgreSQL and Python?

unborn sentinel
#

What issues are you having

wide osprey
#

Nothing, just would like to get some tips to prevent beginner mistakes. ^^

peak bison
#

Does anyone know if this guy

#

comes with something like inspectdb from django-orm or sqlalchemy automap

#

Because I've been reading yet I couldn't find anything on that

torn sphinx
#

Hey

hearty thorn
#

I have a question about sqlalchemy query objects... if I set up a query like ```
query=session.query(mappedClass).filter(mappedClass.field=='critera1')

is there a way to update the critera1 with criteria2, or should I just create a new query object for the new criteria2?

**Update: ** I think maybe I should just create the query by itself like `query=session.query(mappedClass)` and then when I need  to filter it and return results I'll  just call `query.filter(mappedClass.field=='criteria').one()` as needed but still curious if I could just update the filter criteria.
quiet ermine
#

Getting issues with migrations (flask-migrate) with postgresql

#

Basically: sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: database "owen" does not exist

#

All the URLs for sqlalchemy/flask-migrate are pointed to the right postgres are not set to owen

quiet ermine
#

Oh shoot I just used alembic and it fixed right away

inner lintel
#

i want to store ['apple', 'orange', 'bannana'] on redis

#

then get them like a list

#

not one by one

#

how ?

quiet ermine
#

.items()

#

@inner lintel

torn sphinx
#
Traceback (most recent call last):
  File "testdb.py", line 19, in <module>
    loop.run_until_complete(connect_database())
  File "/usr/lib/python3.5/asyncio/base_events.py", line 466, in run_until_complete
    return future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "testdb.py", line 13, in connect_database
    database=DB_NAME
  File "/home/turtle/.local/lib/python3.5/site-packages/asyncpg/pool.py", line 400, in _async__init__
    await self._initialize()
  File "/home/turtle/.local/lib/python3.5/site-packages/asyncpg/pool.py", line 417, in _initialize
    await first_ch.connect()
  File "/home/turtle/.local/lib/python3.5/site-packages/asyncpg/pool.py", line 125, in connect
    self._con = await self._pool._get_new_connection()
  File "/home/turtle/.local/lib/python3.5/site-packages/asyncpg/pool.py", line 463, in _get_new_connection
    **self._connect_kwargs)
#
  File "/home/turtle/.local/lib/python3.5/site-packages/asyncpg/connection.py", line 1688, in connect
    max_cacheable_statement_size=max_cacheable_statement_size)
  File "/home/turtle/.local/lib/python3.5/site-packages/asyncpg/connect_utils.py", line 551,
in _connect
    raise last_error
  File "/home/turtle/.local/lib/python3.5/site-packages/asyncpg/connect_utils.py", line 543,
in _connect
    connection_class=connection_class)
  File "/home/turtle/.local/lib/python3.5/site-packages/asyncpg/connect_utils.py", line 513,
in _connect_addr
    connector, timeout=timeout, loop=loop)
  File "/usr/lib/python3.5/asyncio/tasks.py", line 400, in wait_for
    return fut.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/usr/lib/python3.5/asyncio/base_events.py", line 731, in create_connection
    infos = f1.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/lib/python3.5/socket.py", line 733, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
#
import asyncio
import asyncpg
from settings import *


loop = asyncio.get_event_loop()

async def connect_database():
    conn = await asyncpg.create_pool(host=DB_HOST,
                                    port=DB_PORT,
                                    user=DB_USER,
                                    password=DB_PASS,
                                    database=DB_NAME
                                    )
    print('[*] Connected to database: $1', DB_NAME)
    

loop.run_until_complete(connect_database())
#

Not sure if the error is from my connect_database() function or from using PostgreSQL in a docker container

#

I have verified that the creds passed into create_pool() are correct

gleaming basin
#

whats the best and easiest databases?

#

that avoids downloads if possible

serene thicket
#

depends on your use case @gleaming basin

gleaming basin
#

currency

serene thicket
#

you can get MSSQL Server Express for free, and will handle most basic needs, but i know many dont like MSSQL

#

you can also get a postgress DB for free

gleaming basin
#

MongoDB seemed to be fine, but I cannot do stuff with it

serene thicket
#

i personally, have always been a MSSQL person, so id recommend it, especially if what you are doing isnt anything big

gleaming basin
#

it says that my bot is connected

serene thicket
#

i dont have much experience with Mongo unfortunately

gleaming basin
#

but their documentations do not help at all

serene thicket
#

i would recommend posting your code here (with any pw/auth tokens removed) with a question outlining your exact issue

#

someone will be able to better help you that way 😃

gleaming basin
#

ok, thanks!

serene thicket
#

Hoping someone might be able to help me out here.
https://mystb.in/atagosucok.py

I have a script that generates a set of data from an API call and then inserts it directly into a MSSQL Server database.

The issue i am having is that my insert query, which is meant to handle deduping, doesn't seem to work. The WHEN MATCHED statement seems to always resolve to false, resulting in all my data being reinserted instead of either updated or only inserting new records.

What i think is the issue is that my ``ON (target.company_id = '[{company_id}]') isn't working.

My guess is based off what I'm seeing in the print, that the target table isn't getting defined.

Any one have any ideas what's going on? or what I'm missing here.

#

for reference the insert query is on lines 100 - 118

serene thicket
#

nvm, disregard, i have realised the utter stupidity that is what i was doing..... didnt need the [] in the variables for sql statement

inner lintel
#

how to sort redis dat from hiegher to lower

#

i used db.get

#

multibe time for diffrent queries

#

and results is

#

5

#

2

#

1

#

7

#

i want em to be
7
5
2
1

#

how ?

inner lintel
#

^^

quiet ermine
#

You can reverse it

#

something = db.get().items()[::-1]

#

Or something like db.get().order().desc().items()

#

¯_(ツ)_/¯

#

@inner lintel

inner lintel
#

not one db.get

#

i used like 5 times

#

and everyone returns a value

dawn pulsar
#

Any known vulnerabilities for Maria DBs with select access and sanitized inputs?
Please ping

#

Or how would you do a mysql.connector injection attack, surely that wouldn't give me more access? If I have select only access, then even commenting won't help right?>

ionic pecan
#

nothing we can help you with

harsh osprey
#

Hey. Whenever I try to use json.dump(...) in the file it will start a new line.
Example:

{"Line": 1}

*After dumping *

{"Line": 1}
{"Line": 1, "page": 2}

Any reason why and/or how to stop it?

#

My current workaround is just deleting the file, making a new blank one, then using .dump(...) After

ornate isle
#

@harsh osprey you haven't specified what you're expecting it to be instead--a merge of those 2 dicts as one? the 2 dicts but on one line? if you're expecting them to be merged, then this is by design. you are opening the file i am gussing in append mode, rather than write (w) mode. if you want to always have the file be the combined data (in one json object, not on separate lines) then you need to read the json data from the file, update it, and write all of it back out again, in write mode, not append.

hazy mango
#

@harsh osprey open the file in w mode not a

harsh osprey
#

Ah that's why

subtle flax
#

I have the following postgres insert code in my application: ```python
for name in to_create:
new_role = await create_role(name=name)

await conn.fetch(
"INSERT INTO color_roles (guild_id, role_id) VALUES ($1, $2)",
guild.id, new_role.id
)``` Could I do 1 INSERT instead of N somehow?

pure scroll
#

what kind of postgres python driver is that?

subtle flax
#

asyncpg

subtle flax
#

Thanks

hoary wren
#
@bot.command()
async def quest(ctx):
    daily_quest = ['Roll a dice.', 'Send 4 messages', 'Use a bot command']
    quest = daily_quest[random.randrange(len(daily_quest))]
    with mydb.cursor() as cursor:
        sql = "INSERT INTO discord(userid,quest,points) " \
        "VALUES(%s,%s,%d)"
        cursor.execute(sql, (ctx.author.id, quest, 1))
    mydb.commit()
    print("Added user %s to database." % ctx.author.id)
#

I want to store user data in my databse but it gives me this error AttributeError: enter

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

ornate isle
#

@hoary wren something is wrong with your context manager (the with statement). doesn't look like you're supposed to be using it with db.cursor()

torn sphinx
#

someone good with sqlalchemy?
i want to select from db only rows with certan value..
reading documentation right now cant find it..

#

data = db.Table('MATCH_OP', metadata, autoload=True, autoload_with=engine)
query = db.select([data] )
ResultProxy = conn.execute(query)
ResultSet = ResultProxy.fetchall()
#

i got this far..

ionic pecan
#

add .where to the end of select

#

with the condition you want

torn sphinx
#

thank you mate

torn sphinx
#

what DB is easiest to use for discord bots?

ionic pecan
#

Postgresql

ornate isle
#

haha, no, postgres is not the easiest database. it is arguably among the best though

#

@torn sphinx if you dont know much about databases, i guess ask yourself how important the data is. if you want to use something really easy, you can use sqlite and something like dataset: https://dataset.readthedocs.io

#

with dataset, you essentially write to the database like it's just a python dict. if the sql database doesn't have the table or fields you are trying to write to, it invisibly just creates them.

#

if you are okay with it being filesystem-based, there are things like TinyDB, that write to json files but let you still query them like a 'real' database: https://github.com/msiemens/tinydb

#

would i use any of these in some mission-critical application with invaluable data? no. i'd use postgres (and replicas, and backups, and other things.) but if you want easy, there you go

torn sphinx
#

How can I store a datetime object in Postgresql?

ornate isle
#

@torn sphinx in a timestamptz field. and if using sqlalchemy this would be as TIMESTAMP(True). (you almost certainly want to store the time zone with it, so make sure you're also using datetimes properly)

torn sphinx
#

I'm sorry, I don't follow

ornate isle
#

@torn sphinx are you writing raw sql or using sqlalchemy or something? are you asking what field type to use in the postgres schema? basically most people default to just using postgres' timestamp field type, but this does not store time zone information, i.e. it can be a total mess if your app, say, let users get push notifications at certain times and you weren't storing the time zone data in the database. when do you send it?

#

so i'm saying you should most likely use postgres' timestamp with time zone field type in your db schema (also known as timestamp tz), which translates to the TIMESTAMP(True) field if you are using sqlalchemy. but that this also means you should store the timezone when you save the datetime object. datetime in python also doesn't hold time zone info by default. so how to handle that kinda depends on how you're writing to the database

#

a lot of people (myself included) either use pytz to add proper time zone info to datetime objects, or other time libraries entirely, sucha as pendulum. (i default to pendulum over datetime pretty much always, not just for db stuff)

#

sorry that it is probably more complicated than you thought haha. lots of folks just dont see this one coming until they realize all their time data has been wrong for a long time, or when python throws errors if you try to compare timestamps and it doesn't know the time zones

torn sphinx
#

When I create a table do I create a TIMESTAMP like this: c.execute("""CREATE TABLE IF NOT EXISTS Users( UserID BIGSERIAL, Xp INTEGER, Sparks INTEGER, Level INTEGER, Premuim BOOLEAN, LastLogin TIMESTAMP)""")

#

@ornate isle?

ornate isle
#

@torn sphinx i gave you the answer... i'd use TIMESTAMP WITH TIME ZONE instead of just TIMESTAMP, and you should try to understand why this is so you dont run into trouble later

torn sphinx
#

I read what you said

ornate isle
#

but it's going to yell at you if you then try to just insert a datetime object without a timezone

torn sphinx
#

How can I pass a datetime object with a timezone?

#

Idk pytz

ornate isle
#

if whatever you are building isnt going to be dealing with people scheduling things in different time zones and such, what you are doing is ok. but insert datetime.utcnow() into the db not datetime.now()

#

if you do need it to not break if people are using the thing you are building across time zones and where that accuracy matters, then you will need to become familiar with these things

torn sphinx
#

It's for a discord bot

#

Strftime

#

I use that for UTC times

ionic pecan
#

@ornate isle timestamptz does not store the timezone

#

and dataset is a library, not a database - if you use an abstraction like dataset or sqla it really does not matter which database you‘re using apart from setup

pulsar quarry
#

How do I find the CK and FK for the Guest table? So far I have CK = guestNo and guestName PK = guestNo FK = guestNo.

#

Just not sure if its correct.

copper echo
#
sql execute ALTER TABLE public.stars 
ALTER COLUMN author_id TYPE BIGINT,
ALTER COLUMN guild_id TYPE BIGINT,
ALTER COLUMN message_id TYPE BIGINT,
ALTER COLUMN bot_message_id TYPE BIGINT,
ALTER COLUMN channel_id TYPE BIGINT,
ALTER COLUMN starboard TYPE BIGINT
``` Why when i run my command to change type of column it gives me error
#
<class 'asyncpg.exceptions.DatatypeMismatchError'> - column "author_id" cannot be cast automatically to type bigint
HINT:  You might need to specify "USING author_id::bigint".
ionic pecan
#

which type does it currently have

#

@copper echo are you using int or string currently?

copper echo
#

nvm i added USING (author_id::bigint) and it worked

pulsar quarry
#

@ionic pecan Hi, can you help with the question above? appreciate it

ionic pecan
#

i don't think you can use name or address as a candidate / primary / foreign key

#

multiple people can live at one address, multiple people can have the same name

pulsar quarry
#

@ionic pecan So for guest table, there would be no CK, PK?

ionic pecan
#

why not the guest number?

ornate isle
#

@ionic pecan timestamp with time zone stores the timestamp in UTC, accounts for the offset if the timestamp you give it specifies it is from a specific time zone, and then gives it back to you in the proper offset when requested, so while you are correct in that it does not store "America/Los_Angeles" in the field, it does do this conversion for the user and for as long as i can remember has been the recommended approach to storing timestamps for most situations

ionic pecan
#

the offset is the database time zone though

ornate isle
#

and like most others, seems to trail off without everyone in full agreement on anything except you should probably use timestamp with time zone and still use UTC for everything if you can

#

in this case it makes sense for a lib like ecto to default to using timestamptz because this can result in assumptions that it shouldn't make

#

but ive always felt for sanity it is best as a developer to be explicit about it, especially with python's history of tz-naive datetimes and more importantly perhaps all the people and app that use(d) them not fully properly understanding this

gleaming basin
#

Hello

distant epoch
#

Hi
Is it possible to insert stuff with sqlite in a variable table?
this inserts variables:
c.execute('''INSERT INTO table (id, name, value) VALUES(?,?,?)''', (id_var, name_var, value_var))
but I need to insert in a table with a variable name, sadly this doesn't work:
c.execute('''INSERT INTO ? (id, name, value) VALUES(?,?,?)''', (table_name, id_var, name_var, value_var))
any ideas?

polar osprey
#

Boring Background Information
Hey guys. I am responsible for something that could be called a small warehouse for 1 month every year. Each year we would go and collect more or less the same material. (about 4 Trucks full) and then work with that stuff for 2 weeks and then give it back.
During these 4 weeks I'm responsible for all the stuff. Up until now I've been working with an Excel table but it's annoying and I want to make something better with Python and a MySQL database. (Python for the interface and to create PDF reports). I'm currently thinking about how to best set everything up and thought maybe one of you guys could help me out.

Actual Question
The main problem I face is that I have material (let's take a certain kind of brush for example and has an ID:5886) that belongs into 3 different kind of boxes. Box A has it two times. Box B and box C have it once. On top of that, Box C is also in a bigger Box Z.

How would I best go about organizing my Data to accommodate this?

I know of two ways:
a.) Have a table Material and a second table Material in Boxes. The second table would then specify what is in which box and underbox and what not. The problem I see with this if I want to generate a list with everything that is in Box Z I would have to run multiple querries (afaik).

b.) I read an article about the Adjacency List Model. The problem I see with this method is that one item can only have one parent. Which would work in my case.

Any advice?

pure scroll
#

as far as I know classic solution for this is to have table
product which would just say hey i'm a brush of that model, of that vendor
then you would have another table for so called stock keeping units where items of that table are actual physical items.
Means every brush of the same type would have a different record in this table
and then you would reference where this item is actually placed within the warehouse.
so if this is what your materia in boxes table about then you are doing it right

#

as for the second part of multiple queries this is actually not an issue as long as those queries are running fast. In fact there are cases when running multiple queries would work faster than doing that within 1 query. But nevertheless you could always use join statements to return such things in 1 database request.

distant epoch
pure scroll
#

so, afaik connection.execute is creating so called prepared statements, the advantage of prepared statements on DB is that query can be validated before parameters are inserted to it. so if you do multiple inserts with the prepared statement it will save execution time on your database.
now having that said, you can guess that in order to validate prepared statement you need to know which table it belongs to, means you can't pass table name as a parameter here.

#

what you can do is you can pre-format your query with the table name first, by using python string formatting. Please also keep in mind that if table name is a user input thing, and not just a variable that is hardcoded somewhere (in other words table name string cannot be considered as trusted) you will also need to escape it first, in order to avoid possible sql injection attacks.

distant epoch
#

ah I see that makes sense thanks a lot

#

and yes it is an user input, I will try to make something to filter it

carmine heart
#

Unless the user can also create tables dynamically, you have a predetermined list set of possible values.

distant epoch
#

Exactly

knotty parcel
#
gnoring exception in message
Traceback (most recent call last):
  File "/home/runner/.local/lib/python3.6/site-packages/discord/client.py", line 251, in _run_event
    await coro(*args, **kwargs)
  File "/home/runner/cogs/levels.py", line 19, in on_message
    if resultc[0] == 0:
TypeError: 'NoneType' object is not subscriptable
#

@carmine heart

indigo mason
#

resultc is None apparently

knotty parcel
#

@indigo mason

#

It is

#

As that server never changed anything

#

@ionic pecan

ionic pecan
#

sup

indigo mason
#

@knotty parcel So what's your issue?

#

You can't do

#

None[123]

knotty parcel
#

I already did a querie

#

@ionic pecan

dbc= sqlite3.connect('main.sqlite')
      cursor= dbc.cursor()
      cursor.execute(f"SELECT leveling FROM main WHERE guild_id = {message.author.guild.id}")
      resultc = cursor.fetchone()
      if resultc[0] == 0:
        cursor.close()
        dbc.close()
        return
      elif resultc[0] is None:
        cursor.close()
        dbc.close()
ionic pecan
#

i have no idea why you're pinging me

knotty parcel
#

It doesn't work

#

And because I saw you helping people up chat I thought you would now how to fix it

indigo mason
#

Please don't ping random people

ionic pecan
#

!ask

delicate fieldBOT
#
ask

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.
• 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
• Keep your patience while we're helping you.

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

indigo mason
#

You are not owed help

knotty parcel
#
Ignoring exception in message
Traceback (most recent call last):
  File "/home/runner/.local/lib/python3.6/site-packages/discord/client.py", line 251, in _run_event
    await coro(*args, **kwargs)
  File "/home/runner/cogs/levels.py", line 19, in on_message
    if resultc[0] == 0:
TypeError: 'NoneType' object is not subscriptable
indigo mason
#

resultc[0]

#

resultc is None

#

I'm not sure what you want us to say

ancient bough
#

Anyone know how to fix this, it's for my Discord bot

So I'm trying to create a database, but I'm getting errors...

Code -

@bot.event
async def on_ready():
    db = sqlite3.connect('main.sqlite')
    cursor = db.cursor()
    cursor.execute('''
        CREATE TABLE IF TABLE NOT EXIST main(
        guild_id TEXT,
        msg TEXT,
        channel_id TEXT
        )
        ''')

Error -

    ''')
sqlite3.OperationalError: near "TABLE": syntax error```
slate spire
#

IF NOT EXISTS, not IF TABLE NOT EXIST

ancient bough
#

Tysm

slate spire
#

no probs

torn sphinx
#

anyone can tell me why employee_id is returning sqlite3.IntegrityError: NOT NULL constraint failed: Client.employee_id

tawny sail
#

Read the error

#

It says the condition for not null failed

#

I guess u forgot to pass a value for it or maybe dint set it for auto incriment

#

@torn sphinx

ancient bough
#
class WelcomeCog(commands.Cog, name='Welcome'):

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

    @commands.Cog.listener()
    async def on_member_join(member):
        print(f'{member} Has just joined the server.')
        embed = discord.Embed(colour=0xdf0d22, description=f"Welcome to the server! You are the {len(list(member.guild.members))} member!")
        embed.set_thumbnail(url=f"{member.avatar_url}")
        embed.set_author(name=f"{member.name}", icon_url=f'{member.avatar_url}')
        embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
        embed.timestamp = datetime.datetime.utcnow()

        channel = member.guild.get_channel(id=576735952827973654)

        await channel.send(embed=embed)

    @commands.group(invoke_without_command=True)
    async def welcome(self, ctx):
        await ctx.send('Available Setup Commands: \nwelcome channel <#channel> \nwelcome text <message>')

    @welcome.command()
    async def channel(self, ctx, channel:discord.TextChannel):
        if ctx.message.author.guild_permissions.manage_messages:
            db = sqlite3.connect('main.sqlite')
            cursor = db.cursor()
            cursor.execute(f'SELECT channel_id FROM main WHERE guild_id = {ctx.guild.id}')
            result = cursor.fetchone()
            if result is None:
                sql = ("INSERT INFO main(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 main SET channel_id = ? WHERE guild_id = ?")
                val = (channel.id, ctx.guild.id)
                await ctx.send(f'Channel has been updated to {channel.mention}')
                cursor.execute(sql, val)
                db.commit()
                cursor.close()
                db.close
#

I'm trying to make a data base so then i'm able to set a welcome channel, now, I'm trying to make it set the Channel ID and the Guild ID into the database

#

But it's not setting anything

quasi storm
#

What module should I use for asynchronous postgres, im looking at aiopg but im open to other suggestions

torn sphinx
#

@tawny sail : Yea i forget to pass the id, haha.

tawny sail
#

@quasi storm aio sqlalchemy

bleak pecan
#
    async def _update(self, usr, gem):
        connection = await self.bot.db.acquire()
        async with connection.transaction():
            query = "UPDATE contenders SET gems = (gems + $1), pool = (pool - ABS($1)) WHERE id = $2"
            await self.bot.db.execute(query, gem, usr.id)
        await self.bot.db.release(connection)

    async def _pool(self, usr, gem):
        connection = await self.bot.db.acquire()
        async with connection.transaction():
            query = "UPDATE contenders SET gems = (gems - $1), pool = (pool + $1) WHERE id = $2"
            await self.bot.db.execute(query, gem, usr.id)
        await self.bot.db.release(connection)

await self._pool(usr,  gems)
await self._update(winner, gems)
await self._update(loser, -gems)```

any way those 2 methods be reduced to just one... without another arg
pure scroll
#

you mean two update methods?

torn sphinx
#

Hello everyone. I have a problem with a .sql file. python can't recognize input

#

any idea ?

bleak pecan
#

I mean merge _pool & _update with something in the query,
but theres probs no way without another arg, unless there's some math tweak...

pure scroll
#

you can do abs on python side

#

that is your math trick 😄

pure scroll
#

ah okay, I see it has + vs -

#

so I guess if you do + - abs(value) it's the same as - abs(value)and calculation of-abs(value)` you can have on python side

#

but to be honest it feels wrong

#

those are two different actions, and there is no reason to re-use the code

#

the code could and should be re used only if two things are repenting same logical action, if not then you should not do it, having copy-paste in this case is not a crime

bleak pecan
#

just felt wrong as they're so similar, I think i'll just add an optional arg, yea the SQL abs works there... that saved me a check, cant think of away without another flag/arg/marker so i'll have to go with that

lapis zenith
#

Im trying to make a script to search through multiple large text files for a string

#

but I keep getting this error

#

What is going wrong here?

#

I tested it with 2 small files (<10k lines each) and it was working fine

slim echo
#

Have you googled the error?

#

UnicodeDecodeError happens when there is an invalid character in the file

lapis zenith
#

hmm ok, I just downloaded some random database to test my program, i guess some of the emails have weird chars in them?

slim echo
#

I guess that's not out of the question

#

So my guess will be to read the file as bytes

#

So open("file", "rb")

lapis zenith
#

oh, ok. Im getting another error now tho cause idk how to work with bytes

#

pretty new to this, only finished 1 year of compsci :/

bleak pecan
#

state an encoding in open may do. utf-8 probs best bet

#

if not latin-1

lapis zenith
#

^ that worked, thanks!

torn sphinx
#

Hey guys

#

i need a help with

#

Article.objects.filter(tags='Destaques')

#

this return me a error like:
ValueError: invalid literal for int() with base 10: 'Destaques'

#

when i make a:

>>> Article.objects.all()[0].tags
<django.db.models.fields.related_descriptors.create_forward_many_to_many_manager.<locals>.ManyRelatedManager object at 0x7f2ed4f329b0>
carmine heart
#

I think Django expect the id of the Tags, here. In which field within your Tags should that 'Destaques' be?

torn sphinx
#
>>> t = Article.objects.all()[0].tags.values()
>>> t
<QuerySet [{'id': 3, 'name': 'Destaques', 'created': datetime.datetime(2018, 10, 22, 11, 23, 38, 193246)}, {'id': 2, 'name': 'Notícias', 'created': datetime.datetime(2018, 10, 22, 11, 16, 2, 842396)}]>
#

i'm making like this

#
>>> t.filter(name='Destaques')
<QuerySet [{'id': 3, 'name': 'Destaques', 'created': datetime.datetime(2018, 10, 22, 11, 23, 38, 193246)}]>

#

but i can't use delete method

#

to delet this tag

carmine heart
#

Okay, when you use filter like that on your Article objects, tags refers to the entire object, so filter will work by default on the id field

#

If you want to filter tags based on the name field, you need to specify that

#
Article.objects.filter(tags__name='Destaques')
#

should work in your case, I think

torn sphinx
#

oh

#

let me try

#

@carmine heart : and if i want to delete this tag?

#

actually

#

i don't want delete the object

#

just the tag

#
class Tag(models.Model):
    created = models.DateTimeField(
        auto_now_add=True)
    name = models.TextField(unique=True)



class Article(models.Model):
    created = models.DateTimeField(
        auto_now_add=True)
    publish_date = models.DateTimeField(
        default=timezone.now)
    updated = models.DateTimeField(
        default=timezone.now)
    image = models.BooleanField(
        default=False)
    image_updated = models.DateTimeField(
        default=timezone.now)
    show_image = models.BooleanField(
        default=False
    )
    owner = models.ForeignKey(
        'auth.User', 
        related_name='articles', 
        on_delete=models.CASCADE)
    title = models.TextField()
    subtitle = models.TextField(
        blank=True)
    tags = models.ManyToManyField(Tag)
#

this is they relationship

#

tag is a many to many field

carmine heart
#

I'm not too sure, I haven't used Django that much myself

torn sphinx
#

yea

#

bt

#

the whey you said it's a good whey

#

i found one thing

#
for article in Article.objects.filter(tags__name='Destaques'):
    article.tags.remove()
#

but help(article.tags.remove) dont have documentation

#

😢

#

yeaaa

#

i got it

#

@carmine heart : you're a god.

#

thank you

#
>>> for article in Article.objects.filter(tags__name='Destaques'):
...     article.tags.filter(name='Destaques').delete()
#

just saving here.

pure scroll
#

that is not really efficient way. since you would make one delete statement per article

#

Tags.filter(name="Destaques").delete() would have better performance

torn sphinx
#

@pure scroll : This look the same thing

#

But tags is a relationship between tag and article

#

are two different things

pure scroll
#

you can query Tag table directly

#

ah okay, it's m2m

#

I see now

torn sphinx
#

Not actually. Because tag is indepently from article.
Tag exist even when a article dont.

pure scroll
#

sorry, forget what I just said

torn sphinx
#

but you remind me about performance. That's actually a serious thing i have to think about

#

take about 2 minutes to delete all the destaques tags

pure scroll
#

well, that is the bad part of orms, they hide such things

#

I'm not sure how would you access this relation table using django orm

#

but you could do that with 1 delete statement in pure sql

#

like

delete from <tags_to_articles>
using tags
where tags.id = tags_to_article.tag_id
and tags.name = "destaques"
torn sphinx
#

this work in django? @pure scroll

pure scroll
#

you need to lookup for how to execute raw statements in django

#

also you need to know the table name that stores this m2m relation

#

probably there is a way to do that using ORM, but I never had to do that in django before. maybe someone else could come up with a different way, not a raw sql

pliant oxide
#

Hi, I got a question about collecting data (via Python and Scrapy and SQLAlchemy, but it could doesn't matter) and inster it to database.
I got Items defined like this

class Country(scrapy.Item):
    code = scrapy.Field() # PL, IT
    name = scrapy.Field() # Polska, Włochy

class City(scrapy.Item):
    country_id = scrapy.Field() # code of Country
    code = scrapy.Field() # POZ, ROM
    name = scrapy.Field() # Poznań, Rzym

class Airport(scrapy.Item):
    city_code = scrapy.Field() # code of City
    code = scrapy.Field() # POZ, CIA
    name = scrapy.Field() # Ławica, Ciampino

class Airline(scrapy.Item):
    code = scrapy.Field() # FR
    name = scrapy.Field() # Ryanair

class Flight(scrapy.Item):
    from_dest = scrapy.Field() # code of Airport
    to_dest = scrapy.Field() # code of Airport
    airline_code = scrapy.Field() # code of Airline
    flight_time = scrapy.Field() # 1:25
    price = scrapy.Field() # 979.32
    date = scrapy.Field() # /Date(1557124200000)/

class Offer(scrapy.Item):
    outbound_flight = scrapy.Field()
    inbound_flight = scrapy.Field()

And I got response something like this:

"AirportsNames": [
    {
        "Key": "POZ",
        "Type": "Airport",
        "Value": "Ławica"
    },
    {
        "Key": "BGY",
        "Type": "Airport",
        "Value": "Bergamo Orio al Serio"
    }
],
"AirportsCityCodes": [
    {
        "Key": "POZ",
        "Value": "POZ"
    },
    {
        "Key": "BGY",
        "Value": "MIL"
    }
],
"AirportsCountryCodes": [
    {
        "Key": "POZ",
        "Value": "PL"
    },
    {
        "Key": "BGY",
        "Value": "IT"
    }
],
"CityNames": [
    {
        "Key": "POZ",
        "Type": null,
        "Value": "Poznań"
    },
    {
        "Key": "MIL",
        "Type": null,
        "Value": "Mediolan"
    }
],
"CountryNames": [
    {
        "Key": "PL",
        "Value": "Polska"
    },
    {
        "Key": "IT",
        "Value": "Włochy"
    }
],
#

My script which is getting that reponses async (there are much more responses like that) creates Items from this response. I want to insert this to database, I know how to do it in a simple way, but I don't know how to add relation, for example City record from Cities table to Country record from Countries table.
I know I can do something like "Get ID of country in countries table by CityToCountry code and then assign it to {country_id, cityname, citycode}", but what if this country record not exists in table yet (because it's async)?
I think I can use MySQL database triggers, so I will send {airportName, airportCode, cityName, cityCode, countryName, countryCode} and then this triggers will insert in proper tables data.

#

`
In a simple question: Is it a good way to do it with a trigger when I don't know how to do it in a code (maybe it's impossible)?

unkempt sable
#

!warn 442005563451244544 Do not post copypasta in on-topic channels

delicate fieldBOT
#

:incoming_envelope: :ok_hand: warned @pure basin (Do not post copypasta in on-topic channels).

indigo mason
#

He did it in D.py too :-/

brave lotus
#

sorry if this is a really stupid question, i'm very new at this! so lets say this is one of my tables its row values:

food(name, emoji, rarity)

and here's some entries:

(pizza, 🍕, common)
(apple, 🍎, common)
(taco, 🌮, uncommon)
...and like 47 more

now i want to make a table that keeps track per user of how much they got of each food. so my table and row values will be this:

food_user(user_id, name, count)

now if i wanna store that for each person i'd have to do something like 50 times this for each person:

(1, pizza, 6)
(1, apple, 73)
...48 more

is there any way i'm overlooking that would not require 50 seperate entries just for one user?

limpid hound
#

@brave lotus it all depends on what you're trying to adapt the code for.
To an extent, you can choose to add columns per type of item.
I have seen horrible examples of this where they had thousands (!) of columns

#

but if you expect to see a large amount of different types of items, I'd go with a design closer to this:
[ user ] - [ item ] - [amount ]

#

having 50 columns just for the different types of items sounds like a design I'd stay away from (it's not flexible in the long run)

#

unless you're expecting to do really heavy analysis of your users item counts, I'd have a hard time motivating a wide table

brave lotus
#

what exactly do you mean with the design you wrote? in what place would it be?

limpid hound
#

ah, my bad, I misread your question to begin with!
Either you write a insert for every row (with the current table-design that you described) which would be a row for every item and person (I'd skip any row that's currently set to 0 items) or you're going to have to write some code that will generate the necessary rows.

Not knowing the amount of user_id's you're planning on entering data for, the amount of time saved by writing code to do it for you is uncertain 😃

#

if you absolutely don't want one row per item per user_ID, you would have to build a "wider" table with many columns (one column per item-type)

pure scroll
#

is there any way i'm overlooking that would not require 50 seperate entries just for one user?
what is the issue with having that?

#

that is classic m2m relation you have

limpid hound
#

you could also have a column storing JSON or XML if you fancy that, but I'd recommend going for one row per item-type+user_id

brave lotus
#

ah yes exactly, i asked because in both a narrow and wide way it seems like it would not scale well when the amount of items goes up even more

limpid hound
#

@brave lotus one way of keeping the amount of rows "low" would be to not store any 0-values

#

but it depends on the application you're writing, what's expected out of it and how you want to work with it

#

if you're worried about the table-performance, I would make some kind of calculation on what the worst-case scenario would be in 1~5 years, filling the table with data that corresponds to what you're expecting in your worst case scenario and see how it behaves

brave lotus
#

basically to me it seemed like a problem that you're creating so much entries for just one person, but if it isn't then i'll just take that and go on with it. i'm just new to working with databases

#

ah yeah that's a good point, i'll keep that in mind

limpid hound
#

a good practice (that I don't see often enough) is to expect some kind of worst case scenario and see how the application works under those

pure scroll
#

which DB is that btw?

brave lotus
#

thanks so much for the help! means i can continue with my project haha

torn sphinx
#

hmm.. how would I go about escaping quotes in this SQL statement? sql print("INSERT INTO records (ipaddr, port22, tcpbanner) VALUES('{}', TRUE, \"{}\");".format(y["ip"], """{}""".format(str(shlex.quote(banner)))))

#

I thought shlex.quote would work, but an sql error is returned

#

(im using aiomysql)

#

it doesnt come with prepared statement support apparently so...

tawny sail
#

Use %s

torn sphinx
#

I'll try lol

pure scroll
#

@torn sphinx why do you need to escape it in the first place?

torn sphinx
#

because sometimes tcpbanner has characters like ' and it breaks the query

pure scroll
#

you have execute method on database adapters

#

that is able to do escaping correctly

valid ruin
#

i use sqlite3 library but am not very good with it

#

i am trying to store user data, i thought about using csv but thats too simple

#

i need something more... capable

carmine heart
#

capable of what exactly?

#

I mean, there's a lot you can do with text files

harsh pulsar
#

@valid ruin you can try an ORM like Sqlalchemy, peewee, or pony

#

But i strongly recommend learning how to use a "low level" sql library like the sqlite3 one in python

#

ORMs tend to be "big" libraries

#

Basic sql libraries like sqlite3 arent that hard to use and are all more or less the same as each other

valid ruin
#

i just found out about the pickle library

harsh pulsar
#

Definitely don't do that

valid ruin
#

i just want to save variables permanently

harsh pulsar
#

Hm

valid ruin
#

hm

harsh pulsar
#

I cant say I recommend it

#

What is your hangup with sqlite

valid ruin
#

i just dont know it that well, ik basics in sql but not how to use it in python

#

python 3 to be specific

#

im going to give pickle a try, but if not I'll be back >:(

#

lol

harsh pulsar
#

if it breaks, you get to keep both halves lol

#

python actually defines a specification for sql libraries, its relatively standardized

#
import sqlite3

connection = sqlite3.connect('appdata.db')

def read_app_parameter(parameter_name):
    cursor = connection.cursor()
    cursor.execute('select value from app_parameters where name = ?', (parameter_name))
    return cursor.fetchone()[0]

something like that

void otter
#

Sqlalchemy works great with sqlite and postgresql

torn sphinx
#

@pure scroll I was using await cur.execute and it wasn't working properly

pure scroll
#

@torn sphinx can you show that par of the code?

torn sphinx
#

well

#

I can show you the broken version

#

I edited it earlier and its not inserting anymore but yeah sec

pure scroll
#

okay, yeah that is what I thought

#

cur.execute("INSERT INTO records (ipaddr, port2323, tcpbanner) VALUES('{}', TRUE, '{}');".format(y["ip"], """{}""".format(str(banner))))
generally speaking most of the times formatting database queries as a strings is not a good idea

#

what you should be doing is this

cur.execute("INSERT INTO records (ipaddr, port2323, tcpbanner) VALUES(?, TRUE, ?)", (y["ip"], str(banner)))
#

this way you let database adapter to handle escaping of the values, plus this way you will avoid possible sql injections

torn sphinx
#

idk if that even works, aiomysql doesn't support prepared statements

#

oh wait

#

I didnt scroll down

#

they must've forgot to document it

#

I'll try lol

#

lol ugh, its not inserting any records

#

but its not throwing any errors

pure scroll
#

are you calling commit ?

#

because according to your code you call commit every 100 records

#

but if you are processing less than 100 records you won't call commit at all

torn sphinx
#

I am

#

I have print("Inserting records") await conn.commit() print("Done")

#

at the end

pure scroll
#

okay, I don't think you are supposed to do inserts on cursor

#

this should be done on connections itself

torn sphinx
#

oh

#
AttributeError: 'Connection' object has no attribute 'execute'
#

uh

pure scroll
#

yeah, okay just went throught docs, you really have to use cursor for all the db commands, sorry it's a bit different with postgres

#

my bad 😃

harsh pulsar
#

@torn sphinx usually you have to create a cursor. some database libraries let you call execute on the connection directly as a shortcut

pure scroll
#

can you share the current state of the code

torn sphinx
#

sec

pure scroll
#

ah, you don't have await before any of the execute calls

#

just add await to all the execute calls

torn sphinx
#

oh oops

#
INSERT INTO records (ipaddr, port2323, tcpbanner) VALUES('181.112.136.50', TRUE, "'SSH-2.0-OpenSSH_7.2\r\n'");
Traceback (most recent call last):
  File "dbinsert.py", line 47, in <module>
    loop.run_until_complete(test_example_execute(loop))
  File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
    return future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "dbinsert.py", line 37, in test_example_execute
    await cur.execute("INSERT INTO records (ipaddr, port2323, tcpbanner) VALUES(?, TRUE, ?)", (y["ip"], str(banner)))
  File "/home/root/.local/lib/python3.5/site-packages/aiomysql/cursors.py", line 237, in execute
    query = query % self._escape_args(args, conn)
TypeError: not all arguments converted during string formatting
Exception ignored in: <bound method Connection.__del__ of <aiomysql.connection.Connection object at 0x7ff7a50e6e10>>
Traceback (most recent call last):
  File "/home/root/.local/lib/python3.5/site-packages/aiomysql/connection.py", line 1072, in __del__
  File "/home/root/.local/lib/python3.5/site-packages/aiomysql/connection.py", line 298, in close
  File "/usr/lib/python3.5/asyncio/selector_events.py", line 566, in close
  File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
RuntimeError: Event loop is closed
#

if you want it shorter, TypeError: not all arguments converted during string formatting

pure scroll
#

might be that i's using %s instead of ?

#
await cur.execute("INSERT INTO records (ipaddr, port2323, tcpbanner) VALUES(%s, TRUE, %s)", (y["ip"], str(banner)))
torn sphinx
#

lemme c

#

(its inserting for now 😂)

#

I think it is anyway lol, hasn't committed yet

hazy mango
#

@torn sphinx what version of python are you using?

torn sphinx
#

it worked, tx @pure scroll

#

@hazy mango 3.5.2

hazy mango
#

Any particular reason you haven't updated?

#

@torn sphinx

torn sphinx
#

nope

#

well on my laptop I can't, on the VPS I have no reason to

#

all new features are probably useless to me

hazy mango
#

I was thinking f-strings

torn sphinx
#

oh I despise those lol

hazy mango
#

They make database work a lot easier in my opinion

torn sphinx
#

they look like a typo every time I see one

hazy mango
#

Not really?

torn sphinx
#

they really do

#

its as if someone accidentally put an f in there

patent glen
#

...most things you do with databases you should not be using f-strings for

#

or any kind of string formatting

#

it can be necessary for dynamic table or column selection, but that's usually a sign of an awkward database design

torn sphinx
#

nooow

#

is there something like arrays in mysql

#

that can easily have data added to it

#

I know json is there but its effort to add values to it

hazy mango
#

I do lol, because I do like f"select x,y from z where w='{w}'" @patent glen

patent glen
#

@pure scroll %s is correct for mysql, different databases have different ways of doing it (and different libraries - psycopg uses %s but asyncpg uses the native postgresql $1)

#

@hazy mango that's really a bad idea what if w has ' in it

hazy mango
#

It never does in my case

#

*cases

patent glen
#

it's not a great habit to get into

hazy mango
#

My friends always said it was better than using ?

indigo mason
#

Your friend is incorrect I'm afraid

torn sphinx
#

I doubt it is

indigo mason
#

F-strings are great for string formatting sure

#

But you're not formatting a string

#

You're formatting a request that's about to execute on your database

hazy mango
#

So what’s the preferred way of doing it?

indigo mason
#

Even if your string is properly cleaned or not user generated, there's no point risking it or not using the proper format

#

What if someone put something malicious as w? Someone could drop your entire table through a command

torn sphinx
#

so is there something like a list in mysql? basically every record in the db has sub-records, atm I have boolean columns but thats going to be a lot of columns when I go on to add more.

hazy mango
#

Yeah I know but as I’ve said that will never happen

#

It’s only ever me using the database

indigo mason
#

As stated, still a bad habit to get into

#

And for what?

torn sphinx
#

Its also a pain whenever inserting the records lol, I have a lot of duplicates

hazy mango
#

Well how would I do selections based on variables?

indigo mason
#

Same as usual?

hazy mango
#
data = list(cur.execute("SELECT name FROM logins"))
users = [x[0] for x in data]
username = input(“Enter Username:\n”)
if username in users:
    dats = list(cur.execute(f"SELECT salt, password FROM logins WHERE user='{username}'")```what would the last statement become?
hazy mango
#

I can’t view links

#

Can you send screenshot pls

indigo mason
hazy mango
#

use %s for SQLite3 as well?

#

Or would it be ? for SQLite3

#

@indigo mason

patent glen
#

@hazy mango cur.execute('select salt, password from logins where user=?', [username])

#

it's ? for sqlite3, or... :name, I believe, if you use a keyword dictionary

hazy mango
#

Is it [] or ()?

#

@patent glen

patent glen
#

it can be either, but if you use () with a single argument you need (username,)

hazy mango
#

Oh yeah fair enough

patent glen
#

personally i find [] more readable in addition to being less likely to trip people up on the comma thing

hazy mango
#

Yeah I'll use [] then

#

Thanks :~)

minor maple
#

Does anyone know about flask?

ionic pecan
#

!ask

delicate fieldBOT
#
ask

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.
• 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
• Keep your patience while we're helping you.

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

#
ask

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.
• 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
• Keep your patience while we're helping you.

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

void otter
#

@sterile yokeNav#4064 what do you want to know

kindred cargo
#

is sqlite an online database?

void otter
#

it's local database

kindred cargo
#

what are some good online databases?

void otter
#

postgres

#

mysql

#

ms sql

#

i use postgres for my site

indigo mason
#

MySQL is ok but there's little reason to use it if you can use Postgres

dusty scroll
#

i'm getting Access denied for user 'xxx'@'localhost' (using password: YES) even though I am using the correct password

#

and i am connecting from localhost

silent tapir
#

how do you try to connect to it?

kindred cargo
#

i'm facing a query api rate limit issue in google sheets, what other database has a higher limit?

void otter
#

postgtres

kindred cargo
#

i'm using a discord bot to store user data. postgtres is a good database for it? @void otter

void otter
#

yes

#

maybe mysql

#

i use postgres for my web app and it works fine

kindred cargo
#

is it an online database? @void otter

#

idw it to be a local one

void otter
#

yes it is online db

#

sqlite is local

kindred cargo
#
import discord
from discord.ext import commands

client = pymongo.MongoClient('mongodb+srv://xx:xx@xx.mongodb.net/test?retryWrites=true')
db = client.get_database('Users')
records = db.get_collection('wager_lounge')
test = {'test':'1'}
records.insert_one(test)
print('t')```
why does it not add the document?
void otter
#

do you have your tables set up

kindred cargo
#

yeah i do!

worldly flint
#

anyone can teach me python ? home alone here T_T

carmine heart
#

@worldly flint This isn't the right channel for such a question (this is a channel for questions about databases). Also, while people can help you and explain concepts to you when you have questions about them, this isn't the right place to look for someone who teaches Python TO you. It's probably best to go with one of the many available resources or, if you want a tutor, to look in your local area for someone who's willing to teach you Python.

worldly flint
#

I see

raw onyx
#

quick question, why do people say aiosqlite is bad?

ornate isle
#

@raw onyx i dont know who "people" are

#

the lib works reasonably well from my limited usage. i think people just get caught up in the whole "sqlite db is a file" thing. you cant have multiple people using it at once, it is locked while in use, the normal sqlite issues, but you can sometimes run into this more frequently with aiosqlite if you don't know how to use it properly

#

but as far as i know there is nothing inherently wrong with it.

#

most of the time i'm not using it directly, but using it with sqlalchemy via the async databases library

tropic zealot
#

@raw onyx Because, at the end of the day, SQLite is still a file-based system that can write only finish executing as fast as the hard disk the file is on.

#

It's not terrible, but there are better options out there that you should look into considering.

ornate isle
#

@tropic zealot better options than aiosqlite? or sqlite in general? i think @raw onyx was asking about the aio lib rather than the db itself. also sqlite is fantastic in many situations and also disks can be pretty damn fast these days

raw onyx
#

my question was about the "is aiosqlite really that bad to the point where i should avoid it"

#

i should've said it like that

ornate isle
#

the library or the database

#

aiosqlite is a async driver for sqlite the database

#

to which are you referring

raw onyx
#

im just asking if what people say is true or not

#

im no expert on databases

ornate isle
#

okay so you are asking about the database itself

#

aiosqlite is not a database. sqlite is the database, aiosqlite is the package python uses to talk to sqlite asynchronously

raw onyx
#

well, then the question is "is sqlite bad?"

ornate isle
#

to answer your question, sqlite is amazing and is the most widely-deployed piece of software on earth (truth) but there are certain things you definitely do not want to use it for

#

if your database is being accessed by multiple apps, or youll have multiple server instances, or anything like that, you cant really use it

#

if your app will just live in one place on one server accessing one db, you can use it.

#

but only one thing can access it at a time, so if you have multiple threads or workers that try to use it, theyll each have to take their turn because its just one big file

#

in practice this basically means you probably shouldnt use it for something that you expect to be getting hundreds of requests/second to or something.

#

but yes i do know it's a pain to get started with the other databases. that is fine. what a lot of people do is use sqlite during development and switch to postgres over time

#

if you are using an ORM or query library (as opposeed to just writing raw SQL) this is even easier because to switch from one db to the other you just change the line of code that specifices which db you are using and most/all of the code will just work

raw onyx
#

the bottom line im getting is, that i shouldn't use sqlite for any large application?

ornate isle
#

i would use postgres

#

and most will say this

#

but it is fully acceptable to use sqlite in the meantime while you build the app and learn postgres, it shouldnt have to slow you down or anything

#

is the app a web app that people will be using?

raw onyx
#

no, it's a discord bot, it currently uses aiosqlite for it's database interaction

ornate isle
#

ahhh okay

#

so see that is an example of something i would think sqlite is fine for

#

the bot talks to the db and nothing else

#

i have a few bots running off even more hilarious options than sqlite

#

just make sure to back up the database frequently since it just lives as a file on the same server as the bot

raw onyx
#

okay

ornate isle
#

my recommendation would be use sqlite for this

#

make your bot

#

also over time learn postgres just to know it

#

eventually if you choose you can swap them out

raw onyx
#

swapping them out shouldn't be hard, right?

#

or require rewritting the code for the database

ornate isle
#

youre using the discord bot api? im not too familiar with it. does it handle the database stuff for you, you just give it the db url?

#

or is this your own db code

raw onyx
#

it's my own db code

#

the discord library doesn't do that stuff for you

ornate isle
#

right. were you planning to use an ORM like sqlalchemy or just write raw sql

raw onyx
#

i guess you could say raw sql?

ornate isle
#

so in that case the syntax is similar but not the same. for basic stuff it will look very familiar but a few thigns would have to be changed

#

like field types are a bit different

#

bear in mind i'm just coming at this from a 'launching now is preferred to taking time to learn new stuff if the current thing will work' approach

#

if you dont mind digging into postgres a bit, that might be better since youre writing raw sql and dont want to have to change your code later

raw onyx
#

alright

#

thanks for the advice

ornate isle
#

and then youll knw postgres which youd use for anything really big

#

and youll want to use the asyncpg library

raw onyx
#

yes, i know that

torn sphinx
#

is there a word for total number of queries

#

something.. like.. population?

#

I'm looking for the technical term

harsh pulsar
#

What would the context be

pale crest
#

@torn sphinx Maybe a "batch"?