#databases

1 messages · Page 151 of 1

jaunty galleon
torn sphinx
#

I thought it was more of a database problem

proven arrow
#
CREATE TABLE users (
    user_id INTEGER NOT NULL AUTO_INCREMENT,
    email varchar(255) NOT NULL,
    PRIMARY KEY (user_id)
);
CREATE TABLE items(
    item_id INTEGER NOT NULL AUTO_INCREMENT,
    name varchar(255) NOT NULL,
    PRIMARY KEY (item_id)
);
CREATE TABLE user_inventory (
  user_id INTEGER NOT NULL,
  item_id INTEGER NOT NULL
  PRIMARY KEY (user_id, item_id),
  FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
  FOREIGN KEY (item_id) REFERENCES items(item_id) ON DELETE CASCADE
)

@upper basin This is how the tables would look.

#

To get count then you can do SELECT COUNT(*) FROM user_inventory WHERE user_id = ?

upper basin
#

🧐 quick question about this, it would have a different row for each object?

proven arrow
#

Yes

upper basin
#

i see, i think that solves my problem, thanks for your help!

cedar pagoda
#

How do I Insert, with python, a tuple into a sql database?

median swift
#
rows = await self.client.conn.fetch("SELECT * FROM moderation WHERE type <> $1 AND userid = $2 AND serverid = $3", "Note", str(user.id), str(ctx.guild.id))

This is asyncpg, how do I order this based on numerical order of the id column?

proven arrow
median swift
cedar pagoda
proven arrow
cedar pagoda
proven arrow
median swift
#

it is a BIGSERIAL column, does that affect things?

#

I've solved it

#

It was because there were empty stuff in my database which must of affected things

#

thanks for your help 🙂

proven arrow
# cedar pagoda yes
data = ['Helle wqda', 'awdawd a dawda', 'awdadw awdawd wadawd awdawd']
connection = db.connect(...) # whatever your connection is
connection.execute('INSERT INTO table_name (column_1, column_2, column_3) VALUES (?, ?, ?)', data)

each ? in the query corresponds to a value from the list

torn sphinx
#

How an earth do I format this data from MongoDB, I have been trying for days

        for result in results:
            id = str("_id")
            username = str("username")
            reason = str("reason")
            warner = str("warner")
            embed.add_field(name ="e", value = "Id:"+reason"Username:"+username"Reason:"+reason"Warner"+warner, inline=False)
proven arrow
#

What data?

cedar pagoda
# proven arrow ```py data = ['Helle wqda', 'awdawd a dawda', 'awdadw awdawd wadawd awdawd'] con...
db.execute("INSERT INTO applychoices VALUES (?, ?)", emoji, q_list)

I have it like that but I get this Error:

_ClientEventTask exception was never retrieved
future: <ClientEventTask state=finished event=on_command_error coro=<bound method Bot.on_command_error of <lib.bot.Bot object at 0x000001F36065F1C0>> exception=InterfaceError('Error binding parameter 0 - probably unsupported type.')>
Traceback (most recent call last):
  File "C:\Users\Lasse\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 348, in _run_event
    await self.on_error(event_name, *args, **kwargs)
  File "C:\Users\Lasse\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Lasse\Documents\Discord Bots\None\lib\bot\__init__.py", line 137, in on_command_error
    raise exc.original
  File "C:\Users\Lasse\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Lasse\Documents\Discord Bots\None\lib\cogs\modmail.py", line 191, in addapply_command
    db.execute("INSERT INTO applychoices VALUES (?, ?)", emoji, q_list)
  File "C:\Users\Lasse\Documents\Discord Bots\None\lib\db\db.py", line 66, in execute
    cur.execute(command, tuple(values))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

Line 66 in "C:\Users\Lasse\Documents\Discord Bots\None\lib\db\db.py"

def execute(command, *values):
    cur.execute(command, tuple(values))
proven arrow
#

The second parameter is supposed to be an iterable. See how i did it

pliant tiger
#

I'm using this as an example, but when I retrieve a list I store inside of an SQLite database, I want to parse this as a real list, not as this weird string thing. Is this possible?

pliant tiger
cedar pagoda
proven arrow
#

This would work,

my_list = '["this is", "a test"]'
print(json.loads(my_list))
proven arrow
cedar pagoda
# proven arrow the list

the tuple (q_list) is changing from e.g:

['Helle wqda', 'awdawd a dawda', 'awdadw awdawd wadawd awdawd']

to

['Helle wqda', 'awdawd a dawda']

So its changing his lenght "randomly", how do I make it when the lenght of the tuple is not given?

pliant tiger
proven arrow
cedar pagoda
# proven arrow Doesn't make sense. Show all the relevant code for that part
        q_list = []


        embed = Embed(title=f"Question Setup for {emoji}",
                      description=f"Starting Question query...",
                      colour=EColour,
                      timestamp=datetime.utcnow())

        QEmbed = await ctx.send(embed=embed)

        while question_wait:

            counter = counter + 1

            embed = Embed(title=f"Question Setup for {emoji}",
                          description=f"Send your {counter} Question which should be asked to applicant or type `stop` to stop adding questions.",
                           colour=EColour,
                           timestamp=datetime.utcnow())

            await QEmbed.edit(embed=embed)


            def checkM(m):
                return m.author.bot is False and m.channel.id == ctx.channel.id

            question = await self.bot.wait_for("message", check=checkM, timeout=1200)
            await question.delete(delay=2)
            if question.content == "stop":
                question_wait = False

            else:
                q_list.append(question.content)


        embed = Embed(title=f"Question Setup for {emoji}",
                      description=f"Finished the Question query.\nDo you want to confirm these Questions?",
                      colour=EColour,
                      timestamp=datetime.utcnow())

        await QEmbed.edit(embed=embed)


        await QEmbed.add_reaction("\U00002705")
        await QEmbed.add_reaction("\U0000274c")

        def checkYN(reaction, user):
            return user.bot is False and reaction.message.id == QEmbed.id and str(reaction.emoji) in ["\U00002705", "\U0000274c"]

        reaction, user = await self.bot.wait_for("reaction_add", check=checkYN, timeout=120)


        if str(reaction) == "\U00002705":

            print(q_list)

            db.execute("INSERT INTO applychoices VALUES (?, ?)", emoji, q_list)
def execute(command, *values):
    cur.execute(command, tuple(values))
proven arrow
#

What object is db? and what is emoji?

delicate fieldBOT
cedar pagoda
proven arrow
#

db is an sqlite connection object or a custom class?

#

And emoji you are passing is a discord.Emoji class. Its not a type supported by the database.

cedar pagoda
torn sphinx
whole mica
#

mongodb question
how can i delete all data from a collection except the one with '_id': 0

burnt turret
whole mica
#

it worked thanks!

upper basin
#

is it possible to select the sum of three columns and order it by the sum?

#

'select SUM(threestars*fourstars*fivestars) from users order by SUM(threestars*fourstars*fivestars) limit 10' doesn't seem to work

burnt turret
#

By sum of three columns, do you mean the sum of all values of those columns? Or just the sum per each row

upper basin
#

per row

burnt turret
#

Actually, I'm guessing you mean the latter because the first option wouldn't have multiple rows to order by

#

Yeah so you aren't supposed to use the SUM function there

#

SUM finds the sum of values over multiple rows, not columns

#

The query would just be something like

