#databases

1 messages · Page 47 of 1

restive violet
#

through hashing

#

very important!!!

hollow tiger
#

that's true

restive violet
#

it's O(1) to get an item

#

in a list it's O(n)

hollow tiger
#

O(n)?

#

what does that mean?

restive violet
#

big o notation

#

it means you scale n times with however big input list size is to find result

#

so len 5 is 5 times going through

#

len 50 is 50 times.. etc

#

because you have to iterate

#

https://en.wikipedia.org/wiki/Hash_table @hollow tiger it's called a hash table (or rly map with a dict) outside of python

In computing, a hash table (hash map) is a data structure that implements an associative array abstract data type, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value ca...

#

sorry if i went too much, but hashing is extremely important concept in CS/interviews/efficiency. 😃

hollow tiger
#

sry i was doing smth

hollow tiger
#

can i store a dictionary into a column for a table? :o

dull scarab
#

Not that I'm aware of, but you could break it up into

dict name/ id | key | value```
hollow tiger
#

uh

#

cuz i'm doing a dict in a dict

#

and i have to have the dict in the table

#

would json.loads() and json.dumps()ing everything work?

#

not recommended rite

dull scarab
#

will the structure always look the same?

hollow tiger
#

no

dull scarab
#
"Testing": {
    "apps": {},
    "prefix": [
        "/"
    ],
    "members": {}
}```
hollow tiger
#

well that's the base structure

#

so i have "testing" as a table and "apps", "prefix", and "members" as columns

dull scarab
#

will the apps dict have more dicts?

hollow tiger
#

yes

#

dicts in dicts

#

and lists

dull scarab
#

Well, I guess you could create a table with those keys and parse jsonstring to it

#

I can't tell you if it's bad or good unfortunately

hollow tiger
#

yea so that's why I was kinda hesitant in choosing sql

#

i'm not sure about hte best way to store the data

dull scarab
#

There's probably some relational setup you could do to make it work, but I don't know too much about em 🤷🏾

hollow tiger
#

i'm really inexperienced with databases

#

i only use a big json file

#

