#databases

1 messages · Page 44 of 1

rancid bronze
#

hey yall - i want to have a table that has information about an entity and i want users to be able to supply categories that they can associate with these entities -- how should I model the categories?

#

should I have an entities table and a categories table that has (entity_id, category name) that would just be a list of the associations between categories or..?

#

thanks!

viral crag
#

Yeah, so what you're referring to is a join table

#

It depends on the kind of relationship you need to have with your data

#

If you need to be able to go from entity to category, and also from category to entity, and there might be more than one relationship for either of those situations, then you do need a join table

#

a join table is basically just an extra table with three columns

#

a primary key ID, and two IDs - one each for each table you're associating

#

so ID, entity ID, category ID

#

if you're using an ORM, that's likely to be quite easy to do in it

rancid bronze
#

Ohh that makes total sense

#

Thanks

#

Can you look over some models for me in a bit? @viral crag

astral blaze
#

coming from the PHP-world, just started with python: which DB-engine would u suggest using?
MongoDB? NoSQL?

viral crag
#

Depends entirely on your use-case

#

I like RethinkDB

astral blaze
#

just to keep it short:
I wanna check some routine every 5 mins and insert / update some records everytime.

#

not talking about many records... lets say 100 or so

viral crag
#

That's not what I meant

#

It depends how strict your schema enforcement needs to be, and how data in your tables relates to data from other tables

astral blaze
#

not strict as all, only 1 table probably without any relations

#

at least for now - it might expand later, but just for now

#

i'll check out RethinkDB

#

thanks

vagrant jewel
#

@astral blaze mariadb or mysql works fine for simple stuff

astral blaze
#

i'm familiar with mysql, just wasn't sure if it is the "way to go" with python

vagrant jewel
#

works for me 😉

#

check out pymysql, super simple stuff

astral blaze
#

will do, thanks for the input 😃

ornate abyss
#

I mean if it's only one table just use sqlite

torn sphinx
#

hey just wanted to say SQLbolt is helping me learn SQL a lot

#

it's somewhat mobile friendly so i get on it while im on the toilet too

viral crag
#

Classy

vestal apex
#

(you're more likely to get responses if you give the details up front, unfortunately we humans are lazy and if people feel like they have to extract the situation out of you they're less likely to bother with helping)

#

maybe you could provide us with some details like your operating system, the method you're using, and the particular troubles

stoic pendant
#

haha no problem

#

please could someone help me figure out the issue i'm having with installing mysql server on windows 7?
during the configuration part of the install, it's failing to initialize database. i get warnings, but no actual errors that i can identify. actually when i installed this earlier today i managed to get to the 'starting server' portion of the config but it said it was taking too long and i didn't understand why. i decided to reinstall and now i'm getting this initializing database failed error message.
this is the .ini that people seem to ask for on stackoverflow altho it doesn't include the log which i'm not sure how to find.
https://pastebin.com/TfphSDHn

i'm going to restart my pc and do a clean install of mysql server again and see what happens, but this will be the 3rd time i've tried it this way...

stoic pendant
#

yeah, a fresh install did it, annoyingly. i have learned nothing except that i hate mysql 😂

vestal apex
#

F

torn sphinx
#

what do you guys think of Peewee as an ORM?

ionic pecan
#

seems nice

viral crag
#

It's not great, but it works

near cradle
#

it's kind of like django, right?

#

I don't know peewee but the django ORM is okay, so I guess I'd feel the same about peewee

#

I've heard they're more or less the same.

ionic pecan
#

Why would I choose Peewee over SQLAlchemy though? SQLAlchemy seems far more mature

viral crag
#

Peewee is smaller and comes with a migration framework

#

there's also peewee-async

#

but to be honest, SQLA has a nicer api, and alembic is way better than peewee's migration features

versed coyote
young flame
#

Hello, I am looking for a nice way to log live data to something other than a csv file. Any ideas on the best way to get started with databases like a simple tutorial

viral crag
#

If you're logging live data, are you sure a database is what you want?

#

I mean, what you're asking for is a bit ambiguous

#

Are you storing time-series data? Or are you talking about aggregating log messages?

steel slate
#

why not store them as a file with an about.txt that holds the 12 variables?

young flame
#

@viral crag I am storing 120 currency pairs with 12 variables each approximately. My current saving method is to just save my latest dataframe once it reaches a count of 24 hours
However for backtesting purposes I like to review past data in graphs instead of raw data. I was hoping if I store my data in a database vs a csv excel file I would be able to have a gui for the database so I can easily view historic data

#

@steel slate I was using csv files before and it works. However I just set it to save once it hits a count of 24 hours. Im not sure how to update a csv file with the last line of a dataframe

viral crag
#

So you're storing.. What exactly?

#

That didn't really answer my question

#

Transactions? Totals?

young flame
#

Live stock ticker data
"e": "24hrTicker", # Event type
"E": 123456789, # Event time
"s": "BNBBTC", # Symbol
"p": "0.0015", # Price change
"P": "250.00", # Price change percent
"w": "0.0018", # Weighted average price
"x": "0.0009", # Previous day's close price
"c": "0.0025", # Current day's close price
"Q": "10", # Close trade's quantity
"b": "0.0024", # Best bid price
"B": "10", # Bid bid quantity
"a": "0.0026", # Best ask price
"A": "100", # Best ask quantity
"o": "0.0010", # Open price
"h": "0.0025", # High price
"l": "0.0010", # Low price
"v": "10000", # Total traded base asset volume
"q": "18", # Total traded quote asset volume
"O": 0, # Statistics open time
"C": 86400000, # Statistics close time
"F": 0, # First trade ID
"L": 18150, # Last trade Id
"n": 18151 # Total number of trades

#

I also store current coin counts and calculated netWorth in lists. Also trade positions

desert ice
#

gdude, what he wants is to store live-streamed data from a stock-exchange/brokerage/data-provider which is to be used for later research on the data. Maybe there is real-time calculations + plotting involved too

#

Ideally one would want a csv like file for each ticker and also for each day or week or month ( as maybe convenient). Smaller chunks are easier. I have no idea about databases. So not sure if databases are better to text dumps

viral crag
#

Databases can be queried, which is the point

#

It sounds like what you want is something like rdb

#

A timeseries database with incremental backoff over time

desert ice
#

A little off-topic. After a little background study, it appears that the data he posted is level 2 data compared to level 1 which is just the prices. Some basic reading suggests people mostly use level 2 data for HFT algorithm trading. Pretty expensive given that they say HFT is only workable if you have a workstation close to the exchange i.e. high real estate costs leading to high price for your hired e-space. If those are for crypto-coins rather than real world currency-pairs ( USD-JPY or USD-EUR) you speak of, then it is a different game I guess

broken belfry
#

definitely crypto:
"s": "BNBBTC", # Symbol Indicated Binance Coin to Bitcoin trading.

young flame
#

its crypto. And its not hft, its algo trading

#

@desert ice you explained what i meant pretty well

#

my bot is alreary in papet and live trading however it was just in level 1 data tgrough api calls. The next stagr of development is to go to level 2 websocket live stresm

torn sphinx
#

How can I build a database with sqlite with information about songs like singer, titel, date of release etc. ?

silent mauve
#

i'm sorry i can't help you

broken linden
#

@torn sphinx just do it like you would do it in normal sql databases, if you dont know what those are/how they work do a bit googling, basic sql is really easy to learn

torn sphinx
#

I don't find good tutorials

#

can anyone create a little database and send it?

broken linden
torn sphinx
#

so I could learn from this

broken linden
#

a little database

#

sec

torn sphinx
#

but it says at this site you sent that this tutorial is for mysql, .... but not for sqlite

broken linden
#

sql syntax is quite similar

#

there are only very specifc things which are different

#

INSERT INTO, SELECT FROM, UPDATE and ALTER should always work in all dbs

#

and if you are using sqlite i doubt you need much more if any

torn sphinx
#

okay thanks

#

so do you have a database?

broken linden
#

i can come up with some sql to make one

#
CREATE TABLE Players
(
    "id" SERIAL PRIMARY KEY NOT NULL,
    "nick" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "progress" TEXT DEFAULT '{}' NOT NULL,
    "alliance" int DEFAULT 0 NOT NULL,
    "bio" TEXT
);
#

although i am not sure if sqlite has the "TEXT" datatype

torn sphinx
#

so this is just a table right?

broken linden
#

yes

torn sphinx
#

it says invalid syntax when I run it

broken linden
#

does it

#

well i guess thats too postgresql then

#

CREATE TABLE humans (date text, name text, age real)