SELECT threestars + fourstars + fivestars AS 'total' FROM users ORDER BY total LIMIT 10
``` I think
upper basin
#

o.O i see, didnt know sum was over rows only

#

thank you

inner sentinel
high geyser
spark geyser
#

Hello

torn sphinx
#

Can someone help me structure my MongoDB data for my Discord bot

#

This is what I did but it doesn't work :\

eager igloo
#

def server_prefix(bot, message):
    stats = prefix_table.find_one({"guild_id" : message.guild.id})
    if stats is None:
        return "p?"
    else:
        prefix = stats.get("prefix")
        for i in stats:
            return i

how can I get it to return just the data? Currently it returns <function server_prefix at 0x000001B0E9CC8F70>

eager igloo
#

What you mean?

#

Ik

#

It should be stats["prefix"]

#

I think

#

didn't work

#

😦

remote plinth
eager igloo
#

I just need to know why it is returning the wrong thing

remote plinth
#

!e

def test(x):
   return x += "!"
print(test)
#

whoops

eager igloo
#

what is !e for?

remote plinth
#

!e

def test(x):
  y = x += "!"
  return y
print(test)
eager igloo
#

I don't understand

#

Let me explain

remote plinth
#

this

eager igloo
#

I have a MongoDB database

#

and I have prefixes

#

I need this command to go into the database and pull the server's prefix

#

and return the prefix

eager igloo
remote plinth
#

im just showing u the different without () lmao

eager igloo
#

I am so confused

remote plinth
#

can i see full code

#
  • when u call the function
eager igloo
#
prefix_table = database["Prefixes"]
mycol = database["levelling"] 

prefix = 'p?'




def get_prefix(bot, message):
    stats = prefix_table.find_one({"guild_id" : message.guild.id})
    if stats is None:
        prefix = 'p?'
    else:
        prefix = stats.get("prefix")
    return commands.when_mentioned_or(*prefix)(bot, message)

bot = commands.Bot(command_prefix=get_prefix,
case_insensitive = True
)
  
def server_prefix(bot, message):
    stats = prefix_table.find_one({"guild_id" : message.guild.id})
    if stats is None:
        prefix = 'p?'
    else:
        prefix = stats.get("prefix")
    return commands.before_invoke(*prefix)(bot, message)
#

the server_prefix is what I am working on

#

the difference is that get_prefix is to set the prefix

#

and server_prefix is just to make a variable holding the prefix

#
    def __init__(self, bot):
        self.bot = bot
        print("Prefix Module Loaded")

    @commands.command(aliases=['prefix'])
    async def change_prefix(self, ctx, prefix):
        stats = prefix_table.find_one({"guild_id" : ctx.guild.id})
        if stats == None:
            prefix_table.insert_one({"guild_id" : ctx.guild.id, "prefix" : prefix})
        else:
            prefix_table.update({"guild_id": ctx.guild.id}, {"$set":{"prefix":prefix}})
#

here is my cog where I upload the prefixes it works

#

@remote plinth

remote plinth
#

.

eager igloo
remote plinth
#

im reading

#

get_prefix working right?

eager igloo
#

yep

#

it allows to use the prefix and to mention the bot

#

The idea is to use the server_prefix in my help command

#

so that it uses the server's prefix in it

#

I can't use the ctx.prefix because it will use the @bot

remote plinth
#

basically, the prefix that the user entered in message u can get it by ctx.prefix

#

for example //help
ctx.prefix: //

eager igloo
#

yes

#

but here is the problem

#

if they do

#

@bot help config

#

it would say

#

the command is

#

@bot enable

#

I want it to say the server's prefix enable

#

that is why I need this function

#

@remote plinth

remote plinth
#

try not using the function and put this in ur help cmd use prefix variable for it

    stats = prefix_table.find_one({"guild_id" : message.guild.id})
    if stats is None:
        prefix = 'p?'
    else:
        prefix = stats.get("prefix")
eager igloo
#

ok

#

gimme 1 sec

#

@remote plinth now neither of my prefixes work

#

idky

remote plinth
#

huuuh

pallid shard
#
timestamp=datetime.datetime.now()``` How to change timezone here
#

GMT+2

#

me timezone

jaunty galleon
#

Isn't related to databases

jaunty galleon
worldly panther
#

Does INSERT IGNORE or INSERT OR IGNORE inserts data only if it doesn't exist? I've read that's its functionality but every time I run my application after closing it it inserts the data again. I'm using SQL

brazen charm
#

generally yes

proven arrow
past current
#

Hi everyone, when creating a social media application, should I create a profiles table or just put all user data in the users table?

proven arrow
#

With 2 tables you can store identity data (email, password) in users, and the profile data in profiles.

past current
#

@proven arrow I see, yeah with two tables I belief It would be better.

proven arrow
#

Yeah that would be my choice also. Profile data can get big depending on what you store. Best to keep it as a different set.

past current
#

@proven arrow thank you very much again for you help. ☺️

worldly panther
proven arrow
worldly panther
#

I understand. I'm using SQLite 3

proven arrow
#

Im not familiar with pyqt5, but if you have access to the sqlite database from a db browser/cli then can you show the output of: SELECT sql FROM sqlite_master WHERE name = 'your_table_name'

worldly panther
#

I have a SQL IDE extension that opens the database in table-format. However I can try sending that command

smoky radish
#

i always recommend you use a proper database tool, my favourite is DBeaver tho that is not a lightweight tool

harsh pulsar
#

Squirrel sql isnt bad

#

For sqlite3 you can use db browser

worldly panther
#

I'm going to check those out

#

Thanks

high comet
#

a postgres
user table
contains everything about a particular user
its contains 3 values/variables
1 ) name
2) bal
3) assests

assest would contain 0 no of values . but with time no of values would increase . There is limit to values.
The values that are in asset contain their own set of charactericts .
How this can be done

robust fjord
#

What's Row Level Security in PostgreSQL?

#

I've looked at the docs, but I didn't really understand it.

rain plank
#

What's the difference between serial and int generated by default as identity in postgesql?

proven arrow
proven arrow
remote plinth
#

    @commands.Cog.listener()
    async def on_guild_join(self, guild):
        sql = f"INSERT INTO welcome (id) VALUES ({guild.id})"
        self.db.execute(sql)
        self.db.commit()

    @commands.Cog.listener()
    async def on_guild_remove(self, guild):
        sql = f"DELETE * FROM welcome WHERE id = {guild.id}"
        self.db.execute(sql)
        self.db.commit()
```will this work? new to sqlite
proven arrow
#

Yeah but for delete you can remove *

#

Delete removes the entire row, so no need for that.

remote plinth
#

DELETE FROM?

grim vault
#

Please use bindings, not f-strings

        sql = "INSERT INTO welcome (id) VALUES (?)"
        self.db.execute(sql, [guild.id])```
proven arrow
#

Yeah, and you should avoid using f strings to make queries with parameters. See why, #databases message

grim vault
remote plinth
#

edited

    @commands.Cog.listener()
    async def on_guild_join(self, guild):
        sql = "INSERT INTO servers (id) VALUES (?)"
        self.cur.execute(sql, [guild.id])
        self.db.commit()

    @commands.Cog.listener()
    async def on_guild_remove(self, guild):
        sql = "DELETE FROM servers WHERE id = ?"
        self.cur.execute(sql, [guild.id])
        self.db.commit()```
#
    @commands.command()
    @commands.guild_only()
    async def prefix(self, ctx, prefix):
        sql = "UPDATE servers SET prefix=? WHERE id=?"
        self.cur.execute(sql, [prefix, ctx.guild.id])
        await ctx.send(f"prefix is set to {prefix}")
```and this?
#

(thanks!)

grim vault
#

Looks good on the SQL side (because I have no clue about the bot thing).

brave bridge
#

I need to store two kinds of data:

  • one is very rarely written to and is being read often
  • the other one is a sort of an 'event log', to which certain events are written fairly often; I also need to run folds on these events (perhaps with some kind of cache)
    What would be a good way to store this data?
    I think I can get away with something simple like SQLite for the first one, but for the second one I assume I need something which supports concurrent writes.
proven arrow
#

Not sure what is your concern? Picking the database or somethign else?

brave bridge
#

Or maybe I could just use Postgres for everything

proven arrow
#

There is no standard. Sqlite can work as well, just a matter of how you use it.

brave bridge
cunning jolt
proven arrow
#

Sure it does make sense, if you start face issues with sqlite for that. Not sure how much or often you will be inserting.

brave bridge
cunning jolt
#

a time series database would also be an option

cunning jolt
proven arrow
#

I have event logging setup for when i develop locally. Inserts around 30 per request excluding whatever else is going on in the app. Never had an issue.

brave bridge
#

I want to support about 50-100 insertions per second. I guess it will be fine

cunning jolt
#

i mean i use loki with a scylla backend, which handles log like data damn well

proven arrow
brave bridge
#

as always lemon_pleased

brave bridge
# cunning jolt then what is it?

Users of the service will submit content that will be rated ("judged") according to a series of tests. I want to store those judgements so that it's possible to collect statistics and inspect how individual submissions succeeded/failed.

cunning jolt
#

i mean sql should work