:(

dull scarab
#

Like have a dict and a list table

#

And have them reference rows within the same table if their nesting dicts/lists

#

and a 3rd table which is just your app name having references to dicts/lists/values

#

But again, I don't know this stuff :P

hollow tiger
#

that's

#

so hard to manage lmao

#

should i just do multiple json files in a folder?

#

emulate a db

#

instead of one big json file

ionic pecan
#

Postgres has a JSONB column type which supports storing JSON

#

There‘s also NoSQL databases for storing it directly

hollow tiger
#

ppl told me not to use nosql

ionic pecan
#

It really depends on what you store in that members dict.

#

Is it a mapping of user ID -> some values that don‘t change?

midnight verge
#

@hollow tiger if your data is structured, there is no reason to use NoSQL

hollow tiger
#

well

#

members could mean py "members": { "TheDutchSwagBoi": {}, "Many_Roblox2": {}, "gagi12": {} }

#

but sometimes within those can be multiple dicts

#
            "Voidlegend": {
                "moderator": [
                    "Voidlegend#5150 and JohnApocalypse",
                    "13",
                    "USA and UTC 05:00",
                    "Yes, and it will help because I won't have to be taught as many commands",
                    "By ridding it of hackers and jerks",
                    "I work hard and  won't give up"
                ]
            },```
#

sometimes there's multiple items within that dict tho

midnight verge
#

try to figure out a relational structure

hollow tiger
#

uh

#

whats that?

pallid schooner
#

@fair laurel I have in mind to store user data to a db

fair laurel
#

Well, that is what relational databases do

pallid schooner
#

Hmm

#

Can you suggest a tutorial

fair laurel
#

Do you have any experience with SQL yet?

pallid schooner
#

@fair laurel Yes I do, thanks btw

tacit dagger
#

hey, how can i select the same row in two different fields in sqlite?

fair laurel
#

What do you mean?

#

Selecting two columns from the same row?

tacit dagger
#

yes

fair laurel
#
SELECT Column1, Column2
FROM MyTable
WHERE MyCondition = True
tacit dagger
#

👍🏿

#

for MyCondition part i should write WHERE rowID = ?

#

not just 1 row, multiple rows

fair laurel
#

If you have multiple rows, you could do WHERE rowID IN ("1", "2", "3")

tacit dagger
#

how about when there are many rows like 1000, i can't write all of them like this: ("1", "2", "3")

#

and i want to select them seperately not all at once

patent glen
#

then use a loop

#

i'm not sure why separately vs all at once (or in batches of 100 or whatever amount works) matters though

tacit dagger
#

can you check this out? maybe this is better explained

patent glen
#

i'm confused

#

where are multiple rows coming in

#

your question description implies you want one row

#

and WHERE rowID = ? works fine for that

#

what did you mean by "not just 1 row, multiple rows"

#

and I don't understand what you mean by increment it automatically at all - what are you incrementing

tacit dagger
#

i mean how should i tell sqlite to do this, when row 1 in field 1 is clicked, row 1 from field 2 being fetched

patent glen
#

and honestly if you want to select the corresponding english word for a given esperanto word, it probably makes more sense to select by the esperanto word instead of rowid

#

ok what does this have to do with multiple row

#

if each query is for just one row

#

look i can explain how to do what it looks like you're trying to do, but your actual questions are all over the place with stuff like "not just 1 row, multiple row" and "increment it automatically" that don't seem to have anything to do with that

tacit dagger
#

oh i didn't know i could fetch english column according to Esperanto word. how should i do that?

#

i've read many tutorials but couldn't figure it out

patent glen
#

"SELECT English FROM Words WHERE Esperanto = ?" - and you have to get the word from the tkinter listbox somehow

#

what does self.listbox.curselection return

tacit dagger
#

what it returns is that it should write the English word which is fetched and write it in textbox

patent glen
#

Try something like this: python def enter_meaning(self, tag): for index in self.listbox.curselection(): esperanto = self.listbox.get(index) results = self.cur.execute("SELECT English FROM Words WHERE Esperanto = ?", (esperanto)) for row in results: self.textbox.insert(tk.END, row)

#

(I wouldn't trust the rowid to match the index anyway, tbh)

tacit dagger
#

it works 😄

patent glen
#

[though might be better to check len(self.listbox.curselection()) == 1 etc, since this won't actually do anything useful with multiple items]

tacit dagger
#

when someone else for example runs the app on their pc, is it garanteed that the rows come in this order? or i should order them to ensure that?

patent glen
#

you should probably avoid caring about the order at all

#

or do you mean for having the list sorted alphabetically?

tacit dagger
#

now the list is alphabetically ordered, cuz i wrote alphabetically

#

in db

patent glen
#

I don't think it's guaranteed to stay stable, you should add an order by clause to your select esperanto query

#

i mean, if you never make any further changes to this db file it won't matter but

#

it's just good practice, if you had a real database with rows being deleted and inserted all the time you'd need it

tacit dagger
#

no i won't make changes to db

patent glen
#

in general it's not good practice to rely on the order of rows from a query with no order clause

tacit dagger
#

i wrote this self.cur.execute('SELECT Esperanto FROM Words ORDER BY rowID') now it is ordered 😃

patent glen
#

...lol

tacit dagger
#

no :/

patent glen
#

i think it'll always be ordered by rowid, for sqlite, but again if you were using this database for real there's no guarantee that the rowid would stay related to the order you want

#

honestly i'd just order by esperanto

tacit dagger
#

oh. i got it.

#

thanks a bunch. i've been stuck for God knows how many years xD

orchid charm
#

How to make SQLite not Give a Tuple in a fetchall

#

Like, all parts of it are ('SomeValue',),

#

How to make it not do that

torn sphinx
#

you could do fetchone

#

or fetchrow

#

it depends on the situation

orchid charm
#
while Give in Item:

Item is a List Fetchall

#

But everything in it comes in this format
('SomeValue',)

#

@torn sphinx So what do you think could fix this Issue, cause i have no idea. I am trying to make it Turn Give into something else if it is in Item

torn sphinx
#

I have no idea what your code is

#

No idea what your row looks like

#

No idea what you are doing

#

How do you expect me to help?

#

Oh, I see. You want to iterate over a tuple ('SomeValue', )

#

you can do that

#
>>> tup = (5,)
>>> for i in tup:
    print(i)

5
#

look up python tuples

orchid charm
#

Yeah.
If Give is Grizzco Employee
And a item in Item is ('Grizzco Employee',)
They are not equal

#

Which means this won't work

torn sphinx
#

oh

#

It's a tuple

#

I said look up python tples

#

they are nearly* identical to lists (immutable whilst lists are mutable)

#

Also, again

#

send all code

orchid charm
#

What else am i not showing you

#

The Await is just a Message the bot sends if it can't find the users ID in the Database

torn sphinx
#

What does your database look like? (e.g. is it ```
id | name | something_else |

5 | hi | some |

#

and what is your command

orchid charm
torn sphinx
#

I can't help if I don't know what you are doing btw

#

What's your SQL statement

#

And paste all your code in a codeblock

orchid charm
#

Its just a RNG Shop

#

But i am trying to make it not give the user the same item multiple times

#

Also, What do you mean by SQL Statement

#

All SQL stuff that goes with the command is in the command

torn sphinx
#

So give me the command

#

I can't help until I see it

orchid charm
#

.metro vending-machine

torn sphinx
#

no

#

the code

orchid charm
#

I showed you it?

torn sphinx
#

no your didn't

#

yougave snippets

orchid charm
#

Also, this is a Database Related Problem. It just happens to be in Discord.py This would not change any Database stuff anyways

orchid charm
#

@torn sphinx So why does the Fetchall Give Tuples in the first place

torn sphinx
#

because you can't modify the data

#

is immutable

#

tuples are immutable

#

this is basic python

torn sphinx
#

hello guys

#

is there a way

#

to delete only a certain field in my table? I'm using sqlite syntax with python module

#

I have like a countdown program sending alerts each X hours until deadline is reached for a certain project name

#

and once the reminder message has been sent to the user i'd like to delete this alert in my line

#

so only this alert

#

which query would correspond to my needs?

#

@torn sphinx

torn sphinx
#

Im not sure but you can add another column to show if the job is done preferably a boolean value. Then you can delete it with where

#

it's like

#

imagine having

#
id | name | something_else |
----------------------------
5  | hi   | some           |```
#

like DELETE FROM users WHERE mail_sent = 1

#

and wanting to delete only some

#

see what imean

#

but keeping the id and name

#

Yep i do add a fourth column change the respecive column after you do your job

#

then alter the column you wanna change

#

or just change the column you wanna change would be way simpler

#

UPDATE x SET something_else = 'lol'

#

of course add a where according to your needs

#

I'd probably do the 4 column thingie because i like having neat stuff

#

oh pretty good

#

like when the alert is sent

#

change the datetime object in the column by a 0

#

for examplre

#

to say it's done

#

UPDATE users SET something_else = 0 WHERE alert_available = 0 would be my way

#

But you can also go with UPDATE users SET something_else = 0 WHERE user_id = variable

#

and put execution after the alert function

broken linden
#

Deleting only single fields in tables is not the point of tables

#

You either delete a row update a field or add a row

#

Or get a row/field

torn sphinx
#

He is updating a field

#

Turns datetime into 0

#

But id really prefer using a tag for active/alert available column

broken linden
#

to update a row do
UPDATE table_name SET myfiield = 0 WHERE awesome_id=35545;

cosmic gulch
#

Hi im getting started with mongoDB

#

by defualt you have a db

#

test

#

and i inserted some keys and values

#

but how would i see them

#

using python

#

like make a get request to the db?

#

if I dont make sense

#

i apologize

viral crag
#

You need a mongodb library

#

Eg pymongo

cosmic gulch
#

gg

#

i Have questions about MongoDb but im pretty sure if i ask them people will get annoyed

#

😄

vestal apex
#

that's an odd thing to be sure about

steady loom
#

Hey there

#

Oof internet connection issues

#

Anyway

#

I'm trying to set up a Google firebase database

#

Except I can't read the database

#

I've made sure that I have the perms, and can write and everything

#

But for some reason, reading won't work

#

I'm on mobile atm, but will post code etc later

#

Just asking in case this is a common problem

orchid charm
#

In SQLite how to get the newest item added?

#

NVM Figured it out

#
        cur.execute('SELECT SplatNet2 FROM Splatoon2')
        Query = cur.fetchall()
        C = len(Query) - 1
steady loom
#

re google firebase - i managed to thoroughly search google and found my answer

patent glen
#

@orchid charm I don't think sqlite guarantees that rows come back in insertion order, you should include a timestamp column and order by it if you need it.

ornate abyss
#

Or order by primary key descending and limit 1

patent glen
#

sure, that'd work if there's a primary key that's increased with each new row

orchid charm
#

Ok this is a d.py rewrite question

#

and a Database question

#

This is not adding the information to the database when it needs to

#

And it is not sending to multiple channels like it is supposed to

#

It is only sending to the first one

#

Actually it just decided to send to the second one, but it took like a minute or so to do. and i figured out some of my problem

#

I accidentally put the asnycio.sleep in the wrong place

#

But it still is not updating the database

#

Wait NVM it just worked too?

ionic pecan
#

i have no idea what you mean with your question. not adding the information to the database when it needs to, well, when is it supposed to do that? I see two hundred conditions here and have no idea what's going on, and I have no clue what you're trying to accomplish with the now.minute == 12 either, is this some sort of cron clone?

gusty spindle
orchid charm
#

Possibly

#

I figured the thing out

#

and it now works

gusty spindle
#

fair

orchid charm
#

What i do is i do
C = len(Query) - 1

#

Cause the final Result is almost always the newest

gusty spindle
#

yes but doesnt that mean youre loading your entire database every time?

orchid charm
#

I am going to be doing other stuff with the fetchall

cosmic gulch
#

need help with pymongo

#

I added data in the DB

#

but i want to print the info in console

torn sphinx
#

@cosmic gulch Can i get help

vestal apex
#

to both of you:

#

bot.tags['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.

wind nexus
#

Hi

#

im working on making a backend with flask and pyodbc (Azure Sql server)
does this seem like a good way to add a new user :

def Create(User,Email,pwHash):
    """Create New User, Return 422 if username or email in use!"""
    try:
        cursor.execute("SELECT count(*) FROM Accounts WHERE Username=? OR Email=?",(User,Email)) # Check Username or Email is in use
        data=cursor.fetchone()[0]
    except pyodbc.Error:
        return "DB Error"

    if data==0:
        # NOTE: When including any input from user in the query, use ? Mapping. in the function below, First ? is User, second ? is pwHash and third ? is Email
        try:
            cursor.execute("INSERT INTO Accounts(Username, PasswordHash, Email) values (?, ?, ?)",(User,pwHash,Email))
            cursor.commit() # Used to "save" changes to database.
        except pyodbc.Error:
            return "DB Error"
        return "200"
    else:
        return "422"
#

also, what would be a "best practice" way of using return with pyodbc and flask, i dont think that my current way of returning those number codes is a good idea

dull scarab
#

Can you not use the placeholder ? in place for tablenames in sqlite

wind nexus
#

why

dull scarab
#
async def execute(query, *args):
    async with aiosqlite.connect(db_path) as db:
        await db.execute(query, *args)
        await db.commit()```

```py
    @commands.command()
    async def cleartable(self, ctx, table:str):
        await db.execute("DELETE FROM ?;", table)```

Traceback (most recent call last):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 60, in wrapped
ret = await coro(*args, **kwargs)
File "E:\Python\Bot Projects\boiletplate\cogs\moderation.py", line 27, in cleartable
await db.execute("DELETE FROM ?;", table)
File "E:\Python\Bot Projects\boiletplate\utils\database_manager.py", line 25, in execute
await db.execute(query, *args)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiosqlite\core.py", line 200, in execute
cursor = await self._execute(self._conn.execute, sql, parameters)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiosqlite\core.py", line 159, in _execute
raise result
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiosqlite\core.py", line 136, in run
result = fn()
sqlite3.OperationalError: near "?": syntax error```

#

wait, old traceback

#

(i tried with delete * from ? earlier )

wind nexus
#

the code above is working, and i understood that using the placeholders is more secure than passing raw values to the query

#

oh wait

#

facepalm

#

i misread that question

dull scarab
#

oh wait, i have to pass table as a list

wind nexus
#

i thought you asked whyt im using ? in the query

dull scarab
#

no no

wind nexus
#

Too much coffee

dull scarab
#

GWqlabsNotLikeNoot I'm confuss

#

You simply can not use placeholders for column or table names. welp

#
await db.execute(f"DELETE FROM {table};")``` Solved it, so i guess `?` is just not allowed for column/table names
wind nexus
#

is there a limited list of table names that you want to be able to be passed to funtion

#

or is that a variable that user types in

dull scarab
#

It's an utility method for me to wipe it as i test

#

So i mean, no security issues

#

But I was just bamboozled that i couldn't use the ? placeholder

#

dont mind me, wrong discord

gusty spindle
#

ooh, since when did aiosqlite exist? :o

deft badge
#

Since the 25th May 2017

gusty spindle
#

very well

cosmic gulch
#

Hi

#

i was wondering how i can host my REST on ubuntu vps

#

i made a little rest

#

that connects to mongoDB mlab

#

but it only hosts on localhost:5000

#

i just want to listen to a port

#

not bound to localhost

ionic pecan
#

you make it listen on all interfaces i'd guess

#

that is, make it bind to the host 0.0.0.0

#

and allow 5000 through your firewall

#

but i have never used mongodb, god knows if that's secure

cosmic gulch
#

@ionic pecan how do i bind it to 0.0.0.0

#

pymongo automatically binds to

ionic pecan
#

that's something that you'll have to consult your documentation for, i don't use mongodb

cosmic gulch
#

Ah

#

ok

ionic pecan
#

pymongo is a client library as far as i'm aware

olive geyser
#

hey

#

hello

#

how can u select data from mysql workbench? from 2 different tables

#

<@&462664414559076362>

viral crag
#

is that really something worth mentioning 6,000 people over?

#

you want a JOIN query

#

you'll have to look that up yourself

olive geyser
#

thank you

orchid charm
#

How to get the greatest value in a DB?

#

SQLite

torn sphinx
#
SELECT max(column) from table_name``` ?
orchid charm
#

Cause i am trying to get the "Newest" Row in a Database

#

And a idea i had was to put a number row

#

SELECT * FROM * WHERE max(*)

#

So would that work?

torn sphinx
#

uh

#

explain better what youre trying to do again

orchid charm
#

So i have a Script that needs to find the newest Row

#

And a idea i had was to have a Number Column on each row

torn sphinx
#

so

#

the highest number is the newest row/

orchid charm
#

Then get a Specific Value from the row where a specific column has the greatest value

#

Example:
Insert - Item
1 - XXX
2 - YYY
3 - ZZZ

#

I would want to get ZZZ

#

Cause 3 is greater than the rest

torn sphinx
#

you can do sql SELECT * FROM table_name ORDER BY column_name DESC LIMIT 1;

#

i think that works

orchid charm
#

Oh?

#

Ok

torn sphinx
#

thatll give the first row sorted by column_name

orchid charm
#

cur.execute('SELECT Item FROM Splatoon2 ORDER BY Index DESC LIMIT 1;')

#

sqlite3.OperationalError: near "Index": syntax error

torn sphinx
#

what happens if you put back ticks around Index

orchid charm
#

?

torn sphinx
#
SELECT Item FROM Splatoon2 ORDER BY `Index` DESC LIMIT 1;```
orchid charm
#

That gives the one with the lowest value

#

('XXX',)

torn sphinx
#

change DESC TO ASC

orchid charm
#

cur.execute("SELECT SplatNet2 FROM Splatoon2 ORDER BY 'Index' ASC LIMIT 1;")

#

('XXX',)

torn sphinx
#

use backticks

#

not quotation marks

orchid charm
#

?

torn sphinx
#

literally you couldve copy pasted my code

orchid charm
#

cur.execute('SELECT SplatNet2 FROM Splatoon2 ORDER BY 'Index' ASC LIMIT 1;')

#

So like that?

torn sphinx
#

copy

#

what

orchid charm
#

It gives a invalid syntax

torn sphinx
#

i

#

wrote

#

above

orchid charm
#

Oh i thought you were using '

torn sphinx
#

no sir

orchid charm
#

Well hopefully this works

#

Ok it might have worked

#

It gives White V-Neck Tee

#

Which is Number 9

#

Whats with that?

#

The "Index" Goes from 0 to like 40+

torn sphinx
#

huh thats weird lol

orchid charm
#
conn = sqlite3.connect('/Users/shanehawkins/Desktop/Bot/bot/tartar.db', timeout=8)
cur = conn.cursor()
cur.execute("SELECT SplatNet2 FROM Splatoon2 ORDER BY `Index` DESC LIMIT 1;")
X = cur.fetchone()
cur.close()
print(X)
torn sphinx
#

i dont see why it shouldnt work 🤔

orchid charm
#

Is it cause the type of Index is Text?

torn sphinx
#

oh lol maybe

#

change it to int or bigint or something

orchid charm
#

Hopefully this works cause i got 16 mins left to figure this out

#

I think it works now!

#

Thanks

torn sphinx
#

np

mellow kindle
#

anyone know if there's any way to add a couchdb instance as a data source in PyCharm?

orchid charm
#

So this loop is not Inserting like it is supposed to, why?

olive geyser
#

hey

#

someone to help?

dull scarab
#

bot.tags.get 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.

olive geyser
#

im a bit noob, but here i haave 4 screenshots from ym database,. At table "Horarios" we can find a "cod_dia" that is on table "horariosdia", then we can find "cod_hora" that is on table "horarioshora" and then we have "cod_disciplina" that we can find on table "disciplinas"

#

but with the subjects (that means disciplinas on my project)

#

can u guys help me with this?

#

<@&462664414559076362>

viral crag
#

THE ELUSIVE EVERYONE MENTION

#

mentioning 6,000 people for help with your problem

#

you still haven't really asked a question though

olive geyser
#

i need to select data from 3 different table and build something like on the last picture

#

can someone help me?

viral crag
#

I mean, can they? Maybe

#

But you're not asking a question

#

like

#

You're asking for help, but you're not being specific about the help that you need

#

@olive geyser Are you looking for a designer? Or do you not know where to start? Do you have any code?

#

We need specifics

olive geyser
#

i just have the database

#

i need someone that can help me design this

#

i need to select data from 4 different tables

#

i dont know where to start

#

i need to build a "school schedule"

viral crag
#

OK, well you have a few options

#

First of all though, you need to decide what medium you'll use to generate the table

#

you have Pillow, if you want to generate an image directly

#

You could generate an Excel spreadsheet

#

or, and this is the preferred method, you could generate some HTML that displays it

olive geyser
#

i want html that displays it

viral crag
#

OK

#

You're already using MySQL, so you'll need to look into the SQL modules available for Python

#

I would also suggest Jinja2, which will let you write a HTML template - much easier and cleaner than outright generating HTML yourself

#

You're also going to need to know HTML and CSS

olive geyser
#

but my question is

#

how can u select data from the same database but from different tables

viral crag
#

You need to structure your database in a way that allows for that

olive geyser
#

well i did

viral crag
#

OK

#

then you need to learn how to use JOIN queries

#

INNER JOIN/OUTER JOIN/JOIN

#

they're a bit tricky, but that is what you need

olive geyser
#

i dont knwo how to join, i already search on google but i did not understand

viral crag
#

unfortunately, not many people do know how to use those queries

#

because, well, they're tricky

#

It seems like your dataset is small though, if it's small enough to fit into memory then you can probably just get all the relevant information in separate queries, and filter it in python

olive geyser
#

my brain has stopped

viral crag
#

if you don't really understand, then you might need more experience before tackling this

clear delta
#

If you're more of a visual learner, this might help

#

@olive geyser

olive geyser
#

thank you

clear delta
#

There are a ton of other 'visual' SQL join explainers out there, try them till one of them clicks

dense basin
#

alright, it only happens when using an in-memory database

quartz mortar
#

are there any books or resources on how to work with databases for beginners

ionic pecan
#

with databases in general or with databases from python?

quartz mortar
#

from python

ionic pecan
#

Sqlalchemy has a great tutorial if you‘re using an SQL database

#

If you want to keep at rather raw SQL, there‘s libraries for that, sqlite3 is built-in and works with sqlite, and you can look for a database adapter for the others

quartz mortar
#

thanks! bookmarked

#

im trying to work on a project where theres data stored in a database about how many reportable incidents happen under a certain medical staff, typically a nurse, and want to have that data graphable

#

its kinda hard bc i also need to include recent procedures theyve had and drugs theyve taken from a database bc those could be contributing factors

ember dew
#

Anyone got an opinion on formatting incredibly long SQLAlchemy query chains?

versed coyote
#

No

ember dew
#

Clearly lol

versed coyote
#

Yes

wind nexus
#

is anyone here atm?

#

does this look like a good database for user information and login etc?

#

@viral crag you seem to know about databases?

ionic pecan
#

password salt?

wind nexus
#

im using argon 2 and the output from it sort of has the salt and hash in same string

ionic pecan
#

ah

wind nexus
#

im wondering if the relations etc are used in a proper way and if the database looks like someone could maintain it after i get hit by a bus

#

or does this look better

wind nexus
#
class User(db.Model, UserMixin):
    __tablename__ = "User"
    user_id = db.Column(INTEGER, primary_key=True)
    email = db.Column(VARCHAR(255), index=True, unique=True)
    password_hash = db.Column(VARCHAR(255))
    activated = db.Column(BIT)
    skills = db.relationship('Skills', backref='user', lazy=True)
    details = db.relationship('Details',uselist=False,back_populates='user')

class Details(db.Model):
    __tablename__ = "Details"
    id = db.Column(INTEGER, primary_key=True)
    first_name = db.Column(VARCHAR(255))
    last_name = db.Column(VARCHAR(255))
    trial = db.Column(BIT)
    subscription = db.Column(BIT)
    signup_date = db.Column(DATETIME)
    subscription_id = db.Column(VARCHAR(255))
    profile_picture = db.Column(VARCHAR(255))
    user_id = db.Column(INTEGER, db.ForeignKey('user.user_id'))
    user = db.relationship("User", back_populates="details")

class Skills(db.Model):
    __tablename__ = "Skills"
    id = db.Column(INTEGER, primary_key=True)
    skill_name = db.Column(VARCHAR(255))
    user_id = db.Column(INTEGER, db.ForeignKey('user.user_id'))

does this look like a proper use of one to one and one to many?

#

@near cradle

near cradle
#

that looks promising, yes

wind nexus
#

hmm its giving me

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship User.skills - there are no foreign keys linking these tables.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
wind nexus
#

YAAY i managed to make it work with the azure sql server

wind nexus
#

so, for my dev enviroment, i wanted a quick way to clear the database, but didnt want to do it accidentally, so

print("/////////////////////////////")
print("// WARNING WARNING WARNING //")
print("/////////////////////////////")
print(" ")
print("THIS WILL RESET THE ENTIRE DATABASE!")
print("!!ALL DATA WILL BE LOST FOREVER!!")
print("IF THIS IS WHAT YOU WANT, TYPE 'CONFIRM'")
conf = input("Do you want to reset the database?> ")

if (conf == "CONFIRM"):
    print("Importing database...")
    from app import db
    print("Import Completed!")
    print("These tables will be removed:")
    meta = db.metadata
    for table in reversed(meta.sorted_tables):
        print(table)
    print("Do you want to proceed?")
    print("There is no turning back after this! All data WILL BE LOST!")
    stage2 = input("Type 'YES' to wipe everything: ")
    if(stage2 == "YES"):
        print("Starting db.reflect()...")
        # db.reflect()
        print("Reflection Done, Wiping starts now")
        print("ERASING ALL DATA...")
        # db.drop_all()
        print("Database has been erased!")
    else:
        print("CANCELED")
else:
    print("CANCELED")
#

commented out until i want to actually have the script functioning

deft badge
#

Nice

clever viper
#

Seems like in the time that you could open the script, uncomment it, and then run it, you could just wipe the DB?

wind nexus
#

no need to uncomment anymore, just uncommented while i tested the safeguards

clever viper
#

Ohh

#

Okay

sturdy orbit
#

Whats the easiest option to store objects in a database, we're using sqlalchemy at the moment, and i HATE it!

#

is zodb any good?

broken linden
#

one doesnt store objects in databases, those objects just represent normal SQL and get translated to SQL. Personally I'd either use psycopg2 (pure SQL) or sqlalchemy

wind nexus
#

@near cradle im getting

flask_login\mixins.py", line 39, in get_id
    raise NotImplementedError('No `id` attribute - override `get_id`')

when trying to do a login, my table does not have id column, instead its called user_id
do i need to somehow override it?

near cradle
#

when you do what

wind nexus
#
site-packages\flask_login\mixins.py", line 37, in get_id
    return text_type(self.id)
AttributeError: 'Users' object has no attribute 'id'
#

trying to use the mixin stuff and flask login

ionic pecan
#

that error sounds fairly clear

#

"No id attribute - override get_id"

wind nexus
#

but how do i do it

ionic pecan
#

I suppose the default is using the .id attribute, but you seem to be able to customize it using get_id

#

Check the docs for flask_login

wind nexus
#

so this is in the mixins source:


    def get_id(self):
        try:
            return text_type(self.id)
        except AttributeError:
            raise NotImplementedError('No `id` attribute - override `get_id`')
#

do i just copy part of that to my User class and change self.id to self.user_id

wind nexus
#

how should i implement linkedin login with flask?

cinder pelican
#

there's an example for LinkedIn

woven stag
#

Whats a good way of Learning databases related to Python?

ionic pecan
#

As in, using databases with Python?

woven stag
#

Seems good

ionic pecan
#

bannyThink I'll take that as a yes

wispy fable
#

(or if you aren't really concerned about all the server and permission related stuff, you can use sqlite3, which is part of the standard library)

jade topaz
#

ok this is a bit of a longshot but does anyone here use neomodel? OGM for neo4j databases? I can't figure out how to set UNIQUE constraints on a property, e.g given a Person node with an email property, no two Person nodes can share the same email address .

the docs don't say anything about unique constraints on properties (apart from the UniqueIdProperty), and the source code for both the Property and StringProperty classes don't seem to accept a 'unique' kwarg. I'm kinda lost here

jade topaz
#

ok so it looks like unique_index might be what I need. more testing is required

fringe seal
#

I was wondering how I should be using SQL on python

#

would anyone know how it would work?

#

or recommend a library?

patent glen
#

@fringe seal sqlite is built in, and there are libraries for other database engines that use basically the same interface. Also look at sqlalchemy and maybe pandas [pandas is more for table-like objects to load it into, it's compatible with sqlite and sqlalchemy]

celest barn
#

also please use prepared statements

ember dew
#

I would mention that if you are being serious about your database

#

then you probably want to do migrations

#

which are quite convenient with SQLAlchemy + Alembic

#

but that sqlite3 doesn't support a lot of the migration mechanisms without workarounds

fringe seal
#

ok thanks!

jade topaz
#

just want to add, in case you don't find the built-in libraries to your liking there are ORMs like SQLAlchemy and Peewee that make it easier to work with SQL databases

torn sphinx
#

Fairly new to Python and MySQL and i'm wondering why this code returns the error 'Lost connection to MySQL server during query':

#
import pymysql

connection = pymysql.connect(user='no', passwd='no', host='no',port=0000, database='no')
cursor = connection.cursor()
query = ("SELECT * FROM discordtable")

cursor.execute(query)
ionic pecan
#

that's a bit weird, but that somewhat like a one-time issue, did you try this multiple times?

torn sphinx
#

Yeah, I have tried running the code multiple times.

viral crag
#

What's in the table?

#

And are your details correct?

#

What happens if you run the query using mysql directly?

torn sphinx
#

The table contains name & verification.

#

Yes, my details are correct. I have no issue with connecting to the database.

#

Oh, I do have some issues with connecting.

#

This is weird..

rancid pier
#

I suggest the typical approach: do it with CLI, if it works it's either you or the module (mostly you) 😁

stable violet
#

c1.execute("SELECT ? FROM Hours WHERE UID=?", (keyword, uid)) looking to do something fairly simple, how do you put the column in to be defined later? I was thinking it would be using ? still but that doesn't look to be the case

#

And I'm having trouble finding it in the python docs (sqlite3)

quick marlin
#

i remember this being asked before

#

i think they came to the conclusion that you cant

#

so probably just c1.execute("SELECT ? FROM {} WHERE UID=?".format(column), (keyword, uid))

#

and safeguard against sql injection yourself i guess

#

¯_(ツ)_/¯

neat reef
#

one thing you can do it's just py c1.execute("SELECT * FROM Hours WHERE UID=?", (uid,)) and then only use the row you want

stable violet
#

thank you guys

viral crag
#

If you need to dynamically pick a table to use, then you're using any SQL database wrong

keen cedar
#

So, my bot provides a wallet system for every server.
In every server, each member may be given a wallet, and every wallet has a certain amount of coins.
The wallets aren't uniform, meaning the member's balance in server A might be different from their balance in server B, so, as I understand it, this is a many to many relationship...

How do I even implement that? I'm new to SQL and as far as I've gathered using many to many relationships is considered a bad practice but I don't know how else to implement this. And it's a roleplay bot, so I can't exactly make the wallets uniform. I'm using MySQL's workbench to edit the tables, by the way, not sure if this is important 🤔

patent glen
#

wallet table with both a server id and member id as key columns

#

that's one to one: one "server+member" to one wallet.

#

(many to many relationships aren't inherently bad either, imagine you have a non-unique item that can be in many players' inventory. but that's not what this is.)

#

fundamentally "servers" to "members" is a many to many relationship as well

keen cedar
#

I'm a bit confused... So, let's say we have three members. Their ID's are 1111, 2222, 3333. There are two servers, their ID's - 111111, 222222.
1111 has a wallet in server 111111 with 500 coins, and another one in server 222222 with 300 coins.
2222 has a wallet in server 111111 with 300 coins, and another one in server 222222 with 100 coins.
3333 only has a wallet in server 222222 with 700 coins.
What would the table look like?

patent glen
#
member server coins
1111   111111 500
1111   222222 300
2222   111111 300
2222   222222 100
3333   222222 700```
#
create table wallet (
  member int not null, server int not null, coins int not null default 0,
  constraint member_server unique (member, server)
);```
keen cedar
#

Not sure what the third line does since I'm pretty new to SQL, but thanks, I'll try to figure the rest out from here

open gull
#

im guessing the third line makes it so you can't have two records with the same member and server

wispy fable
#

Why not make a table for every server?

tight ravine
#

I currently have a website built in python that is using SQLite . Is it worth switching to MySQL?

ionic pecan
#

@wispy fable because that's not how you SQL

#

you don't programmatically create tables

#

specifically:
*SQLite works great as the database engine for most low to medium traffic websites (which is to say, most websites). The amount of web traffic that SQLite can handle depends on how heavily the website uses its database. Generally speaking, any site that gets fewer than 100K hits/day should work fine with SQLite. The 100K hits/day figure is a conservative estimate, not a hard upper bound. SQLite has been demonstrated to work with 10 times that amount of traffic.

The SQLite website (https://www.sqlite.org/) uses SQLite itself, of course, and as of this writing (2015) it handles about 400K to 500K HTTP requests per day, about 15-20% of which are dynamic pages touching the database. Dynamic content uses about 200 SQL statements per webpage. This setup runs on a single VM that shares a physical server with 23 others and yet still keeps the load average below 0.1 most of the time.*

#

but if you want e.g. redundancy with multiple servers then mysql is probably better, although i never used it so i wouldn't know

tight ravine
#

My biggest issue right now is that SQLite uses two temp files with data that seem to not update the main database so I can't make up to date backups of the main database

neat reef
#

So, I have the current table in SQLite

create table if not exists `notes`
(
    user TEXT PRIMARY KEY,
    id INTEGER NOT NULL,
    content TEXT NOT NULL,
    CONSTRAINT _unique UNIQUE (user, id)
);

I want to do the following when deleting and inserting into the table.
Lets say I have

'user1', 1, 'First user1 note'
'user2', 1, 'First user2 note

And now user1 adds two notes, but then decides to delete the second one, instead of

'user1', 1, 'First user1 note'
'user2', 1, 'First user2 note'
'user1', 3, 'Third user1 note'

(there's a gap between 1 and 3)
I want

'user1', 1, 'First user1 note'
'user2', 1, 'First user2 note'
'user1', 2, 'Third user1 note'

removing any gap between

#

How could I do it?

quick marlin
#

you want the id to change?

#

that sounds like a really bad idea

neat reef
#

for what im trying to do isn't a big deal

quick marlin
#

it just conceptually makes no sense

neat reef
#

is more like an index more than in id

quick marlin
#

an id is supposed to refer to something uniquely

#

well i dont see why you'd care

neat reef
#

yeah, the name is actually bad

quick marlin
#

just pull all user notes

#

into a list

#

and you have a list of all notes that you can access by index

#

or something

neat reef
#

well, the main purpose of id is to take track of the insertion order

#

because, i can't rely on the order of the the data or time

quick marlin
#

relay?

neat reef
#

yep

quick marlin
#

if you want insertion order make sure the id is always incrementing

#

like, in sqlite the id has to have the AUTOINCREMENT thingy

#

or it'll re-use ids

neat reef
#

oh right, i completely forgot that

loud spade
broken linden
#

because you are telling it to?

summer sable
#

IDENTITY in sql is often used for incrementing uniquely, not deleting later, but also not losing order of insertion, @neat reef
Also, hi @quick marlin from another life!

neat reef
#

yeah, i changed it

loud spade
#

@broken linden im telling it to put it in row 1, I assume, but it creates a new row

broken linden
#

well how are you telling it to do that

loud spade
#

I’ll send it in a bit xD, atm on my phone

loud spade
#

@broken linden

    c.execute("SELECT * FROM Bannedwords")
    print(c.fetchall())
    if not c.fetchall():
        if word in c.fetchall():
            await client.send_message(ctx.message.author, "`{}` is already being censured!".format(word))
        else:
            while True:
                listword = ''.join(random.choice(string.ascii_uppercase) for _ in range(6))
                c.execute("CREATE TABLE IF NOT EXISTS Bannedwords(word1 TEXT)")
                try:
                    c.execute("ALTER TABLE Bannedwords ADD COLUMN {} TEXT".format(listword))
                    c.execute("INSERT INTO Bannedwords({}) VALUES(?)".format(listword), (word.lower(),))
                    await client.send_message(ctx.message.author, "I have succesfully cencured the word `{}`".format(word))
                    break
                except:
                    print('except')
                    c.execute("INSERT INTO Bannedwords(word1) VALUES (?)", (word,))
                    await client.send_message(ctx.message.author, "I have succesfully cencured the word `{}`".format(word))
                    break
#

database for my discord bot

ionic pecan
#

never, ever, use format strings to insert into databases

#

although it's your own data, it's just bad

broken linden
#

yeah you are gonna get hacked pretty quick if you do that

ionic pecan
#

and you shouldn't be programatically altering your table either

loud spade
#

oh frick

ionic pecan
#

what's the grand scheme of things you want to accomplish with adding the column

loud spade
#

if I add the words in the word1 column, the output would be like this: [('fuck',), ('fuck',), ('dick',), ('dick',), ('dick',), ('duck',), ('frick',)] like multiple lists, right?, and if I am trying to check if a word is in there, it got messed up

#

(sorry for the bad language, but it censors cursing words)

ionic pecan
#

huh

#

so you add one column per censored word on a guild?

loud spade
#

yeah

ionic pecan
#

okay

#

there's two ways of doing this properly

#

way 1: have a colum for each censored word

#
guild_id | word
---------+------
15151515 | javascript
15151515 | porn
15151515 | nix
loud spade
#

LOL!

ionic pecan
#

then you just run ```sql
SELECT word FROM censored_word WHERE guild_id = ?;

#

and you have your censored words for that guild

#

as a list of tuples, like you shown above

#

way 2 assumes you're using postgres: postgres has an array type, so you can just do ```sql
guild_id | words
---------+------
15151515 | ["javascript", "porn", "nix"]

#

but i would use the first variant regardless for compatability reasons

loud spade
#

okay, thanks for your advice. But I dont understand how to implement way 1, could you explain that more ?

ionic pecan
#

instead of adding a new colum for the newly added word, add a new row

#

so in your else, run ```sql
INSERT INTO censored_word VALUES (?, ?);

with params `guild_id`, `word`
#

if you don't want to check for uniqueness yourself you can just place a uniqueness constraint on guild_id <> word

loud spade
#

okay, thanks. I will work with that and if I have any troubles, which I hope I dont, I will ask for help 😃

#

thank you!

deft yacht
#

is there someone who has worked with the cassandra and Python ?

ionic pecan
#

bot.tags['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.

noble wasp
#

how motivational

#

kinda applies to all things

viral crag
#

You should read the page it links to

#

and the page that page links to

#

:P

warm hare
#

Hey

#

What is the best way to store web stats?

#

Such as requests

#

But I do want to be able to grab them like requests per second

#

or

#

per day

#

shit like that

#

what would be the best way to store it and to grab it again to display to admins?

#

If someone answers please ping me

#

I am currently using MongoDB, I'm not moving away from it but I would look into using a second one

#

edis in use already too

deft badge
#

You could use something like statsd and grafana if you want professional looking graphs & stats

#

You can also use datadog to tally things up for you

warm hare
#

datadog does not have a good free plan

#

will look into grafana

#

How would I use Flask with Grafana tho

deft badge
#

You'd run grafana seperately, they don't integrate by default, and you'd write some kind of before_request handler in flask to add something to the source of your grafana data

warm hare
#

ugh this shit is confusing lol

warm hare
#

oh

#

I was just looking into Elasticsearch

#

Elastic APM should do the job ig

low beacon
#

So I have a Json that has nothing in it except the {} how would I dump a name to a dict as well as the value?

torn sphinx
#

once you load it into a variable

#

just do something like

#
data = {}
data['mykey'] = 'myvalue'```
#

then you can dump it back into the file

low beacon
#

Okay thanks

#

So that will dump the value

#

And the key thank you

torn sphinx
#

well

#

it wont change it in the file

low beacon
#

Yeah

torn sphinx
#

youll have to change it in python, then dump it back into the file

#

using json.dump

low beacon
#

Yyp

#

Yup

torn sphinx
#

ok then yeah u got it

low beacon
#

Yeah no

#

That didn't work

#

But it's a discord.py related error so I won't ask you

noble anchor
#

This is my first time trying to dump csv data into postgres, and I am getting a connection error.
What is the correct way to initiate the connection?

conn = psycopg2.connect("host=localhost dbname=postgres user=postgres")
ionic pecan
#

post the full error

#

also that's probably better done through psql

noble anchor
#

Full error:

File "db.py", line 2, in <module>
    conn = psycopg2.connect("host=localhost dbname=postgres user=postgres")
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
ionic pecan
#

is the server running?

noble anchor
#

Doesn't "host=localhost" start the server locally?

ionic pecan
#

no, that connects to a local server

#

postgres works based on client / server

#

unlike e.g. sqlite

noble anchor
#

do you happen to know any tutorials that include this step?

ionic pecan
#

are you on windows or unix?

noble anchor
#

ok cool, thanks!

hidden siren
#

so guys,

#

is python the best back end language?

gusty spindle
#

is yellow the best colour?

hidden siren
#

yes

wispy fable
#

The answer is maybe

#

It really depends on what you're doing and hat you're willing to learn to do the thing most effectively

#

Any reason why you're asking in #databases, @hidden siren ?

hidden siren
#

Because i want to learn a good language for backend. is database the wrong chat?

wispy fable
#

It really depends on what you mean by backend

hidden siren
#

mongoDB sounds funny.

#

so mongoDB in german means literally retardDB.

wispy fable
#

lol

ornate abyss
#

Not wrong

#

Mongo has the big rart

torn sphinx
#

hi

#

how can i decode a sqlite 3 database ?

ionic pecan
#

bot.tags['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.

ionic pecan
#

what does "decode" even mean

daring cairn
#

So I'm having a weird issue, with rethinkdb. This line works in my debug command. But not in actual code:

#
    async def _unpunish_data(self, ctx, member):
        """Removes punish data entry and cancels any present callback"""
        gid = ctx.guild.id
        gdb = await r.table('mute').get(str(gid)).run(self.bot.conn)
        await r.table('mute').get(str(gid)).replace(r.row.without(str(member.id))).run(self.bot.conn)
        gdb['muted'] -= 1
        await r.table('mute').insert(gdb, conflict="update").run(self.bot.conn)
#

And I'm not sure why, I know the function is being called and is functioning through that line because it removes 1 from 'muted'. Just seems to ignore that line with no errors.

#

I've printed all of the IDs to make sure they are correct, and they are.

viral crag
#

Why are you storing "muted" as an integer?

#

Feels like a boolean

#

Anyway, set conflict="replace"

#

"update" is generally not what you want

daring cairn
#

@viral crag I might not have explained my issue enough, there is no issue with that line, it works fine. And update is what is needed there. This is the line that isn't functioning:

        await r.table('mute').get(str(gid)).replace(r.row.without(str(member.id))).run(self.bot.conn)
#

I see the first link for the image showing that info ^ was broken so I removed it. Apologies

#

Probably just placement because I'm saving after actually, I put it in between so I would know the code was touched but now I see how that could be an issue, let me try moving it lower

#

Yep, all about placement. Sorry I was really tired when working on this haha, problem solved 😅 I'm so stupid sometimes

patent glen
#

@torn sphinx What are you trying to do?

torn sphinx
#

mh decoding an sqlite 3 encoded file

patent glen
#

that's not really an answer

#

i already showed you how to open a sqlite3 database... is this the same one you were working on last week, with the key/value columns with utf-16 encoded JSON?

rough river
#

I got this error when trying to open pgAdmin4 postgres application server could not be contacted

#

I added the postgres bin and lib path to my system environments and that didn’t help

torn sphinx
#

hey

#

using pymongo

#

is there a way to get info from the database back

#

in format so that one value is descending from greatest to leasst

#

in order

#

so if it's like

#

name: "Bob"
age: 1

name: "Steve"
age: 100

name: "Pete"
age: 20

#

if i have that kind of thing

#

how would i get it so the info for Steve comes first, then Pete, then Bob

#

since that's the greatest to least order of age

vestal apex
torn sphinx
#

ahhhhh

#

thank you

rough river
#

How would I add an value to a database

vestal apex
#

that depends 200% on the database in question

rough river
#

postgres

vestal apex
#

how are you interacting with it?

rough river
#

What do you mean?

vestal apex
#

I imagine you're using a library

rough river
#

asyncpg

vestal apex
#

let us know if you need help from there

torn sphinx
#
for doc in botUsers.find().sort('bal', pymongo.DESCENDING):
            print(doc)

why is this not printing anything?

vestal apex
#

presumably there are no docs to be found

torn sphinx
#

but there are

vestal apex
#

are you certain? try iterating over botUsers.find() without sorting it

torn sphinx
#

still nothing

vestal apex
#

that sounds an awful lot to me like it's not finding anything. is botUsers the right collection?

torn sphinx
#

wait

#

ohhhhhh

#

also

#

if i have a list of values

#

is there a way i can do botUsers.find()

#

but like

#

find all values of name that are in the list

#

so if i have a list with Bob and Steve in it

#

it only gives me the docs for Bob and Steve

vestal apex
torn sphinx
#

thanks

rough river
#
db = await asyncpg.create_pool(**credentials)```

```py
@bot.event
async def on_command():
    async with connection.transaction():
        await connection.execute("INSERT INTO cmdcount VALUES(1)")
``` That does not seem correct at all but am I closer to getting it. I have a table named cmdcount.
#

How would I connect to my database because I don't think that is

vestal apex
#

you should rename db something like pool or connection_pool for clarity, you get a connection from that pool temporarily with py async with pool.acquire() as connection:and then you can use your transaction

#

also are you sure you want an INSERT query and not an UPDATE query?

#

@rough river

rough river
#

Would I save the previous cmd count then add one to it

#

credentials = {"user": "no", "password": "no", "database": "no", "host": "no"}
pool = await asyncpg.create_pool(**credentials)

@bot.event
async def on_command():
    async with pool.acquire() as connection:
        UPDATE cmdcount
        SET count = ' '
#

I'm confused what to put in the set or what to change

vestal apex
#

your previous code with the transaction was correct, you need to wrap it python async with pool.acquire() as connection: async with connection.transaction(): await connection.execute("UPDATE cmdcount SET count = count + 1 --etc.")

rough river
#

Ok

#

Everytime I run pgAdmin 4 it says fatal error application server could not be contacted do you know how I can fix that

vestal apex
#

I'm afraid I do not

rough river
#

Ok

#
                       ^
SyntaxError: invalid syntax``` It says that when I try to run the file
hoary wharf
#

Are you inside a coroutine?

rough river
#

no

#

im using on_command

vestal apex
#

what Python version are you using?

#

(on_command is a coroutine)

#

(assuming you defined it with async def)

#

wait... are you sure you're defining pool in on_command?

#

that seems like an odd place to do it

rough river
#

no

#

Im defining it after all the imports in my general file and in my main after the prefix

#

I'm using 3.5

vestal apex
#

it's gotta be done in an async context, somewhere like on_ready

rough river
#

so in my on ready I put pool = await asyncpg.create_pool(**credentials)?

vestal apex
#

you should make pool an attribute of your bot

#

bot.pool

#

but yes that's the general idea

rough river
#

pool = await asyncpg.create_pool(**credentials) NameError: name 'asyncpg' is not defined That's what it says when I run it @bot.event async def on_ready(): print("uwu bot is ready!") await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='uwu')) pool = await asyncpg.create_pool(**credentials) theres the full onready

#

and I'm confused what do you mean by you should make pool an attribute of your bot

vestal apex
#

assign to bot.pool instead, pool will only exist local to on_ready

#

did you import asyncpg?

rough river
#

I thought I did apparently I didn't.

rough river
#

For bot.pool if I have other files using it do I have to redefine it

#

I have a cog that uses it but I don't have an on ready for it

vestal apex
#

the bot instance is passed to your cog module via setup, so it's actually the perfect place to store the pool

rough river
#

So I shouldn’t have to redefine it?

vestal apex
#

correct

rough river
#
    @commands.command()
    @commands.guild_only()
    @commands.cooldown(1, 5, BucketType.user)   
    async def about(self, ctx):
        '''Gives information about the bot'''
        async with self.bot.pool.acquire() as connection:
            async with connection.transaction():
                query = 'SELECT count FROM cmdcount;'
                await ctx.send(f'{cmd_count}')
``` I used that and when I use that it says SELECT count FROM cmdcount;
#
@bot.event
async def on_command():
    async with bot.pool.acquire() as connection:
        async with connection.transaction():
            await connection.execute("UPDATE uwu.cmdcount SET count = count + 1")

For that it says anytime an command is used Traceback (most recent call last): File "C:\Users\tasty\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 220, in _run_event await coro(*args, **kwargs) TypeError: on_command() takes 0 positional arguments but 1 was given

#

I thought about them but I couldn't fix it

torn sphinx
#

it takes one argument ctx

torn sphinx
#

@patent glen yes but i lost the file GWqlabsYaoLUL

rough river
#
        async with self.bot.pool.acquire() as connection:
            async with connection.transaction():
                cmdcount = '''SELECT count FROM uwu.cmdcount;'''  
                await ctx.send(f'{cmdcount}')

I'm using that to read from my database the table name is uwu.cmdcount and It adds 1 to count on command use but it returns [] everytime

radiant helm
#

Possibly super common question, but is there any python package that allows mssql connections and that supports ssl?

#

I've tried pymssql but in the setup.py a variable is set so that no linking is done to the OpenSSL libraries

patent glen
#

(apparently microsoft wants you to use pyodbc)

radiant helm
#

Yeah, I've seen that, I'm just worried about security and how easy it would be for someone to intercept it unencrypted

patent glen
radiant helm
#

Ooh! I had totally missed that last comment about the encrypt setting! Thanks so much haha, sometimes you just need a second pair of eyes.

tacit dagger
#

Hi. In one sqlite cell, i want to write both left to right and right to left. Like this:
Some ltr text
Then a horizontal line
Then some rtl text
Is it possible to do that?
My db is premade with db browser for sqlite application.

#

And when i fetch my results from db, some of them have { } around them. How can get rid of that? 😞

patent glen
#

you're gonna have to be more specific about how you're fetching them and how you're printing them that puts the { } around them

#

and what do you mean "one sqlite cell"? sqlite is a database, it doesn't have cells or visual layout

#

can you post a screenshot of what you're talking about

tacit dagger
#

Cells i mean those which i've shown in pics

rough river
#

My cmd that reads the number of cmds used from a table but it returns [] when I use the command

patent glen
#

@tacit dagger I don't see where you're seeing {}, and I still don't know what you mean by right to left text - are you wanting to also translate into hebrew or arabic?

tacit dagger
#

yes i will later want to add persian translation to those cells

#

persian is rtl

rough river
#
    @commands.command()
    @commands.guild_only()
    @commands.cooldown(1, 5, BucketType.user)   
    async def about(self, ctx):
        '''Gives information about the bot'''
        async with self.bot.pool.acquire() as connection:
            async with connection.transaction():
                data = self.bot.pool.fetch('SELECT count FROM uwu.cmdcount;')
                await ctx.send(f'{data}')

I changed my code now it sends <coroutine object Pool.fetch at 0x04E7DED0> whenever I run the command

torn sphinx
#

🇦 🇼 🇦 🇮 🇹

#

also pretty sure you would use connection.fetch

rough river
#

take out self.bot.pool and replace with connection

#

I put await before self and replaced self.bot.pool with connection

#

and now it sends []

#
@bot.event
async def on_command(ctx):
    async with bot.pool.acquire() as connection:
        async with connection.transaction():
            await connection.execute("UPDATE uwu.cmdcount SET count = count + 1;")
``` That might be my problem because the command seems fine I don't think that is right
torn sphinx
#

well

#

hmm

rough river
#

Does it look wrong

#

I tried looking at asyncpg examples but it doesn't help

#

Should I close the connection the example closes it I think I should

#

It gave me more errors with the connection close

deft yacht
#

I hope some one here has worked with the casssandra..
i am running into the trouble..

I have a huge table around (100+ columns) and i am tranferring the data from the csv file to the cassandra..

but i am getting stuck at this error

```AttributeError: 'float' object has no attribute 'encode'````

how can i pinpoint the column which is causing this trouble?

versed coyote
deft yacht
#

company's code. not mine.

versed coyote
#

Ah, had a feeling

#

How are you doing the query?

#

And reading the CSV?

deft yacht
#

something like this

df = pd.read_csv(path/to/csv

versed coyote
#

So is Python automatically determining the types then?

deft yacht
#

and then passing it to the function to insert such as this

                " " \```
#

nope. I have made the table in the cassandra with the help of the data types in the dataframe from the csv file

versed coyote
#

Sorry, what I meant is are you just putting the data raw from the CSV into the query?

#

You might need to do some type casting before hand

deft yacht
#

higher point of view

 from cassandra.cluster import Cluster
    cluster = Cluster(ip_address)
    session = cluster.connect(keyspace_name)
    query = "INSERT INTO data(date,time,open,high,low,last) VALUES (?,?,?,?,?,?)"
    prepared = session.prepare(query)```

for item in dataFrame:
session.execute(prepared, (item[0],item[1],item[2],item[3],item[4],item[5]))```

versed coyote
deft yacht
#

You might need to do some type casting before hand
How do i know which column typecast and which not?

versed coyote
#

That would be based on the types of the table in Cassandra itself

#

so if it's a varchar, then it should be a str, if it's decimal, then float

deft yacht
#

where can i find more info about this?
%s,%s

versed coyote
#

Those are string thingys...word is escaping me one sec

deft yacht
#

i need to know for the other data types also

versed coyote
#

string format operators

deft yacht
#

so as the commentor said on that thread


query = "INSERT INTO table_str (rowid,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as,at,au,av,aw,ax,ay,az,ba,bb,bc,bd) VALUES (uuid(),%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
for row in cassandra_df_list:
    session.execute(query, row)```

So the string operator are cassandra specific or python?
versed coyote
#

general actually, but in this case Python

#

They're used in printf in C, print in Python, e.g. print("Hello " % my_name)

#

So %s is just saying "a string-type thing goes here"

deft yacht
#

so for text, varchar it would be %s
for decimanl it would be %d
and for float it would be %f

versed coyote
#

Yes

deft yacht
#

i think i will modify my query a bit and see the result
can i tag you in say 12-15 hours here if i come across any problem?

versed coyote
#

Sure thing

deft yacht
#

what would be the string thing for the integer?

versed coyote
#

%d

deft yacht
#

okay.

versed coyote
#

%d is python int, %f is python float, %s is python str

#

So if you read the cassandra table, when it says "integers", use int and %d, etc.

deft yacht
#

what about the timestamps?

versed coyote
#

The python driver would usually do the conversion automatically, but it doesn't do that with prepared statements

#

Those are datetime.datetime, at least thats what the driver takes

#

There are a few exceptions like those

#

I don't know what they all are though, might be worth digging around the driver docs or code a bit

deft yacht
#

correct i am using prepared statement..
i hope this won't go fruitless

#

for time being i can leave it as ?

versed coyote
#

Yes I think so

#

Good luck, hope it works out tomorrow

deft yacht
#

let see.

#

thanks for the help! 😃

versed coyote
#

np

rough river
#

<coroutine object Pool.fetch at 0x04E7DED0> Do you think you can help me with that?

versed coyote
#

What about it?

#

What is that from?

rough river
#

What does it mean

versed coyote
#

It's a python object

#

Looks like a fetch query for something, so probably a function object

rough river
#

I'm trying to get my command to send the amount of commands used but when I use the command it sends that. Can I change that to understandable text

versed coyote
#

Can you post the code?

#

Also might be better to ask in a help channel

rough river
versed coyote
rough river
#

I think it's the sql part though

#

Yes it's for my bot

#

It's using asyncpg

versed coyote
#

I don't know anything about discord.py or what it's using for a SQL backing

#

Maybe someone else can help, sorry

rough river
#

Ok. No problem.

deft yacht
#

does any one know what is the string format operator for the double and the boolean ?

patent glen
#

@tacit dagger the same cells as English? Why wouldn't Persian be another column? And/or why do you want to show both English and Persian at the same time? Also, the sqlite browser or whatever that is isn't really meant for end-user presentation.

#

It's not really good to define your data schema solely based on how it looks in the UI.

tacit dagger
#

ok i got it. so i will write my Persian data in another column. that's much better.

patent glen
#

well, maybe

#

the other possibility would be to have a column for what language

#

like instead of Esperanto, English, have Esperanto, Definition, Language, and let Language be 'en' for english or 'fa' for persian, that'd be more scalable

#

you'd need to add filters or select distinct to your queries

tacit dagger
#

yep that's better. i will consider this in future editions when i improved more in programming.

versed coyote
#

@deft yacht double in Cassandra is a float in Python, %f

#

Bool I'm not sure

deft yacht
#

i just used ? in both the cases 😃

versed coyote
#

Cool

deft yacht
#

but thanks for the helping hand!
really thank full.
maybe in future i will ask a lot of questions here 😄

versed coyote
#

Definitely

#

That's our purpose, to help 👍

wind nexus
#

anyone here atm that can help me with sqlalchemy

ionic pecan
#

bot.tags['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.

wind nexus
#

if i have

class Users(UserMixin, db.Model):
    __tablename__ = "users"
    user_id = db.Column(INTEGER, primary_key=True)
    details = db.relationship('Details', uselist=False, back_populates='user')

class Details(db.Model):
    __tablename__ = "details"
    id = db.Column(INTEGER, primary_key=True)
    first_name = db.Column(VARCHAR(255))
    last_name = db.Column(VARCHAR(255))
    user_id = db.Column(INTEGER, db.ForeignKey('users.user_id'))
    user = db.relationship("Users", back_populates="details")
#

can i set the first name and last name with

User.details(first_name=first_name,last_name=last)
#

current_user.details(first_name=first_name,last_name=last)
db.session.add(current_user.details)
db.session.commit()
#

thats weird

ionic pecan
#

details isn't callable, I believe

#

you need to do something like current_user.details = Details(first_name=first_name, last_name=last)

wind nexus
#

hmm ho wabout the add/commit?

ionic pecan
#

I don't know, what about it?

wind nexus
#

do i need to do the add thing

#

as the user already does exists in db

#

and im just adding the details

ionic pecan
#

If you modify the .details attribute on current_user, the session will notice and the user will be marked as dirty. On the next commit, it's going to write the changes you made to the database

wind nexus
#

ooh

#

hmm i made some changes to my db structure, added some fields to tables, how should i do the migration as when i try to do flask db migrate i get Table 'skills' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

ionic pecan
#

did you put the same table in one migration twice?

wind nexus
#

?

ionic pecan
#

sorry, badly phrased. Your migrations are independent of your app, correct?

#

As in, they aren't loaded when you start your app

wind nexus
#

yeah i do them manually

#

its a early testing version

ionic pecan
#

well, the error sounds like you have two definitions of that table, which doesn't sound like a good idea. If you're in early testing then you can probably just add new columns and use create_all or whatever the function was named again, but I recommend just using migrations regardless

wind nexus
#

hmm i reverted all changes on the models.py file that had all database classes and now it allowed my to run the db reset script i have

#

now to readd changes and run migrate

ionic pecan
#

I didn't have my bot's migrations properly worked out when I started writing them either, but whether it executes 30ms faster or slower doesn't really bother me :P

wind nexus
#

im a idiot, i had a leftower class there that i used to copypaste stuff

ionic pecan
#

haha

wind nexus
#

YESYESYESYSE i got it working properly and adding the right DATA

#

hmm, it keeps adding a new row to the details table every time i submit a form and sets the userid on the latest only

#

how can i make it to edit the existing field instead

ionic pecan
#

I think you can check if the reference (e.g. user.details) is None

#

and well, if it isn't, there's already an associated object and you can update that

wind nexus
#

hmm maybe i could do it the same way i do the password:



class Users(UserMixin, db.Model):
    __tablename__ = "users"
    user_id = db.Column(INTEGER, primary_key=True)
    email = db.Column(VARCHAR(255), index=True, unique=True)
    password_hash = db.Column(VARCHAR(255))
    skills = db.relationship('Skills')
    details = db.relationship('Details', uselist=False, back_populates='user')


    def set_password(self, password):
        salt = secrets.token_urlsafe(16)
        argonType = argon2.low_level.Type.D
        self.password_hash = argon2.low_level.hash_secret(
            password.encode(), salt.encode(), 1, 8, 1, 64, argonType)
#

would it be like


def set_details(self, first_name, last_name):
    self.details= Details(first_name=first_name, last_name=last_name)
#

and have that under Users class?

ionic pecan
#

That would create a new associated details object every time, even if there's already one

wind nexus
#

hmm

ionic pecan
#

I really don't know right now whether self.details would just be None if there's none associated with the user yet, but if yes, you could try:

def set_details(self, first_name, last_name):
    if self.details is None:
        self.details = Details(first_name=first_name, last_name=last_name)
    else:
        self.details.first_name = first_name
        self.details.last_name = last_name
deft yacht
#

cross posting from the #help-croissant

alright.. got few questions...
i am trying to move my data from the CSV which is more or less treated as a dataframe..

I have a column which has int values.
and even when i did the dataframe.dtype it comes out the int64 for that column

but when i run the script to actually transfer the data from the dataframe (CSV) to the cassandra

I am getting this error

TypeError: Received an argument of invalid type for column "columnName". Expected: <class 'cassandra.cqltypes.FloatType'>, Got: <type 'str'>; (required argument is not a float)

any help

wind nexus
#

OOOh so i can call those columns individually

deft yacht
#

is that for me?

wind nexus
#

sorry no

ionic pecan
#

Is there a way to reduce PostgreSQL memory usage? My cluster is using around 200 MB of memory, but my database is like 20 MB big at most. IIRC postgres forks for connections, and one of my apps uses a pool of ten connections. Is it a smart idea to reduce pool size?

viral crag
#

That seems like about the size you'd expect

ionic pecan
#

Hmm

#

Not sure if I really need 10 connections though

#

The only somewhat expensive thing I'm doing is running a SELECT on each message event from Discord

#

but that's rather fast

torn sphinx
#

Hello I use SQLite, how can I check if a record already exist in a table? for now I use

user_id_check = ctx.message.author.id
c.execute('SELECT * FROM users WHERE "user_id" = ?', (user_id_check,))
for row in c.fetchall():
    #my code
return await bot.say("{}, You are not registered".format(ctx.message.author.mention))```But I think its not right
ionic pecan
torn sphinx
#
c.execute('SELECT COUNT(1) FROM users WHERE user_id=?',(user_id_check,))
    for asd in c.fetchall():
        print(asd)```
So I have this it will print 0 if the userdoesn exist. and it will be 1 if the user exists So how can I make it say when the user doesnt exist "You are not registered" same if exists "You are already registered".
torn sphinx
#

This wont work

user_id_check = ctx.message.author.id
c.execute('SELECT COUNT(1) FROM users WHERE user_id=?',(user_id_check,))
data = c.fetchall()
print(data)
if data is 0:
    return await bot.say("You are not registered.")
else:
    await bot.say("You are registered.")```
#

print(data)
[(0,)]

#

@ionic pecan You still there?

ionic pecan
#

it returns a single row with a single column telling you how many matching rows it found

#

if you're working with discord.py then working with lists and tuples hopefully isn't an issue

torn sphinx
#

btw why dont you do c.fetchone() instead

wind nexus
#

what am i doing wrong here:

 currentSkills = current_user.skills.query.filter_by(skill_name=skillName).first()
ornate abyss
#

What's the error

torn sphinx
#

hi

#

how can i do to read a firebase database ç

#

?

patent glen
#

@torn sphinx look at the python-firebase package on pypi

torn sphinx
#

thx

torn sphinx
#

I've created a dabase for an RPG game. I added a table called users and inside there I have peoples ID xp coins etc. Now I need to add weapons. and inside there I will add weapons but how?
How can I add more than one weapon inside weapons field? I only have item1 how can I add item 2 insde there too?

dull scarab
#

You could create an inventory table of sorts that links an item and an user

#
id        item id
1        1
1        4
2        1```etc
torn sphinx
#

But if I want to print out someones items with

for items in inventory:
    print(items)
``` it will also print their id :/
#

or maybe I can use
print(items[1:]) idk if that will work

#

nope that will just remove 1 number from his id :/

dull scarab
#

Well, I don't know how your databases are structures, nor what db you're using

torn sphinx
#

SQLite

dull scarab
#

Inventory would, in my example, link 2 other tables like user <-> items

#

So you could keep a database of all items (not connected to any user)