torn sphinx
#

postgresql?

broken linden
#

another sql database

torn sphinx
#

ah

#

something with TABLE is wrong

broken linden
torn sphinx
#

This is so long

torn sphinx
#

so is anyone in here good with sqlite and can create databases with sqlite?

#

I really need your help

barren mesa
#

I wouldn't say I'm really good, but I've used it quite a bit

#

I can probably help

torn sphinx
#

it'll be enough for sure

ornate abyss
#

literally read the docs linked

heavy warren
#

Hi. Can someone help me to put together a request for sqlLite?

DB is very simple. One column with numeric User_Id where each user has his own table with numbers
Looks like
[
00001 [001, 002, 003 ...]
00002 [001, 002 ...]
00003 [001, 003, 005 ...]
...
]
Is it the right choice of DB structure in terms of performance?

I need to fill out the database with data structured in the same way in local variables.
Users should not be duplicated in dB. In case if user already exists, simply add the numbers to his table.

That's how I suppose to do it.

    for u in user:
        users_cursor.execute ("INSERT INTO users [id] VALUES [", u[0],"] IF NOT EXISTS")
        
        for num in u [1]:
            users_cursor.execute ("INSERT", num, "IN .... IF NOT EXISTS WHERE id =", u[0])

Is there any option to add everything in one line?

(I'm not that much experienced with databases, so I would be grateful for help)

near cradle
#

instead of looping over .execute, you should just compile these lines into a single string with semicolons seperating each instruction and then execute when you're done looping. that way you're just making one database interaction instead of len(users) * len(u[1]) database interactions.

#

this is a bigger problem with other databases, though

#

sqllite is usually a local file, but if you were using a remote database, for example, using this approach might mean having to send data over a network 1000 times instead of 1.

#

I'm afraid I don't know any truer ways of making it one line off the top of my head, though.

ornate abyss
#

Each user has their own table, what?

heavy warren
#

there could be a lot of numbers related to each user, I thought to use string instead of table, but it doesn't seem to be the right solution

#

any better ideas?

ornate abyss
#

A table per user isn't a good idea

#

I'm unsure as to what you're doing to suggest a better alternative mind

heavy warren
#

A condition is easy accessibility of all numbers of the specified user.
I can put them all in one column, with a duplicated user id. I do not know if this method is better. In the long term, one more table will be needed, where each number of the user is assigned another array of numbers. And also an additional info.

Like:

u1, somedata, [num1, num2, num3...]
somedata somedata somedata
[subnum1 [subnum1 [subnum1
subnum2 subnum2 subnum2
subnum3 subnum3 subnum3
... ] ...] ...]

u2, somedata, [num1, num2, num3...]
somedata somedata somedata
[subnum1 [subnum1 [subnum1
subnum2 subnum2 subnum2
subnum3 subnum3 subnum3
... ] ...] ...]

...

Each sequence should be easily accessible by root numbers. Including specified "somedata"
Also the entire sequences of this structure should be easily transferred to the program memory, in the same form.

To be honest, I have no idea which solution is the best.
In the python it's a 3-dimensional list. It takes to much memory, so cannot be handled without periodic interaction with database.

wispy fable
#

Each row of a table can hold as much data as you like

#

You're working with softwaren not paper and pen, so your limitations should only be a concern when you actually find them

heavy warren
#

The point is to easily get the exact right data, What if string could reach over million symbols?

#

then I have to parse all that huge string in python just to discard most of this data?

torn sphinx
#

Can anyone explain how to use the join command to connect tables with each other?

heavy warren
#

Looks like sqlite uses Hdd cache file for Execute operation, before to attach data to db file with Commit.
Is that so?
Is there any way to force it use ram memory instead?

ionic pecan
#

Anybody know some SQLAlchemy? I'm trying to figure out the best way to return a list of a single column, e.g. ```py
session.query(MyModel.column).distinct().somehow_get_a_list()

Right now I get a nested list of tuples, no matter how I query. There must be some SQLAlchemy way to do this. ```py
(Pdb) session.query(Follow.follows).distinct().all()
[('volcyy',), ('vao92',)]

I've got this flatten-the-result-list pattern quite often here, so I'd be happy for any suggestions how it's possible to simplify this.

viral crag
#

x[0] for x in y?

#

I think there is a proper way to do it in SQLA, but I'm not sure how it works

ionic pecan
#

Yeah that's the short form of what I currently have, but I feel like there must be the right way

ionic pecan
loud spade
#

[SQLite3] What do you call it when you create a table and one var is numbers and karakters? Like c.execute("CREATE TABLE IF NOT EXISTS DiscordOffers(user TEXT, datestamp TEXT, id REAL, confirmed REAL)")

#

where user is like MK#7906

ionic pecan
#

to my knowlege that's not a data type in sqlite

#

if you want to store users, it's probably better to store them by ID, since the ID cannot change while username / discriminator can

loud spade
#

Yeah I thought of that too :p

#

@ionic pecan can you use an if else statement with c.execute? sorry to ask, but i am new to sqlite3 :p

ionic pecan
#

not sure what you mean, can you show some code ?

loud spade
#

I’ll do that in a bit, I’m currently on my phone xd

ionic pecan
#

👍

loud spade
#

@ionic pecan

id = ctx.message.author.id #discord thingie, so something like 2309328592385209
c.execute("SELECT * FROM DiscordUsers WHERE id=user")
for row in c.fethcall():
    print(row)

how do you use an if and else statement here? Like if it has an outcome, that it will say like yes or something

ionic pecan
#

like if there is one user with the given ID?

loud spade
#

yeah

ionic pecan
#

then you can do e.g.

c.execute("SELECT * FROM DiscordUsers WHERE id=user")
result = c.fetchone()
if result is not None:
    # result is the queried user's row
else:
    # result not found
#

also, I assume that the WHERE id=user is just a placeholder, but keep in mind that you should generally use parameter substitution

loud spade
#

Ah

#

Okay

#

Thank you!

ionic pecan
#

👍

dusty scroll
#

I'm trying to use pymysql to add the values of a certain field up for 60k+ records, is there a faster way of doing this rather than going through all of the entries and adding the values up?

unreal tartan
dusty scroll
#

i'm doing a bulk select

unreal tartan
#

So yes, you first have to make a list of tuples of all the stuff you wish to insert.

dusty scroll
#

so just use executemany too?

unreal tartan
#

What do you mean with a bulk select?

dusty scroll
#

I have 60K+ records in a table currently, I need to get the value of a column from each individual record

unreal tartan
#

okay so that's a normal select and you loop over the rows right?

dusty scroll
#

yes

ionic pecan
#

c.execute("SELECT * FROM DiscordOffers WHERE user=user") will always return the same

#

where user=user literally selects any DiscordOffer where user=user

#

completely independent of what's happening around it

#

you wanna pass the user into the execute

#

I think it's ```py
c.execute("SELECT * FROM DiscordOffers WHERE user=?", (user,))

but I could be wrong, you should re-check the docs to be sure
loud spade
#

I am sorry, but I forgot what ? did and (user,))

#

I know user is a var

#

but what is the usage of it?

ionic pecan
#

by using ? and passing (user,) as the second parameter, you're essentially putting the value of user into that WHERE user= statement

loud spade
#

oh

#

like .format ?

ionic pecan
#

safely, that is

#

nope, was about to mention that

#

imagine this, you have an app where users submit comments

#
comment = await get_comment_from_user()
c.execute("INSERT INTO comments VALUES ({}, {})".format(user, comment))
#

okay now, this is bad, why? you're just inserting untrusted user stuff into your database

#

imagine comment was something like 'hello'); DROP TABLE comments;

#

then the SQL could be something like

INSERT INTO comments VALUES (
    91032123,  -- user id
    'hello'); DROP TABLE comments;
)
#

do you see the issue? 😛

loud spade
#

ah

#

yes

ionic pecan
#

that's why you use ? for substituting parameters. you don't need to worry about that then

loud spade
#

you can literally input code from the inside 'hacking' it

#

I think I saw that on yt too

#

where someone talked about that

ionic pecan
#

yeah

#

basically, don't do that

#

super important

#

but yeah. now back to your question, now you should get the DiscordOffer corresponding to the user

loud spade
#

it worked btw

#

just tried it

#

thanks again!

#

+respects

ionic pecan
#

finger_gun_dank no problem

tawny sail
#

hey guys,i was just wondering about what is the best database to use for my programming ? i use MySQL ,the workbench n all,but in youtube while browzing tutorials,u see people use various others which i dint know about..so just a little thought on this

steel slate
#