grim vault
#
TBL: ...\Message.csv, 134469512 in 1376.99s ~ 97655/s```
brave bridge
#

yeah, I was just concerned with SQLite not supporting writing + (doing something else at the same time) (if I understand it correctly)

grim vault
#

SQLite on an SSD.

brave bridge
#

huh, that's impressive

#

Can you share the benchmark?

grim vault
#

That's my own importer from an csv backup into a new database.

proven arrow
brave bridge
#

ooh

#

thanks

#

I think I'm underestimating sqlite

brave bridge
#

tested on my computer, when pooling rows in chunks of 1000, I get 100-200k writes per second

#

that's alien technology

mystic vale
#

MB someones can help me. How I can remote connect to Mysql DB using python?

wooden stirrup
#

With mysql.connector

mystic vale
#

Probably some guides

#

Or other stuff.

wooden stirrup
mystic vale
wooden stirrup
#

Show me the code

#

But probably not here

#

(And don't forget not to show the password of the db)

wooden stirrup
main pelican
#

Error:

raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: <pvt>.mongodb.net:27017: [Errno 11004] getaddrinfo failed, Timeout: 30s, Topology Description: <TopologyDescription id: <pvt>, topology_type: Single, servers: [<ServerDescription ('<pvt>.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('<pvt>.mongodb.net:27017: [Errno 11004] getaddrinfo failed')>]>
sullen mauve
#
GRANT ALL PRIVILEGES ON 'typing_gamedb'.* TO 'typing_game' @'%';

It says im doing something wrong? anything obvious?

torn sphinx
#

hello im kinda starting on python if sombady can pls help me with a script :
i need a py script that can create a txt file on the directory i want pls help and ty

inner sentinel
#

Asyncpg or aiopg?

burnt turret
inner sentinel
#

ye

#

and sqlalchemy

torn sphinx
#

Idk rly i just start 🥶

#

@inner sentinel so do u know i look on many website but found nothing

jaunty galleon
#

@burnt turret You said ping you? I have a table called ranks_system, with mebmer_id, guild_id and xp (All are bigint, not pkey or something). How will I make the levels work? A giant dict with xp as key and level as value?

burnt turret
#

why do you need a dict?

#

the level will always be related to the XP right?

jaunty galleon
#

Yeah

burnt turret
#

so you can just have that calculation run when you retrieve the data

#

wdym by dict of xp to level though

torn sphinx
#

@burnt turret can u help me too pls?

#

Once u finish

burnt turret
#

uh what's your question

jaunty galleon
burnt turret
torn sphinx
#

Fuck sry

#

Imma look

#

Ty

jaunty galleon
#

But I want to do it with levels, how will I detect if the user progressed a level? Should I get his xp from before and than after and do the calculations ?

burnt turret
#

wdym by before and after?

#

shouldn't the database be the only source of truth

jaunty galleon
#

Should I also store the level than?

burnt turret
#

how are you planning on calculating the level?

jaunty galleon
#

First, on_message giving the member 10 more xp. Before giving the xp ,I get his data, and than giving him the xp. Than I get the xp after giving him, And doing this calculation:
current_level = round(new_xp ** 0.30)
old_level = round(old_xp ** 0.30)
if current_level > old_level:
new level
else:
not a new level

burnt turret
#

🤔

#

anyways i'd keep it this way itself? i.e calculating the level in python code itself

#

if you're fancy and want the db to do that work you could define a function for it

jaunty galleon
#

Wdym let the python do it?

burnt turret
#

don't store level info

jaunty galleon
#

Yeah I won't

burnt turret
#

whenever you need the level, calculate it with python code

jaunty galleon
#

This is what I sent above

#

Let me do a small code to present what I did

#
@commands.Cog.listener()
async def on_message(self, message):
  old_xp = await a_func_i_made_to_get_the_xp(message.guild.id, message.author.id)
  await update_xp(message.guild.id, message.author.id, 10)
  new_xp = await a_func_i_made_to_get_the_xp(message.guild.id, message.author.id)
  if round(new_xp ** 0.30) > round(old_xp ** 0.30):
    #new level
  else:
    #not a new level```
burnt turret
#

i don't know how to feel about querying the database 3 times for every message 🤔

#

i guess databases are built for that sort of load

jaunty galleon
#

Any better idea maybe?

burnt turret
#

i'd still try reducing the number of queries though

jaunty galleon
#
@commands.Cog.listener()
async def on_message(self, message):
  old_xp = await a_func_i_made_to_get_the_xp(message.guild.id, message.author.id)
  new_xp_test = old_xp + 10
  if round(new_xp_test ** 0.30) > round(old_xp ** 0.30):
    #new_level
  else:
    #same_level
  await update_xp(message.guild.id, message.author.id, 10)
#

Is that better?

burnt turret
#

i guess so lol

jaunty galleon
#

Is there better idea?

burnt turret
#

how is that level system working though

jaunty galleon
#

Wdym?

burnt turret
#

shouldn't the new_xp ** 0.3 always be bigger?

jaunty galleon
#

It's round

#

Oh I get it

#

I am stupid

#

Damn it's harder than I thought

burnt turret
#

i'm too tired to think this through right now 😅

#

i haven't implemented it so this is a rough idea

#

but i'd have some sort of exponential function as you have for determining the amount of xp needed to get to next level

#

then i'd do a single update query, with a RETURNING clause that will return the new xp

#

then i'll check if the new xp meets the conditions for new level

jaunty galleon
#

Yeah but where do I store the levels

burnt turret
#

you don't need to

jaunty galleon
#

How much will I give a user every message?

burnt turret
#

🤔 a fixed amount, you can decide that

jaunty galleon
#

Yeah but if I gave him more than 0.5 it'll be wrong

burnt turret
#

wrong how?

jaunty galleon
#

It'll always go to the next level

burnt turret
#

on what intervals should the levels increase

#

it'd be simple if you had fixed intervals

#

not so simple if you want each interval to be bigger than the last

#

but that's just math

jaunty galleon
burnt turret
#

ah lol

#

maybe a geometric progression could work here 🤔

#

but that math stuff isn't on topic for this channel 🙁

jaunty galleon
#

Yeah

#

I'll get to it later tho

#

Thanks

burnt turret
alpine moon
#

How can I insert something if ID doesn’t exist but Update if ID exists?

proven arrow
alpine moon
#

Got it thanks

chilly nimbus
#

I need a database for my discord bot
I know basic sql does that mean i know postgres, also is psycopg2 easy to use if you know sql basic

proven arrow
chilly nimbus
#

wdym pick it up quickly

#

they arent the same?

#

arent the queries that ill need to send the same

proven arrow
#

Yeah mostly the same, some different syntax between different database systems.

chilly nimbus
#

different wha

#

i spent an hour llearning sql

#

i need to spend another for postgres goddamn

#

i just need to do basic stuff, setting up the table, having unique ids for contents, updating tthe databasee etc

proven arrow
#

Yeah don’t worry it’s good. You don’t need to spend any more. If you have a question where something you learnt doesn’t work you can look it up for Postgres.

chilly nimbus
proven arrow
#

Yes

chilly nimbus
#

alr

#

one more thing

#

could yo please suggest me a postgres video

proven arrow
#

Sql is a language that has a standard. Database vendors can choose to follow it or follow it with some variations in certain places. For example add their own features etc.

chilly nimbus
#

or a tutorial

#

where postgres is used in python, psycopg2 i mean

#

because i am really new in sql

proven arrow
#

Also you should use asyncpg for a discord bot

chilly nimbus
proven arrow
#

It works but psycopg it’s not async module.

chilly nimbus
#

i see

#

ty

proven arrow
wet stump
#

Hello help me

frigid glen
torn sphinx
#

i need help

#

help me to fix this eiro

#

hello

fierce sand
#

hello,sadly i cant help

wet stump
#

Write a query that swaps places for nearby students – 1 to 2, 3 to 4, and so on, if the number of students is odd, you do not need to change its numbering.

chilly nimbus
#

how do i establish a connection to postgres with asyncpg, i have the database in my pc

#

while installing postgresql, after the installation finished, it said failed to load sql models, will i face any issues if i dont resolve this issue, also, whats the problem

chilly nimbus
#

how do i fix this error
could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

spice whale
#

hello I have created an collection with mongoDB and in this is a array of User IDs. Now I want something like that:

if ctx.author.id in db[ids]:

But with the db[ids] its not working.

The Code:

cluster = MongoClient("STRING")
db = cluster["id"]
collection = db["Premium"]
proven arrow
chilly nimbus
soft escarp
#

my mongo db stuck here for while

#

.from mongoDb compass*

dim sluice
#

Damn

#

Bro

soft escarp
#

yes?

torn sphinx
#

am so good at databases

#

feel free to ask me

chilly nimbus
#

@torn sphinx i literally referenced my question, if you wanna answer then answer already

soft escarp
#

still not working i even created test user for that cluster! but still it's stuck in connecting thing!

