#databases
1 messages · Page 184 of 1
I don't think these columns should be unique in this table (Customer_Orders) because the customer might want to use the same payment for different orders?
In customer order table
Customer payment methods id and order status code are given as foreign key
Should we create foreign key for that
Looking at the schema it seems like there are some tables missing. A foreign key allways neeeds to reference an existing unique constrain.
And to be forthright I have no interest in solving this problem right now.
I just wanted to give you a general direction so you might be able to solve it yourself.
Iam trying to figure
Good luck.
Hey, question. When using Wildcards in Jupyter notebook is the query lookup case sensitive? For instance is it necessary for me to LOWER my records in a name field? Say I'm looking for records which have J/j
Will a Look up LIKE '%J% find both 'j' and 'J'?
It's database dependent. SQLite does support a NOCASE collation (for ASCII chars).
i see, thank you!
i have been using mongodb and it's slow in responding what do you suggest i use now
i need help with my economy discord bots pay command. this is the code:
https://pastebin.com/RJe4mafJ
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
and this is the error i get: Traceback (most recent call last): File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke await ctx.command.invoke(ctx) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke await injected(*ctx.args, **ctx.kwargs) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: no such column: getter_id
Hi there, I’m making a discord game bot at the moment, so I’m using the repl.it database however I have no idea how to split everyone’s progress into different profiles so when a cool down starts for one person it’s also for all other players
I wasn’t sure which server to put this in as it’s to do with databases and discord bots but thought it was more relevant here
Hey, for those who understand english well. I don't Please help me understand what it is that I'm supposed to figure out.
I have a DB of many domestic carriers. And I'm supposed to figure out which of the carriers have the 'best OPT' now apparently OTP is defined as the 'Rate of On-time flights with a 15min buffer on departure and arrival.'
I don't know how to what that last statement means nor how to figure out what the best OTP is.
This is my ER Diagram
With the data you have you need to calculate for each flight how often they are delayed. Group that by the carrier to then which you can sort by the best .
Oh, I think I understand when you put it that way. So the 'Best' in this case be the carrier which returns the carrier with the least 'delays'?
or lowest rather
Of course. No delay means happy customer right 🙂
Thank you!!
One question
There is a schema with tables names and column name and they are connected
In one of a table's column is marked as foreign key but there is no same colomn in other tables
How to define it as foreign key
Hi there, I’m making a discord game bot at the moment, so I’m using the repl.it database however I have no idea how to split everyone’s progress into different profiles so when a cool down starts for one person it’s also for all other players
I wasn’t sure which server to put this in as it’s to do with databases and discord bots but thought it was more relevant here
It doesn’t have to have the same name to reference a foreign key.
I'll explain
Tab1
Id int ;
Tab2
Foreign key(name) references tab1(name)
Will that works?
No, because you don’t have a column called name in the table 1. It should reference id. And also the column of table 1 you want to reference should be either the primary key or a unique key.
Constraints
?
Yes there is a primary key
There are two types of foreign key
1 connects other table directly
And another with reference connects
Am i right
There’s only one type.
To connect two tables the column in tab1 must be primary key ?
Already answered.
Primary or unique
Got it
From this schema
In customer order table
The customer payment method id is mentioned as foreign key
But there's no column in other tables
How should I create foreign key
Going off naming conventions and looking at the FK, I would expect a payment methods table. I don’t see one, or any payment/transactions related tables so you would need to add them in.
This is the question from assignment
Ok thanks
so just create column without mentioning foreign key
Thanks a lot sir
No i did not say that. But ok.
Any other solution there?
I told you what to do.
You want to store payment data so create payment related tables.
Or tell your teacher to give more detail.
We have to create table from the given schema only so this is good
Just leaving column without foreign key
What is the difference between "joins" and "Foreigns Keys". I have a hard time understanding how to insert data into my Database with a foreign key.
But It seems, I can get everything I need without a Foreign Key and with only Joins.
Joins is used to display the tables
Foreign keys are connect with other table so that we can join two tables
Yeah I think I know what Foreign Keys do, but I dont understand how to use them correctly. And I think the "Join" method can do similar things.
Even if this solution is not the best
Joins are 4 types they are distributed into sets method
Left outer join
Inner join
Full join
Right outer join
yes
We can create Foreign key only if
Other table's column is either primary key or unique
Join method is just to display tables of both with the methods
Yes. I know. I dont understand how I can "INSERT" data.
I am reading the MYSql doc. right now
problem solved 🙈
Yes you can get everything without the foreign key. But you would use a foreign key to enforce integrity and consistency over the data. When making relationships between tables this is quite useful. Most importantly when you assign a foreign key it helps to ensure the parent/child relation actually exists (i.e, you can’t reference a value that doesn’t exist). Also for actions like cascading it is very useful (for example, deleting a parent should delete child rows).
Yeah I just found this out. So I got Foreign Key kinda wrong.
But one question I have right now.
I have my Tabel one with lets say
User:
id, name, age
and
Table2:
id, user_id INT, FOREIGN KEY (user_id) REFERENCES User(id), text
I now want to print every user where "text = "hello"".
I thought I could do it with
SELECT * FROM User, Table2 WHERE User.text = 'hello';
This kinda works, it shows the right Users, but every user very often. Basically every id and user_id get assigned to every user.
So
User id user_id
User1 1 1
User1 2 2
User1 3 3
User1 4 4
User1 5 5
I thought the Foreign Key would assign the exact user_id to the id from the first table.
It shows every possible combination and I dont know why.
The foreign key generally relates to the primary key in another table
yes. So I probably created the second table wrong?
with the information you gave, I assume that id is the primary key in table one
Do I create a Foreign Key like this
CREATE TABLE Data_used (id int PRIMARY KEY AUTO_INCREMENT, user_id INT, FOREIGN KEY (user_id) REFERENCES User(id) ,test boolean DEFAULT false, server_id BIGINT NOT NULL)
(The Tabel User was created before with id as Primary Key)
With that, it would reference a column in another table with a different primary key. In your example, it would reference user_id in table 2. So for each unique user in the first table there will be an entry in the second table with the users unique id
yes
I would put foreign key at the bottom
@rocky arch but you have not specified in the query what columns to join on
ahhh so I probably have to change the Tables? So that my User Table has the Foreign key.
ah yeah he didn’t
And no idea what data you are expecting. Not sure I understand what the requirement is
I was confused at your select query there
I want to see every user where "test = false/true".
I changed some stuff from the original code to make it easier to understand, but its probably harder now...
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
this is an example of what the table should look like. It’s mostly preference to it at the bottom but I generally see FK at the bottom like this
Oh Okay I thought on the top.
You should always show the code you have rather than replace with dummy names. Also personal preference but INNER JOIN reads better.
Okay I will send a screen of the real data. I didnt want to confuse you, sorry.
You have to join tables
So Foreign is really just to secure your data?
I thought I "connected" the Data.id with my Data_used.id
I thought I already mentioned this earlier, but you didn’t specify the columns to join on @rocky arch
So it’s resulting in a cross join which is what you described with the result your currently getting
Yes that makes sense
But what is the difference between these two?
Only the last line is different
once with INNER JOIN and once with WHERE. The result is the same
no it isnt. oh no
I am sorry guys, sql just confuses me 😬
if anyone knows how to work with postgresql, please ping me
What specifically are you looking for? Just asking with help for a variant of SQL doesn't really tell us if I know/don't know this one off feature of Postgres @torn sphinx
well im trying to set it up
i want to create tables and im currently trying to do it through 3rd party platforms and nothing's working
i tried pgAdmin
now im trying Forest Admin and i cant seem to get it going
so i want someone to help me set up things
@somber iron
So are you trying to do it through like a GUI outside of python, that's a little outside of my wheelhouse. I would recommend the python package like sql alchemy for sql syntax if you're willing to do it through python
I typically use mssql/sqlite3, so anything specifically for postgres that's a gui I am going to be of little use.
well the only reason im trying to use guis is because idk sql
like i dont need a lot of help
all i need to know is how to create tables and how to insert values in them
@somber iron
@torn sphinx https://tableplus.com is easy to use with a simple UI.
ok ill try ty
@proven arrow oh dude, its already working
i already connected it
this seems to be an amazing program
tysm
@somber iron what are lists in sql
i cant find arrays
these are the options for data types
Why is my input from python not getting reflected in my database in MySQL
I can share my code
The code is working perfectly fine except for the fact that it’s not doing its job
@celest sleet so how do i do it?
What I've seen is like some sort of delimited list, then you call split on the result.
does the package you're using require commit() to be called on the data to save?
Yes, lets take a look
import mysql.connector #this is used to connect our mysql database
from datetime import date # this is used to get date and time
fine_per_day =5.0 #global variable
def clear():
for _ in range(1):
print
#This allows us to add Details regarding books, such as the Name, Author, Publisher
def add_book():
conn = mysql.connector.connect(
host='localhost', database='library', user='root', password='password')
cursor = conn.cursor()
title = input('Enter the Name of the Book :')
author = input('Enter the Author of the Book : ')
publisher = input('Enter the Publisher of the Book : ')
pages = input('Enter Number of Pages in Book : ')
price = input('Enter MRP of the Book : ')
edition = input('Enter Edition of the book ')
copies = int(input('Enter number of copies : '))
sql = 'insert into book(title,author,price,pages,publisher,edition,status) values ( "' +
title + '","' + author+'",'+price+','+pages+',"'+publisher+'","'+edition+'","available");'
for _ in range(0,copies):
cursor.execute(sql)
conn.close()
print('\n\nCongratulations!!! A New Book has been added successfully')
wait = input('\n\n\nPress X to proceed')
This is just a part of it.
not sure what u meant there but what should i select from that list?
Small chunk
by default auto commit is off, and I don't see you turning it on.
you can do text, then insert a delimited list to that one field, then when you retrieve it you can call split on the data to get the list.
if you have cursor.commit() after cursor.execute() does it work?
also, it's recommended not to directly concat values for sql calls (Sql injection), most packages let you do substitutions. Not sure about the mysql package though.
whats a delimited list?
Nope (but I think I might be doing it wrong)
!e
var_a = "a|b|c|d|EF"
print(var_a.split("|"))
@somber iron :white_check_mark: Your eval job has completed with return code 0.
['a', 'b', 'c', 'd', 'EF']
@torn sphinx ^
I will try to make auto commit true and see if things change
so just a normal list?
It's delimited with a pipe. see that var_a input
so which one is that delimited list
- var_a = "a|b|c|d|EF"
- ['a', 'b', 'c', 'd', 'EF']
var_a is the delimited list, that would be the value in the DB.
could i just do repr(mylist)?
instead of adding this |
i could store as repr(mylist) and then when i pull it back do list(mylist), no?
@somber iron
cursor.execute('''SELECT link FROM Data WHERE id ='''+str(number))
for x in cursor:
print(x)
This should get me a Link. In the MYSQL terminal it gives me exactly what I want, but in python it looks like this
[('Link',)]
(I removed the Link, its alright) Why is there a "[", a "(" and why is there a ","? I can understand a "(" and a "[" but why a comma? And is there any way to get a clean output?
In "MYSQL" this
SELECT link FROM Data WHERE id =6
gives me
link
without anything else.
Could not commit table structure. Error message: UNIQUE constraint failed
i have all data filled in
but i cant seem to link tables by using forin keys
every time a worker makes a transaction on my app, I am recording the moment this happens
each transaction has a field that contains the moment the transaction happened
should I save the moment the transaction happens as timestamp, e.g., 1647371394, or should I use datetime.datetime?
@rocky arch Because the row returned from the cursor is a tuple. so you would need to reference by the index of the tuple, or my the value name if your package supports that.
It could be in either, if you're doing a datetime.datetime though I would recommend storing as UTC personally. Depends what is going to be using that date - human or machine?
how do i set primary key when creating a table? (postgresql)
("""CREATE TABLE IF NOT EXISTS modlogs
(guild_id BIGINT, channel_id BIGINT)""")
``` this is my code
i want to make guild_id the primary key
so should i change it to:
(guild_id BIGINT PRIMARY KEY) ?
pls ping on reply
hi this is my first time trying mongodb i worked with sql before how can you do that if you search for a field and its empty how can you do that with an if statement?
@torn sphinx that depends, what is the value the package returning? Is it the keyword None?
Yes that is correct.
what do you mean by that? didnt understand it correctly
When you query the database, and you have the result - what is the result?
i printed it, it came up a list so i can basically do if an field is none
yes that should work, if the value is being returned as None, and not like an empty string, or a 0 (depending on datatype)
ye it works now
Sounds good, glad you figured it out!
:)
are there any good tutorials on sqlite?
Tutorial for what? To use the api with python ? Or learning sql ?
learning sql
human
i'm using SQLite3 with python and it's giving me this error when i try to insert some data
Can i not do this?? ```sql
CREATE DATABASE FamilyMembers;
USE FamilyMembers;
CREATE TABLE People
(
PersonID int identity (1, 1) not null,
PersonInitials char(5) not null,
PersonAge int not null,
PersonWeight_KG float not null,
PersonHight_FT float not null,
PersonDescription varchar(100) null
);```
SQLAlchemy:
To store my salt and encoded password (both of type byte) - I should create a "Unicode" field or "UnicodeText" field?
(ref: https://docs.sqlalchemy.org/en/14/core/type_basics.html)
Is there an easy way to copy column into another column in sqlite3+python?
I remember i did it somehow only with sqlite3 query, but don't remember how exactly
how do i get the modlogs channel id from this? (postgresql)
async def get_logs_channel(self, guild_id, channel_id):
data = self.bot.db.execute("SELECT channel_id FROM modlogs WHERE guild_id=$1, channel_id=$2", guild_id, channel_id)
@lunar pier you can do UPDATE table_name SET new_column = old_column
Oh, thanks 😅
Which library for postgres ?
asyncpg
oh yes
It has fetch or something like that
its for my bot.. i think it has
fetchrow ?
Yeah that would work. But if you want just one value then it has fetchval as well.
Fetchval would give you that value
Fetchrow gives a record object back. So you have to go through that to get the value then otherwise
data = await self.bot.db.fetchval("SELECT channel_id FROM modlogs WHERE guild_id=$1, channel_id=$2", guild_id, channel_id)
``` ?
Looks fine as long as you know that query will return one row.
Otherwise it’ll just get the first row probably
hmm
How many records are you expecting back?
uhh idk just a channel id and guild id
im trying to make a modlog
i have everything ready, now the only part is to get the channel id which the user saves
and then send the msg to that channel
So how many channels can the user save?
1
Then fetchval would work
okay
As you only need the value of the first matching row
yeah
if they want to change it, i made it so like it updates the old one
@set.command(aliases=['log'])
@commands.has_guild_permissions(manage_guild=True)
@commands.bot_has_permissions(manage_guild=True)
async def logs(self, ctx, channel: discord.TextChannel):
"""
Set a mod-log channel.
To replace a log channel, simply run this command again.
"""
try:
await self.bot.db.execute("INSERT INTO modlogs (guild_id, channel_id) VALUES ($1, $2) ON CONFLICT (guild_id) DO UPDATE SET channel_id=$2 WHERE modlogs.guild_id=$1", ctx.guild.id, channel.id)
await ctx.send(f"{self.bot.yes} Mod-logs channel set to {channel}")
except Exception as e:
print(e)
@proven arrow
Didn’t we agree already to use fetchval to get the id you want?
ok ty
I don't think this is valid SQL. What's with the , channel_id = $2 part? You want to select the channel_id it doesn't belong in the where clause (with an wrong syntax).
what should I do here then?
i dont know what to do here, i wrote this
def author_exist(lastname, initials):
cursor = conn.cursor()
SQL = '''SELECT Lastname, Initials, AuthorID
FROM Author
WHERE Lastname = ?
AND Initials = ?'''
temp = cursor.execute(SQL, lastname, initials)```
You can fetch the data, and then use the result to see if anything was returned.
so i create a py result = temp.fetchall()
and then do py if result == None: return 0 else: return result
or am i doing it wrong
If none is return then yes, but I’m not sure what the return type is for no match found. You can inspect the returned type to see what you get back.
but will that function then return the AuthorID? since thats what the exercise says
Well if you inspected the value you would see what you get back. It will be some sort of list. So you need to access the value from there.
how can i fetch data from my postgreSQL db in a form of a dict
i dont want to rely on indexing
I am using atlas search and i want to search a user with the text whether it's included in the name, username or email, and also want to use partial text search. So when I am using the autocomplete for partial text search then i am not able to use wildcard so that i can use search from any field. Can someone help me with this?
Do i need to have a database to make commands for my app
if you are storing a lot of relational data, yes
but if its just to learn and make something small, you don't really need to have a db
ok
Would appreciate anyone who's even slightly proficient at interacting with mongo collections / documents with pymongo to help me over at #help-chili 🙏 - thanks peoples
Hi, is there a certain datatype for gender in MySQL?
oh ok
and the column name is gender
alr thx : )
There's enum data standards it seems if you were to go via that route :
The four codes specified in ISO/IEC 5218 are:
0 = Not known;
1 = Male;
2 = Female;
9 = Not applicable.```
so then to create a table which has the column GENDER in a database, there is no certain datatype. is that what you meant? sry if i'm being dumb😶
No sorry - I'm saying if you were to store them as enum values, rather than a string e.g. "Male" - there seems to be data standards - your choice on how you do it but there isn't a specific data type for GENDER per se
Ohk. Understood better now. Thanks for the help!
How do I send api data to json?
Does this work?
Hi everyone, I posted this question in stack overflow: https://stackoverflow.com/questions/71503023/how-can-i-select-the-largest-value-when-an-ungrouped-column-is-dependent-on-a-gr, if anyone can help I would appreciated. I posted the link since I thought it would be to long to re-ask here.😫
I am new to Redshift. I have two tables, ticket_booking and ticket_review, the relation of the two tables is one - many. Which when combined looks like:
https://i.stack.imgur.com/y5v7c.png
The resu...
i need help
why doesnt this work?: ```py
await cursor.execute("UPDATE money_data SET balance = balance + ? WHERE user_id=?", (amount, getter_id))
await db.commit()
await cursor.execute("UPDATE money_data SET balance = balance - ? WHERE user_id=?", (amount, USER_ID))
await db.commit()```
i get no error when i run it but it simply doesnt do what i want it to do
This sounds like it should work.
The only reason it might not do anything is if the user IDs are incorrect
maybe you can add RETURNING user_id at the end, and then print the query results?
this is where i define the ids: py @client.command(name="pay") async def pay(ctx, member: discord.Member, amount): db = client.database_connection cursor = await db.cursor() USER_ID = ctx.message.author.id USER_NAME = str(ctx.message.author) getter_id = str(member.id) getter_name = str(member)
i think i defined them correctly
no, it's showing an error
So what exactly is not working?
Where?
And how do you know that the rows with these IDs already exist when making the payment?
If the rows don't exist, there's nothing to update.
i dont know bro. the whole command is working. it sends what it should be sending. but then it doesnt update their bal
it checks if it exists
and if it doesnt exist it creates one
Can you do ```py
await cursor.execute("UPDATE money_data SET balance = balance - ? WHERE user_id=? RETURNING user_id", (amount, USER_ID))
and then print `await cursor.fetchall()`?
(and the same for +)
there's no unique exclusion or constraint matching the ON CONFLICT specification
I’ve no idea, what that is sorry 🙈🙈
lol ok
It means the guild_id has no unique constrain.
show the create table command.
this changed nothing
the same thing happens
This is not supposed to change anything. It's supposed to print the changed IDs for debugging
also, can you show the whole command maybe?
!paste
Pasting large amounts of code
If your code is too long to fit in a codeblock in discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.
ok i will wait
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
What I asked you to do is: ```py
await cursor.execute("UPDATE money_data SET balance = balance - ? WHERE user_id=? RETURNING user_id", (amount, USER_ID))
print(await cursor.fetchall())
await cursor.commit()
then you should get some extra output in the terminal
RETURNING lets you output something from an INSERT or UPDATE statement
Oh, wait
this code isn't even executing
It only executes if amount < 0
wait
right but your UPDATE is in the else: branch
btw, you should not commit after the first UPDATE
otherwise, if the second UPDATE fails (because of some I/O error, because the bot program stops etc.), you will add to one user's balance but not subtract from the other's
yes
ok it should be working now imma test it thank u
everything works now thank u
I'm trying to put a string in my db with sqlite3 and it's not going too well. Here is my code:
import sqlite3
name = "this is a test"
conn = sqlite3.connect()
c = conn.cursor()
c.execute("""CREATE TABLE hello (
name text
)""")
c.execute("INSERT INTO hello VALUES (?)", name)
c.execute("SELECT * FROM hello WHERE name=?", (name))
print(c.fetchall())
conn.commit()
conn.close()
you must use (name,) in both cases, instead of name / (name) ```py
c.execute(query, (name, ))
!e ```py
import sqlite3
name = "this is a test"
conn = sqlite3.connect(":memory:")
c = conn.cursor()
c.execute("""CREATE TABLE hello (
name text
)""")
c.execute("INSERT INTO hello VALUES (?)", (name,))
c.execute("SELECT * FROM hello WHERE name=?", (name,))
print(c.fetchall())
@storm mauve :white_check_mark: Your eval job has completed with return code 0.
[('this is a test',)]
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 14 supplied.
and i changed name to name,
That error happens because it's trying to assign each letter from name to a different ?, but you only supplied one ?
oh
ah the one valued tuple back at it again
so how would i assign every letter from name to the same ?
create a tuple that only contains name, (name, )
note that this is different from just (name) and from name,
#does not works here:
function(name) # it tries to decompose name into each letter from name
function(name,) # passes the argument name, with a trailing comma
function((name)) # same as just function(name)
# would work:
function((name,)) # passes a tuple of length 1, which contains name
tuple syntax is the best paren reuse I have ever seen, but it's just as confusing
wait until you learn about comprehensions
yo mean [book for book in book_shelf for book_shelf in library]?
yes, but when you use () it is a generator comprehension (lazy evaluated, yields one at once, will be exhausted after you go through it once) instead of a tuple
they work for sets and dictionaries the same way they work for lists though ```py
{x for x in range(10)}
{x: x**2 for x in range(10)}
oh ill show show you my amazing code, guess what it does. ```py
_, node, i = min([(costs[node], node, i) for i, node in enumerate(frontier)])
frontier.pop(i)
@storm mauve
this is actually considered good practice by some random utuber that was calculating sum of matrix with comprehension instead of loops
I'm pretty sure that you could just remove the [] by the way
min((costs[node], node, i) for i, node in enumerate(frontier))
rly?
yep
nice
so for prefixed with expression is generator
inside of ()
epic
rust is also full of surprises, did you know @ is used in pattern matching
we are getting kinda offtopic for this channel at this point
so what channel do you suggest? #community-meta maybe
!ot
Off-topic channel: #ot2-the-original-pubsta
Please read our off-topic etiquette before participating in conversations.
In MySQL,
I have a table named Channel
id guildId position
id is primary key unique
guildId and position are both unsigned int, and together they form a unique constraint
How can I do something that doesn't break this unique constraint such that:
update a channel current's position field value to x (assuming x is bigger than current.position)
update all channels between current.position and position x and decrement their position field by 1
update current.position to position x
hello guys good morning
PizzaHat/cogs_hidden/events.py lines 16 to 17
await self.bot.db.execute("""CREATE TABLE IF NOT EXISTS modlogs
(guild_id BIGINT PRIMARY KEY, channel_id BIGINT)""")```
I also get this error from this code
@set.command(aliases=['log'])
@commands.has_guild_permissions(manage_guild=True)
@commands.bot_has_permissions(manage_guild=True)
async def logs(self, ctx, channel: discord.TextChannel):
"""
Set a mod-log channel.
To replace a log channel, simply run this command again.
"""
try:
await self.bot.db.execute("INSERT INTO modlogs (guild_id, channel_id) VALUES ($1, $2) ON CONFLICT (guild_id) DO UPDATE SET channel_id=$2 WHERE modlogs.guild_id=$1", ctx.guild.id, channel.id)
await ctx.send(f"{self.bot.yes} Mod-logs channel set to {channel}")
except Exception as e:
print(e)
``` error: there is no unique or exclusion constraint matching the ON CONFLICT specification
It should work: https://www.db-fiddle.com/f/mjuJQQtriUVTZDSEGFt3ch/0
And the WHERE modlogs.guild_id=$1 is not needed, an ON CONFLICT does only update the row t where the conflict occured.
I first tried:
INSERT OR REPLACE INTO modlogs
Did you add the primary key after the table existed? It won't recreate it. Can you check with an db utility to see if it realy is the PK?
but then someone suggested me this, same thing as I was trying to do
one sec
pretty sure it is primary key
If it is, it would work.
there's no problem with the code?
No, but as I said, the where clause is not needed.
yes
okay I will try it, thanks
did you look at my link?
But it will not change the error, because the DB says that there is no unique constraint on guild_id, so it can't be a PK.
Yes, you can try different things with an database.
hmm, I will delete the table and recreate it.. that should work
ok it seems to store the channel id.. but the bot is not sending msgs to that channel, code given below
async def get_logs_channel(self, guild_id):
data = await self.bot.db.execute("SELECT channel_id FROM modlogs WHERE guild_id=$1", guild_id)
id = data[0]
channel = self.bot.get_channel(id)
return channel
@Cog.listener()
async def on_message_edit(self, before, after):
try:
em = discord.Embed(
title=f"Message edited in {before.message.channel}",
color=self.bot.success,
timestamp=before.created_at
)
em.add_field(name="-Before", value=before.content, inline=False)
em.add_field(name="+After", value=after.content, inline=False)
em.set_author(name=before.author, icon_url=before.author.avatar.url)
em.set_footer(text=before.author.id)
channel = await self.get_logs_channel(before.guild.id)
await channel.send(embed=em)
except:
pass
should i make it print the error?
An execute doesn't fetch data, so I'm pretty sure id = data[0] doesn't work.
You need .fetchone() or (is it fetchrow() for pg?) something first.
hmm
fetchrow gets a row
which is record obj
you only have one column, so you can use fetchval()
async def get_logs_channel(self, guild_id):
channel_id = await self.bot.db.fetchval("SELECT channel_id FROM modlogs WHERE guild_id=$1", guild_id)
# check for None maybe?
channel = self.bot.get_channel(channel_id)
# and some error checking here?
return channel
Not needed, but easier to debug if something fails.
and you can't use a None channel anyway
async def get_logs_channel(self, guild_id):
data = await self.bot.db.fetchval("SELECT channel_id FROM modlogs WHERE guild_id=$1", guild_id)
if data:
return []
else:
print("No data")
channel = self.bot.get_channel(data)
return channel
``` this might work?
No
Think what your code is doing.
- fetchdata
- if there is no data return an empty list, if not print "no data"
- get the channel (but this is only done if there is NO data)!
- return a channel which will never be found
You only can do get_channel() if there is data, so yes, you need to rethink your if statement.
async def get_logs_channel(self, guild_id):
data = await self.bot.db.fetchval("SELECT channel_id FROM modlogs WHERE guild_id=$1", guild_id)
if data:
channel = self.bot.get_channel(data)
return channel
else:
print("No data")
``` this makes sense to me
looks better, yes. You may want to add a return None after the if (which id done atomaticly by python if there is no return statement) to be clear about it. And maybe a better error message.
async def get_logs_channel(self, guild_id):
channel_id = await self.bot.db.fetchval("SELECT channel_id FROM modlogs WHERE guild_id=$1", guild_id)
if channel_id:
return self.bot.get_channel(channel_id)
print(f"No modlog channel for {guild_id}")
return None
hmm k
so this will work?
can anybody here help with SQLAlchemy
Yes, that should work.
ok tysm
but be aware that:
...
channel = await self.get_logs_channel(before.guild.id)
await channel.send(embed=em)
this can still fail if the channel is None (not found in the function).
ok
this event is confusing me bcz it takes in only b4 and after
but others take in msg, guild, user so that way im able to get the guild id
I have a TodoList Class That Has a Relation with ListItem Class
So a TodoList Object has a one to many relation with the ListItems
The ListItems have a column called completed
It can either Be NULL/False or True
The _edit part suggest that there is a message before and after the edit.
So here's what i need help in
I want To see If All of the completed are True, For a List
And Return True/False depending on it
yes thats how the event works.. i thought for a while on getting the guild id
can you ppl help pls?
I'm not familiar with SQLAlchemy, sorry. Maybe someone with knowledge will jump in.
do you know SQL?
Yes
something like:
select
listtable.id,
not exists (select 1 from itemtable
where itemtable.list_id = listtable.id
and not itemtable.completed) as "all_done"
from listtable
maybe
ok Thanks
hi i made this command trying to sort values in my sqlite database but it doesnt work. this is my code: py @client.command(name="lb") async def top10(ctx): db = client.database_connection cursor = await db.cursor() await cursor.execute(f"SELECT user_id, balance from money_data WHERE user_id = {ctx.guild.id} ORDER BY balance DESC LIMIT 3 ") result = await cursor.fetchall() embed = discord.Embed(title="Leaderboards", colour=discord.Colour(0x6790a7)) for i, x in enumerate(result, 1): embed.add_field(name=f"#{i}", value=f"<@{str(x[0])}> on Level {str(x[1])} with {str(x[2])} Total XP", inline=False) await ctx.send(embed=embed) print(result)
pls help me
i have there text index for title, body and info in atlas search and i want to do a partial text search from all these three at once, but when i am using autocomplete then i can't use the wildcard so how can achieve that? can someone help with this?
don't use f-strings
await cursor.execute(f"SELECT user_id, balance from money_data WHERE user_id = {ctx.guild.id} ORDER BY balance DESC LIMIT 3 ")
use bind parameters (and in this case you might want guild_id as column not the user_id?):
await cursor.execute("SELECT user_id, balance FROM money_data WHERE guild_id = ? ORDER BY balance DESC LIMIT 3", (ctx.guild.id,))
for i, x in enumerate(result, 1):
embed.add_field(name=f"#{i}", value=f"<@{str(x[0])}> on Level {str(x[1])} with {str(x[2])} Total XP", inline=False)
...
You only select two columns SELECT user_id, balance FROM ... so you can't use x[2] that would need a third column to be selected.
cursor.execute('INSERT INTO banwords VALUES(%s, %s)', (message.author.id,1))2022-03-17T11:31:55.477270+00:00 app[worker.1]: psycopg2.errors.NumericValueOutOfRange: integer out of range
postgresql
For guild and user IDs you need a BIGINT datatype, not INTEGER.
(userid BIGINT, count INT) BIGINT uses
Do you mean that your table banwords already uses an BIGINT for the userid?
Used INT, then I changed to BIGINT
You'll need to recreate the table to get the change or execute an alter table command.
okay
ALTER TABLE banwords ALTER COLUMN userid TYPE BIGINT;
okay, thank you
warning = cursor.execute('SELECT * FROM banwords WHERE userid = %s', (message.author.id,))
cursor.fetchone()
if warning == None:
cursor.execute('INSERT INTO banwords VALUES(%s, %s)', (message.author.id,1))
base.commit()
emb=discord.Embed(title="Предупреждение", description=f"Участник **{message.author.name}** получил предупреждение за грубое оскорбление, случай 1.",color=discord.Color.from_rgb(128, 0, 255), timestamp=message.created_at)
emb.set_thumbnail(url = message.author.avatar_url)
emb.set_footer(text="© Автоматические предупреждение")
await message.channel.send(embed=emb)
elif warning[1] == 1:
cursor.execute('UPDATE banwords SET count = %s WHERE userid = %s', (2,message.author.id))
base.commit()
emb=discord.Embed(title="Предупреждение", description=f"Участник **{message.author.name}** получил предупреждение за грубое оскорбление, случай 2.",color=discord.Color.from_rgb(128, 0, 255), timestamp=message.created_at)
emb.set_thumbnail(url = message.author.avatar_url)
emb.set_footer(text="© Автоматические предупреждение")
await message.channel.send(embed=emb)
```
elif warning[1] == 2:
cursor.execute('UPDATE banwords SET count = %s WHERE userid = %s',(3,message.author.id))
base.commit()
role = message.guild.get_role(921806885831716875)
emb=discord.Embed(title="Предупреждение", description=f"Участник **{message.author.name}** получил предупреждение за грубое оскорбление, случай 3. Участнику выдан мьют, за грубые оскорбления, предупреждения сбросились.\n||Обращайтесь к админам, чтобы они сняли мьют||",color=discord.Color.from_rgb(128, 0, 255), timestamp=message.created_at)
emb.set_thumbnail(url = message.author.avatar_url)
emb.set_footer(text="© Автоматические предупреждение")
await message.channel.send(embed=emb)
await message.author.add_roles(role)
cursor.execute('DELETE FROM banwords WHERE count AND userid')
base.commit()
always Value 1, does not change, why?
This: cursor.execute('DELETE FROM banwords WHERE count AND userid') will delete all entries as soon as one reaches count 2.
You want most likely:
cursor.execute('DELETE FROM banwords WHERE userid = %s', (message.author.id,))
And are this two separate code lines?
warning = cursor.execute('SELECT * FROM banwords WHERE userid = %s', (message.author.id,))
cursor.fetchone()
It would need to be the other way around:
cursor.execute('SELECT * FROM banwords WHERE userid = %s', (message.author.id,))
warning = cursor.fetchone()
And you don't need to set the count to 3 if you are going to delete the entry anyway.
@pure mortar from #career-advice , what do cloud dbs require? Clustering? The closest I've seen was with Mongo Atlas and its trial cluster.
many cloud dbs are columnar DBs, so many traditional SQL queries are inefficient on these dbs. Atlas is good for document DBs. pick up some cloud and/or tooling like snowflake or dbt, etc. and you should be competitive enough for entry-level DE
many relational schemas dont fit for cloud data warehouses (obv. depends on which one), but i think you should understand DE from less of a relational DB approach
otherwise you might as well go and be a DBA

What do you recommend between snowflake and dbt?
At least something that costs 0€ for practice
check out seattle data guy for DE resources https://youtu.be/SpaFPPByOhM
How do you go from 0 to data engineer?
What is the road map to data engineering?
These are all valid questions that I will walk you through over the next 15 minutes.
The purpose of this video is to help you break down all the various skills we seem to need to know as data engineers in 2021 into manageable bites.
So you don’t feel overwhelmed...
skip the stuff you already know
focus on the other stuff you dont
best approach
he also has ton of other good DE videos too
Oh wait he also proposes some DE projects. Interesting.
yep yep
better for your resume tbh
than normal projects
that might be too general
Brother one question
I created a table with a column as a data type bigint
I want to change to varchar
How to change
hi i have a question
i use an sqlite database for my discord bot and i wonder if i can make a command which will register everysingle member in a discord server and save their information (id and balance) in my db file? would i have to make a loop which registers them all or can i just get the member list and get them all?
you'll need a loop to create a table for each member
in the server
or make so that they have no balance
untill post a msg
and register discordID into db and set default balance on their first msg
if no record exists
thats actually exactly what i did
(just think its more api friendly)
maybe thats good enough idk
nah honestly thats good enough
i already made that so i dont need to change anything then
i think its a good way u also dont care about idlers
but yeah if u still anyway want check n put all users into db
u will need like a loop "task"
i will have to loop it yes
check every hour or so
i will have to make a command and give everyone money manually then
no if u give balance when created if not existed
cus i already have the bot going with a json file as a database
if they exists their balance is same
and i want everyone to have as much money as they had before
so i will have to manually fix that
u can just write a function if u have that stored
and just !copydb
call command let it read ur old json or shit
n insert it to the discordID
yes u only need to do it once
like u read ur json
go throu all
check if its in db then insert balance to them
no i just realised i dont think that will work
cus in my sqlite file i have user_name, user_id and user_bal
on my json file i only have user_id and user_bal
if u scrabble all ur users now 1 time n make rows of em
yeah
lets try explain it again
lol
Json -> read loop
database -> check if exists insert json.user_bal to db and same with user_name then
but first run a code once that grabs all users u have atm to db
all u need is discordid for that rest columns can be empty
cause u will update it with json
and then code so that any one type a msg if not exist in db insert it with default balance
alright i get it
then its on automaticly
ye its easy peasy
@compact marlinfeel free to ask if anything showsup
#databases can anyone get voice chat and help mw with SQLite studio not rushing anyone
is anyone in here experienced with AWS?
note the lack of wings of the inner plane
That used to be XML in an SQL DB
Nah too much coffee will do that to you .... watch coffee intake...
is this the right channel to ask a question about sqlalchemy and marshmallow?
can anyone get voice chat and help mw with SQLite studio not rushing anyone
my prokect is due toady
seems reasonable
rushing?
nvm what i said
@harsh pulsar where to post sample code? git? or paste here?
if it's not too long feel free to paste it here. otherwise you can post it on https://paste.pythondiscord.com and share the link here instead
and if you want to post shorter code snippets, you can use code formatting
!code
Here's how to format Python code on Discord:
```py
print('Hello world!')
```
These are backticks, not quotes. Check this out if you can't find the backtick key.
@burnt turret @harsh pulsar Thanks both for help.
i have a replit sample database
i made some economy commands
in that
i want to make a list so that the person with the highest currecy
to cone at the top
ik ik ima peasant
Isnt the replit "database" just a json file 
you can just search "how to sort a dictionary"
Hellooo. I have a scatter plot with probabilities that change on the y axis and constant positions on the x axis. Do you know how to animate it or a good tutorial?
hmm okk
get all items -> sort the dictionary using the sorted method
@random bobcat
oof = {"guy1":10,"guy2":20,"guy3":30}
x = sorted(oof.items(), key=lambda x: x[1], reverse=True)
for o in x:
print(o)
# ('guy3', 30)
# ('guy2', 20)
# ('guy1', 10)
!d sorted
sorted(iterable, /, *, key=None, reverse=False)```
Return a new sorted list from the items in *iterable*.
Has two optional arguments which must be specified as keyword arguments.
*key* specifies a function of one argument that is used to extract a comparison key from each element in *iterable* (for example, `key=str.lower`). The default value is `None` (compare the elements directly).
*reverse* is a boolean value. If set to `True`, then the list elements are sorted as if each comparison were reversed.
Use [`functools.cmp_to_key()`](https://docs.python.org/3/library/functools.html#functools.cmp_to_key "functools.cmp_to_key") to convert an old-style *cmp* function to a *key* function.
ohh
MySQL v8 is giving me this error
single quotes are for strings. use double quotes for quoting a column name
ROW_NUMBER() OVER w AS "p"
k
(also you probably don't need to quote this name at all)
also can you post your code a text in the future, using a code block?
position 35 is the char before w
Here's how to format Python code on Discord:
```py
print('Hello world!')
```
These are backticks, not quotes. Check this out if you can't find the backtick key.
SELECT c.id,
ROW_NUMBER() OVER w AS p
FROM Channel c
WINDOW w AS (ORDER BY position);
thanks
CREATE TABLE IF NOT EXISTS `Channel` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`guildId` INTEGER UNSIGNED NOT NULL,
`name` VARCHAR(64) NOT NULL,
`description` TEXT NULL,
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`position` FLOAT NOT NULL,
`parentId` INTEGER NULL,
`ratelimit` INTEGER NOT NULL DEFAULT 0,
`type` ENUM('textChannel', 'categoryChannel') NOT NULL,
INDEX `Channel_guildId_idx`(`guildId`),
UNIQUE INDEX `Channel_guildId_name_key`(`guildId`, `name`),
UNIQUE INDEX `Channel_guildId_position_key`(`guildId`, `position`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Here's my entire table
INSERT INTO Channel (id, guildId, name, position, type) VALUES (1, 1, 'ch1', 1, 'textChannel');
INSERT INTO Channel (id, guildId, name, position, type) VALUES (2, 1, 'ch2', 2, 'textChannel');
INSERT INTO Channel (id, guildId, name, position, type) VALUES (3, 1, 'ch3', 3, 'textChannel');
3 test rows
thanks!
that makes this so much easier
wait, try backticks instead of double quotes...
i don't use mysql much, maybe it doesn't use double quotes
it still doesn't work
SELECT c.id,
ROW_NUMBER() OVER `w` AS `p`
FROM `Channel` c
WINDOW `w` AS (ORDER BY `position`);
https://www.db-fiddle.com/f/whr4PZzxRqNsVDcTECSEV7/1 works fine for me
An online SQL database playground for testing, debugging and sharing SQL snippets.
must be a typo elsewhere in your code 🤷♂️
or a bug in whatever console you are using
An online SQL database playground for testing, debugging and sharing SQL snippets.
that's with everything quoted using backticks
what..
Im trying to replicate the thing without ROW_NUMBER()
it works but it doesn't actually work
rank field is completely empty
I think my console is bugged in some way...
@harsh pulsar u there?
possibly, i'm not sure. maybe restart the console?
@stray moss What is your MySQL version?
Strange. The DB-fiddle salt rock lamp linked gives the same error you have if you switch MySQL version to v5.7.
What software are you using to make the queries?
it's a cloud provider
I'm using their console interface
I sent a support letter already
Could you run SHOW VARIABLES LIKE ‘%version%’; maybe?
idk what it is but it seems like something 🙂
hmm wait @stray moss , it says here
https://dev.mysql.com/doc/refman/8.0/en/innodb-introduction.html
InnoDB is a general-purpose storage engine that balances high reliability and high performance. In MySQL 8.0, InnoDB is the default MySQL storage engine. Unless you have configured a different default storage engine, issuing a CREATE TABLE statement without an ENGINE clause creates an InnoDB table.
se seems like InnoDB is expected?
hmm, it's showing 8.0.23 but 8.0.21 on the other screenshot 🤔
oh wait, it's innodb version vs the "version" version?
I dont even know it just shows that
me neither, I have no idea about MySQL to be honest
maybe you should bump your question 🙂
same I wanted to use Postgres but this provider said free 10 GB MySQL so I got hooked in
innodb is the "storage engine", but that shouldn't have anything to do with query parsing
https://vitess.io/ who knows then
maybe it doesn't support WINDOW for all we know
i never used this
sad
unfortunate, but there's the answer
Hello I'm currently designing the structure of a database and I have the following table that I would like to transform it to 3FN. The following table contains time ranges from start to end showing availabilities of a referee on a weekday.
CREATE TABLE AVAILABILITIES(
ref_num int8 NOT NULL,
weekday varchar(50) NOT NULL,
start_time time NOT NULL,
end_time time NOT NULL,
FOREIGN KEY (ref_num)
REFERENCES REFEREES(ref_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
Notice that each referee can have different time ranges for the same weekday
if position is unique you can do it like:
select id,
(select count(*) from Channel b
where b.position <= a.position) as "row_number"
from Channel a
order by position

Let me try
So you want to extract that time range into separate table?
I'm not sure, I feel like there is a transitive dependency between start and end time with ref_num
Hm, is there? 🤔
Both start and end time are fully dependant on weekday
But I think that weekday do not depend on ref_num...
How so? I don't quite understand how they could be dependant on a weekday here, they're logically related but there's no dependency
I might have similar rows with the same ref_num and weekday and only start_time and end_time will change from one row to another
But i feel like I would have the same issue moving it to another table since I need to keep the relation with the referee
Makes sense, but 3nf doesn't give you much in this situation imo
Times are quite individual, these are not concrete entities like country which is used in some examples
I agree
Thank you so much
UPDATE `Channel` AS `ch`, (
SELECT `id`,
(SELECT COUNT(*) FROM `Channel` AS `b`
WHERE `b`.`position` <= `a`.`position`) AS `p`
FROM `Channel` AS `a` WHERE `guildId` = 1
ORDER BY `position` ASC
) AS `td` SET `ch`.`position` = `td`.`p` WHERE `ch`.`id` = `td`.`id`;
Error:
target: gnc.-.primary: vttablet: rpc error: code = InvalidArgument desc = Incorrect usage of UPDATE and LIMIT (errno 1221) (sqlstate HY000) (CallerID: planetscale-admin): Sql: "update `Channel` as ch, (select id, (select COUNT(*) from `Channel` as b where b.position <= a.position) as p from `Channel` as a where guildId = :vtg1 order by position asc) as td set ch.position = td.p where ch.id = td.id", BindVars: {#maxLimit: "type:INT64 value:\"100001\""vtg1: "type:INT64 value:\"1\""}
if the timeslots are the same for each referee you can make a timeslot table and reference that.
Just doesn't make a lot of sense in this context imo
If you change start or end time it would be different availability entry, so i wouldn't extract it into separate table
Exactly
anyone see any errors?
I will leave it as it is
I am not the most knowledgeable but I use it for basic needs. What's up?
Hey guys,
I have a database with stats from Discord users.
A table is basically
Discord_id, wins, coins
I want to get ever Discord_id exactly once. Exactly where "wins" is max. And I want to show all 3 values.
@faint blade this is the update query
UPDATE `Channel` `ch`
INNER JOIN (
SELECT `id` `id2`,
( SELECT COUNT(*)
FROM (SELECT * FROM `Channel`) `b`
WHERE `b`.`position` <= `a`.`position`
) AS `p`
FROM `Channel` `a` WHERE `guildId` = 1
) `td` ON `ch`.`id` = `td`.`id2`
SET `ch`.`position` = `td`.`p`;
SELECT * FROM Channel;
UPDATE `Channel` `ch`
INNER JOIN (
SELECT `id` `id2`,
( SELECT COUNT(*)
FROM (SELECT * FROM `Channel` ORDER BY `id`) `b`
WHERE `b`.`position` <= `a`.`position`
) AS `p`
FROM `Channel` `a` WHERE `guildId` = 1
) `td` ON `ch`.`id` = `td`.`id2`
SET `ch`.`position` = `td`.`p`;
SELECT * FROM Channel;
Have you tried something like this?
I added an ORDER BY
just ran, doesn't work either
Will you be running this query often? If you're violating a constraint, then you're probably not doing something you're meant to

No
What I am doing is
Try flipping it to be descending: ```sql
UPDATE Channel ch
INNER JOIN (
SELECT id id2,
( SELECT COUNT(*)
FROM (SELECT * FROM Channel ORDER BY id DESC) b
WHERE b.position <= a.position
) AS p
FROM Channel a WHERE guildId = 1
) td ON ch.id = td.id2
SET ch.position = td.p;
SELECT * FROM Channel;
well still doesn't work
Can you continue this thought?
mycol = db["customers"]
If i just do this ^ will the collection be created if I don't insert any data?
Ok but I have to go eat
Here:
I want to change the table from:
id name position
1 ch1 1
2 ch2 2
3 ch3 3
and move ch3 to position 1 but I need to update ch1 and ch2 because their positions need to move back by 1
id name position
3 ch3 1
1 ch1 2
2 ch2 3
Basically like discord channels... When you move it to a different position all the other positions being affected by it need to update by 1 or -1
Hey guys, (asked my question at a bad time, so here we go again 🙈 )
I have a database with stats from Discord users.
A table is basically
Discord_id, wins, coins
I want to get ever Discord_id exactly once. Exactly where "wins" is max. And I want to show all 3 values.
My thoughts: "DISTINCT", but I can only get one column.
"GROUP BY", but "GROUP BY" does not give me the max win value.
(currently the last one was executed).
Would work fine, if I could use the "ORDER BY id" before the "GROUP BY".
I am sorry I have been searching around for a while but could not find anything easy to fix this. Especially for such a niche database as Vitess
There are multiple rows with the same Discord_id, and you wish to select the row with the highest wins?
yes. with the highest "wins" or the biggest "id" (this would be the last entry, which would be the same as highest "wins")
So I want the top score from everyone
@faint blade sorry are you still here? you are currently my only hope 😂 👀
I have my solution already but it just needs to update from the last row to prevent conflicts...
An online SQL database playground for testing, debugging and sharing SQL snippets.
this is the snippet
I need to fix the error
I am sorry for the delay, I had to do something. You'll want something like: ```sql
SELECT * FROM UserData GROUP BY (Discord_id) ORDER BY wins DESC; -- You can add LIMIT 10 to select top ten
All good thanks.
This would not work. Because "Group By" does not take the highest "wins". And "Order by" would just order all users according to the wrong "wins". 🤔
What do you mean "wrong 'wins'"?
Group by just gives me one "wins" for each user. And this "wins" is not the highest.
This is your command.
And as you can see, the "wins" are all very low. But they should be way higher.
I need something like "GROUP BY(user_id WHERE user_id = max).
I know this is not sql... but this is basically what I need
Right now its "GROUP BY(user_id WHERE user_id = min)" haha
Can you try this? (adding MAX() over wins)```sql
SELECT * FROM UserData GROUP BY (Discord_id) ORDER BY MAX(wins) DESC; -- You can add LIMIT 10 to select top ten
No because "ORDER BY" just affects the values after "GROUP BY".
So GROUP BY "deletes" everything except one value. And I cant order one value
Order by has to be in front of Group by, but this does not work
Ah, hmm, I found this: ```sql
SELECT Discord_id, MAX(wins) as max_win FROM UserData GROUP BY (Discord_id) ORDER BY max_win DESC; -- You can add LIMIT 10 to select top ten
@faint blade hi
An online SQL database playground for testing, debugging and sharing SQL snippets.
I have the snippet here
could u help me try to fix the update error?
I think the error is caused by it updating 0.5 to 1 when it's already taken
but 1 is getting updated to 2 next so idk
it needs to somehow update starting from the last one
Yeah I get what you mean, but I do not know the fix. Several sources even seem to say it's flat out impossible: https://dev.mysql.com/doc/refman/8.0/en/ansi-diff-foreign-keys.html
In an SQL statement that inserts, deletes, or updates many rows, foreign key constraints (like unique constraints) are checked row-by-row. When performing foreign key checks, InnoDB sets shared row-level locks on child or parent records that it must examine. MySQL checks foreign key constraints immediately; the check is not deferred to transaction commit. According to the SQL standard, the default behavior should be deferred checking. That is, constraints are only checked after the entire SQL statement has been processed. This means that it is not possible to delete a row that refers to itself using a foreign key.
Okay I still try to understand how it works... but I think it works. Thank you so much!
Hurray
. What's confusing about it? The order of what's evaluated maybe?
yes exactly
because only "SELECT user_id, MAX(wins) FROM UserData" gives me one result
Yeah that's because you select the user ID with the highest amount of wins
this is honestly so sad
I don't fully understand it either, but by having MAX(wins) as max_win we evaluate the highest number of wins before/during the GROUP BY which we can later use in ORDER BY I suppose... maybe? 😅
I do not have much experience with GROUP BY and other aggregate functionalities like HAVING
Yeah I thought the same, but it also works without "as max_win" haha
Yeah
- like often recommended in different Stack Overflow comments you'll have better luck with PostgreSQL because it follows the SQL standard quite closely.
How does it look like then? Do you just have ORDER BY MAX(wins)?
I've been trying to solve this issue for the past week lol, every day I advance one step and now this is the last step and it doesn't work
The difference is, that "order by wins" does not work correctly.
But thank you so much. This is exactly what I need 🙏
Try doing SELECT user_id, MAX(wins) wins FROM UserData GROUP BY (user_id) ORDER BY wins DESC;. That will probably show the wins column in order (which was picked arbitrarily out of all rows present).
Is there actually any kind of deeper reason you picked Vitess? I have never heard of it before
Nope, it just advertised free cloud hosted 10 GB and I was like "sure, vitess must be like postgres, they're both SQL! right?"
then I read "YouTube uses Vitess" and I was like "This must be a good database system"
seems like it was created by Youtube for Youtube? which might mean that it is overly specialised
-because the command you're doing is veeerryyy harmful for performance. See, since you're modifying close to every single row your database will need to lock all of those rows in the entire database which means that no other connection can update those rows (it may affect other operations as well, I am not sure which operations can run concurrently)
😅 if only... PostgreSQL is closest to the SQL standard
Can't you host a PostgreSQL instance locally? That's what I do on my free VPS.
oh, sad. What's another way to handle something like discord channel positions then?
I don't have a VPS the website is hosted statically with serverless functions as api
Well, first I would recommend thinking over it.. do you really need to store positions?
then how would I know what order the channels are
Which serves what purpose?
the client should be able to move channels up and down
so like
# ch1
# ch2
# ch3
they should be able to drag ch3 and put it in front of ch1
I can store positions in the browser localstorage... but what if they use multiple devices often? that would make it really messy and for new members too
anyway I'm a bit late and scrolled through briefly but that highest wins per player might be a PARTITION BY use case? not sure
I.. don't think so? Legit never heard of it lol
The elements of the PARTITION BY list are interpreted in much the same fashion as elements of a GROUP BY clause, except that they are always simple expressions and never the name or number of an output column. Another difference is that these expressions can contain aggregate function calls, which are not allowed in a regular GROUP BY clause. They are allowed here because windowing occurs after grouping and aggregation.
From PostgreSQL
I mean, really, the second question is just to reconsider the constraint. Do you really need it to be unique, afaik Discord positions aren't guaranteed unique.
Discord sends a CHANNEL_UPDATE for each affected channel iirc
wait that's a good point...
then how....?
Order by snowflake
yeah but how are the positions being ordered??
if only one CHANNEL_UPDATE gets sent

@faint blade just checked Discord's gateways -
in ready event they do give a unique position field for each channel object
Categories are ordered by positions, within categories channels are ordered by order. If order is equal then I it's determined by its ID but I can't remember whether older/newer snowflakes come before/after
It says "the position will always be unique"?
no I just looked at the payload
in a guild JSON hashmap there's a "channels" key its value is an array and each channel object has a unique position field
That's like going for a walk in the forest and determining that you're the last human alive 😅
Observed behaviour doesn't have to be guaranteed 😬
You observe that you're the human there, but you can't guarantee that you are
OK let me ask some discord api wrapper maker, maybe they know discord channels actually work
I am failing to find the issue/discussion on the Discord API docs where a code snippet was shared
I looked deeper, seems like category channels' positions are other channels' positions
but it means "where" the category channel goes
splits up things
but the text channels, their position doesn't repeat
I think it's only unique for text channels
@faint blade so I continued to explore the discord API
I found out that when I have 10 text channels, and I move the 10th to 1st
discord will send 10 CHANNEL_UPDATE events for each channel
Yes ^^
now that's another problem 😭
I can update now
but I can't send multiple channel_update events
what would be the best way to store value A and value B on a json, where value B depends on value A? for example user -> choice
i was thinking list of dicts but that is overkill and i already messed it up 😅
Hello everyone! I have this problem with marshmallow and sqlalchemy. I don't know how to serialize 'join results'. I want schema to serialize all result data (including nested data), but I don't get them out anyhow. It's all in the code and comments. I hope someone will have time and eye to see the problem and get back with clue. Thanks in advance. https://paste.pythondiscord.com/xojivadobo
You're trying to serialize result of sqlalchemy core query, try using orm
@paper flower reading documentation, I didn't even know there is difference
thanks for pointing out
Basically you have to replace
with engine.connect() as conn:
result = conn.execute(select(User, Address.email_address).select_from(User).join(User.addresses).where(User.id == 1)).first()
with
with Session() as session:
stmt = select(User).options(selectinload(User.address)).filter(User.id == 1)
user = await session.scalar(stmt)
selectinload would load related entities using separate select ... where id in ... query, there are other loading methods btw
And session.scalar should return a single result
@paper flower thanks, I wasn't even aware this core query and ORM stuff
I copied your reply and got whole section in documentation on this topic
will be reading today I guess
😉
You can get general overview or core and orm here https://docs.sqlalchemy.org/en/14/tutorial/index.html#unified-tutorial
thanks man, I appretiate it. You probably saved me tons of time
Hello people. I'm trying to get the average difference between travel date and booking date.
Here's my dummy data.
Here's what I've done so far:
SELECT bookingcustomeremailaddress, AVG(traveldate - bookingdate) as avg_days
FROM FLIGHT
GROUP BY bookingcustomeremailaddress
here's the output:
what am I doing wrong? TT
does it have anything todo with
the fact that they are date data type ??
You sure that they are date datatypes and not just strings? Which DB is that?
mysql
anyway it's been solved already. thanks though!
Hey guys, I am a beginner to Django and i have some database question puzzling me.
I hope to seek some advice here.
So I am aware that Django gave us a default user model.
Now, when i created another model class Employee with fields like gender, birthday, as well as i wanted to include first_name, last_name (both which exist originally in User model).
My first question:
Is the best way to link both classes using OneToOneField?
My second question:
Apart from OneToOneField linking the User. Is it possible to have OneToOneField for both first_name and last_name under Employee model?
Or if i should just add a normal CharField for firstname and lastname under Employee model and ignore the default firstname and lastname field in the given User model?
What I am trying to achieve is I want the user to be able to edit their first name and last name by accessing the ModelForm of Employee instead of ModelForm of the User model.
Or if there's a better way to handle my situation?
Here's my Employee class.
class Profile(models.Model):
FEMALE = 'F'
MALE = 'M'
GENDER_CHOICES = [
(FEMALE, 'Female'),
(MALE, 'Male'),
]
# user_id = models.ForeignKey(User, on_delete=models.CASCADE, related_name="employee")
user = models.OneToOneField(User, on_delete=models.CASCADE)
# Should i use OneToOneField here?
fn = models.CharField(max_length=50)
ln = models.CharField(max_length=50)
gender = models.CharField(max_length=20, choices=GENDER_CHOICES)
designation = models.CharField(max_length=50)
phone_number = models.CharField(max_length=15)
date_joined = models.DateField(auto_now_add=False)
manager_id = models.ForeignKey(User, on_delete=models.CASCADE, related_name="manager")
department_id = models.ForeignKey("Department", on_delete=models.CASCADE, related_name="employee_department")
address = models.TextField()
Hi, I have a table in this schema, it has contracts information, like where the job is, what is the salary, if it has a retirementplan etc...
Id, Continent, Country, City, Industry, Job, Salary, RetirementPlan, HealthInsurance, and it continues...
And I'm trying to get average values for the contracts in cities, I want to know what is the average contract in Amsterdam, like, what is the average salary, does they have Health insurance in general or not, etc... For the all columns, if it is a continues value, I want to get the average, if it is categorical string value, I want to get the most frequent string.
For continues values, I have a query which gives it to me, but I'm stuck at getting the most frequent string categorical values in columns.
SELECT c."Continent", c."Country", c."City", avg(c."Salary")
FROM "Contracts" c
GROUP BY c."Continent", c."Country", c."City"
For example, in RetirementPlan column, the possible values are: "Yes", "Very rare", "Not common". What should I add to this query, to get what is the most frequent value for a city.
So it should return something like
Europe | Netherlands | Amsterdam | 3000 | Yes
3000 is avg(salary), Yes is the most frequent value for amsterdam, in the column RetirementPlan
We can imagine it like
Table:
Continent | Country | City | Salary | RetirementPlan
Europe | Netherlands | Amsterdam | 1000 | Yes
Europe | Netherlands | Amsterdam | 2000 | Yes
Europe | Netherlands | Amsterdam | 3000 | Very rare
Europe | Germany | Berlin | 4000 | Not common
Europe | Germany | Berlin | 5000 | Yes
Europe | Netherlands | Berlin | 6000 | Not common
I want to get
Continent | Country | City | Average Salary | FrequentRetirementPlan
Europe | Netherlands | Amsterdam | 2000 | Yes
Europe | Germany | Berlin | 5000 | Not common
I'm still looking for the solution
UPDATE tableName
SET ... = ...
ORDER BY fieldName DESC;
I want to do something like this in MySQL v8 but they removed this feature from 5.7
There is a Berlin in the Netherlands?
I can only think of:
SELECT Continent, Country, City, avg(Salary),
(select C.RetirementPlan from (
select B.RetirementPlan, count(*)
from Contracts B
where (B.Continent, B.Country, B.City) =
(A.Continent, A.Country, A.City)
group by 1
order by 2 desc limit 1
) as C)
FROM Contracts A
GROUP BY Continent, Country, City;
Thank you! Everybody knows that the Berlin has started to move around the world.
🙂
yo guys how do I make NOT NULL constraint in MySQL only apply on a condition
Anyone ever worked with neo4j? I'm using it for a project at work and I'm interested to know how others feel about it, just in general.
It was proprietary and we weren't too enthusiastic about their clustering modes
Interesting. What would you have used instead, given the choice?
We looked at neo4j, dgraph, janusgraph and another one I forgot the name of.
We ended up with janusgraph + bigtable.
But overall the state of distributed graph DBs has a lot of room for improvements and I would suggest to also evaluate if a regular mysql/postgres would work. But that's also highly dependent on your workload
I played around with it a bit a while ago (just for testing it though, nothing big or serious) - it is interesting, but designing the database schema/model properly (efficient and easy to work with) can be nontrivial even for things that should™️ be simple.
https://maxdemarzi.com/2015/08/26/modeling-airline-flights-in-neo4j/ for example ; that guy seems to know what he's doing... but I'm not sure where should you draw the line for the efficiency vs usage trade-off
hey i want to select a specific row in a table in php how would i do that.
Anyone know any free resources I can use to learn DynamoDB with Python
:incoming_envelope: :ok_hand: applied mute to @torn sphinx until <t:1647764616:f> (9 minutes and 59 seconds) (reason: duplicates rule: sent 4 duplicated messages in 10s).
await self.bot.db.execute("DELETE FROM modlogs WHERE guild_id=$1", guild.id)
``` will this delete the row from a table of the specified guild? (pgsql)
Assuming the asyncpg module is used, it will delete all rows with that specific guild id.
yup thanks!
Don't forget to commit
i think asyncpg autocommits by default
Hello the following table structure makes sense?
CREATE TABLE PRI_KEY(
pk int8 NOT NULL
);
CREATE TABLE FOREIGN_KEY(
fk int8,
FOREIGN KEY(fk)
REFERENCES PRI_KEY(pk)
ON DELETE SET NULL
ON UPDATE CASCADE
);
Hi guys,
I have a flask app, so when I hit on routes which are not in the same file, it always throws 404 not found error:
Directory structure:
-> user
-> __init__.py
-> models.py
-> routes.py
-> app.py
app.py:
from flask import Flask
app = Flask(__name__)
from user import routes
@app.route('/') # THIS ROUTE WORKS
def index():
return '<h1>HOME</h1>'
if __name__ == '__main__':
app.run(debug = True)
routes.py:
from app import app
@app.route('/register', methods=['GET']) # THIS DOESN'T WORK - 404 NOT FOUND ERROR
def register_user():
return '<h1>Register a user!</h1>'
someone brought an interesting question to the dpy server, i suggested they ask here but seems they didn't.
CREATE TABLE album_position(
album_id int REFERENCES album,
image_id int REFERENCES image,
position int
)
with a table like this, what would be the best way to keep position as the index of the image in the album, and be able to re-order them
like we have 5 images, positions 1,2,3,4,5 and want to move the one at position 5 to position 2, changing its position and updating position of all rows after
is there a better structure or way of thinking about this or does sql have some "easy" way to do this? (postgres specifically)
my first instinct is to do something like the following (or sql equivalent)
rows = execute('SELECT * FROM album_position WHERE album_id = ? ORDER BY position', (album_id,))
rows[new_index] = rows.pop(old_index)
start_update_at = min(new_index, old_index)
update_rows = [position, row.image_id for position, row in enumerate(rows[start_update_at:], start=start_update_at+1)]
executemany('UPDATE album_position SET position = ? WHERE image_id = ?', update_rows)
Arrays might be an alternative, but I think the lack of referential integrity makes them not worth it.
What you are doing makes sense, but I believe you should be able to do it with fewer queries
Use a where clause to limit the rows you're updating based on their position, and then just add/subtract from its position
You'll also need the album_id in that update statement. But what if the image is not unique within the album? It won't work then.
def getGuildData(guild):
#BRUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUuu
return guilds.find({ "guildid": int(guild.id) })```
this is for pymongo
it always returns nothing but in the mongo database
{"_id":{"$oid":"62364adbdcb4674fac34568b"},"guildid": {"$numberLong":"947945871692148756"}
its right there
Can anyone help out with a very specific SQLAlchemy question about scoped_session and query object
want to create a loop or something. all my images are in order. like part_1, part_2, part3 . want code to automatically do code for like part[i+1].png
from skimage import io, img_as_float
import numpy as np
image= io.imread(r'C:\Users\guest\Dropbox\con1_outfolder_split_30sbeforepeak2min30safterpeak\part_1.png')
image = img_as_float(image)
print(np.mean(image))
hello
i have a question on py mongo
why does this find one id and not the correct info i need and how can i fix it so it finds what i need and provides all info
@commands.Cog.listener()
async def on_member_join(self,member : discord.member):
results = collection.find()
for result in results:
if result["_id"] != member.guild.id : return
else:
for query in result:
if str(query) == "_id":
pass
else:
server = result[query]
channel = self.client.get_channel(server[0])
first_message = server[1]
second_message = first_message.replace("{member.mention}",f"{member.mention}")
third_message = second_message.replace("{member}",f"{member}")
final_message = third_message.replace("{member.guild}",f"{member.guild}")
await channel.send(f"{final_message}")```
do you still need help with this?
if data := collection.find_one({"_id": member.guild.id}):
...
Dont use pymongo for discord bots, cause its blocking, use motor (async wrapper for mongo in py)
how can i fix it so it finds what i need and provides all info
can you tell how your data looks like? 🤔
Best package to use for MongoDB? i'm newbie with mongo so i'm searching for something which will help me to learn to, with a good docs if it is possible
Best package to use for MongoDB?
pymongo(https://pymongo.readthedocs.io/en/stable/)
But if you want in async-await/concurrency use motor (https://motor.readthedocs.io)
whats a good database to use for low activity databases?
Depends on what you need
sqlite3
Sometimes you can just use the file system. Sometimes SQLite3 is the right choice. Sometimes you might just want a single JSON file.
just a database that i can add more overtime, no big amounts of data. but just enough that a database is needed
how much data do you plan to store? and what kind of data? and how do you want to query it? and how often to read/update?
its a username and password system, with a boolean added for verified users, and one for unverified.
like email verification
personally, i dont prefer .json .txt as db. i suggest you to use sqlite3
how many users will you have?
I think you should be good with SQLite.
like 50 at LEAST
SQLite should be fine, even for thousands or tens of thousands of users
Also, I hope you will remember to hash the passwords:
- How NOT to Store Passwords! (with subtitles) https://www.youtube.com/watch?v=8ZtInClXe1Q
- see
bcrypthttps://pypi.org/project/bcrypt/
i'm getting some issue:
from motor import motor_asyncio
client = motor_asyncio.AsyncIOMotorClient('localhost', 27017)
db = client.test_databse
collection = db.test_collection
async def do_insert():
document = {'id': 1}
result = await db.test_collection.insert_one(document)
import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(do_insert())
there is no current event loop
Just do asyncio.run(do_insert())
attached to a different loop
Can you show the full error and your new code?
thanks
traceback:
Traceback (most recent call last):
File "C:\Users\giacomo.dimatteo\PycharmProjects\MongoTest\main.py", line 12, in <module>
asyncio.run(do_insert())
File "C:\Users\giacomo.dimatteo\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\giacomo.dimatteo\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete
return future.result()
File "C:\Users\giacomo.dimatteo\PycharmProjects\MongoTest\main.py", line 9, in do_insert
result = await db.test_collection.insert_one(document)
RuntimeError: Task <Task pending name='Task-1' coro=<do_insert() running at C:\Users\giacomo.dimatteo\PycharmProjects\MongoTest\main.py:9> cb=[_run_until_complete_cb() at C:\Users\giacomo.dimatteo\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py:184]> got Future <Future pending cb=[_chain_future.<locals>._call_check_cancel() at C:\Users\giacomo.dimatteo\AppData\Local\Programs\Python\Python310\lib\asyncio\futures.py:384]> attached to a different loop
code:
from motor import motor_asyncio
client = motor_asyncio.AsyncIOMotorClient('localhost', 27017)
db = client.test_databse
collection = db.test_collection
async def do_insert():
document = {'id': 1}
result = await db.test_collection.insert_one(document)
import asyncio
asyncio.run(do_insert())
Not really python, but does anyone here some understanding of mysql? Im trying to order this table in order of the numbers that comes within the table after the letters, in the column emnekode i want to put in all data with xxx1000 before its showing the data with xxx2000
you should generally initialize all the async objects inside an async function. Like this:
import asyncio
from motor import motor_asyncio
async def do_insert():
client = motor_asyncio.AsyncIOMotorClient('localhost', 27017)
db = client.test_databse
collection = db.test_collection
document = {'id': 1}
result = await db.test_collection.insert_one(document)
asyncio.run(do_insert())
AsyncIOMotorClient, can takes io_loop 🤔 iirc
loop = asyncio.get_event_loop()
client = AsyncIOMotorClient(..., io_loop=loop)
btw I don't find any changes in the database
asyncio initially supported passing the event loop everywhere, but later it dropped that. So now libraries are slowly deprecating this.
(and you shouldn't be passing the event loop around)
righ right, Python 3.10 removed the loop parameter,
You have not specified ordering of that column yet, and also the column is a string so to order by the integer part, you can get the substring of it and order by that value.
is json a database 
A "database" is some place where you persist data, and which you can query for the data you saved.
JSON is a data representation format.
However, a JSON file could be used as a "database"
well if it is, how would I add more items in an array
Like ```py
[
{
"guild_id": 9129390129309123 {
"users": 123123123123,
"users": 123123123123123,
}
}
]
Some thing like that
but when I do, each time the "users" gets written over by the new one
I try to use different methods and i get AttributeError: 'dict' object has no attribute 'append'
!e
A dictionary maps keys to values. It doesn't make sense to append to a dictionary, you can only set a key in it.
users = {}
users["alice"] = {"balance": 420}
users["bob"] = {"balance": 69}
print(users)
users["alice"] = {"balance": 10}
print(users)
@brave bridge :white_check_mark: Your eval job has completed with return code 0.
001 | {'alice': {'balance': 420}, 'bob': {'balance': 69}}
002 | {'alice': {'balance': 10}, 'bob': {'balance': 69}}
Can you tell more about what you want to store?
Ok, so this is for a discord bot
I need to use the command !add pilot <userID> right
file is pilot.json, and each time it is ran, each server should have its own array
it adds the userID to the json file , and saves it for another command called queueing
Queuing will be a whole other thing that ill figure out but all of my things are stored in a json
You could have a dictionary mapping the guild ID to a list of users, like:
{
"pilots": {
"9129390129309123": [123, 456],
"9129390129309123": [456, 789]
}
}
``` or
```json
{
"9129390129309123": {
"pilots": [123, 456]
},
"9129390129309123": {
"pilots": [456, 789]
}
}
well I like the last one more..
How would I do that?
do what? Create such a dictionary?
Yep, and append new users each time
Do you know how to work with dictionaries and lists?
well
with open("balh.json", "r") as f:
d = json.load(f)
d[str(ctx.guild.id)] = {}
d[str(ctx.guild.id)]['pilots'] = []
with open("balh.json","w") as f:
json.dump(d, f)
```?
would it be that?
That would work, if there is no such guild in the file yet
Ok, and if I wanted it like this
Would I do like
var = [userID] and then d[str(ctx.guild.id)]['pilots'] = var
That would overwrite the list of pilots with a list of just that one user
that again, how do I add to it?
Use the append method on the list
so d[str(ctx.guild.id)]['pilots'].append(var)
sounds correct
alright, ty, imma try
d[str(123981923123123)]['pilots'].append(var)
AttributeError: 'str' object has no attribute 'append'```
ill be afk for a bit so brb
Can you show what d is?
The error is saying that you're trying to append to a string.
Hey all, pardon me. Can someone please point me in the direction of good DB practice sites for beginners. Projects, exercises or whatever to sharpen my 'skill'
If you're just starting out, I recommend SQLBolt: https://sqlbolt.com/
Then you might want to make a simple application, like a To Do list. Python conveniently comes with an interface to SQLite: https://docs.python.org/3/library/sqlite3.html
I see, thank you very much @brave bridge I'm on these right now.
You mean print d right? D is the json.read
right
What is motor
!pypi motor
hi
i need help with sqlite3 i keep getting an error ive had it for 3 days and havent managed to fix it
its getting frustrating
@tall quiver when asking a question, please provide the details: the code, the error, and everything else relevant
otherwise we don't know what to suggest you
i have in one of the help channels
but i will again here
soo basically
i have this save function
def save():
global password, username
password = passwordCreateEntry.get()
username = usernameCreateEntry.get()
insert_password = """INSERT INTO masteraccount(password)
VALUES(?) """
cursor.execute(insert_password, [password])
db.commit()
insert_username = """INSERT INTO masteraccount(username)
VALUES(?) """
cursor.execute(insert_username, [username])
db.commit()
loginWindow()
messagebox.showinfo("Account Created", "Account has been successfully created")
sqlite3.IntegrityError: NOT NULL constraint failed: masteraccount.username
and i keep getting this error
# Database for accounts
with sqlite3.connect("account_vault.db") as db:
cursor = db.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS masteraccount(
id INTEGER PRIMARY KEY,
password TEXT NOT NULL,
username TEXT NOT NULL);
""")
let's move to #help-apple then
okay
I will not use motor and want to stick with pymongo but why is it giving this error?
@commands.Cog.listener()
async def on_member_join(self,member : discord.member):
results = collection.find_one({"_id": member.guild.id})
for result in results:
print(result)
if result["_id"] != member.guild.id : return
else:
server = result[query]
channel = self.client.get_channel(server[0])
first_message = server[1]
second_message = first_message.replace("{member.mention}",f"{member.mention}")
third_message = second_message.replace("{member}",f"{member}")
final_message = third_message.replace("{member.guild}",f"{member.guild}")
await channel.send(f"{final_message}")``` why is it only giving the _id as the result and not the value?
find_onereturns a single result document, not an iterable of results- a document is returned as a
dict - iterating over a dict iterates over its keys, which in this case are strings
that should be enough information to help you understand the error 🙂
yup it is way to complicated for what i am trying to do lol
what do you mean?
let me give you the updated one
@commands.Cog.listener()
async def on_member_join(self,member : discord.member):
results = collection.find_one({"_id": member.guild.id})
for query in results:
print(results)
if results["_id"] != member.guild.id : return
else:
server = results[query]
channel = self.client.get_channel(server[0])
first_message = server[1]
second_message = first_message.replace("{member.mention}",f"{member.mention}")
third_message = second_message.replace("{member}",f"{member}")
final_message = third_message.replace("{member.guild}",f"{member.guild}")
await channel.send(f"{final_message}")```
that is what i am stuck on now
but results can only ever be 1) a dict, or 2) None
so you are still iterating over a dict, which means you are iterating over keys
so how do i fix this so it works properly
what are you trying to do
create a fully customize able welcome message i using the database
that's too general
more specifically, what are you trying to do
how is this code intended to work
ok
server[0] is susposted to get the channel
server[1] gets the message it sends everytime a person joins
that's a weird format
so you actually do want to iterate over the keys of the result?
i would argue that this is not a good document layout, but if it's already done then it's not worth changing it
it looks like there are only ever 2 fields on the document, _id and the actual id as a key
is that right?
i'm talking about the database layout, not your code
database layouts do not arise naturally from the fog of time
you, the programmer, chooses it
now it's possible that you have buggy code writing unintended data to the database
in which case your first job is to fix that code, instead of trying to press on working with an incorrect database
well the _id is the id of the guild, the other id is for the array because i had nothing better to put there
what does this database actually contain?
for that matter, why are you even using mongodb? this is reason #1 why i recommend against it unless you are already an experienced developer, it encourages bad stuff like this
i saw it the first time. what do these values mean?
the id of the server, the channel it sends to, the message it sends to that channel everytime a member joins
and i will not use a json file and no sql
what's wrong with sql?
why wouldn't you store it in this format?
{
"_id": 12345, // opaque document id, created by mongo itself
"server_id": 9876, // discord guild id
"welcome_channel_id": 4567, // welcome channel id
"welcome_message": "Hello!" // welcome message text
}
this maps 1:1 to a single sql table
well the _id would be the server id so then just 2 fields
sure, that's fine too
i find it very hard to learn
CREATE TABLE welcome_messages (
server_id BIGINT PRIMARY KEY,
welcome_channel_id BIGINT,
welcome_message TEXT
)
i have tried once