@tawny sail are you going to have a million or more rows in your database?

tawny sail
#

not more than 40 or so

steel slate
#

then it won't matter much to you

tawny sail
#

oh

steel slate
#

SQLite would be fine for you

tawny sail
#

i did work on that,but i shifted to mysql later on lol

steel slate
#

I don't see any reason to change it back if you already have something in place

#

small scale stuff isn't going to have a major impact unless you have been asked to evaluate efficiency or justify design choices

loud spade
#

@ionic pecan sorry to disturb (again), but is this a REAL or a TEXT? <discord.message.Message object at 0x103a72130>

torn sphinx
#

thats a message object

#

do you want to get the content of that message?

loud spade
#

no not like that. I am trying to get a function that I can use client.delete_message(message_object)

#

and you cannot use the id

#

of the message

torn sphinx
#

you can

#

theres something that retrieves the message object from id

loud spade
#

really?

#

lemme check this out

#

no, but the ID is known

#

it just needs to delete the message

#

it saves the message object in a database

#

and on request it deletes the message

torn sphinx
#

....?

#

idek what youre saying

loud spade
#

like

#

if you do this

#

message_id = await client.say("test".format(ctx.message))

message_id_code = message_id.id

print(message_id)
print(message_id_code)

>>> <discord.message.Message object at 0x103a72130>
>>> 848948498895895898

You can't use message_id_code as a parameter in delete_message()

torn sphinx
#

thats why i said use client.get_message

#

it builds message objects from id

loud spade
#

OH

#

I am sooo dumb

torn sphinx
#

lol

#

but yeah

loud spade
#

thanks!

#

F

#

+respect

torn sphinx
#

but if you want to do that youll have to store both channel ids and server ids i think..

#

lemme see

#

actually nvm

#

only channel id

#

so youll want both py message_object = await client.say("whatever") message_id = message_object.id message_channel_id = message_object.channel.id

#

message_id and message_channel_id

#

and then when you want to get that message object you can do py channel = client.get_channel(message_channel_id) message_object = await client.get_message(channel, message_id)

loud spade
#

thanks

#

ill look at it :p

#

@torn sphinx when I try to store the ID in SQLite3, I get a number like 4.3953257433176474e+17

#

is REAL for floats?

torn sphinx
#

REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.

yeah looks like

loud spade
#

what is then the best to store it?

torn sphinx
#

i dont know much about dbs, but maybe BIGINT

loud spade
#

lemme try

torn sphinx
#

or tbh you can store it as a string cuz ids are strings in the async version of discord.py

loud spade
#

well, BIGINT worked 😄

torn sphinx
#

ah ok

#

im not sure if itll be a str or an int when you retrieve it from the db, but make sure you pass strings whenever an id is required

loud spade
#

if I retrieve it from the database it is this: ('359012789852831744', '2018-04-27', 439536454150914059)

#

so all good

torn sphinx
#

ah ok so make sure you convert it to a string for the get_member and get_message stuff

loud spade
#

isn't it to retrieve the id like this:

#
async def done(ctx):
    try:

        name = ctx.message.author.id
        c.execute("SELECT * FROM DiscordOffers WHERE user =?", (name,))
        for row in c.fetchall():
            print(id)
        conn.commit()
        await client.say("{} I have succesfully closed your offer.".format(ctx.message.author.mention))

    except:
        raise
#

i used print to look if it works

#

frick

#

I get <built-in function id>

torn sphinx
#

yeah because you dont use id anywhere else

#

maybe you mean row?

loud spade
#

If i use row, I will get ('359012789852831744', '2018-04-27', 439536454150914059), what I don't, what I think. Since the ID is only relevant in this function

torn sphinx
#

i think * will do everything in that row

#

so use column names instead

loud spade
#

it kinda works

#

but only

#

I get this

#

(439536454150914059,)

torn sphinx
#

its a tuple so youll have to do [0]

#

but wouldnt it be better to store it as text?

loud spade
#

why that?

torn sphinx
#

because thats an id right

#

but on rewrite theyre ints

loud spade
#

well, the outcome is 439536454150914059

#

so I think no problemo

torn sphinx
#

no problem but storing it as a string will just save you from casting it to a str

#

also you need both the message id and channel id to retrieve the message again

loud spade
#

channel ID is always equal to the same channel

#

@torn sphinx just save it as TEXT you mean?

#

or

torn sphinx
#

yes\

loud spade
#

lemme try

#

thank you for taking your time to help me btw

#

I appreciate it!

torn sphinx
#

np

glass bough
#

So I'm trying to make a mix of a database and a GUI, I will have songs and details about them. I am very new to python but I want to learn, this is a project I've been stuck on and I want some help, I have tried a few times, I can give those as examples, but they are very disorganized and sloppy as again I am inexperienced. I am 14 so if you can't handle the annoying voice you don't have to try to explain it. Thanks

#

hopefully you can teach me something further about classes and some basic concepts that I can't quite grasp from youtube videos

broken linden
#

first of all ages isnt really important here, im 15 even the server owner is

if you are going for a databse plus GUI mix maybe take a look at sqlite and the sqlite3 module from the python standard library and for a farely simple GUI take a look at tkinter, from the standard library too

or do you have any specific questions

glass bough
#

I've been attempting to use those two exactly

#

I'm just struggling apparently

#

its mostly some slightly more complex pieces of tkinter. It might not actually be complex, but it was difficult for me

versed coyote
#

GUI library designed for people who are learning

#

Though this is off-topic for databases channel 😛

loud spade
#

@glass bough Tkinter is fairly easy to understand, only getting the input from textboxes sucks ass

#

I couldn't get that working

glass bough
#

lol

broken linden
#

nearly everything from tkinter is what you expect every GUI framework to have in some form now days tbh

loud spade
#

@broken linden bist du Deutsch?

broken linden
#

indeed

loud spade
#

:0

#

das ist sehr schön

broken linden
#

trotzdem schreiben die Server Regeln aber Englisch vor ¯_(ツ)_/¯

loud spade
#

I know, but just wanted to test you :0

glass bough
#

I think I understand most of it, I just don't know how to make my code clear and to use some more advanced pieces to make it make more sense, using that appJar thing is like relearning it a bit

loud spade
#

@broken linden deleting a row in SQLite3 is like c.execute("DELETE FROM DiscordOffers WHERE id=?", (name,)) right?

broken linden
#

its like that in every sql dialect

loud spade
#

@glass bough maybe try to learn the basics of Python

#

@broken linden I am having a problemos lol

#

😅

#

look

broken linden
#

i mean

#

i am not sure about sqlite3 cause i never used it

#

but in my database projects i usually have

loud spade
#

it causes some sort of 404 error

broken linden
#

stuff like

glass bough
#

I know a bit of python, but some of the things that people put in these chats confuse the hell out of me

broken linden
#
def showID_to_name(ID):
    app.config["CUR"].execute('SELECT title FROM "shows" WHERE id = %s',
                              (ID, ))
    return app.config["CUR"].fetchall()
#

and yes

#

that new line is needed

loud spade
#

is that MySQL?

broken linden
#

that is postgresql

#

but they work nearly the same at this very basic syntax

loud spade
#

but could you look at my code please meine Curry wurst?

#

bitte?

#

xd

broken linden
#

so you get some sort of 404

#

could you show me?

loud spade
#

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: NOT FOUND (status code: 404): Unknown Message

#

wait maybe I know it xd

#

nope

#

scheisse

broken linden
#

is the code you showed inside a cog or not?

#

(if you dont know what a cog is i am sure it isnt)

loud spade
#

I choose the second option

#

lmao

#

but this is the right code

#

but it gives an error

#
#close offer
@client.command(description = "close an offer", pass_context = True)
@commands.has_any_role("Verified")
async def done(ctx):
    try:

        name = ctx.message.author.id
        c.execute("SELECT id FROM DiscordOffers WHERE user =?", (name,))
        for row in c.fetchall():
            message_id = row[0]

            channel = discord.utils.get(client.get_all_channels(), name="offers")
            message_object = await client.get_message(channel, message_id)

            await client.delete_message(message_object)
            c.execute("DELETE * FROM DiscordOffers WHERE user=name")



            conn.commit()
        await client.say("{} I have succesfully closed your offer.".format(ctx.message.author.mention))
        

    except:
        await client.say("{} You do not have any open offers!".format(ctx.message.author.mention))
        raise
broken linden
#

im not sure but

#

you have to put a @ command there dont you?

#

oh

#

sorry

loud spade
#

it is that you use like !done

broken linden
#

GOD DAMN IT

loud spade
#

and that acts like done()