minor ruin
#
            "name"    TEXT,
            "user_id"    TEXT,
            PRIMARY KEY("user_id")
            );'''```
#

Yes

#

That’s sql code

kind void
#

anyone here using tortoise orm have a better example of using signals? their docs don't really have much information.

short flicker
#

Anyone familiar with SQLAlchemy and doing/maintaining a Type 2 table?
Ideally having SQLAlchemy manage the eff_date, exp_date, and current_ind meta data flags

good articles/videos are more than welcome
thank you for anyone able to contribute

raw blade
#

hello! i'm trying to save a nested dictionary using mongoengine, in doing so i need to create a subclass of mongoengine.Document but my dictionary has integer keys along the way--how do people typically deal with this?

harsh pulsar
#

sometimes there's no option but to recursively walk through a dict and replace values

#

but idk what mongoengine can support - maybe it does the number-to-string conversion for you?

white vortex
#

Hello I'm trying to make a password manager exactly like what kalle hallden made but the change is that the app encrypt the password and put it into the database and decrypt it when I want to show the passwords

#

my program is something like this
: list
DATA IS HERE
wait a sec
: list

ROWID = 1
Service name = facebook
Email = test@gmail.com
Password = secretpasswordlol

ROWID = 2
Service name = youtube
Email = test@gmail.com
Password = PASSWORD

:
looks like this
but in linux if i typed strings database.db it will show the data and the tables
looks like this
SQLite format 3
tableaccountsaccounts
CREATE TABLE accounts (
service DATATYPE text,
email DATATYPE text,
password DATATYPE text)
youtubetest@gmail.com PASSWORD
facebooktest@gmail.comsecretpasswordlol
can I encrypt the password??

harsh pulsar
#

@white vortex encrypt the password in your python application, save the encrypted password to the db

#

Or maybe use sqlite and encrypt/decrypt the entire db at once

cyan yacht
#

Is it possible to subclass aiomysql.connection.Connection and connect to that?

grim vault
#

(user) is not iterable use (user,) or [user]

jaunty galleon
#

tf you changed your nick to @velvet ridge

#

Math is not easy

grim vault
#

datas is either None if nothing is found or a tuple not a 2D array.

#

result = await datas.fetchall() and after that use result.

#

or append .fetchall() to the execute line.

#

Yeah, I think so. I don't use the async version.

#

also data != datas you are using both names

#

maybe you can just:

async for row in db.execute("SELE..."):
  # row is now your selected columns
#

But as I said, I have no clue about the async stuff.

dusty peak
#

Write the SQL sentence that provides an 8% increase in the salaries of all employees working in the 'Internet Banking' project. help me

dusty peak
#

UPDATE EMPLOYEES SET maas= CASE proje_adi WHEN 'Internet Banking' THEN maas+(maas*.08) ELSE maas END WHERE proje_adi IN ('Internet Banking');

#

is it ture?

brazen charm
#

Thats a very complicated query for what it's doing

#

Although this looks awfully alot like a homework / school work question 👀

dusty peak
#

excersice

#

help

brazen charm
#

well

#

firstly

dusty peak
#

We don't see sql in school yet

brazen charm
#

CASE proje_adi WHEN 'Internet Banking' THEN maas+(maas*.08) ELSE maas END you dont need any of this

dusty peak
#

hmm

brazen charm
#

there isnt any point doing that when you have that WHERE proje_adi IN ('Internet Banking'); which is filtering out the stuff you want in the first place

#

going onto WHERE proje_adi IN ('Internet Banking');, you shouldn't use the IN keyword like that

#

it's only a single item so you're better off doing WHERE proje_adi = 'Internet Banking';

subtle drift
#

morning from Greece

jaunty galleon
#

How to handle db that doesn't work? After once or twice after I open the db(PostgreSQL(asyncpg(elephantsql))) but it says:
Too many connections for role "usernameindb"

grim vault
#

Close the connection after you used it? Or keep a global connection to use all the time?

#

and: maas+(maas*.08) = maas * (1 + 0.08) = maas * 1.08

dusty peak
#

keep maybe

#

close

remote plinth
#

pls help

#

i can't access to my server host's terminal so how can i set mongodb path using python

#

if there is a way

#

i can't mongod --dbpath ....

harsh pulsar
#

You usually shouldn't be starting mongod from inside python @remote plinth

#

You don't need to know the db path to connect a client

remote plinth
#

i mean

#

i didnt do mongod --dbpath "path/..."

#

i cant

torn sphinx
#

Does anyone know whats wrong? I use SQLite3

harsh pulsar
#

If its on your physical machine then the hostname is localhost

burnt turret
#

the port should be 27017 by default as well

remote plinth
#

what i need:
i want to set my db path like using mongod --dbpath "the path" but i don't have access so some stuff to do this in my host so im asking if there is a way to set the path using python

harsh pulsar
#

@remote plinth are you trying to start the mongo server?

#

Or connect to an existing server?

remote plinth
#

start

#

if i cant set the path using python, i will switch db i have some sql knowledge ._.

white vortex
#

password manager

harsh pulsar
#

trying to run a mongo server by running a python program might be considered an attempt to circumvent restrictions. are you trying to do this on repl.it or something?

white vortex
#

wut no

harsh pulsar
white vortex
#

when i type list

#

it just return null

remote plinth
#

i can change main file's name..

harsh pulsar
#

"I am using X platform which imposes A B and C requirements."

remote plinth
#

i'll screenshot

remote plinth
#

me

white vortex
#

i'm getting confused

harsh pulsar
#

@white vortex in the discord interface, it should show which post i am replying to

remote plinth
#

here is the startup command, if i start the server it will run the main file

harsh pulsar
#

see the small line above my message? @white vortex that is the message i am replying to

white vortex
#

i'm blind

#

xD

harsh pulsar
#

who is providing this to you?

remote plinth
white vortex
remote plinth
#

the admins can change startup command to me when i claim ticket in their discord server

burnt turret
#

perhaps it would be better to use mongodb atlas in this case then?

naive sandal
#

@dense lily I would ask your question here

remote plinth
#

meh 512mb ..

harsh pulsar
burnt turret
#

what sort of data are you storing? @remote plinth

white vortex
remote plinth
#

512 is enough but would be better if i had in local host

#

ima try sqlite

#

ik both so np

white vortex
white vortex
burnt turret
#

can you give some more context? i don't see a no ratelimits in your code but it is there in your output?

#

can you explain what you want to be doing + what is happening that is not expected

grim vault
#

Have you tried:

async for row in db.execute("SELECT guild_id, user_id, content FROM log WHERE user_id = ?", (user_id,)):
    # row is a tuple(guild_id, user_id, content)
    print(row)
white vortex
#

how can i encrypt a database?????

grim vault
#

add a LIMIT 4 to the end of the statement of the SELECT

#

Do you have a column you can order by?

#
sql_stmt = (
  "SELECT guild_id, user_id, content"
   " FROM log"
  " WHERE user_id = ?"
  " ORDER BY current_time DESC LIMIT 4"
)
async for row in db.execute(sql_stmt, (user_id,)):
    # row is a tuple(guild_id, user_id, content)
    print(row)```
#

descending order to get the newest first

#

The column current_time is used, whatever it is in there.

#

for that user, yes.

white vortex
#

how can i encrypt a database???

grim vault
#

It's highly database dependent.

chilly nimbus
#

"UPDATE channelinfo SET status = $1 WHERE channel_id = $2", status, channel_id

#

when i run this query, its creating a new row instead of updating

dull vessel
#

what problem?

obsidian carbon
#

Anyone familiar with Dictalchemy?

remote plinth
#

database disk image is malformed sqlite error

grim vault
# dull vessel what problem?

Your value will be a tuple of the columns of the paroli table, so -> (1, "123") or maybe even a list of one tuple because you used .fetchall() -> [(1, "123")].
Just print out the value and take a look.

#

And I'm pretty sure if value != text: is not what you want.

dull vessel
#

yeah thanks

chilly nimbus
#

@grim vault

grim vault
chilly nimbus
#

help bro

grim vault
#

How do you know it's the UPDATE which is generating the new row?

chilly nimbus
#

will that make an error

chilly nimbus
#

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UniqueViolationError: duplicate key value violates unique constraint "channelinfo_pkey"
DETAIL: Key (channel_id)=(836218450560417796) already exists.
i am also getting this error when trying to update

#

@grim vault

grim vault
#

also?

#

Whats the table look like?

#

Do you have some sort of SQL-shell to access the database to try out commands?

#

UniqueViolationError looks like an INSERT failure.

chilly nimbus
chilly nimbus
chilly nimbus
grim vault
#

The screenshot looks like another way to access the database.

chilly nimbus
#

When it comes to skills I get only common skills everyone has and when it comes to errors, I get the most rare errors,

chilly nimbus
#

And i can use it as shell

#

Let me try

grim vault
#

Lets try it then:

UPDATE channelinfo SET status = 'err' WHERE channel_id = 50560417796;```
chilly nimbus
#

It updated the values, and when nothing was present, it didnt show any errors but didnt inster anything either

#

But it just doesnt seem to work in python

#

Wtf

dull vessel
#

how to use last value?

#

self.rows[?][1]

grim vault
#

-1 is the index of the last entry in python.

chilly nimbus
#

What do i do @grim vault

grim vault
#

More debugging?

chilly nimbus
grim vault
#

I do it like:

stmt = "UPDATE table SET volumn = value WHERE column = ? AND column = ?"
bind = (value1, value2, value3)
print("SQL:", stmt, bind)
res = curs.execute(stmt, bind)
chilly nimbus
#
async def upchnl(self, ctx, status : str):
        await self.bot.mydb.channel_test(ctx.channel.id, status)
        await ctx.send("Done")
async def channel_test(self, channel_id, status):
        await self.db.execute("INSERT INTO channelinfo (channel_id, status) VALUES ($1, $2)", channel_id, status)
    
    async def up_chnl(self, id, value=15):
        await self.db.execute(f"UPDATE channelinfo SET status = {value} WHERE channel_id = {id}")

🙂

grim vault
#

argl, f-strings in SQL.

chilly nimbus
#

i just thought i was calling up_chnl but i was calling upchnl

chilly nimbus
#

:>>>>>>

#

alr now i can start working on my database

grim vault
#

Yes, and please, no f-strings!

#

Even if you're sure whats in the variable.

chilly nimbus
chilly nimbus
grim vault
chilly nimbus
#

but what is sql injection

grim vault
#

Basicly someone could use a SQL-command as username which can be executed on your database.

#

f"UPDATE users SET name = '{user_name}' WHERE user_id = {user_id}"
-> I enter as username '; DROP TABLE users; --
-> UPDATE users SET name = ''; DROP TABLE users; --' WHERE user_id = 1
-- is the line-comment in SQL like # in python-code

chilly nimbus
#

i see

dull vessel
#

why dont working?

grim vault
#

What do you expect? You select the entry where pass = 123 and that's what you get.

dull vessel
grim vault
#

I really don't know what you want. DELETE to remove entries in the table, UPDATE to change, INSERT to make new one and SELECT to retrieve them. I suggest looking for an SQL-Guide/Tutorial online.

dull vessel
#

i need write 123 = ok
write 333 = ok

grim vault
# dull vessel i need write 123 = ok write 333 = ok

I hate screenshots. But here we go:

pass_input = int(self.lineEdit.text())
check_pass = cur.execute("SELECT COUNT(*) FROM paroli WHERE pass = ?", (pass_input,)).fetchone()[0]
if check_pass > 0:
    # passed the test```
dull vessel
#

go

grim vault
#

SELECT COUNT(*) will count the rows where the given condition is true. Only .fetchone() is needed because COUNT(*) will only produce one line and the [0] is because the result is a tuple and we only need the first entry of it.

remote plinth
#

is 512mb (mongodb) enough? im storing data like prefixes, welcome channels, etc...

brazen charm
#

Err probably

remote plinth
#

k..

brazen charm
#

Like unless you're storing a bunchhhhhh of data I doubt you'll get over 512MB or even close

remote plinth
#

well if its not enough i'll make another acc

#

for another collection hehe

brazen charm
#

I mean that is against ToS and they have things to prevent that but sure

#

I mean you gotta have a pretty big bot and store alot of stuff to get to 512MB really

#

unless you're storing a bunch of images in said DB

remote plinth
#

kk no problem tho

dapper halo
#

Hey guys, please I need help coming up with an SQL code.

I have a table with datetime field (2020-05-26 00:00:00:00),

I want to write a query that groups all similar month and year together and count them.

i.e

Date | Count
2020-05 | 4
2020-01 | 6
2019-04 | 2
2018 - 11 | 8

dapper halo
#

🆘

grim vault
#

What is the database system and what is the column type?

torn sphinx
#

Ive changed to portgresql and i didn't understand whatt i need to change in my code

jaunty galleon
#

Why won't you send the code and we would see what you got?

jaunty galleon
#

Are you sure this is PostgreSQL

torn sphinx
jaunty galleon
#

Ah ok

#

!d asyncpg I would recommend using asyncpg(the only one I know, many people use it)

delicate fieldBOT
torn sphinx
#

but i did not understand what i need to change in my code

jaunty galleon
#

Well, it's a little more complicated

torn sphinx
jaunty galleon
torn sphinx
#

I donwloaded it

#

downloaded*

jaunty galleon
#

There is a free version, yes

jaunty galleon
#

Let me google it one sec

jaunty galleon
#

No, that is to view the data

torn sphinx
#

so how the app called

#

?

jaunty galleon
jaunty galleon
#

When it asks for password, input(I don't think it'll show the password you are writing) to the prompt, and than continue

#

You have a default user named postgres

torn sphinx
#

Oh thats why i didnt see the password

jaunty galleon
torn sphinx
#

Done it

#

Why it doesnt pint nnothig

#

print

#

@jaunty galleon

jaunty galleon
#

It doesn't print CREATE DATABASE ?

torn sphinx
jaunty galleon
#

And just so you know, this is a database, not a table. A database is constructed out of tables

torn sphinx
jaunty galleon
#

You make tables in the python script

torn sphinx
torn sphinx
jaunty galleon
#

Yeah that's weird

torn sphinx
#

I can create with pgadmin too

jaunty galleon
#

Oh try it than

#

Make sure you know the username and password and local host

torn sphinx
#

Ik

#

I create and it worka

#

What now?

jaunty galleon
#

!d asyncpg.pool.Pool

delicate fieldBOT
#

class Pool```
A connection pool.

Connection pool can be used to manage a set of connections to the database. Connections are first acquired from the pool, then used, and then released back to the pool. Once a connection is released, it’s reset to close all open cursors and other resources *except* prepared statements.

Pools are created by calling [`create_pool()`](https://magicstack.github.io/asyncpg/current/api/index.html#asyncpg.pool.create_pool "asyncpg.pool.create_pool").
grim vault
#

The question is why you want to change from sqlite to postgresql? Do you have performance trouble? Do you have size trouble?

torn sphinx
grim vault
#

Ok, that sounds inconvenient.

torn sphinx
jade swan
#

I have this command

    @commands.command(aliases=['acc', 'prof', 'profile'])
    async def account(self, ctx, member: discord.User = None):
        if member == None:
            member = ctx.author

        data = getUserById(member.id)
        print(data)
        return

(it's way longer than just that) I'm trying to print the data that returns because it passes a check that it shouldn't pass since the data should be not found.

This is the getUserById function :

def getUserById(user_id: int):
    results = accs.find({"_id": user_id})
    return results

This is what I get :

<pymongo.cursor.Cursor object at 0x0000026B1D182EE0>

Can someone please tell me what is that?

harsh pulsar
jade swan
#

sorry edited last time

#

because when i do a for loop it says local variable 'result' references before assignment

harsh pulsar
#

you have to iterate over the Cursor object to get your data

#

in this case you should just use find_one instead of find since you are expecting at most one document

#
def getUserById(user_id: int):
    return accs.find_one({"_id": user_id})
jade swan
#

alright let me do it

harsh pulsar
#

if you did need to use find because you were expecting multiple documents, you could use list() to consume the entire cursor and collect the results in a list

jade swan
#

works! thank you!

#

nah im looking for a single document

harsh pulsar
#
cursor = accs.find({"color": "red"})
results = list(cursor)
#

more generally,

cursor = accs.find({"color": "red"})
for doc in cursor:
    # do something with each doc
quaint sky
#

Hello I am new to mssql and originally Ive been using mysql. For mssql unlike mysql, when I try pandas dataframe.to_sql it throws me operational error saying integer cant be with text type. My data contains mixture of numeric values and text values in a column.

My column names are inserted properly and from mssql it seems as it has automatically set the column type as text type. From stack overflow I realized text type does not allow integer, and I should use varchar type.

Is there a way where I can tell mssql that my columns in pandas dataframe will be varchar type from Python?

harsh pulsar
#

@quaint sky you might need to convert the integer values to strings in python before saving

quaint sky
#

I tried df[columnName].apply(str) but I still got tthe same error

harsh pulsar
#

.apply doesn't replace the column

#

what's the name of the column? i can show you an example

#

if the column is called y, you can use df.assign(y=df['y'].apply(str)).to_sql(...)

dull vessel
#

Who can help me pls??

quaint sky
#

Sorry for the late reply! Im currently on my way to work.. Ill try it once I arrive.

harsh holly
#

When implementing data in google colab does anyone know why i get this error? ```AttributeError Traceback (most recent call last)
<ipython-input-10-22a39e0f2182> in <module>()
1 import io
----> 2 dataset = pd.read_csv(io.BytesIO(uploaded['companiesForML'.csv]))
3 dataset.head(10)

AttributeError: 'str' object has no attribute 'csv```

trim marten
torn elm
#

the specific error looks to be caused by your single quote not including the .csv file extension. It thinks you are trying to call a method named csv on your 'companiesForML' string.

harsh holly
#

yea i got that fixed im not sure how i missed that

trim marten
#

Ugh, oh yeah me too 🙃

harsh holly
#

lol

#

thanks tho

trim marten
#

🤣 Am just going to go back to the work I was avoiding

harsh pulsar
#

In a MongoDB aggregation, is it possible to combine these 2 $addFields stages into a single one?

[
  {
    "$addFields": {
      "fooCollection": { "$first": "$fooCollections" }
    }
  },
  {
    "$addFields": {
      "fooItems": "$fooCollection.items"
    }
  }
]

(edited for readability; i don't care about explicitly creating the fooCollection field if there's a way to do it in one step)

woeful garnet
#

hi

#

can i ask for a help?

torn sphinx
#

ad = {'Fatih':'Terim','Ali':'Rıza'}
def kayit(gelen_ad,gelen_soyad):
ad[gelen_ad] = gelen_soyad
print((""""Adınız {} Soyadınız {} olarak kayıt oldunuz.

    """).format(gelen_ad,gelen_soyad))

sor = input("Giriş yapmak istermisiniz? y/n >>>")
if sor=='y':
    giris_kontrol(gelen_ad,gelen_soyad)
else:
    exit()

def giris_kontrol(gelenad,gelensoyad):
s_d = False
for i in ad:
ad_g = i
soyad_g = ad[i]
if gelensoyad==soyad_g and gelenad==ad_g:
print(("""
Giriş başarılı oturum açıldı
Hoşgeldiniz {} {} Bey
""").format(ad_g,soyad_g))
s_d= True
if s_d==False:
print("""Hesap bulunamadı,Kayıt olmak istermisiniz?
y/n
""")
sor = input(">>>")
if sor == 'y':
ad_Sor = input("""
İsim:
""")
soyad_Sor = input("""
Soyad:
""")
kayit(ad_Sor,soyad_Sor)
else:
print("Çıkılıyor")
exit()

def kullanici():
ad = input('Adınız >>>')
soyad = input('Soyadınız >>>')
giris_kontrol(ad,soyad)

kullanici()

#

kebap + I run

little flicker
#

I'm not sure how that would ever work, since rows[0][3] is not 4 in the example you gave

#

Also, the whole bit there is weird. Why iterate through them but only look at the first row?

torn sphinx
torn sphinx
#

I need HELP...I want a tree text file...I have some mp3's and folders in my e drive...each folder has mp3's and subfolders with more mp3's...there r other pdf and other format files as well...I want to create a text file which contains all the mp3's names in lists under album names with album numbers all sorted by hierarchy and order....for example...album1 would contain the names of all the mp3's in the home page of e drive...album2: names of all the mp3's in the 1st folder of e drive...album3 would be names of all mp3's in 1st subfolder of 1st folder...if 1st subfolder has no more sub folders, album 4 would be the names of all mp3 files in 2nd subfolder of 1st folder...if 1st folder has just 2 subfolders then move on to the next folder in e drive and so on...All other files lyk pdf,etc r to be ommited

pseudo lake
#
async def get_last_row_id_in_db(self, table_name):
        last_row_id = (await self.db.query('''SELECT MAX(ID)  FROM ?''', (table_name, )))[0][0]
        if last_row_id == None:
            last_row_id = 1
        else:
            last_row_id += 1
        return last_row_id

gives:

Traceback (most recent call last):
  File "C:\Users\feroz\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "d:\Feroz Programs\FerozCollaborationProjects\ClassBot\cogs\economy.py", line 43, in create_account
    last_economy_row = await self.get_last_row_id_in_db("Economy")
  File "d:\Feroz Programs\FerozCollaborationProjects\ClassBot\cogs\economy.py", line 163, in get_last_row_id_in_db
    last_row_id = (await self.db.query('''SELECT MAX(ID)  FROM ?''', (table_name, )))[0][0]
  File "d:\Feroz Programs\FerozCollaborationProjects\ClassBot\utils\utils.py", line 11, in query
    await cursor.execute(sql,vars)
  File "d:\Feroz Programs\FerozCollaborationProjects\ClassBot\asqlite.py", line 144, in execute
    return await self._post(self._cursor.execute, sql, parameters)
  File "d:\Feroz Programs\FerozCollaborationProjects\ClassBot\asqlite.py", line 53, in _call_entry
    result = entry.func(*entry.args, **entry.kwargs)
sqlite3.OperationalError: near "?": syntax error
#

cant "?" be used like that?

#

and if it cant is there anyway i can dynamically provide the table name?

tribal cargo
#

Hej hej, I try to use csv Data to create data in my database. I'm using Flask and SQL alchemy. I tried this code:

#

def add_romms():
    tempsession = Session()
    with open('rooms.csv', 'r') as csv_file:
        csv_reader = csv.reader(csv_file)
        next(csv_file)
        for row in csv_reader:
            new_room = Room(roomnumber=row)
            tempsession.add(new_room)
        tempsession.commit

pseudo lake
#

?

tribal cargo
#

and I created the following Error:

long peak
#

how would you go about designing the tables where a reservation can only have 1 coupon

the top? or the bottom but with a unique constraint on the reservation_id?

tribal cargo
#

I thought I could asked about python here

#

It seems my table is view only.

proven arrow
long peak
#

there's a coupon table for that, thus the coupon_id FK

proven arrow
#

Then the top

#

Have the foreign key coupon_id inside the reservations table.

long peak
#

ignore the FK on the redeeemed_date, my mistake XD

long peak
proven arrow
#

Redeemed is at the time of booking no?

#

Seems redundant to me

long peak
#

you're right though doing that would prevent us from adding more metadata on the reservation coupon but I guess I'll just go and ask upfront the CTO what are these fields he's anticipating 🤔 but if we assume, the fields would grow, do you think the design looks good? @proven arrow

proven arrow
#

The extra table would have the booking_id, coupon_id, and your other columns you want to store data about.

long peak
#

which is the bottom one in the screenshot, right?

proven arrow
#

Yeah looks like that. But not sure why you have a foreign key on the so called metadata

long peak
#

the reedeemed_date having a FK there is a mistake haha, kindly ignore that xD

proven arrow
#

Oh yeah makes sense then

#

Well you have your options. Choose one that works for you. Generally there is no right/wrong apart from the basic rules, and there can be multiple way of doing the same thing. It’s a matter of picking the one that suits your business requirements best.

long peak
#

either really works thus the confusion haha, but one thing for sure that works for now best is the top one so I think I'll go for that and keep things simple, I'm having a hard time imagining what other metadata could be added there as well tbh

#

aight, thanks for the input @proven arrow and have a good day ahead!

elder elk
#

I'm inserting data into DB using json files
with os.walk() but after some times speed decreased
any solution for this

delicate fieldBOT
#

Hey @elder elk!

Uh-oh! It looks like your message got zapped by our spam filter. We currently don't allow .txt attachments, so here are some tips to help you travel safely:

• If you attempted to send a message longer than 2000 characters, try shortening your message to fit within the character limit or use a pasting service (see below)

• If you tried to show someone your code, you can use codeblocks
(run !code-blocks in #bot-commands for more information) or use a pasting service like:

https://paste.pythondiscord.com

elder elk
#

please find link for code that I'm using

elder elk
#

millions

#

at starting it shows good speed

#

but as time passes its get slower

torn sphinx
proven arrow
#

Well your making a call to the db for each insert. And since each insert comes with overhead, and so for millions of items that will obviously add up. Generally the best is to use executemany() which inserts many rows at a time, in a single db call.

elder elk
#

can please suggest correction in code, use above link @proven arrow

proven arrow
#

Not sure what psycopg2 has for the executemany function but you can look it up in their documentation. It should be called something like that.

proven arrow
torn sphinx
torn sphinx
proven arrow
#

Change from what?

torn sphinx
#

sqlite

proven arrow
#

Well what library do you intend to use for this, and what changes have you made already to your new database?

brave bridge
#

How can I use an SQLite version different to what Python was built with? I have built an sqlite3 binary myself.

brave bridge
#

Ubuntu 18.04

proven arrow
#

Oh, not sure about linux systems, but for windows you just replace the DLL file in the python installation folder. For linux it is probably similar but just different file type.

brave bridge
#

the website provides a 32-bit binary, which doesn't work on my VPS

#

so I built it from source

#

oh, I figured it out

#

there's a pysqlite3 pypi package which uses the system binary, apparently?..

#

or something like that

#

it has the right version

#

Huh, for some reason pysqlite3 doesn't want to install on my VPS.

#
# python -m pip install pysqlite3
python -m pip install pysqlite3
Collecting pysqlite3
  Using cached pysqlite3-0.4.6.tar.gz (40 kB)
Using legacy 'setup.py install' for pysqlite3, since package 'wheel' is not installed.
Installing collected packages: pysqlite3
    Running setup.py install for pysqlite3 ... done
Successfully installed pysqlite3
brazen charm
#
Using the binary package

A binary package (wheel) is available for linux with a completely self-contained pysqlite3, statically-linked against the most recent release of SQLite.

$ pip install pysqlite3-binary
#

why this over the inbuilt setup though?

brave bridge
#

and I need 3.35

#

I think I'm figuring it out

#

okay, I give up. I'll use lastrowid instead of RETURNING

jaunty galleon
#

Can anyone help me with a PostgreSQL elephantsql problem? Not really python-related

#

It's an asyncpg error

wet stump
#

Hello help me

#

with postgresql

#

pls

civic shale
wet stump
#

My query gives me bad answer

civic shale
#

Let's see it.

wet stump
#

How many clients were activated and deactivated during June of 2013. Please provide both numbers as a result of one query.

#
select count (case when status = 'ACTIVATED' then 1 end) as activated
,     count (case when status = 'DEACTIVATED' then 1 end) as deactivated
from      exaster
where      status in ('ACTIVE', 'DEACTIVATED')
and     ( to_char (deletion_date, 'YYYY-MM') = '2013-06' and to_char (activation_date, 'YYYY-MM') = '2013-06') 
;```
civic shale
#

And it returns what?

wet stump
#

Data sample

civic shale
#

And what are you trying to get it to return?

wet stump
#

Count of activated and deactivated persons during 2013-06 period

civic shale
#

Hmm

wet stump
#

Something is wrong with my query

civic shale
#

Yes.

#

I can't see where.

#

Sorry.

#

The only thing I can think of is the deletion and activation dates

#

Are they made to be the same?

wet stump
#

maybe

#
select count(*) as activated, count(*) as deactivated from exaster where status='ACTIVE' OR status = 'DEACTIVATED' and SUBSTRING(TO_CHAR(deletion_date, 'YYYY-MM-DD'), 1 , 7)='2013-06' and SUBSTRING(TO_CHAR(activation_date, 'YYYY-MM-DD'), 1 , 7)='2013-06'```
#

I tried this also

#

Result

grim vault
#

How about sum() instead of count and add a else 0 in the case.

wet stump
#

Seems not helped

grim vault
#

Some databases have a filter option for aggregate function.

wet stump
#

which one?

#

I am on postgresql

grim vault
#

Yes it does have them, at least v13 docu says so.

#

aggregate_name (expression [ , ... ] [ order_by_clause ] ) [ FILTER ( WHERE filter_clause ) ]

wet stump
#

never used this

grim vault
#

count (*) filter (where status = 'ACTIVATED') as activated ? I never used it myself.

wet stump
lusty inlet
#

I suggest taking this a step at a time -- e.g. first take a look without aggregating, so you can be sure that the where is doing what you want

wet stump
#
 select count (*) filter (where status = 'ACTIVATE') as activated
,     count (*) filter (where status = 'DEACTIVATED') as activated
where ( to_char (deletion_date, 'YYYY-MM') = '2013-06' and to_char (activation_date, 'YYYY-MM') = '2013-06') 
;
grim vault
#

no from?

#

and add deto the second activated :)

wet stump
grim vault
#

ok, make a select * from ... as Matt suggested.

wet stump
lusty inlet
#

missed your * between select and from

#

I guess postgres doesn't mind just quietly selecting nothing at all lemon_grimace

grim vault
#

But we now know that the 0, 1 result were correct.

wet stump
lusty inlet
#

you're only catching rows with activation AND deletion that month -- is that intended?

wet stump
#

How many clients were activated and deactivated during June of 2013. Please provide both numbers as a result of one query.

lusty inlet
#

Hmm, ambiguous wording in the first sentence, but.... if you're going to have two different numbers I have to assume that these are separate groups. (1) how many clients were activated? (2) how many clients were deactivated?

wet stump
#

In one query

lusty inlet
#

... so you want the date filter to pick up OR

wet stump
#

how

lusty inlet
#

That way you're grabbing both (1) and (2)

wet stump
#

how

lusty inlet
#

right now you have where (... and ...) --- change to where (... or ...)

wet stump
grim vault
#

Now add the count(*) filter .. again

lusty inlet
#

thinking about it having a look over the data, it probably makes sense to just move the where conditionals up into sum()s instead.
select sum(case when [activation_date in june 2013] then 1 else 0 end) as activated, etc.

#

current(?) status seems unrelated to the question

grim vault
#

Looking at <#databases message> the data is not very clear. Is the deletion_date set also on deactivation? It doesn't seems to get set for suspended.

untold quartz
#

How do I SELECT from a table where (the "last name" and "first name" columns concatenated together with a space in between) contains a certain substring?

#

in Microsoft SQL Server

waxen temple
#

you can use concat and check against it, just replace = :your_string with sub string matching

SELECT 
  * 
    
FROM 
  person p
   
WHERE
  CONCAT(p.first_name, ' ', p.last_name) = :your_string;
cold ocean
#

this is my messages table. is there a way to make it so when doing "SELECT * FROM messages WHERE sender = ?" it will return the number of row it is opposed to the id colomn?

#

like for row 4 that has an id of 36 it will return 4 instead of 36

proven arrow
fleet mountain
#

can someone help me setup pymongo

quaint sky
#

Hello, I have a new question. In MySQL we can change the innodb pool size so that it doesnt cause memory error but I cant find how to change the setting in MSSQL. I searched on google and people recommended splitting the dataframe and send it to sql. However, I want to find a way to increase the pool size for mssql so that it doesnt cause memory error when I use pandas df.to_sql.

lusty inlet
quaint sky
nimble wyvern
#

So i tired to add new field life in to collection (used mongodb)

collection.aggregate([
    { "$addFields" : { "life" : 1}}
])

It will run but no changes seen

nimble wyvern
eager cloak
#

is there any way to write into existing excel file and keep the original formatting intact?
openpyxl and xlsxwriter engines in pandas are removing that existing formating

cyan yacht
#

I'm using aiomysql
Is it possible to subclass aiomysql.connection.Connection and connect to that?
So that I can have different functions as methods for the subclass

tight smelt
#

I need a free database which is simple to use for my discord bot. Its going to be a small currency system

#

recommendations please

cold ocean
wet stump
wet stump
grim vault
#

Haven't we solved this yesterday? <#databases message>
Your where clause was wrong the count filter should work now.

midnight birch
#

how can i store bytes using sqlalchemy

grim vault
# wet stump count(*) filter?

Yes, this one:

select
  count(*) filter (where status = 'ACTIVATED') as activated,
  count(*) filter (where status = 'DEACTIVATED') as deactivated
from exaster
where (
  to_char(deletion_date, 'YYYY-MM') = '2013-06' or
  to_char(activation_date, 'YYYY-MM') = '2013-06') 
;```
wet stump
grim vault
#

I'm not sure using the status column is the correct way, but the questions and dataset is a little vague.

#

And what should I do with the screenshot? It tells me nothing.

wet stump
#

Ok so what could be correct answer?

grim vault
#

Do you have a deactivation_date column?

wet stump
#

yes

grim vault
#

What about:

select
  sum(case when to_char(activation_date, 'YYYY-MM') = '2013-06' then 1 else 0) as activated,
  sum(case when to_char(deactivation_date , 'YYYY-MM') = '2013-06' then 1 else 0) as deactivated
from exaster;```
wet stump
#

Answers are different

grim vault
#

Of course. status can now be ACTIVE for rows where deletion_date is 2013-06. The difference is expected. I don't know what the correct answer is.

#

And deletion != deactivation

wet stump
#

So with sum is better?

grim vault
#

From my perspective sum() is the better choice, yes.

south marlin
#

Would here be the best place to ask on how to structure my project with Flask and SQLAlchemy? Specifically on abstracting the router, db models, and other aspects of the ORM.

wet stump
#

Query by which you could extract active clients at the 2013-06-19.

#
select * from exaster where status='ACTIVE' and activation_date='2013-06-19';```
#

I used this query

#

How many active clients had more than one SIM card on 2013-06-19. Unique client could be identified by using unique device information (prepaid customers are usually not identified in the systems).

#

why null?

proven arrow
wet stump
#

I am having issue

#

I am getting null

#

It is

#

hmm weird

wet stump
grim vault
#

You should start with an SQL tutorial. In this case you are missing the from so the database has no clue which table you are referencing.

wet stump
#

Oh sorry I miss it.

#

With from also same situation

grim vault
#

Look closely at the HINT line.

wet stump
#

Yes I looked

#

I tried this also

lusty inlet
#

there's a space in the column name 🤦‍♂️

wet stump
lusty inlet
#

there's something worse than a space in the column name 🤦

wet stump
#

I have no idea what can cause it?

grim vault
#

loose the exaster. or quote the column and column name separately.

lusty inlet
#

ah of course it thought you were trying to specify the column exaster.imei_a of the table exaster when you put the whole thing in quotes

wet stump
#

So I need to separate?

oak carbon
#

can anyone help me that why is that tz info is coming ?

wet stump
#

Seems working but very strange

brazen charm
#

Not really strange

#

Table got created with a space in it

#

so it expects that space

grim vault
#

s/table/column

oak carbon
#

because of that tzinfo i can not compare time

grim vault
#

One is a naive datetime and the other is a timezone aware one. How do they get assigned?

oak carbon
#
def ist_time():
    time_now = datetime.utcnow()
    ist_time = time_now + timedelta(hours=5, minutes=30)
    return ist_time

premium_time=datetime.datetime(2021, 5, 27, 20, 20, 26, 564740), premium_expire=datetime.datetime(2021, 6, 26, 14, 50, 26, 564740, tzinfo=datetime.timezone.utc)

#

they get saved like this ^

grim vault
#

Shouldn't be. There must be something else.

>>> from datetime import datetime, timedelta
>>>
>>> def ist_time():
...     time_now = datetime.utcnow()
...     ist_time = time_now + timedelta(hours=5, minutes=30)
...     return ist_time
...
>>> a = ist_time()
>>> b = ist_time() + timedelta(days=30)
>>> a
datetime.datetime(2021, 5, 27, 21, 36, 2, 775298)
>>> b
datetime.datetime(2021, 6, 26, 21, 36, 2, 776296)
>>> ```
#

Use datetime.now(timezone.utc) it's better to have an aware datetime.

oak carbon
#

yh i even tried printing it before inserting in db it its printing in good format

#

but when i save the tz thing come

grim vault
#

Maybe your database driver has some datetime adapter.

wet stump
#

Hello

#

Why I am getting null?

#
SELECT 
 account_id ,
 COUNT(*) count
FROM 
 exaster
GROUP BY
 account_id,
 "exaster"."imei_a "
HAVING
COUNT(*) > 1;```
#

How many active clients had more than one SIM card on 2013-06-19. Unique client could be identified by using unique device information (prepaid customers are usually not identified in the systems).

#

Oh sorry I forget

#
SELECT 
 account_id ,
 COUNT(*) count
FROM 
 exaster
 where status='ACTIVE' and activation_date='2013-06-19'
GROUP BY
 account_id,
 "exaster"."imei_a "
HAVING
COUNT(*) > 1;```
harsh pulsar
#

what's the point of that HAVING?

wet stump
#

had more than one SIM

harsh pulsar
#

oh, it's > not >=

grim vault
#

@oak carbon Try:

def ist_time():
    time_now = datetime.now(timezone.utc)
    ist_time = time_now + timedelta(hours=5, minutes=30)
    return ist_time
harsh pulsar
#

you have a space in your imei_a column

#

did you really mean "imei_a ", or "imei_a"?

grim vault
wet stump
#

"ime_a "

grim vault
wet stump
#

That column works with queotes

harsh pulsar
#

there's a literal in the column name?

wet stump
#

Main info about the columns of a prepaid customer base:
-- account_id - unique SIM / Client ID
-- msisdn - phone number of the client
-- activation_date - service activation date
-- deletion_date - service deactivation date
-- status - the newest/current status of the service/client
-- imei_a - unique phone/device ID (IMEI).
-- device_brand - Phone brand
-- device_model - Phone model
-- device_type - Phone type

harsh pulsar
#

ouch

wet stump
#

Column is find

#

Now problem with null data

#

Output is null

grim vault
#

It's and artefact from some csv conversion, would be my guess.

harsh pulsar
#

are you getting 0 rows? or data containing a null unexpectedly?

wet stump
#

I am getting null rows of query

#
SELECT 
 account_id ,
 COUNT(*) count
FROM 
 exaster
 where status='ACTIVE' and activation_date='2013-06-19'
GROUP BY
 account_id,
 "exaster"."imei_a "
HAVING
COUNT(*) > 1;```
oak carbon
#

asyncpg.exceptions.DataError: invalid input for query argument $6: datetime.datetime(2021, 5, 27, 21, 46, 1... (can't subtract offset-naive and offset-aware datetimes)

wet stump
harsh pulsar
#

this works just fine

oak carbon
#

@grim vault i tried checking the db query, this was the error

grim vault
#

Ups.

#

I would suggest using timezone-aware datetime (if possible).

lusty inlet
# wet stump I am getting null rows of query

again recommend that you first form your un-aggregated query, be sure you're capturing the cases you expect with where -- then once you're sure that it working, aggregate as you want it

ebon skiff
#
Traceback (most recent call last):
  File "C:\Program Files\PostgreSQL\13\pgAdmin 4\web\pgAdmin4.py", line 98, in <module>
    app = create_app()
  File "C:\Program Files\PostgreSQL\13\pgAdmin 4\web\pgadmin\__init__.py", line 347, in create_app
    if not os.path.exists(SQLITE_PATH) or get_version() == -1:
  File "C:\Program Files\PostgreSQL\13\pgAdmin 4\web\pgadmin\setup\db_version.py", line 19, in get_version
    return version.value
AttributeError: 'NoneType' object has no attribute 'value'
``` got this error in pgadmin, solved when removed, folder 'pgadmin' inside appdata->raoming.
I would like to know why it solved it.
wet stump
#

As I understand account_id can have more than one imei_a

lusty inlet
#

I like helping out with the occasional query or weird spot, but to be honest @wet stump you just have a lot to learn and this probably isn't the best venue for you to work through learning SQL basics

wet stump
#

I know basics, I am using sql not first year, previously worked as pl/sql developer

#

I used also advanced queries, I also was in reverse engineering, data modeling, database programming

lusty inlet
#

I did not intend to insult you.

#

And I'm not the boss of this channel, just voicing my personal view as I've been one of the people helping to solve your questions

wet stump
#

Ok thanks for helping

#

I still don't get where is a problem

torn sphinx
harsh pulsar
#

so it's hard to help beyond this point, without more information

wet stump
#

Null data, no records. I thought to get number of how many accounts have more than one sim

#

imei_a is device id

proven arrow
#

Can you provide a minimal reproducible example?

wet stump
#

So each account_id can have several imei_a id

#

which corresponds to sim

proven arrow
#

Not screenshots, but actual code/fiddle someone can use

wet stump
#

SELECT 
account_id, "exaster"."imei_a " ,
 COUNT(*) count
FROM 
 exaster
 where status='ACTIVE' and activation_date='2013-06-19'
GROUP BY
 account_id,
 "exaster"."imei_a " 
HAVING
COUNT(*) > 1```
#

I wrote query which generate null zero records

#

Active members with active date

proven arrow
#

Thats not reproducible, doesnt tell us anything of how the table looks or what data it contains

wet stump
proven arrow
wet stump
#

Here is data sample

#

account_id - unique SIM / Client ID
-- msisdn - phone number of the client
-- activation_date - service activation date
-- deletion_date - service deactivation date
-- status - the newest/current status of the service/client
-- imei_a - unique phone/device ID (IMEI).
-- device_brand - Phone brand
-- device_model - Phone model
-- device_type - Phone type

proven arrow
wet stump
#

Can you access postgresql?

proven arrow
#

No i dont use postgres or have it installed

wet stump
#

I mean I running in server

#

postgresql admin

#

How many active clients had more than one SIM card on 2013-06-19. Unique client could be identified by using unique device information (prepaid customers are usually not identified in the systems).

#

each account having more than one sim card (device id)

patent cove
#

how do i get it so it doesnt replace privious data

def save():
    with open("journalEntrys.json", "w") as json_file:
        maintext = maintextbox.get(1.0, "end-1c")
        nametext = namebox.get(1.0, "end-1c")
        date = datetime.datetime.utcnow()
        date = date.strftime("%m/%d/%Y, %H:%M:%S")
        data = {
            "name": nametext,
            "text": maintext,
            "utc-time": date
        }
        json.dump(data, json_file)
lusty inlet
#

right now you replace the existing file on the last line with json.dump(data, json_file)

#

and if you change the open line to
with open("journalEntrys.json", "r") as json_file: you'll just open the file to read, not write

torn sphinx
#

Can I use .sql files to run SQL queries with PostgreSQL and SQLite3?

#

But

#

With placeholders.

#

I have a class that is an abstraction of SQLite3 async bridge library methods and I'm wondering if something like this seemed valid if my .sql file looked like the one at the second code block:

async with Database() as db:
  sql_payload = ('here\'s', 'some', 'data')
  await db.run_script('sqlscripts/my-script.sql', payload=sql_payload, commit=True)
INSERT INTO 'someTableName' (
  'huh',
  'hm',
  'oh'
) VALUES(?, ?, ?);```
#

If I wanted to insert here's into someTableName>huh and all the way through all values into their respective columns.

wet stump
#

Hello

#

Help me pls

#

I am getting blannk output

torn sphinx
#

Hi Youall, Do you have any advice for data engineer to pick up python quickly , any courses you recommend

prisma girder
prisma girder
torn sphinx
#

Like executing psql queries?

#

Which are only for PostgreSQL.

prisma girder
# torn sphinx What do you mean by database-specific features exactly?
torn sphinx
#

Oh that is out of scope of my knowledge of SQL but thank you.

#

Time to port everything into .sql files cause I hate integrating SQL with Python.

prisma girder
#

I am using ORM instead of writing SQL queries

torn sphinx
#

ORM?

prisma girder
#

Object Relational Mapping

#

Like SQLAlchemy

torn sphinx
#

OH

#

That thing, well idk how good it is so ykyk.

#

I'll check it out tho I know that Flask had that, maybe it'll fit me better.

prisma girder
#

You can define model

class Address:
  country: str
  city: str
  street: str

class Person:
  name: str
  address: Address

Then you can create Python objects without writing SQL queries

session = ...
address = Address(...)
person = Person("John", address)
session.add(person)
session.commit()
torn sphinx
#

Yeah

prisma girder
#

However you should start with raw SQL queries to understand what is going on and how to work with relational databases

torn sphinx
#

I agree even with the ORM model stuff I'll still need to know how to create databases and stuff.

prisma girder
torn sphinx
#

I barely know how to create a database and let a particular user manage it.

prisma girder
#

I mean tables

#

Not database at all

torn sphinx
#

Oh

#

I have a few database table creation scripts.

prisma girder
#

It's good point to start

torn sphinx
#

I suppose, thank you.

prisma girder
#

You're welcome!

torn sphinx
#

Just to clarify

#

InsertBoosters.sql > sql INSERT INTO "boosters" (serverId, userId, personalRoleId, teamRoleId) VALUES(?, ?, ?, ?);

#

Will work with the paired example of passing in the payload right?

#

Yea

prisma girder
lapis snow
#

Can someone teach me how to make a database?

torn sphinx
#

Looks good

#

Although I should rename each file to an operation so it ends up looking like Select.sql and not SelectBoosters.sql cause that's unnecessarily long ;-;

prisma girder
torn sphinx
#

Yuh

prisma girder
torn sphinx
#

Didn't think that I'd create separate dirs for the scripts at first lol.