#

but then in discord

broken linden
#

yeah

loud spade
#

oh

#

wait wut

broken linden
#

i think you gotta put the command decorator there

#

im not sure though

#

like

#
@client.command(pass_context=True)
async def test(ctx):
  pass
loud spade
#

already have that?

broken linden
#

oh

#

hmmmmmmmm

loud spade
#

I think it is with the database, but not sure

broken linden
#

is there some this exception was a direct cause of xyz thingy?

loud spade
#

xyz thingy?

#

@torn sphinx could you help me?

broken linden
#

sometimes python spits out two exceptions

loud spade
#

@broken linden no discrespect tho, meine liebe herr

broken linden
#

that doesnt sound like correct german o_O

loud spade
#

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/discord/ext/commands/bot.py", line 846, in process_commands yield from command.invoke(ctx) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/discord/ext/commands/core.py", line 374, in invoke yield from injected(*ctx.args, **ctx.kwargs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/discord/ext/commands/core.py", line 54, in wrapped raise CommandInvokeError(e) from e discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: NOT FOUND (status code: 404): Unknown Message

torn sphinx
#

it means it didnt find the message

loud spade
#

@broken linden could be true, cuz i am not a native xd

torn sphinx
#

make sure the message isnt deleted

loud spade
#

oh

#

@torn sphinx you are my savior in needs lmao

#

I love you so fucking much

#

YES

#

It worked

#

@torn sphinx ❤ ❤ ❤

#

btw

#

maybe handy for you

#

using channel = discord.utils.get(client.get_all_channels(), name="name of your channel") to get the channel is easier if the channel name is known

torn sphinx
#

that works altho i think get_server is faster

#

esp if you have a lot of servers

#

or get_channel i mean

loud spade
#

Yeah, but my bot is specifically designed for one server so idk

#

xd

glass bough
#

Well, using that sortof guiding tool (appJar) this has been incredibly simple

#

atleast for the basic parts of it that is

#

Those being being able to add to the table, now I just have to have a simple way to find specific details and pick out those songs

#

this is great:D

lavish ferry
#

how can I fix the error No module named database ?

gusty spindle
#

by installing a module named database, or making sure you are typing the right name

lavish ferry
#

what the command to install database ?

gusty spindle
#

I've never heard of it, but I'd imagine that you'd have to use pip install ... or python -m pip install ...

lavish ferry
#

I will try

gusty spindle
#

if you want to actually use a database module in python, I'd recommend starting with sqlite3 in the standard library

sturdy orbit
#

I'm creating a league of legends discord bot and wanting to store user data, what database do you guys recommend i use? (could potentially be on a LOT of servers)

ionic pecan
#

i use sqlite for mine since i'm too lazy to set up something else

#

postgresql is fairly popular

sturdy orbit
#

its not necessarily going to be VAST amounts of data, just more speed of access i think

viral crag
#

Postgresql is the best sql database

#

We personally use rethinkdb for our database

sturdy orbit
#

nods yupyup

viral crag
#

But that's not an sql database

sturdy orbit
#

ive tinkered with rethink before

#

sod it, i'll give it a try

#

rethinkdb it is

#

thanks gdude

viral crag
#

I think you'll like it

#

But remember, it has no access control

#

Don't expose the port

sturdy orbit
#

nods

#

yupyup

#

thanks buddy

broken linden
#

I am currently trying to get some simple database application to work, im running postgresql and created my main table with

CREATE TABLE "Users" (
  "time" TIMESTAMP NOT NULL,
  "count" INT NOT NULL,
  "invite" VARCHAR(20) NOT NULL
);

however when my python code comes to the insertion point and tries to execute

 INSERT INTO Users VALUES (1524907749.9109762,2392,'jQtfh66')

i get
psycopg2.ProgrammingError: relation "users" does not exist

#

creating the db with lowercase name fixed it

torn sphinx
#

How can you create a column in a "main" table which includes the ID's of other tables?

loud spade
#

just search for !!!!!! problem is here !!!!!!

torn sphinx
#

just one thing i noticed

#

put try: except: on the problematic line

#

not the whole thing

loud spade
#

oh

torn sphinx
#

or else you dont know where the error came from

loud spade
#

okay

#

yeah

torn sphinx
#

but

loud spade
#

sorry

#

xd

torn sphinx
#

whats the problem youre having?

loud spade
#

i get multiple errors tbh

#

but it keeps changing

#

for instance

#

the error I get with this code is : Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/discord/ext/commands/bot.py", line 846, in process_commands yield from command.invoke(ctx) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/discord/ext/commands/core.py", line 374, in invoke yield from injected(*ctx.args, **ctx.kwargs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/discord/ext/commands/core.py", line 54, in wrapped raise CommandInvokeError(e) from e discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnboundLocalError: local variable 'receiving' referenced before assignment

torn sphinx
#

oh i see

#

you need to define receiving before you use it in the for loop

#

also this

#
for row in result_receiver:
                receiving = row[0]``` lets say you do define receiving before you used it
#

itll change in each row

loud spade
#

it searches for the AMOUNT tho?

#

but

#

just assign before the loop and reassign in the loop?

#

or

#

poke?

#

@torn sphinx sorry to ping

torn sphinx
#

you can do like

#

receiving = None before the loop

#

but cant you just reverse the list and get the first item

loud spade
#

how do you mean? sorry for the dumb question.....

torn sphinx
#

ok well

#

explain what that for loop is supposed to do

loud spade
#

so that code gets executed repeatably right?

torn sphinx
#

ok i have a little more time now

#

so you want to keep repeatedly getting the first item in each row?

#

until it reaches the end?

#

isnt that the same as getting the first item in the reversed list

loud spade
#

@torn sphinx I have fixed it 😅

torn sphinx
#

ah ok

loud spade
#

Thanks tho

#

I really appreciate you and what you are doing

torn sphinx
#

btw it might be better to ask more general questions in one of the help channels

#

and thanks 😅

loud spade
#

What do you mean with general ?

#

*questions

torn sphinx
#

just regular questions

#

more people will see it there probably

loud spade
#

oh

#

okay

#

btw

#

could I show you how I fixed it?

#

so you can see if I did it right or not

torn sphinx
#

sure

loud spade
#

nice

#

one sec

#

xd

#

okay

#

here it is :p

ionic pecan
#

the loop is kinda weird, you overwrite the variable every time

#

maybe you just wantto fetch the first result?

loud spade
#

could be yeah

#

could you give the line number where it starts 'overwriting' ?

ionic pecan
#

14

#

if you have multiple results, sendingtip will be set to the last one

loud spade
#

thanks

#

ill edit that in a bit xd

#

love you Volcyy

#

#

and poke

ionic pecan
#

also, not entirely sure, does c.fetchall() ever return None?

#

or does it return an empty sequence when it doesn't get anything

loud spade
#

c.fetchall() gives []

#

if you do this:


result = c.fetchall()
print(result)
>>> []
ionic pecan
#

what's that if result is None check then?

#

maybe you want if not result to check if the result is empty?

glass bough
#

okay, this is a kind of mixed chat question, if I take something from a database and want to print it as a label on a GUI, say all the parts of a row in a table, how would I do this? I am using tkinter and sqlite3

loud spade
#

@ionic pecan yeah forgot that :c

lavish ferry
#

someone know where can I find my token from MongoDB ?

viral crag
#

Token..?

#

MongoDB doesn't use authentication by default

#

To set that up, you have to explicitly set the passwords

#

So there's nowhere to "get" the "token"

#

You're expected to not lose the password

broken linden
#

Maybe he means his discord token

viral crag
#

He said that wasn't it in another channel

broken linden
#

Oh welp

small dirge
#

need some help with my mongodb program. It's using flask-pymongo(basically flask). This is probably the easiest question, but googled for like 25 min and haven't found anything that really makes sense. Basically I'm trying to search through the users collection, and query/find a document that has a certain email field and grab the documents string that has the api key in it and set it to a variable. Anyone know how I would do this?

viral crag
#

Wait so you have a document

#

What is the form of that document? Does it contain both email and api key? @small dirge

small dirge
#

yes @viral crag

viral crag
#

yeah, so you just get the document by the email address

#

and you'll get the whole document

#

including the api key

glass bough
#

How could I set a limitation so I am unable to add something that is already in any of the columns in a database

#

to prevent duplicates

wheat meadow
#

@glass bough you would have to querry the DB for the results and check to see if it's already there

ionic pecan
#

@glass bough I would use a constraint

#

Are you using an ORM or raw SQL?

fresh vale
#

Ok, I have another problem

#

I used and coppied code that insarting data into SQL base

#

I added my query and edited args, but nothing else

#
  File "C:\Python35\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:\Users\saaswww\Desktop\bonkobot\bot.py", line 112, in on_message
    rules_confirm(message.author.id)
  File "C:\Users\saaswww\Desktop\bonkobot\bot.py", line 63, in rules_confirm
    cursor.close()
UnboundLocalError: local variable 'cursor' referenced before assignment```
viral crag
#

you're using cursor before you assign it

fresh vale
#

I now it

#

But i defined it

#
    query = "INSERT INTO users(Discord_ID,rules) " \
        "VALUES(%s,%s)"
    args = (Discord_ID, 1) 
    try:
        db_config = read_db_config()
        conn = MySQLConnection(**db_config)
 
        cursor = conn.cursor()
        cursor.execute(query, args)
 
        if cursor.lastrowid:
            print('last insert id', cursor.lastrowid)
        else:
            print('last insert id not found')
 
        conn.commit()
    except Error as error:
        print(error)
 
    finally:
        cursor.close()
        conn.close()```
viral crag
#

You defined it within the try

#

but you're using it outside of the try block

fresh vale
#

How to make it global?

viral crag
#

No

#

You need to define it outside of the try

fresh vale
#

in rules_confirm() ?

viral crag
#

Yeah

fresh vale
#

yyyyy

#
    try:
        dbconfig = read_db_config()
        conn = MySQLConnection(**dbconfig)
        cursor = conn.cursor()
        cursor.execute("SELECT rules FROM users WHERE Discord_ID= `%s`")
        args=(DiscordID)
        row = cursor.fetchone()
 
        while row is not None:
            print(row)
            row = cursor.fetchone()
 
    except Error as e:
        print(e)
 
    finally:
        cursor.close()
        conn.close()
def rules_confirm(Discord_ID):
    conn = ""
    cursor = ""
    query = "INSERT INTO users(Discord_ID,rules) " \
        "VALUES(%s,%s)"
    args = (Discord_ID, 1) 
    try:
        cursor = conn.cursor()
        db_config = read_db_config()
        cursor.execute(query, args)
        conn = MySQLConnection(**db_config)
        if cursor.lastrowid:
            print('last insert id', cursor.lastrowid)
        else:
            print('last insert id not found')
 
        conn.commit()
    except Error as error:
        print(error)
 
    finally:
        cursor.close()
        conn.close()```
#

I tryied to fix it by defining empty vartibiles, but:

#
  File "C:\Python35\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:\Users\saaswww\Desktop\bonkobot\bot.py", line 112, in on_message
    rules_confirm(message.author.id)
  File "C:\Users\saaswww\Desktop\bonkobot\bot.py", line 63, in rules_confirm
    cursor.close()
AttributeError: 'str' object has no attribute 'close'```
viral crag
#

well now you're defining cursor as a string

#

same thing for conn

#

you've messed up your line ordering

#

look at your try block

#

first line, get a cusor from the connection

#

second line, get the db config

#

third line, execute a query on the cursor

#

fourth line, create a connection using the db config

#

that's what you're doing, and yeah that definitely won't work

#

you need to define your db config, connection and cursor at the top of that function, in that order

fresh vale
#
    db_config = read_db_config()
    conn = MySQLConnection(**db_config)
    cursor = conn.cursor()
    query = "INSERT INTO users(Discord_ID,rules) " \
        "VALUES(%s,%s)"
    args = (Discord_ID, 1) 
    try:
        cursor.execute(query, args)
        if cursor.lastrowid:
            print('last insert id', cursor.lastrowid)
        else:
            print('last insert id not found')
 
        conn.commit()
    except Error as error:
        print(error)
 
    finally:
        cursor.close()
        conn.close()```
#

Now NameError: name 'read_db_config' is not defined

viral crag
#

Well, then I guess you didn't define it

fresh vale
#

A

#

I know

#

I don`t imorted this function 🤦

ashen moth
#

Okay so this is more SQL than Python, but my web server is throwing me an operational error which doesn't happen elsewhere. This is the code:

params3 = ("TRUE", discord_date_to_unix(datetime.utcnow()), "FALSE", discord_id)
        udb.execute("UPDATE UserMainTable "
                    "SET (IsRegistered, RegisterDate, SendRegistrationReminders) "
                    "= (?,?,?) "
                    "WHERE DiscordID is ?", params3)```
#

And here's the error:

#
File "/dobby3/DobbyCommands.py", line 369, in register_user_account_in_db
    if int(res2[0]) == 0:
        params3 = ("TRUE", discord_date_to_unix(datetime.utcnow()), "FALSE", discord_id)
        udb.execute("UPDATE UserMainTable "
                    "SET (IsRegistered, RegisterDate, SendRegistrationReminders) "
                    "= (?,?,?) "
                    "WHERE DiscordID is ?", params3)
sqlite3.OperationalError: near "(": syntax error```
#

Hold up, messed up the formatting

#

There ya go

viral crag
#

what SQL dialect is this?

ashen moth
#

SQLite 3

viral crag
#

I think it's SET VALUES, not just SET

ashen moth
#

I had some issues getting it to work actually

#

Had to install the dev package and recompile Python

#

I'll give it a go, thanks

#

@viral crag Now it's the same error, except that it's displaying near "VALUES"

#

I wonder if it's the brackets actually

ionic pecan
#

Is is a thing in SQL?

#

It's a bit further there, but I recall always using = for this

viral crag
#

oh, it'd be LIKE I think

#

I'm not sure honsetly, the error you have is an SQL syntax error though

ionic pecan
ashen moth
#

I found it. For whatever reason the SQLite version is a right stickler when it comes to formatting

#

I can't use SET c1, c2, c3 = ?,?,?

#

Instead I have to use

#

SET c1 = ?, c2 = ?, c3 = ?

#

On my desktop/laptop it works fine though

ionic pecan
#

Judging from the docs the above should work fine though

ashen moth
#

Hmm

#

It might be to do with the issue I had with it before

glass bough
#

@ionic pecan I am using slite3 I assume that applies to normal SQL, but I'm very inexperienced with some of this stuff

ionic pecan
#

the pages after it explain more about it

glass bough
#

it's still sortof confusing

#

because I don't know where I would put that in my code

#

well I just made it so that when I add something, it takes the name input and deletes the rows with that name and then adds the new one

#

Does this make sense to do?

#

Or is there a better method

glass bough
#

Well, I figured it out in a better way

glass bough
#

so I ended with the constraint of INSERT OR IGNORE

#

but instead of ignoring, is there a chance I can have it tell me when something is repeated

ionic pecan
#

I think you can use ON CONFLICT

#

also that's not a constraint

#

a constraint would be something like name VARCHAR(30) UNIQUE

#

you need to put these constraints to your table creation

ornate abyss
#

Just do an insert and catch the exception

#

Then you know it's repeated

glass bough
#

I put unique in the table part with my insert and ignore in the main body

hexed epoch
#

select from table a row where username == variable can someone translate this to proper postgresql

runic tundra
#

SELECT *
FROM table_name
where username = variable;

barren swallow
#

I have a MySQL database running and I'm using the mysql.connector.
I need to insert millions of rows into a table, what is most efficient (fastest) way to do so?

#

I've tried just

sql = "INSERT INTO users VALUES (%s, %s, %s, %s)"
cursor.execute(sql, (data[0], data[1], data[2], data[3]))

But for one million inserts it takes 30-40 minutes

runic tundra
#

https://stackoverflow.com/questions/6159087/mysql-is-taking-too-long-to-insert-rows
https://dba.stackexchange.com/questions/61029/inserting-60-000-rows-taking-too-long
@barren swallow do either of these help? Basically.. how are you inserting each row? does each one have an insert into statement?

barren swallow
#

I'm currently testing out executemany function

#

but loading 15 million rows into memory isn't ideal so I will need to code it to do 1K at a time or something like that

ionic pecan
#

From where are you getting the rows?

barren swallow
#

ATM I'm using

with open('x.txt', encoding="utf8") as f:
    for row in f:
#

Apparently it just loads it row by row

runic tundra
#

what is in the text file? comma seperated rows?

barren swallow
#

Something like that, yes

ionic pecan
#

Hmm

runic tundra
#

ok if you want something fast and easy convert the text file into a csv and do LOAD DATA FROM INFILE '<filepath>/x.csv' INTO TABLE users

barren swallow
#

hmm

runic tundra
#

otherwise, you want to group rows and do the insert as batches

#

because the executemany is doing an insert every time which is slow

barren swallow
#

So for importing large quantities of rows import is always a better choice?

runic tundra
#

as far as i know yes, as long as you don't need to manipulate the data inbetween

fluid rose
#

Hi

barren swallow
#

Well, I actually need.

runic tundra
#

ok in that case, what you need to do if you want speed is to create an insert statement that inserts lots of rows at once. your executemany statement is adding overhead to every single row you are adding

barren swallow
#

I should parse the file using Python and create a csv file and import the csv file?

runic tundra
#

that could work

small dirge
#

Hey guys, a bit confused

#

Using mongodb, should I be creating a new collection for every user?

#

atm i've been creating new documents for each user

vestal apex
#

it depends on what all you're storing

#

well...

#

no it should pretty much just be documents

#

what makes you think you'd need individual collections?

small dirge
#

not sure, i just have to store a lot of data

#

it would be a lot of fields for each user

#

@vestal apex

vestal apex
#

well you can break user info across multiple collections, but sometimes you just have lots of fields

#

if you've got a considerable number of fields that would work well in, say, a table

#

you might want to consider a relational database

small dirge
#

my project is just starting though, and i want to be able to change and work with things very easily which is why I chose mongodb

#

i don't want to be forced to use a structure right away

barren swallow
#

I was having problems with importing large quantities of data into a MySQL database earlier.
I found the solution, mysqlimport.exe is the best solution. With it I was able to import 100k rows in less than a second

hexed epoch
#

lol that sounds like an ad and half

ionic pecan
#

Question regarding foreign keys.

CREATE TABLE mute (
    expiry DATETIME, 
    active BOOLEAN, 
    infraction_id INTEGER NOT NULL, 
    PRIMARY KEY (infraction_id), 
    CHECK (active IN (0, 1)), 
    FOREIGN KEY(infraction_id) REFERENCES infraction (id) ON DELETE CASCADE
);

This is my schema. When I delete the referenced infraction, the mute doesn't seem to delete. My understanding is that ON DELETE CASCADE would also delete the mute, but it doesn't. What would I use instead?

#

Maybe this is an issue with how I execute the delete. Using SQLAlchemy:

        query = infraction_db.delete().where(and_(
            infraction_db.c.id == id_,
            infraction_db.c.guild_id == ctx.guild.id
        ))
        result = await self.bot.db.execute(query)

runic tundra
#

@ionic pecan have you fixed this?

ionic pecan
#

Yes

#

Turns out SQLite doesn't support Foreign Keys without enabling it

#

so that just failed silently, which annoyed me a slight bit, so I use pgsql now

#

thanks though

runic tundra
#

ok sweet

#

interesting to know

orchid charm
#

Info on Databases?

#

I wanna know how to connect to one, and write to one

gusty spindle
#

depends on what scenario you've got and what you plan to do @orchid charm

orchid charm
#

I just want a Profile system

#

Where i can write stuff in a Specific Cagetories so the bot can grab the info

#

Like if i typed !profile add weapon splattershot It would write to a Weapons Cagetory Splattershot

#

@gusty spindle Also what do you mean by Scenario

#

Do you mean what i am gonna use?

#

If so i have no idea whatsoever

gusty spindle
#

i'm not too sure about DB's, but i know some are better at smaller-scale projects and others are better at much larger-scale things

orchid charm
#

The Bot is not in Github so i can't look at what the Person who devved this bot did with it

dull scarab
#

What's the correct datatype for storing strings converted to bytes in postgres?

#
reason = Padding.pad(bytes(reason, "utf-8"), 16)
enc_msg = self.aes.encrypt(reason)``` kinda message
orchid charm
#

What do i do if i wanted a Sqlite3 discord bot to check if something exists in a db and do something if it does not exist in the db

#

Like i got 3 Columns, UID, IGN, WEAPON

#

How would i make the bot check UID for a Specific value in UID

#

Cool

#

👍

#

So like this?

c.execute('SELECT UID from Profile where name = ?', (name,))
gusty spindle
#

should work

#

but why don't you try and see? ^^

orchid charm
#

Cause bot.run is not working

#

I am currently working on it

#

It worked this time, so time to see if that thing works

#

It gives me a error

#

"name" is not defined

#

@gusty spindle Do you know what i am doing incorrectly?

#

Wait think i fixed it

gusty spindle
#

hmm

orchid charm
#

@gusty spindle Yo do you know what everything in that thing means

#

I want it to look for the Author of the messages ID in Table Profile Column UID

dull scarab
#

are you using postgresql ?

orchid charm
#

No, SQlite3

dull scarab
#

For a discord bot?

orchid charm
#

Yeah

dull scarab
#

Hmm, haven't used sqlite. Most say postgresql works best with discord bots using asyncpg

orchid charm
#

I am using asyncio

dull scarab
#

That's what asyncpg is, an async api for postgres

gusty spindle
#

sqlite3 is good for beginners ^^

orchid charm
#

Anyways, am i having it search correctly

#
c.execute('SELECT UID from Profile where UID = ?', (ctx.message.author.id,))
    rows = c.fetchall()
    if (ctx.message.author.id,) not in rows:
        print('Profile Created!')
    else:
        print(ctx.message.author.name + ' You already have a Profile!')
#

I just want it to see if the Message authors ID is in Table Profile Column UID

#

And if it does it does something, and if it does not it does something else

dull scarab
#

Does it always print profile created?

#

Actually, nvm. I miss read your if

gusty spindle
#

you just need to do if ctx.message.author.id not in rows i think

#

oh wait i don't know

dull scarab
#

Isn't rows a list of rows

#

Not Ids

#

As in you have to loop through the rows and check the id column for each row?

orchid charm
#

This is my DB

dull scarab
#
c.execute('SELECT UID from Profile where UID = ?', (ctx.message.author.id,))
rows = c.fetchall()
for row in rows:
    if ctx.message.author.id != row["UID"]:
        print("Profile Created!")
    else:
        print("Already exists")```
#

Try looping over the rows

#

Actually, you're already checking if the id exists in your query.

#

Just check if rows is None or not

#

fetchall()

Fetches all (remaining) rows of a query result, returning a list. Note that the cursor’s arraysize attribute can affect the performance of this operation. An empty list is returned when no rows are available.

orchid charm
#

Oh

#

So i don't need c.execute('')

dull scarab
#

How will you check if the id is in the database if you don't query it?

orchid charm
#

¯_(ツ)_/¯

dull scarab
#
c.execute('SELECT UID from Profile where UID = ?', (ctx.message.author.id,))
rows = c.fetchall()
if not rows:
    print("Profile Created!")
else:
    print("Already exists")```
orchid charm
#

So i use that?

dull scarab
#

Well, like the method documentation says. It returns an empty list if theres no rows from the query. Your query is asking for a row with a specific ID, which I assume is a primary or atleast unique Key in your database

#

So if it finds a result, you'll only get 1 row, else an empty list. If it's empty you don't have any rows in your db with that Id, hence it doesn't exist

orchid charm
#

Well the thing you gave me worked

#

👍

#

I put my User ID into my Database and tested it

dull scarab
#

And you understand why?

orchid charm
#

Mhm, pretty sure i get it

dull scarab
orchid charm
#

How do i use a database in another folder

#

Sqlite 3

#

so sqlite3.connect

#

But i do the directory it is in instead of name?

#

Ah

#

Thought so

#

How do i grab DB info and post it in chat with the bot

#

How do i grab it

#

Example of doing that

orchid charm
#

@modest haven Well what if there are multiple of the same thing

#

Cause there is gonna be stuff like Weapon in there which multiple will be the same

#

Yeah

#

That is what i was saying

#

How exactly do i do that

#

🤔

#

@modest haven

#

Also wow took me like 10 seconds for the ping to send 🤔

#

That is what a Default DB looks like

#

For what i am gonna be doing

#

So it would need to find the Line with the matching UID then grab from that line IGN, and Weapon then set it to some variable

orchid charm
#

@modest haven So like this?

bot.c.execute('SELECT Weapon from Profile where UID = ?', (ctx.message.author.id))
orchid charm
#

Also what am i doing wrong with this
self.bot.c.execute('SELECT Weapon from Profile where UID = ?', (ctx.message.author.id))
I just want it to grab the line with your username and take from that line weapons

#
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 62, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "/Users/shanehawkins/Desktop/DiscordBot/cogs/member.py", line 30, in profile
    self.bot.c.execute('SELECT Weapon from Profile where UID = ?', (ctx.message.author.id))
ValueError: parameters are of unsupported type

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

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 886, in invoke
    yield from ctx.command.invoke(ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 514, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 71, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: parameters are of unsupported type
#

That is the Error when i use the command that it is linked to

#
@commands.command()
    async def profile(self, ctx):
        self.bot.c.execute('SELECT Weapon from Profile where UID = ?', (ctx.message.author.id))
        Weapon = self.bot.c.fetchone
        WeaponID = Weapon[0]
        await ctx.send('Favorite Weapon {}'.format(Weapon[0]))

This is the command

ionic pecan
#

i believe that you need to pass it an iterable of parameters

#

right now you pass it a single one

orchid charm
#

How do i do that Exactly

ionic pecan
#

you need to add a comma at the end of ctx.message.author.id to make it a tuple, e.g. (ctx.message.author.id,)

#

that's my guess, the traceback is not entirely obvious

orchid charm
#

Why do i keep forgetting to do that

ionic pecan
orchid charm
#

Ok new error

#
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 62, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "/Users/shanehawkins/Desktop/DiscordBot/cogs/member.py", line 32, in profile
    WeaponID = Weapon[0]
TypeError: 'builtin_function_or_method' object is not subscriptable

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

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 886, in invoke
    yield from ctx.command.invoke(ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 514, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 71, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'builtin_function_or_method' object is not subscriptable
#
    @commands.command()
    async def profile(self, ctx):
        self.bot.c.execute('SELECT Weapon from Profile where UID = ?', (ctx.message.author.id,))
        Weapon = self.bot.c.fetchone
        WeaponID = Weapon[0]
        await ctx.send('Favorite Weapon {}'.format(Weapon[0]))
#

That is what i get from the command now when i use it

#

The goal of this command is to Grab from the line that has your User ID in it whatever Weapons is

ionic pecan
#

fetchone is a method, not an attribute

orchid charm
#

So what do i do to fix

ionic pecan
#

you call it

orchid charm
#

Define "Calling it"

#

Just putting self.bot.c.fetchone[0]

ionic pecan
#

this should give you an idea ```py

def add(a: int, b: int):
... return a + b
...
add
<function add at 0x10cb7af28>
add(2, 3)
5

#

fetchone is a method on the cursor

orchid charm
#

🤔

#

So what am i supposed to do with that

#

@ionic pecan Also where do i put that

ionic pecan
#

nowhere
Basically, what you're doing is WeaponID = self.bot.c.fetchone[0]

#

and fetchone is a method

#

you want to access index 0 on the result of fetchone, not the function itself

orchid charm
#

O

#
    @commands.command()
    async def profile(self, ctx):
        self.bot.c.execute('SELECT Weapon from Profile where UID = ?', (ctx.message.author.id,))
        self.bot.c.fetchone
        await ctx.send('Favorite Weapon {}'.format())
#

@ionic pecan How do i access Index 0 then

#

Brain.exe has stopped working, its like 11:00PM PT

runic tundra
#

i think you want WeaponID = self.bot.c.fetchone()[0]

orchid charm
#

cool that actually worked

#

👍

#

Thanks Dom

runic tundra
#

so that calls fetchone, and then you access it with the [0]

#

just so you know what its doing

orchid charm
#

Now all i gotta do is figure out how to write like that

#

So i can write to the Weapon Cagetory that has the UID with my user id in it

runic tundra
#

is it an insert or an update you want to do

orchid charm
#

Update

#

I want to update Weapon from None to something else

runic tundra
#
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
#

thats the general syntax

#

i will let you figure it out

#

let me know if you need help

orchid charm
#

New error

#

I manually wrote to the Weapon Column Splattershot pro

#

Now if i use the command i get this

#
Ignoring exception in command profile:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 62, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "/Users/shanehawkins/Desktop/DiscordBot/cogs/member.py", line 31, in profile
    WeaponID = self.bot.c.fetchone()[0]
TypeError: 'NoneType' object is not subscriptable

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

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 886, in invoke
    yield from ctx.command.invoke(ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 514, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 71, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'NoneType' object is not subscriptable
#
    @commands.command()
    async def profile(self, ctx):
        self.bot.c.execute('SELECT Weapon from Profile where UID = ?', (ctx.message.author.id,))
        WeaponID = self.bot.c.fetchone()[0]
        await ctx.send('Favorite Weapon {}'.format(WeaponID))
ionic pecan
#

fetchone returns None

#

probably because it didn't find anything with the query

#

and you try to access None at index 0

runic tundra
#
result = self.bot.c.fetchone()
if(result != None):
    WeaponID = result [0]
else:
    #error case here
#

@orchid charm bit of error checking

orchid charm
#

So where do i put that

runic tundra
#

delete WeaponID = self.bot.c.fetchone()[0] and put that instead

#

im not sure what your error case is because idk what your program does

#

can just have it printing something for debug purposes

orchid charm
#

Error case here

#

What do i put in that

runic tundra
#

if you can't think of anyting put print("Error fetching weapon id result") or something

orchid charm
#

this Cog is for a Profile system

#

The user Writes information to the DB

#

Then when they type !profile the bot displays it in a embed

#

But for now no embeds

runic tundra
#

ok so in the case there is no weapon id result (database doesn't have one) what do you want it to do?

#

send a message? leave something blank?

#

that is what the error case is for

orchid charm
#

Unset

#

I want it to say instead of a Weapon Unset

#

@runic tundra

runic tundra
#

you want it to say that in the database?

orchid charm
#

Yeah

#

If there is nothing there it is Unset

runic tundra
#

ok so where i put #error case here put a sql update statement

#
self.bot.c.execute("UPDATE Profile SET Weapon='Weapon Unset' WHERE UID=?", (ctx.message.author.id,))
#

@orchid charm give that a go

orchid charm
#
    @commands.command()
    async def profile(self, ctx):
        self.bot.c.execute('SELECT Weapon from Profile where UID = ?', (ctx.message.author.id,))
        result = self.bot.c.fetchone()
        if(result != None):
            WeaponID = result [0]
        else:
            self.bot.c.execute("UPDATE Profile SET Weapon='Weapon Unset' WHERE UID=?", (ctx.message.author.id,))
        await ctx.send('Favorite Weapon {}'.format(WeaponID))
#
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 62, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "/Users/shanehawkins/Desktop/DiscordBot/cogs/member.py", line 36, in profile
    await ctx.send('Favorite Weapon {}'.format(WeaponID))
UnboundLocalError: local variable 'WeaponID' referenced before assignment

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

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 886, in invoke
    yield from ctx.command.invoke(ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 514, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 71, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnboundLocalError: local variable 'WeaponID' referenced before assignment
#

@runic tundra

runic tundra
#

add WeaponID='Weapon Unset' just above the the sql select statement

orchid charm
#

Which one, the else statement? @runic tundra

#

Ok did it

#

Now time to try it when i set the Weapon Value

runic tundra
#

na the first select statement

#

@orchid charm

orchid charm
#

? @runic tundra

runic tundra
#

what don't you understand

#

you asked where to put it i said above the select statement

orchid charm
#

Ah

#
    @commands.command()
    async def profile(self, ctx):
        WeaponID='Weapon Unset'
        self.bot.c.execute('SELECT Weapon from Profile where UID = ?', (ctx.message.author.id,))
        result = self.bot.c.fetchone()
        if(result != None):
            WeaponID = result [0]
        else:
            self.bot.c.execute("UPDATE Profile SET Weapon='Weapon Unset' WHERE UID=?", (ctx.message.author.id,))
        await ctx.send('Favorite Weapon {}'.format(WeaponID))
runic tundra
#

yeah that looks good

#

just need to test it

orchid charm
#

But the thing is i set the DB

runic tundra
#

you need to try one with a weapon set and one that has nothing

#

and see if it works

runic tundra
#

wait

#

urr ok so you need to do some debugging

#

give me a sec

#
    @commands.command()
    async def profile(self, ctx):
        WeaponID='Weapon Unset'
        self.bot.c.execute('SELECT Weapon from Profile where UID = ?', (ctx.message.author.id,))
        result = self.bot.c.fetchone()
        if(result != None):
            WeaponID = result[0]
        else:
            await ctx.send('UID from discord {}'.format(ctx.message.author.id))
            self.bot.c.execute("UPDATE Profile SET Weapon='Weapon Unset' WHERE UID=?", (ctx.message.author.id,))
        await ctx.send('Favorite Weapon {}'.format(WeaponID))
#

@orchid charm try that and see what it sends

#

if the userid matches the one in the database

orchid charm
#

So replace my command with that

#

@runic tundra

#

That is what i get now

runic tundra
#

the user ids are correct but the select is retuning None

#

i gotta go do stuff so i cant help right now hopefully someone else can help

orchid charm
#

Also when Weapon is Null it says None

#

Ok well it actually worked this time

#

I reset my DB to default and it functioned Properly

orchid charm
#

Ok, new Question
How do i set Names value to argument

#

It is Currently Null

#

So how do i set it to a Argument

gusty spindle
#

@orchid charm that should be very easy to find in a google search or on the sqlite3 docs

orchid charm
#

What would i even search, How to Update Table Column where something exists?

gusty spindle
#

yup

#

specify sqlite3 in there though

orchid charm
#
        self.bot.c.execute("UPDATE Profile SET FC=? WHERE UID=?", (arg1, arg2, arg3, ctx.message.author.id,))
#

How do i make it SET FC= to Arg1, Arg2, and Arg3

ornate abyss
#

That doesnt make sense

orchid charm
#

Ok just did a Different method

ornate abyss
#

?

orchid charm
#

3 Different Columns

#

Each one is set to the specified arg

ornate abyss
#

Yea, thatll work

orchid charm
#

FC1 is set to Arg1, FC2 is set to Arg 2, ect.

#

How do i make it require it to be numbers

#

And how do i make it require each arg to be 4 Characters long

ornate abyss
#

Youd do that with python

orchid charm
#
@bot.event
async def on_message():
    self.bot.c.execute('UPDATE Profile SET XP=XP+5 WHERE UID=?', (ctx.message.author.id,))
    self.bot.conn.commit()
#
Ignoring exception in on_message
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 224, in _run_event
    yield from coro(*args, **kwargs)
TypeError: on_message() takes 0 positional arguments but 1 was given
#

What am i doing wrong

steel slate
#

what do you think you are doing?

#

on_message() takes no arguments and apparently on line 224 of client.py you called it with an argument

#

you forgot to do on_message(self):

orchid charm
#

Ohhh

steel slate
orchid charm
#

This was a Database question too so i put it in here just incase it had to do with my DB, also incase i do something wrong with my DB

viral crag
#

it's on_message(message)

steel slate
#

ok, I was joking anyway

orchid charm
#
@bot.event
async def on_message(message):
    async with aiosqlite.connect('profiles.db') as db:
        await db.execute('UPDATE PROFILE SET XP=XP+5 where UID = {}'.format(message.author.id))

What exactly am i doing wrong, it is not adding 5 to XP
XP by default is 0

#

Also using aiosqlite now

#

NVM, i forgot to commit

#

Ok, now i need to figure out how to make the bot check if the users XP is 100, and if it is add 1 to level

#

Or to be more clear, whenever the users xp is a multiple of 100 it adds one level

#

So i become LV 1 at 100 EXP, 2 at 200 EXP ect.

gusty spindle
#

just make their level equal to their EXP // 100

orchid charm
#

Oh?

gusty spindle
#

well

#

EXP // 100 + 1

#

60 // 100 == 0, 632 // 100 == 6 etc

orchid charm
#

so i Select EXP

#

Then how would i check if its a multiple of 100

gusty spindle
#

can you not use google?

orchid charm
#

¯_(ツ)_/¯

gusty spindle
#

"python number has multiple" should give you a result

orchid charm
#
@bot.event
async def on_message(message):
    bot.db = await aiosqlite.connect('profiles.db')
    await bot.db.execute('UPDATE PROFILE SET XP=XP+5 where UID = {}'.format(message.author.id))
    await bot.db.commit()
    await bot.db.execute('SELECT XP from Profile where UID = ?', (message.author.id,))
    result = bot.db.fetchone()
    if (result x%100==0):
        await bot.send('You levelled up!') 
#

@gusty spindle So like that?

gusty spindle
#

i suppose, yup ^^

orchid charm
#
@bot.event
async def on_message(message):
    bot.db = await aiosqlite.connect('profiles.db')
    await bot.db.execute('UPDATE PROFILE SET XP=XP+5 where UID = {}'.format(message.author.id))
    await bot.db.commit()
    await bot.db.execute('SELECT XP from Profile where UID = ?', (message.author.id,))
    result = bot.db.fetchone()
    if (result == x%100==0):
        await bot.send('You levelled up!') 
#
Ignoring exception in on_message
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 224, in _run_event
    yield from coro(*args, **kwargs)
  File "./main.py", line 37, in on_message
    bot.db = await aiosqlite.connect('profiles.db')
TypeError: object Connection can't be used in 'await' expression
#

@gusty spindle That is what happens now

#

It worked before i added result

gusty spindle
#

i have no idea how to use aiosqlite but try removing the await

orchid charm
#

I am gonna migrate to PostgreSQL

#

@gusty spindle

@bot.event
async def on_message(message):
        await db.execute('UPDATE PROFILE SET XP=XP+5 where UID = {}'.format(message.author.id))
        await db.commit()
        await db.execute('SELECT XP from Profile where UID = ?', (message.author.id,))
        row = await bot.db.fetchone()
    if (row == x%100==0):
        await bot.send('You levelled up!') 
#
  File "./main.py", line 43
    if (row == x%100==0):
                        ^
IndentationError: unindent does not match any outer indentation level
#

What is wrong

#

Or wait, i think i know

#

Nvm

#

What does it mean exactly

gusty spindle
#

i have no idea how to use postgresql databases, sorry

orchid charm
#

Oof

#

What databases do you know how to use then XD

viral crag
#

@orchid charm Something wrong with your credentials object

#

Make sure that's the correct way to do what you're doing by reading the docs

orchid charm
#
credentials = {
    "user": "(Not Showing You)",
    "password": "(Not Showing You)",
    "database": "profiles.db",
    "host:": "127.0.0.1",
    }

@viral crag

#

Those are my Credentials

viral crag
#

So are you supposed to pass a dict into that function?

orchid charm
#

?

viral crag
#

I would have expected a connection url

orchid charm
#

a connection url?

viral crag
#

A connection url.

orchid charm
#

How do i get one

viral crag
#

You make one

orchid charm
#

How XD

viral crag
#

It'll be something like uhh

#

"aiopg://username:password@host:port/database"

#

Something like that

orchid charm
#

O

viral crag
#

Unless you just meant to unpack that dict

orchid charm
#

I just wanted that dict to be my credentials

viral crag
#

In which case you needed **credentials

orchid charm
#

Ohh

#

so ** in front of credentials

viral crag
#

In the function call

#

Basically that unpacks the dict into keyword arguments for the function

orchid charm
#

Ohh

#

TypeError: connect() got an unexpected keyword argument 'host:'

#

@viral crag So i just remove host?

#

Ok, it worked

#

but password authentication failed

viral crag
#

No, don't remove host, remove the colon in the host key

orchid charm
#

o

viral crag
#

Alright well, wrong username and password :P

orchid charm
#

How do i change it then

#

@viral crag

#

Cause i don't know what i set it to

viral crag
#

You'll have to look that up

#

I don't know the reset process and I'm going to bed anyway

orchid charm
#

Ya know what, fuck postgres

#

Too stupidly complicated to set up on my mac

gusty spindle
#

by the way you seem to be asking a lot of questions which you can answer with google.

lyric stag
#

postgres is dead simple to set up on mac

#

probably the easiest of all the OSs

orchid charm
#

@lyric stag Also, sqlite3 was easier to install 👍

#

Also i ran Nadekos before this so i already was running sqlite

lyric stag
#

check out that link I posted, it's really easy

orchid charm
#

Hopefully it works this time, i might have to uninstall alot of stuff cause of it conflicting with everything

#

Also wow i am suprised anyone is up rn

#

Its Midnight (in PT)

lyric stag
#

i'm moving out of my uni for the summer

orchid charm
#

?

#

Also how do i connect to a postgresql server with discord.py

hollow tiger
#

aight i'm getting started on mongodb cuz i need that json like shit

orchid charm
#

Json db?

#

I did not even know that was possible

hollow tiger
#

yea lol someone told me about it in this server

#

I think it's perfect for what I'm using

orchid charm
#

Json would be for a smaller bot

hollow tiger
#

but I'm completely new to databases

orchid charm
#

Sqlite would work better probably

viral crag
#

I would advise not using mongo

#

Try rethinkdb

orchid charm
#

^

hollow tiger
#

rethinkdb?

viral crag
#

Also, you're wrong @orchid charm

hollow tiger
#

lmao

orchid charm
#

Nom

hollow tiger
#

oof

orchid charm
#

Sqlite best DB ever confirmed 2018

viral crag
#

JSON files are no good for larger applications, yes

#

These things are far from JSON files

orchid charm
#

Don't you need to apply everything at once to a json file?