#databases
1 messages · Page 124 of 1
sqlbolt do a good bit of stuff
but i learnt most of my stuff with postgres with the normal SQL tutorial with w3schools and then the DO install tutorial
how do i do that sir?
@elder ferry you can have the query to select the department names where the name/id is in a subquery
So the where clause is something like:
WHERE dept_id IN (SELECT dept_id FROM employees GROUP BY dept_id HAVING max(age) < 40);
So it transaltes to: SELECT ... WHERE dept_id IN (department ids where every employee is younger than 40)
Yeah specify the table name before it
Ambiguous is when you have multiple tables in the query that have same column name, so it doesn't know which one you are referring to
And try it, I have no idea what your tables look like, but I just sent something you could follow.
@proven arrow what is the newest version mine is at 3.22.0
I don't remember you'd have to check their release page, but then reason for your error is because window functions was introduced in 3.25
oh ok then ill try to update it
@brazen charm Thanks, those are great
@proven arrow the aiosqlite is the most updated version but the sqlite version isnt
do I have to update sqlite or something
I am not able to update python because I am using a hoster but can I still update sqlite
it is on 3.22.0 and I need at least 3.25
Easiest way is to download the binaries from the sqlite download page and replace it with the one python has.
For example, on windows you can download the pre compiled DLL, and then replace it with the sqlite3.dll file in your python installation folder.
ok I can download it but is there a way I can replace it in cmd because I use repl.it hosting
you can replace stuff through terminal but there isnt a way I can replace it in the python folder
Not sure. I don't use repl. If they allow you to manage the files then yeah otherwise 🤷♂️
what is the file directory for the file
Hi so I am using an online MySQL database server and the table name is on the form of bytearray, so how do I fix it, like how do I get the name of the table then
The bot is ready
[(bytearray(b'Cricket'),)]
571299307052072980
Ignoring exception in command start:
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/discord/ext/commands/core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "<string>", line 674, in start
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/mysql/connector/cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/mysql/connector/connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'dOX6EHic32.CRICKET' doesn't exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/discord/ext/commands/core.py", line 92, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ProgrammingError: 1146 (42S02): Table 'dOX6EHic32.CRICKET' doesn't exist ```
anyone able to help in #help-cupcake
Pls ping me if you know it
Yes, it's just a text file
.sql is just file extension name to give it some meaning that it's for sql
How can i connect my that database to my website which i have installed on my VPS?
Been watching one Tutorial for web Dev and he's using Mongodb...but i don't wana use it as i am using Pgsql in my bot
@feral thorn website is in VPS, database is where?
In same VPS...i was confused on it for a bit. Sorted bud, thanks
what is wrong with this code it says something with the as async with db.execute('SELECT userguild,xp,level FROM level ORDER BY xp DESC as user_rank WHERE userguild = ?', (userguild,)) as cursor:
Can you see and issues in thie Sqlalchemy syntax:
sql_filter_items = f"select * from items where name like '%{item_name}%'"
@sinful condor should as be AS ?
Ok thanks I’ll try that when I can
Don’t worry about intents I’m on my phone but I tried this and got an error
.execute('SELECT userguild,xp,level FROM level WHERE userguild = ? ORDER BY xp DESC as user_rank, (userguild,)) as cursor
It says syntax error eol while scanning string literal
@radiant elbow
I think you want:
.execute('SELECT userguild,xp as user_rank,level FROM level WHERE userguild = ? ORDER BY xp DESC', (userguild,)) as cursor
what module or library I should use to interact with postgresql database?
asyncpg or psycopg2
SQL is good but if I only have 2 values to store, what should I use?
Why not just us a JSON or XML file?
Json would work yeah
why does this code return this with no rank ('449756487510654988', 42, 3) async with db.execute('SELECT userguild,xp,level as user_rank FROM level WHERE userguild = ? ORDER BY xp DESC', (userguild,)) as cursor:
userguild is the user id
what do you mean by "with no rank"?
you selected 3 columns, and it returned 3 values, so that looks sane to me.
I am trying to get the rank with it to put at the end of the leaderboard if they arent in the top 5 but I cant use rank because of my sqlite version
to say what rank they are
thats why I had as user_rank
all as user_rank is doing is selecting the level column and returning it with the name user_rank
ok how can I get what I said
like
1st
2nd
3rd
4th
5th
your place
I have 1-5 but cant figure out this
I am able to get it to work on the new version but I am trying to use repl.it hosting which uses an older version of python
maybe something like this:
select userguild, xp, level, (select 1 + count(*) from level as p2 where p2.xp > p1.xp) as user_rank from level p1 order by xp desc
ok ill try that
@radiant elbow Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 859, 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: near "where": syntax error
oh you put where 2 times did you mean to do that
there's a "where where" in the middle of that that should just be one "where"
edited the one above to fix it.
select userguild, xp, level, (select 1 + count(*) from level as p2 where p2.xp > p1.xp) as user_rank from level p1 where userguild=? order by xp desc
ok
MySQLdb._exceptions.OperationalError: (2006, '') this is killing me
what does it mean? 😒
Nvm figured it out. If anybody ever comes across this I had to remove mysql.init_app(app) from my program... various mysql/flask methods/imports got confused I guess, no idea why it stopped working in the first place (which caused me to mess around with alternatives) but at least its working
from tkinter import *
root = Tk()
e = Entry(root, width=25)
e.pack()
e.insert(0, "Enter your name: ")
def myClick():
hello = "Hello " + e.get()
myLabel = Label(root, text=hello)
myLabel.pack()
myButton = Button(root, text="Send", command=myClick)
myButton.pack()
root.mainloop( )
how can i store the data people input in here^^
@tardy perch are you looking for a database solution or somethiing stored in a file ?
im not sure, i coded a input box, i want to store the data that people input
@tardy perch Do you expect large amounts of data, and to you expect it to change a lot
probably change a lot
when i store this data, can i store more than 1 input?
import pickle
import os
os.system("clear")
IDs = [1, 2, 3]
print("Original List")
print(IDs)
pickle.dump(IDs, open("IDs.dat", "wb"))
IDs.remove(2)
print("Changed List")
print(IDs)
IDs = pickle.load(open("IDs.dat", "rb"))
print("Original List")
print(IDs)
ok i made this, instead of IDs = [1, 2, 3] i want it to be so when people input shit, i can save it
you could store more input if you assign e.g. a primary key that is assigned automatically and then save in a local db
hm im not sure i understand
Perhaps this can help ??
https://www.python4networkengineers.com/posts/python-intermediate/create_a_tkinter_gui_with_sqlite_backend/
Python for network Engineers
@versed robin this is my code
wait wrong one
from tkinter import *
root = Tk()
e = Entry(root, width=25)
e.pack()
e.insert(0, "Enter your ID: ")
def myClick():
#hello = "Hello " + e.get()
myLabel = Label(root, text=("Thank you."))
myLabel.pack()
myButton = Button(root, text="Send", command=myClick)
myButton.pack()
root.mainloop( )
ok this is the correct one
my problem seems to be i want to save and store the e.insert(0, "Enter your ID: ") part
u can run the code, i want to save the data u input
Sorry - cant help on that one
Hi, I am getting an error with some code. Can anyone help?
# Error
AmbiguousColumnError: column reference "isstaff" is ambiguous
# Code
INSERT INTO staff VALUES($1, $2, $3) ON CONFLICT (userid) DO UPDATE SET owner = True AND isstaff = True # $1, $2, $3 are being inputed
kk
yeah that then shows this error UndefinedColumnError: column "staff" of relation "staff" does not exist
oh wait
wait for what ?
Hi, does anyone know how to get this query to show the oldest employees from EACH department instead of showing just the oldest employee?
You can group by department
Hey! I'm just new to using databases, I'm using MySQL and mysql.connector.
I need help on getting the list of error codes and when those errors are raised, I tried searching on the net but I couldn't, can anyone help me with it?
You can find them all here, https://dev.mysql.com/doc/mysql-errors/8.0/en/
Thanks!
this sql statement runs and gives output
select @completed_tasks;
but when i try to do the same in python i get error
self.cur.execute('select @completed_tasks').fetchone() ```
any idea why ?
Thanks in advance.
hi, what are current options to consider when wanting to go for a mutli process safe key-value database?
(non-networked, in process)
what's the error?
good evening, i'm tryn' to connect python to database with my WSL on windows but i keep getting this error
error
code
Please help me, thank u!
"On Authentication method tab- select Use Legacy Authentication method"
thankkkkkksssssssssss a billion dude
you saved a lot of time
thanks again!
but one question how to do it on cli?
I think this is a server side setting isn't it?
once you change the server, you should be able to legacy authenticate with a cli client
I don't know though, I'm just inferring from the stack overflow
anyway thanks a lot bro
hey guys
I'm trying to organise my table and I've stumbled upon a problem with grouping the same values. I want to group marks (2 and 3 together as "oceny slabe", then 3.5 and 4 as "oceny srednie" and 4.5 and 5 as "oceny wybitne")
SELECT CASE WHEN ocena < 3.1 THEN 'oceny slabe' WHEN ocena < 4.1 AND ocena > 3.1 THEN 'oceny średnie' ELSE 'oceny wybitne' END AS kategoria, COUNT(*) AS "LICZBA_OCEN" FROM oceny WHERE rok_akademicki = '2005/06' AND rodzaj_semestru = 'zimowy' AND ocena IS NOT NULL GROUP BY ocena ORDER BY ocena
but what I receive in return is this, which doesn't look so neat. I know that GROUP BY is executed as one of the first commands, so most probably it's still grouping by old "ocena" values (by 2, 3, 3.5, 4, 4.5 and 5)
I tried to do it with a subquery, so that maybe I could retrieve "kategoria" and group by it, which should then group by "oceny slabe", "oceny srednie", "oceny wybitne", but no, it still gives back 6 records
do you maybe see where I'm making a mistake? what do I overlook?
should you group by kategoria?
OH... you can't do that either... you're going to have to group by the case statement itself
or do the subquery to create that new column temporarily
then group on that
SELECT CASE WHEN ocena < 3.1 THEN 'oceny slabe'
WHEN ocena < 4.1 AND ocena > 3.1 THEN 'oceny średnie'
ELSE 'oceny wybitne' END AS kategoria,
COUNT(*) AS "LICZBA_OCEN"
FROM oceny
WHERE rok_akademicki = '2005/06' AND rodzaj_semestru = 'zimowy' AND ocena IS NOT NULL
GROUP BY CASE WHEN ocena < 3.1 THEN 'oceny slabe'
WHEN ocena < 4.1 AND ocena > 3.1 THEN 'oceny średnie'
ELSE 'oceny wybitne' END
ORDER BY CASE WHEN ocena < 3.1 THEN 'oceny slabe'
WHEN ocena < 4.1 AND ocena > 3.1 THEN 'oceny średnie'
ELSE 'oceny wybitne' END
oh, I didn't know I could put case in GROUP BY
let me check
woah, it worked
let me just study it once again
okay, I think I get it now
I shouldn't have grouped it by a column's name "oceny", but rather by the whole condition, right?
is that the case?
SELECT kategoria, LICZBA_OCEN FROM ( SELECT CASE WHEN ocena < 3.1 THEN 'oceny slabe' WHEN ocena < 4.1 THEN 'oceny średnie' ELSE 'oceny wybitne' END AS kategoria, COUNT(*) AS "LICZBA_OCEN" FROM oceny WHERE rok_akademicki = '2005/06' AND rodzaj_semestru = 'zimowy' AND ocena IS NOT NULL GROUP BY ocena )
this is my attempt with subquery, to retrieve that temporary column, also didn't work.
good evening, i am just stuck at a small problem, please check if syntax is correct or not INSERT INTO file(access_key, access_path) VALUES(EIvZsE, EIvZsE.jpeg);
i'm getting this error when i execute this command, any guess why it happens?
the values should be naming variables or have quotes on them
looks like they're not naming variables to me, but rather you just forgot quotes
you're still trying to group by ocena. You want to group by kategoria right?
and you want to do it outside of the subquery
damn brooooo you r awesoooommeeeee
yes, but when I put "group by kategoria" outside the subquery, I receive an error
have you removed the group by ocena inside the query?
yes
what's the error?
it doesn't work with group by ocena, kategoria as well
though it doesnt make sense
either way
try putting a sum on your measure (LICZBA_OCEN) in the first line
usually you group everything you select unless it's an aggregate
so you'd have to group by the second column you select or aggregate it
you know you'll have multiple value under each category - you know how to interpret that better than I do knowing your data. Do you want the sum of those? the max? etc.
hmm, "group function is not allowed here"
even when I try to group by "sum(liczba_ocen)
I need to count them
you are counting them in the subquery
that returns a total
for two of each category
you likely want to sum those?
so select ocena, sum(LICZBA_OCEN)
at the very first line
two sum the result of the count
if they are actual duplicates (multiple entries counted)
then sum will get too much
if it's just splitting the data across two values, then sum should work
Thanks for asking. I solved it
you want to select kategria at the top
sorry, my bad
then group by kategoria at the bottom
instead of ocena, right?
kategoria is the focus outside the subquery
oceana has been used to make kategoria at that point and is no longer needed
the subquery is turning ocena into kategoria
so yeah, no ocena outside the subquery, just group by kategoria and you shouldn't have to group by the measure since you're aggregating it (with a sum).
maybe now conditions are wrong?
now it seems that 2 falls into 1st and 2nd condition
2 and 3
maybe it's messing it up
let me check
oh, since we're already doing a count, I think the sum can't be done here
right, which is why we wanted the sum
so you will probably just have to do single query, group by the full case statement
okay, just as you showed me
alright, I wanted to unserstand it with a subquery, but maybe there is no way like that
either way, I have now a proper amount of records with the single query
thank you for the big help @velvet gazelle !
you are great
np, I'm sure there is some clever hack, I just don't know it. I know some people would just use materialized view as an intermediate step
then they'd essentially create the intermediate table and be able to do a sum and group on it
okay, I see
no worries
have a great one!
Hey Pythoners, planning to build a PostgreSQL in web. Where should I host my database for free?
PythonAnywhere looks like they do not offer Postgres in thier free tier.
what's your use case? There's free postgres databases
already set up, you just sign up for an account on a shared DB
Just for learning
I have already mongoDB and heroku. But this time I like to learn postgres
but if you are wanting to learn to setup the server, that's already done here
Thanks for the elephant!
saw that one. Never thought it is a web postgres
How do I insert values into a table using user input in py?
it's not asking for user input:/
you need to code that part separately. Once you have the values from the user, then you can insert them in to the table as shown in the example above.
If you're trying to figure out how to get input from the user, take a look at AtBS 2e, ch8 (https://automatetheboringstuff.com/2e/chapter8/)
i don't understand, i want the user to enter the values and in the same func I want that value to be inserted into table
You can put it all together in one big function... or you can separate out the database portion and the 'presentation logic'.
You're probably going to have multiple functions involving the database any way - creating the database, inserting records, reading them back, updating records, deleting records, etc.
This might be worth taking a look at: https://www.youtube.com/watch?v=iXYeb2artTE
Learn how SQL works from the ground up and how to use SQLite from your Python apps in this complete Python SQLite Tutorial!
In this video, we build a Python project that uses a SQLite database to store data. We also look at how SQL simplifies inserting, retrieving, and searching for data.
0:00 - Video Introduction / What to Expect
0:30 - Overv...
I'm actually poking my way through my own project right now, using that as a base.
i see
I actually tried tried to it before but it was showing error, so I wanted to know if I was writing the wrong command
I'll watch the video first-
This is about the simplest example I can cobble together in a hurry:
`import sqlite3
conn = sqlite3.connect(":memory:")
c = conn.cursor()
create_table_sql = '''CREATE TABLE IF NOT EXISTS todo (
id integer PRIMARY KEY,
item text NOT NULL,
start_date text,
end_date text
);'''
c.execute(create_table_sql)
insert_item_sql = '''INSERT INTO todo(item, start_date, end_date)
VALUES (?,?,?)'''
item = input("Enter the To-Do item: ")
start_date = input("Enter the start date: ")
end_date = input("Enter the end date: ")
c.execute(insert_item_sql, (item, start_date, end_date))
records = c.execute('''SELECT * FROM todo;''')
for record in records:
print(record)`
Returns:
(1, 'browse reddit', '12/19/2020', '12/20/2020')
Note how getting the user input is separate from creating the SQL query to insert the data?
im using motor package for pymongo
async for item in items.find({}).sort("level", -1): this works
but how do i sort a second time?
tried .sort("field1", -1, "field2", -1): and .sort({"field1": -1, "field2": -1)
Hey guys, I am super new to databases and I am attempting to query through my mongo database, find the objects with a certain tag and append them to a list. I am getting the JSON object must be str, bytes or bytearray, not Cursor when I do this as an error code.
My code is python query = collection.find( { "authentication": "yes" } ) query_parse = json.loads(query) i = (query_parse['Phone number added']) for i in query_parse: sms_list.append(i)
I can't be sure, but shouldn't there be a .fetchall() or .fetchone()?
I sure know it from psycopg2 but I don't know how pymongo works.
(collection.find(..).fetchall())
No scratch that, pymongo is different.
try: for x in query: print(x) to see if that works?
The problematic line is this one query_parse = json.loads(query), since collection.find() returns a cursor. So what you do is iterate the results of collection.find() and then append them to your list. Sorry for the confusion.
yes this works
The problem I am running into is that I only want a specific part of the query appended to the list
so I tried to parse the json to get it but that isn't working, are there any other options for this?
There might be something built in to mongo that I am overlooking
That I don't know sorry 😦
is it possible to find the closest match of a string using MySQL?
You can use sqlite, as an embedded database.
There are different options available each with their own benefits. You can look into LIKE, the SOUNDEX function, or full text search.
am i allowed to ask database homework question here?
!rule 5
5. Do not provide or request help on projects that may break laws, breach terms of services, be considered malicious or inappropriate. Do not help with ongoing exams. Do not provide or request solutions for graded assignments, although general guidance is okay.
I mean...
huh?
Look at rule 5
oh i look at your name think maybe you can help
Oh no, it's me who needs help
oh right ok i shall go read rule 5 now then, thanks
np
hello, just wondering if anyone can help? I have found the departments that have sold at least 1 product, but I need to narrow this list down to only include the departments that sell 5 or more different product types (pType)
so I need to find a query that shows me just the department 5 so dID = 5 because they sell products of 5 different product types
your querry is ryt
just at last
and quantity > 5;
Sorry could elaborate on just at last and quantity > 5?
u want the output to be of only which have dIB as 5 ryt
so
quantity = 5
sry its not > its =
I dont think thats what they are after, since they already filter by quantity
They would need to group by the dID, to get IDs that sell 5+ products, and then filter using this.
SELECT * FROM newagenttickets WHERE deletedstaff IS NULL
alr
guys, i have configured this os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
but python is still working with *cuda*
i dont have nvidia gpu so it isnt working
before it said "using plaidml backend". But i changed something a weeks ago, and i think i undo the changes, and it isnt working
ah nvm, i got it
await cursor.execute(f"SELECT messageid,victimname FROM bounty WHERE victimname ='{victim_name}'")
data = await cursor.fetchall()
if data:
message = [row[0] for row in data] # Gets the message ID
name = [row[1] for row in data] # Gets player name from SQL Query
print(message,name)```
anyone know why its printing like this?
[790395046021169172] ['G.O.A.T']
is there a way to remove the brackets without using str replace?
Your creating list's and wondering why they are lists?
Maybe you meant to just be like
message = data[0],
name = data[1]
Or whatever the requried indexing is
@digital wharf I tried that, but it said tuple is out of index ill try it again 🙂
print(data)
IndexError: tuple index out of range when i use data[0] and data[1]
printing data gives ((790395046021169172, 'G.O.A.T'),)
data[0][0], data[0][1]
@digital wharf awesome got it. seemed to work, weird i usually just use data[0]
and data[1]
Well its a tuple within a tuple
@digital wharf awesome thanks, so to loop through incase there is more then 1 on the DB i can do
if data:
for row in data:
if victim_name == data[row][1]:
await cursor.execute(f"DELETE FROM bounty WHERE victimname ='{victim_name}'")
print(f"Bounty has been cleared from: {victim_name} at : {time}. Updating bounty board.")
await sql.commit()
return 1```
Something like that
Although
That wont work, since looping over the tuple will return the tuple not an int index
so if vic_name == row[1]
awesome thanks 🙂
Also, never format queries like that. It will easily lead to invalid formatting and introduce SQL injection vulnerabilities. All database adapters in Python ship with some safe substitution mechanism, e.g.:
await cursor.execute(f"DELETE FROM bounty WHERE victimname =?", (victim_name,))
There shouldn't be any quotes around the ?, right?
I think you need them because you're passing a language to a language
SQL is going to expect quotes or a defined variable
(or a subquery)
I'm quite certain it shouldn't be there.
!e ```py
import sqlite3
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
cursor.execute("create table bounty (victimname)")
cursor.execute("insert into bounty(victimname) values (?)", ("Some Name",))
print(cursor.execute("select * from bounty").fetchall())
cursor.execute("delete from bounty where victimname=?", ("Some Name",))
print(cursor.execute("select * from bounty").fetchall())
@radiant elbow :white_check_mark: Your eval job has completed with return code 0.
001 | [('Some Name',)]
002 | []
@radiant elbow You mean my whole code is wrong with those quotes? xD
well, yes. The way you've written it, it's got a very serious bug in it: it's vulnerable to SQL injection. If the victim_name that you get contains a ' character, this can wind up terminating your statement and then running an entirely different one. For instance, if someone gave you a victim_name like ';drop table bounty; select ' then your f-string would expand to:
DELETE FROM bounty WHERE victimname ='';drop table bounty; select ''
Is it possible to search for something on MySQL DB by using part of the name. for example if you type part of the name?
@radiant elbow good point never thought of that. ill change it up
which is why fix error is trying to steer you in the direction of using parameter binding instead - it fixes that problem, making it both faster and safer.
you're looking for the LIKE operator. WHERE victimname LIKE '%something%' will find any records where the victimname contains something in the middle of it - the % is a wildcard, so that it can have any characters before or after the something
@radiant elbow i have this
await cursor.execute("SELECT PlayerName, PlayerID from players WHERE %s LIKE concat('%%', PlayerName, '%%')", ([name]))
``` doesnt seem to find anyone who has a similar name though
Hi. I'm creating a calendar database, and I'm getting an deadline, and I'm trying to automatically delete data that's past this deadline. What should I do? In mysql
Should the program call up the data from the database at 12 o'clock every day?
i am inserting data into postgres table using psycopg2, i got schema error, my table name is movies, my schema is public, isnt it?
@nocturne yew did you try inserting into just 'movies'?
yes @frigid glen i created postgres table. i corrected that error, it was wrong database name lol. i am struggling because i am populating that columns with json objects, so i need to use apply.set or something
What's the best way on storing international phone numbers in a database. Supporting as many as possible? I can't seem to find a clear answer to this
Random question what happens if PlayerName is "DROP TABLE players;" 🤔
Sounds like trouble
no, my bad!
Do you mean the data type of the column or the actual format of the number?
Well don't store it as actual numbers Use varchar/text type. And any formatting on how a number is displayed is not the responsibility of the database but rather your applications frontend/presentation layer should handle this.
As for format there is E.164 format
Twilio had good example of this, and it's what they recommend too. Let me see if I can find their link.
Thank youi I shall look into it once I am back 👍
Hello,
how can I get user signup auth refreshtoken and local id from firebase
how i can recup this link on mongodb
I have a question about no such table error or even better known as (main.auth_user_old)...
How should I fix it?
Send the screen shot here @floral cedar
pls
Got it?
uh is this django?
Yes
Try removing the main and just do auth_user_old
I can't even find a table named auth_user old
uhh idk
Do you know where can I find the answer?
idk if it is the problem with django or not but you can check #web-development
Thanks
@client.command()
async def balance(ctx):
async with aiosqlite.connect("Economy") as db:
c = await db.execute("SELECT bankaccount FROM economy1 WHERE user = (?)", (ctx.message.author,))
thedata = await c.fetchone()
await db.commit()
if thedata == None:
async with aiosqlite.connect("Economy") as db:
await db.execute("INSERT INTO economy1 (user, bankaccount) VALUES ((?), (?))", (ctx.message.author, 30000,))
await db.commit()
await ctx.send("Oops! You dont have an account! Oh well, thats fine. I just make you an account and gave you 30,000 credits! Use `.balance` to check your account!")
else:
Mbed =discord.Embed(
title = f"__{ctx.message.author.name}'s Balance__",
description = f"Bank Account : {thedata}",
color = 0xdeaa0c)
await ctx.send(embed = Mbed)
im using aiosqlite, why isnt it working?
Is this how you add a row?
How would I get the default value of a column with an SQL query?
Goood afternoon, has anybody encountered a situation where MongoDB (using Mongoexpress) shows fields in the document list, but when you open the document those fields are missing?
Here is the document from the list with the missing data.
Ooops disregard i discovered the error. When my code was doing a field update, it seems to save a NEW object like:
{ "_id": { "$oid": "5f973aeb1c00661a0b594b92" }, "integration_status": "Inservice", "nest_status": "S2 Maintenance" }
and didnt change the original document with the correct info.
{ "_id": "5f973aeb1c00661a0b594b92",
Still working on figuring out what im going to do about it but at least ive identified the problem. Leaving the info up in-case someone ever needs it.
i keep getting this error
Traceback (most recent call last):
File "D:\jayDEV\Customer Work\DanishPubG\test.py", line 6, in <module>
JsonDict = json.load(OpenFile)
File "C:\Users\Jibriel\AppData\Local\Programs\Python\Python39\lib\json__init.py", line 293, in load
return loads(fp.read(),
File "C:\Users\Jibriel\AppData\Local\Programs\Python\Python39\lib\json__init.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\Jibriel\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Jibriel\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
id check and make sure your dict is actually populated it seems right at the moment JsonDict is None
I'm getting "cursor is not connected" with mysql after every so often, not sure if it's queries or time dependent and I cant figure whats going wrong
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ProgrammingError: Cursor is not connected
mysql.connector.errors.ProgrammingError: Cursor is not connected
unsure why that is happening, but i'd recommend using an async library to connect to your mysql database for a discord bot - check out aiomysql
mysql.connnector is blocking
Error is because you will have closed your cursor somewhere and then you are trying to use the same cursor again.
What is a pool of connections
and what is re-usable connections?
And how do those work
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: no such table: users
sqlite3.OperationalError: no such table: users```What is my error here?
have you created the table users, yet?
yes
this is the db file
import discord
from discord.ext import commands
import random
import os
import sqlite3
conn = sqlite3.connect('mainbank.db')
c = conn.cursor()
client = commands.Bot(command_prefix = "!")
@client.event
async def on_ready():
print("ready")
@client.command(alieses = ["bal"])
async def bal(ctx):
user = ctx.author
await open_account(ctx.author)
Wallet = c.execute(f"SELECT Wallet FROM employees WHERE userid = '{str(user.id)}'")
Bank = c.execute(f"SELECT Bank FROM employees WHERE userid = '{str(user.id)}'")
embed = discord.Embed(title = f"{ctx.author.name}'s balence", color = ctx.author.color)
embed.add_field(name = "Wallet", value = f"{Wallet}")
embed.add_field(name = "Bank", value = f"{Bank}")
await ctx.send(embed = embed)
async def open_account(user):
users = c.fetchall()
if str(user.id) in users:
return False
else:
c.execute(f"INSERT INTO users VALUES('{str(user.id)}','0','0')")
with open ("mainbank.json","w") as f:
users = json.dump(users,f)
return True
@client.command()
async def beg(ctx):
user = ctx.author
await open_account(ctx.author)
users = await get_bank_data()
earnings = random.randrange(1001)
await profit(ctx.author, earnings)
await ctx.send(f"Yooo You just got {earnings}$")
async def profit(user, earnings):
users = await get_bank_data()
users[str(user.id)]["wallet"] += earnings
with open("mainbank.json","w") as f:
json.dump(user,f)
```This is the code
dont mind the syntax i am using it for dpy
where is the create_table syntax?
here
c.execute("""CREATE TABLE employees(
userid real,
Bank integer,
Wallet integer
)""")```
@craggy jewel - employees != users
oh ok
Its a way to keep database connections open so that they can be reused, instead of creating new ones each time. Basically you have a cache/collection of database connections.
And so when something needs a database connection it takes one from the pool, and when its done with it, it can return it back to the pool.
This way you avoid the overhead with connect/disconnect. And this is the main reason for using a pool, because you want to minimise the cost of opening/closing connections.
And the documentation for it: https://magicstack.github.io/asyncpg/current/api/index.html#asyncpg-api-pool
sql = ('INSERT INTO islands(channel_id, cp1, cp2, cp3 coopid, bank, storage, guild) VALUES(?,?,?,?,?,?,?,?,?)')
val = (chann.id, ctx.author.id, "None", "None", coop_id, 0, "None", "blue")
await db.execute(sql, val)```
this causes error
Command raised an exception: OperationalError: near "coopid": syntax error```
and the error isnt informative at all... ;-;
you have a space between cp3 coopid
and you are asking for 9 placeholders with ?, when you just pass 8 with 8 columns
Hi, I have these two events, but they don't add the server ID to the json, how can I fix this
@bot.event
async def on_guild_join(guild):
with open('.//cogs//prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '--'
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@bot.event
async def on_guild_remove(guild):
with open('.//cogs//prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
with open('.//cogs//prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
also, dont send this, people can come in and remove all your code
and mess up things
oh thanks for informing, i have the code copied
hello there, I don't know much about databases, but I got a task in school to display JSON data using python in a clean way on the screen. With pandas I got picrelated, although tidy, I would like something standalone, like a separate window with data, what can I use to achieve that?
I used plotly, thanks anyways! Merry Christmas and Happy Holidays!
Good evening. Is there anyone that has experience with two phase locking (2PL) available to help?
So this
for customer in customers:
name = f"""CREATE TABLE IF NOT EXISTS
{customer.id}(article_count INTEGER, store_id INTEGER)"""
raises sqlite3.OperationalError: near "<customer_id>": syntax error.
<customer_id> is just a placeholder for the id
Does someone have an idea?
What is the actual value?
234395307759108106
Well first why are you creating tables dynamically like that. Is that per user? That's really inefficient, unless you have a real good reason.
And its because name can't start with number
Well it can but you need to escape it
Well, I thought it might be a good idea to add a table for every user, when every user has multiple values (or rows - whatever they're called)
Thats a very bad way to design it. If you want me to explain why i can, but in this case you should have a single table with each row representing a users details.
Each different value you want to store can be stored in columns of that table.
Makes sense.
By escaping you mean putting a \ in front of the {...}, or am I understanding something wrong?
Well you dont need to escape it if your table name is not the user id, or are you still sticking to your original idea with each table per user?
No, I'm not, but lets say I would like to create a table for every store and save the store by it's id. And to this table I'll add rows for each user
Its the same for the store, it should not have its own table. Anyways you can decide for this later.
What database are you using?
sqlite
Ok so you can put the name in single quotes, double quotes, backticks, or in [ ]
Now I get what you mean. The tables name f. ex. would be "stores" and to that you'll add the store_id as row and so on - Had a short brain lag
Thanks sir
Yeah thats what i meant and its ok. In fact one of my coworkers recently wanted to do something like this, and asked me so this was part of the response i sent him, in case you were wondering some of the reasons why.
Very much appreciated
So, I'm trying to make a Discord bot, that saves the messages of a user based on the guild they wrote the message on. But I'm stuck at how I would approach that.
I create a table called "guilds" in which I want to add every server the bot is in. But I have to define the fields when creating the table. How am I suppose to add a new guild after the bot got added to another server?
To add data to the database you can use the INSERT statement to add data to a table.
Hey does anybody have a guide/best practices for connecting to databases? I don't need help, I'm just trying to see if the method i'm using is ideal or I could be doing something better.
Do you use an ORM ?
Yeah I use Alchemy, pyodbc and psycopg2
My script imports from CSV into one database, then updates 2 MSSQL databases and 1 Postgres database. Right now I just initiate the connections via a Class and then use that class throughout the script when i'm moving to a specific database or calling stored procedures on that database.
guys can someone help me
i forgot my name fro postgresql xd
how can i get it again??
Merry Christmas Everyone. Keep Safe.
Also thanks for everyone helping me too when I need help with coding.
Merry Christmas x 🎄🤶
Keep safe x let's all hope 2021 is going to be better XD 😂😂
So, I now have a table called users. The table has a primary key, which is the users id. And another key that is a counter, which increments by 1 every time a listener triggers.
I tried this:
get_current_count = f"SELECT count FROM users WHERE user_id = {user.id}"
current_count = cursor.execute(get_current_count).fetchall()[0][0]
increment_count = f"INSERT INTO users (count) VALUES ({current_count + 1})"
cursor.execute(increment_count)
connection.commit()
But that just adds a new column, with the users id + 1 and the count + 1.
f. ex.
before:
user_id | count
1484932 | 0
after:
user_id | count
1484933 | 1
1484932 | 0
which makes sense, because I didn't define, which users count it should increment. But afaik you cant pass WHERE user_id = user.id into an INSERT INTO command.
How do I solve that?
You want an UPDATE statement to update the data for a row, not an INSERT.
INSERT makes new rows, UPDATE modifies existing rows.
Also, you shouldn't be using f-strings to generate SQL queries. That leaves you with a very common bug called a SQL injection vulnerability. Instead, you should be using parameterized queries.
do you have to documentation of sqlite3
!d sqlite3
Source code: Lib/sqlite3/
SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. It’s also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle.
The sqlite3 module was written by Gerhard Häring. It provides a SQL interface compliant with the DB-API 2.0 specification described by PEP 249.
To use the module, you must first create a Connection object that represents the database. Here the data will be stored in the example.db file:... read more
ty
In sqlite 3
can you use f strings?
like
conn.execute(f"something WHERE name = '{str(input)}'")```
will that work?
You really, really shouldn't.
ok
ok
@client.command()
async def gamble(ctx,amount:int):
await open_account(ctx.author)
user = ctx.author
c.execute("SELECT Wallet FROM users WHERE userid = ?", (user.id,))
Wallet = c.fetchone()
if Wallet[0] <= amount:
value = random.choice([True, False])
await asyncio.sleep(2)
if value == True:
await profit(ctx.author,amount)
await ctx.send(f"Wow! You won the gamble of {amount}$")
elif value == False:
await ctx.send(f"Lol, You suck you lost the Gamble of {amount}$")
amount = amount * -1
await profit(ctx.author,amount)
else:
await ctx.send("You Don't Have that much Money.")``` This is my code
Traceback (most recent call last):
File "C:\Users\arun\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\arun\OneDrive\Documents\DOCUMENTS\DISCORD\DISCORD\econamy\economy.py", line 82, in gamble
await profit(ctx.author,amount)
File "C:\Users\arun\OneDrive\Documents\DOCUMENTS\DISCORD\DISCORD\econamy\economy.py", line 68, in profit
c.execute('UPDATE users SET Wallet = Wallet + ? WHERE userid = ?', (earnings, user.id))
OverflowError: Python int too large to convert to SQLite INTEGER
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\arun\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\arun\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\arun\AppData\Local\Programs\Python\Python38\lib\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: OverflowError: Python int too large to convert to SQLite INTEGER```
this is my error
what should i do?
@craggy jewel Your error is in a different function of the code than the one you sent. And error is because you are trying to store a number larger than the allowed max value. Sqlite can only store 64 bit signed integers.
You can convert it to a string, and store it as text in the database.
ok
actually
i fixed my error
but ty
How can I write this dataframe in csv file
U can use pandas?
using pandas already
whats worng with it
use anything u know
how to do it using pandas ?
U want to convert rows[['Nikhil,'CEO'....]....] into CSV through pandas?
yes
I’m using GitHub and heroku, and motor async for mongodb, and it seems to not work
https://www.geeksforgeeks.org/convert-a-numpy-array-into-a-csv-file/
I think this link help u with Pandas
thanks @woeful quarry
i checked https://www.geeksforgeeks.org/saving-a-pandas-dataframe-as-a-csv/
it helped, that was for numpy arrays
hello, what is the syntax if i need to access the second value in a dictionary
@surreal oyster what do you mean by second value?
The second value given by user input
@surreal oyster Dicts are a key-value pair so you can access the value using the key
But if you want to subscript it and access by index then you can use list(dict.values())[index]
@commands.command(case_insensitive=True)
async def kingdoms(self, ctx, *, empire:str):
"""Gives info about an empire"""
async with self.bot.db.acquire() as conn:
result = await conn.fetchval('SELECT Kingdoms FROM CK3_empires WHERE Empires = $1', empire.title())
await ctx.send(result)```
What's wrong with my sql query?
hey anyone how can i execute an postgres sql file with python psycopg2
guys anyone
plsss
does anyone have an idea on how to make python run faster when reading and writing large number of json files? In my case, about 18000?
https://www.w3schools.com/python/python_mysql_select.asp @rancid badger postgres and mysql whatever, is the same
hey
@regal moss
can u help me with postgres
plss
def CreateTables(cursor):
con = connect()[0]
cursor.execute("""
CREATE TABLE if not exists customers(
customerid INT NOT NULL AUTO_INCREMENT,
customername VARCHAR(225),
phoneno VARCHAR(30),
email VARCHAR(225),
state VARCHAR(2),
PRIMARY KEY (customer_id));""")
con.commit()
con.close()```
and where is error
error py Traceback (most recent call last): File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 62, in <module> Setup() File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 53, in Setup CreateTables(cur) File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 46, in CreateTables PRIMARY KEY (customer_id));""") psycopg2.errors.SyntaxError: syntax error at or near "AUTO_INCREMENT" LINE 3: customerid INT NOT NULL AUTO_INCREMENT, ^
here!
smth wrong with syntax only
remove "if not exists"
still same error!
name of the things right
inside quotes or backtics
single quotes
works?
one sec!
Traceback (most recent call last):
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 62, in <module>
Setup()
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 53, in Setup
CreateTables(cur)
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 46, in CreateTables
PRIMARY KEY (customerid));""")
psycopg2.errors.SyntaxError: syntax error at or near "'customerid'"
LINE 3: 'customerid' INT AUTO_INCREMENT,
^
Process finished with exit code 1
ok remove quotes from fields now, and add quotes only to 'customers'
ok!
ok instead?
remove priamry key: it shoudl be not null AUTO INCRMENT
primary key will be oin bottom
ITS IN THE BOTTOM
def CreateTables(cursor):
con = connect()[0]
cursor.execute("""
CREATE TABLE IF NOT EXISTS 'customers'(
customerid INT NOT NULL AUTO_INCR,
customername VARCHAR(225),
phoneno VARCHAR(30),
email VARCHAR(225),
state VARCHAR(2),
PRIMARY KEY (customerid));""")
con.commit()
con.close()```
("""
CREATE TABLE if not exists 'customers'(
customerid INT NOT NULL AUTO_INCREMENT,
customername VARCHAR(225),
phoneno VARCHAR(30),
email VARCHAR(225),
state VARCHAR(2),
PRIMARY KEY (customer_id));""")
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 62, in <module>
Setup()
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 53, in Setup
CreateTables(cur)
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 46, in CreateTables
PRIMARY KEY (customerid));""")
psycopg2.errors.SyntaxError: syntax error at or near "'customers'"
LINE 2: CREATE TABLE IF NOT EXISTS 'customers'(
^
Process finished with exit code 1
gives me this error!
no, AUTO_INCERMENT i wrote shortuct
lemme try!
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 62, in <module>
Setup()
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 53, in Setup
CreateTables(cur)
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 46, in CreateTables
PRIMARY KEY (customer_id));""")
psycopg2.errors.SyntaxError: syntax error at or near "'customers'"
LINE 2: CREATE TABLE if not exists 'customers'(
^
Process finished with exit code 1
k remove quotes from customers again, I had similar issue long time ago and it was smth about quotes, the similar error
ok lemme try
Traceback (most recent call last):
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 62, in <module>
Setup()
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 53, in Setup
CreateTables(cur)
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 46, in CreateTables
PRIMARY KEY (customer_id));""")
psycopg2.errors.SyntaxError: syntax error at or near "AUTO_INCREMENT"
LINE 3: customerid INT NOT NULL AUTO_INCREMENT,
^
Process finished with exit code 1
after removing quotes!
ughh im behind this thing for more than 3 hours!
("""
CREATE TABLE if not exists customers(
customerid INT NOT NULL AUTO_INCREMENT,
customername VARCHAR(225),
phoneno VARCHAR(30),
email VARCHAR(225),
state VARCHAR(2),
PRIMARY KEY (customer_id));
""")
run and show error again
ok
same errorr!!
ok!!
You need to use the serial type or generated as identity column for auto incrment
love u sooooo much thanks thanks thanks thanks a thousand times!! uff!! thank u
can u just explain that serial and begserial thing
Although serial still works generated as identity is the newer way of doing it, and it's more SQL compliant.
@torn sphinx
dude
ya
def CreateTables(cursor):
con = connect()[0]
cursor.execute("""
DROP TABLE IF EXISTS customers;
CREATE TABLE customers(
customerid BIGSERIAL PRIMARY KEY,
customername VARCHAR(225),
phoneno VARCHAR(30),
email VARCHAR(225),
state VARCHAR(2)
);
""")```
ran this!
no errors
but table is not created!
ohh right!
😅
beginner things!!
sry
bro still no!
no its the first one!
there is not that!
def CreateTables(cursor):
con = connect()[0]
cursor.execute("""
DROP TABLE IF EXISTS customers;
CREATE TABLE customers(
customerid BIGSERIAL PRIMARY KEY,
customername VARCHAR(225),
phoneno VARCHAR(30),
email VARCHAR(225),
state VARCHAR(2)
);
""")
con.commit()
con.close()```
ohh sorry gud ni8
ok i will
ohh ok!
@torn sphinx
the table is there
dude ,what sql are you useing ?
cant you just simply delete the database ?
yes bro
thank u!
@torn sphinx I HAVE A 13 LONG DIGIT WITHOUT DECIMALS I WANT TO MAKE IT THE PRIMARY KEY HOW CAN I DECLARE THAT INSIDE A TABLE?
BIGINT RIGHT
SO BIGSERIAL FOR STRINGS?
BARCODE NO!
LIKE 13 DIGITS
YEAH!
NO ISSUES
I WAS JUST ASKING HOW TO DECLARE IT!
AND
THANKS FOR UR HELP!
sry
caps was on
forgot to switch after typing sequel @torn sphinx
@torn sphinx sry to disturb u again !
def CreateTables(cursor):
con = connect()[0]
cursor.executemany("""
DROP TABLE IF EXISTS customers;
CREATE TABLE customers(
customer_id BIGSERIAL PRIMARY KEY,
customer_name VARCHAR(225),
phone_no VARCHAR(30),
email VARCHAR(225),
state VARCHAR(2)
);
DROP TABLE IF EXISTS
CREATE TABLE orders(
order_id INT,
customer_id VARCHAR(225),
unique_id BIGINT PRIMARY KEY
);
""")
con.commit()
con.close()```
ok!
tnxx
ohh i mean i want it on my other table too
ohh yah!!
sorry bro calling u unnecessarily!
def CreateTables(cursor):
con = connect()[0]
cursor.executemany("""
CREATE TABLE customers(
customer_id BIGSERIAL PRIMARY KEY,
customer_name VARCHAR(225),
phone_no VARCHAR(30),
email VARCHAR(225),
state VARCHAR(2)
);
CREATE TABLE orders(
order_id INT,
customer_id BIGSERIAL PRIMARY KEY,
unique_id BIGINT PRIMARY KEY
);
""")```
error
```Traceback (most recent call last):
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 74, in <module>
Setup()
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 65, in Setup
CreateTables(cur)
File "E:/Varsh_Code/Python/CONNOR_PROJECT/SHIPMENT TRACKER/setup_db.py", line 54, in CreateTables
""")
TypeError: function missing required argument 'vars_list' (pos 2)
Process finished with exit code 1
@torn sphinx
uhmm! ok
where, i havent?
its id and name
can u just paste that part in here!
u mean 2 primary key?
i removed it!
ok?
yeah fixed it!
..
can u just tell in which table?
first table or second one
coz both has each!
yah! dude dont get frustrated im really sorry!! i dont have another help im sorry im a beginner!
yah sry!
i will call anyone else
im sorry!
its in 2 diff tables right?
is that a problem!
im just asking
@torn sphinx
@rancid badger It's certainly possible to have multiple primary keys per table/across different tables, but what I think Rei is saying is that your orders table should not have the customer_id as its primary key. Instead what you want to do is make a relationship between the two tables, and to do this make a customer_id column in the orders table which acts as a foreign key.
Should i ask about json data processing here or in #data-science-and-ml
?
how can i assign a variable's value to an object in a dict, then read it
noting i have 2 objects only
channel1 and channel2
ping me pls i m away
Do u know how to do that
Can u just!! Show me how
What exactly is it you want? The relationship?
Yeah how to make that relationship @proven arrow
Customers table,
CREATE TABLE customers(
customer_id BIGSERIAL PRIMARY KEY,
customer_name VARCHAR(225),
phone_no VARCHAR(30),
email VARCHAR(225),
state VARCHAR(2)
);
Orders table,
CREATE TABLE orders(
order_id BIGSERIAL primary key,
customer_id BIGSERIAL,
-- other columns
constraint fk_orders_customers foreign key(customer_id) references customers(customer_id)
);
pliz
Well since you read the dict you can assign the object to it at that point, when you retrieve it.
It ok 😜, butl you would have to read it and write it back to the dict. You may want to ask in a help channel or general channel as its more suited for there.
Anyways home time for me
How would I get the first, say 5 most recent entries from a database? SQL, Python, either is fine
yeah ill just ask another day
thanks instead if i had 2 tables table_a and table_b that fk think wud have been fk_tableb_table_a isint it?
@torn sphinx Hello! You can use limit and offset to fetch your first 5 records from the database
Is it neceassary to learn about Apache Kafka rather than Spark or is both intertwined
I don't think there is any particular convention to follow, however I normally do it like so fk_ChildTable_ParentTable. But it's not always necessary to give a name. If you were to leave it out the database would automatically generate one for you.
ohh ok !!!
It depends what your storing. You can do it a single column as an array or you can normalise it.
and one more thing this pgadmin is shit!! im now sticking with shell psql
Yeah it's not the best experience our there with that ahah
I was told that by normalising it, id need to use a many to many relationship?
Yes
Yeah, well it's not as complicated as it seems
Well what are you storing?
Does it need querying through the dB?
not sure i understand your q
there are reports generated
off of this info
so the data would be queried at some point
Yes but do you need to inspect those individual values of checkbox seperately from the database or is that all done application side?
there's nothing in place jsut yet
i was hoping to limit to just one response and have the response stored under one column
because it's categorical data
the rest of the questions are boolean
I was previously using a select field
Well I have no idea the importance of your data or how it will exactly be used. But you need to ask yourself, do you need to evaluate those checkbox choices from the customers? For ex, if you wanted to know all the users who have selected a particular option.
Ok so then you can split the data up into multiple tables.
Anyways you may want to take a look online on some survey schemas. My assumptions on how a survery is done or looks like, compared to the one you have for your business needs may be different.
thank you
honestly should just be using googles api for this
instead of handrolling everything
using tortoise-orm right now
trying to create a setter for a field, but PyCharm complains about there being no attribute reference
any tips?
what database do you recommend
Is there someone can check if I have done this exercise correctly? It's about relational algebra expressions
can someone help explain something to me in sqlalchemy
on how to creating threaded comments on a flask application
i need to create a column that takes both a local table id and a foreign key
to return a value of either a id or null
Can I make as many requests to a database as I want when my Code is asynchronous?
Yeah why not. It doesn't make a difference to the db if it's async or not. 10 requests is the same as 100000 requests to a db.
and it wont crash?
Database wont crash no.
[2020-12-24 11:57:54] ERROR - Unknown error in submit_modular!
(<class 'MySQLdb._exceptions.ProgrammingError'>, ProgrammingError(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rank FROM scores STRAIGHT_JOIN users ON scores.userid = users.id STRAIGHT_JOIN u' at line 1"), <traceback object at 0x7f564c40f3c8>)
Traceback (most recent call last):
File "handlers/submitModularHandler.pyx", line 537, in handlers.submitModularHandler.handler.asyncPost
newScoreboard.setPersonalBestRank()
File "objects/scoreboard.pyx", line 233, in objects.scoreboard.scoreboard.setPersonalBestRank
result = glob.db.fetch(query, {"md5": self.beatmap.fileMD5, "userid": self.userID, "mode": self.gameMode, "mods": self.mods})
File "common/db/dbConnector.pyx", line 217, in common.db.dbConnector.db.fetch
cursor.execute(query, params)
File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 206, in execute
res = self._query(query)
File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 312, in _query
db.query(q)
File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 224, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rank FROM scores STRAIGHT_JOIN users ON scores.userid = users.id STRAIGHT_JOIN u' at line 1")
need help
i was modifed this, and change the straight to join only, but still got error
can i ask database related question here?
Sure u can
which one would be the proper way in these examples?
transactions [id, type, staffid, date....]
staffs = joe, jack...
types = [sell, trade, buy]
or
staffs[joe, jack.....]
----
sells[id, staffid]
trades[id, staffid]
buys[id, staffid]
-----
I like mysql
The first one
Query looks fine. You may want to try the query directly against you database from a cli or something and see the error then.
seems like something wrong on the mysql
mysql> SELECT COUNT(*) as rank FROM scores;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rank FROM scores' at line 1
mysql> SELECT COUNT(*) as ranks FROM scores;
+-------+
| ranks |
+-------+
| 0 |
+-------+
1 row in set (0.00 sec)
mysql>
just add s can count
whats wrong with that?
thanks
rank was probably a reserved SQL keyword then
edit - yes it is, https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-R
Right correct, mysql 8 has it as a keyword. They use it for window functions.
wow , i was using that in oct it's working properly, when is 8.0 launch?
I would thought they would ignore it just like mariadb when not used as a function.
long time ago
In older versions (5.7) it'll work fine, as its not a keyword there.
Hello, i must need to find an efficient way of doing searching products from my database. I currently do LIKE but someone said that is not fast. So what should i do? Can i make it indexed column? Will this fix the issue?
i use mysql
Indexing wont necessarily speed it up. Its because of how mysql indexes the words.
It starts from the left so search_term% will work with an index however %search_term% wont
But it also depends how many items you are searching through, and if your wondering what is used generally by applications, then it is Full Text Search. MySQL supports this, or you can use external services like algolia/elastic search/MeiliSearch and many others out there.
ok sir, thanks for clarifying. I currently dont have many product, but in the future will. So i will look into what you said.
Right now I have a SQLite3 Database I am using to store dates and events. The format of the Dates are mm/dd/yyyy. How would I sort this from most recent upcoming to more further upcoming? I want to be using LIMIT, but I'll add that in later. I also want it to not show any dates from before the current date. So far, October, November, and December (with 1s in the month) get put with January. How would I do this? Thanks in Advance.
Well ideally to sort it by dates you would want to use ORDER BY however in your current date format that would not be possible because your dates are in mm/dd/yyyy format. This prevents sqlite from ordering by dates because it expects date to be in the format of YYYY-MM-DD. Its explained here under point 2: https://www.sqlite.org/lang_datefunc.html
So you can either convert your data to match the required format, otherwise you can use the substring function to manually parse it into the correct format.
I have it as mm/dd/yyyy hh:mm:ss right now, and It seems to work better, but I haven't had time to go in depth with multiple tests. I also tried YYYY-MM-DD, but I had the same problem
Assume your date column is called event_date then to parse it to the correct format you could do the following:
SELECT *,
substr(event_date, 7,4) || '-' || substr(event_date, 1,2) || '-' || substr(event_date, 4,2) as new_date
FROM your_table ORDER BY new_date DESC;
Thank you
That wont give you exactly what you wanted in your original question because i didnt fully understand what you meant but it shows how to convert to a date type, which will allow you to do things like ordering, or comparing against dates.
A friend suggested using the Date module to get the mm/dd/yyyy format. Basically, I want to get all the events that happen on the current day and happen after the current date. I want to get them in probably ASC order, to find the closest events
Right i see, makes sense now
So you can do,
SELECT *,
substr(event_date, 7,4) || '-' || substr(event_date, 1, 2)|| '-' || substr(event_date, 4, 2) AS new_date
FROM your_table
WHERE new_date >= date('now') ORDER BY new_date ASC;
Although above still works, i would recommend to change the way you store the dates, in your table as that would be much easier/better overall.
- What format should I make them into?
- If I change to that format, how would I go from the new format you suggest ^^ to
mm/dd/yyyy - I was going to use this column to add times for the events too, but I think I should make a separate column just for times
- See my first message which contains a link and shows you the different storage formats.
- If you want to convert to your desired format then you can use the
strftime()function. Again this is shown in the link i sent. - Not sure what you mean 🤷♂️
Ok, thank you
bro what should i prefer to learn btweeen these two,mongoDB and Mysql?
anyone here
i am given a project to do something with connectivity i cant use tkinter and others i have to use normal pythonn windows only
and i want to do something intresting any ideas
@south bear so can i give you a video that teachets you aiosqllite?
ok
In this Python SQLite tutorial, we will be going over a complete introduction to the sqlite3 built-in module within Python. SQLite allows us to quickly get up and running with databases, without spinning up larger databases like MySQL or Postgres. We will be creating a database, creating a table, insert, select, update, and delete data. Let's ge...
this video will teach about how to use sqllite 3
its very simple to use
👍
and very robust
well you can
should i?
but i think for storing data stuff and configuring it
it should be done on a different file
will be easier to handle
@tame nova can SQLite still be integrated with a system where the user can see their inventory?
ofc
ok
you can create an sql table for a specific user
is that in the tutorial series?
and that user can access their stats and data
well that teaches you how to use sqlite 3 for your applications
bc i've got this so far
import sqlite3
conn = sqlite3.connect('gamestorage.db')
c = conn.cursor()
c.execute("""CREATE TABLE inventory (
first
)""")```
ah
it's on the dataStore.py file
the script has made a file called gamestorage.db
so uh
what now
@tame nova (sorry for all the pings) ^
if that's for applications...
lol
whats JDBC version for python
hey dudes im forced to use shelves as my database for flask
do you guys know any ways or methods to query shelve data?
im currently using .startswith() and a bunch of for, ifelse
I wanna try out a new db, any recommendations?
Which of these is quickest to set up: Cassandra, Raven or Cockroach?
never heard of raven or cockroach, cassandra is pretty good though for nosql stuff
Guys, can somebody help me with MongoDB?
What are clusters, databases and its collections?
What’s the difference between them?
And how should I name my cluster?
I know there is no difference in names
But should I name as my project? Or as my nickname?
Help :<
brother its been 2 hours without any respond, why not go watch a youtube tutorial or read some documentation
{"785147745982742541": {"balance": 2443, "cotton": 0, "stone": 0, "wood": 0, "plastic": 0, "copper": 0, "steel": 0, "glass": 0, "concrete": 0, "paper": 0, "alluminium": 0, "iron": 0, "brick": 0, "oil": 0, "bronze": 0, "silver": 0, "gold": 0, "ruby": 0, "emerald": 0, "diamond": 0, "invtaken": 0, "invavailable": 10, "iulevel": 2, "pulevel": 0}
how can i get the like
for user.id in json top_bal = somthing
like find the top balance of the ids in the json
code:
async with aiosqlite.connect("Economy") as db:
c = await db.execute("SELECT * FROM items WHERE 'number' = 1")
data = await c.fetchone()
name1 = data[0]
description1 = data[1]
cost1 = data[2]
await db.commit()
error:
Traceback (most recent call last):
File "C:\Python\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "first-bot-test.py", line 358, in store
name1 = data[0]
TypeError: 'NoneType' object is not subscriptable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Python\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Python\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Python\lib\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: TypeError: 'NoneType' object is not subscriptable
pls help
im using SQL
my table
?
@wet sierra Your error is because the fetchone method returned nothing from the dB and your trying to index nothing.
Remove the single quotes around the column name from your query and try again.
guys how do i bulk add to flask sqlalchemy
printing your query output is a good way to make sure you're getting the results you want
Does anybody have recommendations on the best way to design a weekly tracking of user data, as in, additional to the cumulative lifetime user data? I want weekly statistics to be viewable separately from overall, but overall to continue updating. The first thought that comes to mind would perhaps be multiple tables, with the overall and weekly tables being updated constantly, but weekly tables wiped each week?
the only problem I can see with this would be that it stores twice the data
Interesting question. I think it depends on how you need to use the data. For example, you could store the weekly snapshots permanently and then aggregate them to get the lifetime data for the user.
The aggregation would be done on demand with a query
I'm not sure what the performance of such query would be like versus being able to access the pre-calculated data directly.
Whether performance would even be a factor depends on your use-case - where and how often such data will be needed, how much data there is, etc.
If you store data twice, then there are concerns of integrity between the two tables, database size, and the potential performance impact of having to do an additional operation (since you'd have an additional table) when adding new data.
Thanks for the response, Im really not sure. I was thinking having the data pre-calculated and saved in the same structure would be the easier option since the data entry queries could pretty much be the same, and then for weekly stats only need to query the weekly table(s)
Im unfamiliar with database snapshots however
I wasn't referring to anything technical with "snapshots"
I just meant the weekly rows that you store for a user could be aggregated
👍
Hm, so use fetchall()
?
No, fetchone is fine. The single quotes were being misinterpreted as a string literal, instead of a column name
Unlike in Python, in SQL single and double quotes mean different things.
Double quotes are used for quoting the name of a column or table, single quotes are used for string literals.
Yep, and sqlite also allows backticks and square brackets alongside the double quotes.
hmm
ok
lemme put them in double quotes
actually i cant enclose it with double quotes
cuz the querie is enclosed in double quotes
Well as I said in my first message you don't need the quotes. Remove them around the column name.
ye ok
They are to be used around column names when it contains a reserved keyword or characters like whitespace.
ahhhh okkk
I need help :(
I get this error from MongoDB.
Command raised an exception: ServerSelectionTimeoutError: connection closed,connection closed,connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 5fe72e6c9f366684e21e2454, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('numix-shard-00-00.dksdu.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('numix-shard-00-01.dksdu.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('numix-shard-00-02.dksdu.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>]>```
Someone please help!
It gives a timeout error, meaning the connection to the server was lost
does this error appear right as you start or does it appear after some time?
@torn sphinx
after sometime.
so that means that after some time the connection to mongo is closed
it looses connection
How do I fix it?
figure out why it looses connection
this is my error:
Traceback (most recent call last):
File "C:\Python\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "first-bot-test.py", line 547, in steal
lobbb = await c.fetchone()
File "C:\Python\lib\site-packages\aiosqlite\cursor.py", line 54, in fetchone
return await self._execute(self._cursor.fetchone)
File "C:\Python\lib\site-packages\aiosqlite\cursor.py", line 31, in _execute
return await self._conn._execute(fn, *args, **kwargs)
File "C:\Python\lib\site-packages\aiosqlite\core.py", line 122, in _execute
return await future
File "C:\Python\lib\site-packages\aiosqlite\core.py", line 98, in run
result = function()
sqlite3.ProgrammingError: Cannot operate on a closed database.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Python\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Python\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Python\lib\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: ProgrammingError: Cannot operate on a closed database.
this is my code:
https://mystb.in/CameronTasteApproaches.python
im using SQL btw
helpppp
i cant find whats not operating under a closed database
im hoping someone can help me
im having issues with flask migrate
getting a
class Phone_Survey(db.Model):
__tablename__ = "phone_survey"
__bind_key__ = "user_data"
survey_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
interviewer_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
first_name = db.Column(db.String(20))
last_name = db.Column(db.String(20))```
class User(db.Model, UserMixin):
__tablename__ = 'users'
__bind_key__ = 'user_data'
id = db.Column(
db.Integer, primary_key=True, autoincrement=True, unique=True)
employee_id = db.Column(db.String(11), unique=True)
first_name = db.Column(db.String(1000))
last_name = db.Column(db.String(1000))```
i have no idea what the issue is
from what i googled it could be a variety of things, but i may have narrowed it down to a indexing issue
but idk
can anyone tell me about an issue iam facing. Iam trying to connect to oracle database using cx_oracle. it requires a availability of instantclient and but after converting my .py file to exe while running the program i get a error as "unable to find instantclient "Access is denied"". although i put the instant client on the path.
I'm a bit confused rn, I tell my program to insert an id (primary key) into a table.
The id I enter is "634838075272331294" but the database registers "2147483647" and I have no clue why
Because your column data type will be an INT, which only allows a 32 bit signed integer, and that is the value you are seeing.
In this case a BIGINT should be able to store your value.
Ah, okay. Thanks
A few days ago you told me to not use f-string. Should I use %s instead or is there a more efficient way?
Yeah that is the way you should be doing it. Use %s,
So like, cursor.execute("SELECT * FROM table WHERE column = %s OR column_two = %s", (value1, value2))
You shouldn't use any type of Python string formatting - not f-strings nor .format() nor % printf-style formatting. Instead, you should include placeholders in your SQL query, and provide the parameters to use in place of those placeholders as the second argument to execute
if the DB API you're using supports %s placeholders then it's OK to do py cursor.execute("SELECT * FROM table WHERE column = %s OR column_two = %s", (value1, value2)) but not to do py cursor.execute("SELECT * FROM table WHERE column = %s OR column_two = %s" % (value1, value2))
But if I do this:
product_id = 38748597002983498234598203
sql = "INSERT INTO users (user_id) VALUES (%s)"
cursor.execute(sql, product_id)
db.commit()
I get a ProgrammingError [...] MariaDB server version for the right syntax to use near '%s)' at line 1, so I guess I did something wrong again
it needs a tuple of parameter values, not just one
you need cursor.execute(sql, (product_id,))
Ah, okay
also... not all databases support %s for the placeholder. I'm not sure what MariaDB wants, so if it's still an error even when you give a tuple of parameters, try using ? instead of %s
Using tuples fixed it. Thanks
if using ? instead of %s works - and it probably does - you should prefer it; it looks less like Python string formatting and won't be as confusing when you revisit it later.
Hey, I need some help figuring out how I should store this
I have a bunch of "missions" that users have to complete - how should I store which missions users have completed in a database? Here's the way that I thought of to do so (there's about 300k users, probably 100 missions):
-Storing it in the "User" table using "mission" as a column (int list), so I can store which missions they've completed by assigning an ID per mission.
However, from what I understand (correct me if I'm wrong), the user rows get updated very often. If the list is large, it'll take a long time to update/retrieve the table the longer the mission lists get.
Is this the proper way to do it or is there something better? I feel like I'm thinking about this wrong
help? :/
it seems to me that a much more reasonable way to represent that would be a completed_missions table where the key is mission ID + user ID. When a user completes a mission, you just insert into completed_missions(mission_id, user_id) values (?, ?)
all 3 of your lobbb = await c.fetchone() should be lobbb = await lolll.fetchone(). c is an old cursor from an already-closed database.
@radiant elbow gotcha - so there wouldn't really be a pkey then right? I guess I would put an index in user_id column so I can fetch a list of missions the user has completed quickly
right - not having a primary key isn't a problem. If you really want to have one you can just use an autoincrement unique ID or something.
thank you so much for your help!
I have this table,
+--------+--------------+
| user_id | used_cmd |
+--------+--------------+
| 1 | 1,2,3 |
| 2 | 1,2 |
| 3 | 4 |
| 4 | 2,3 |
| 5 | 1,2 |
+--------+--------------+```
Each number in used_cmd is correspond to a command.
How can i see what other commands users of command with id of X also used?
For example, all the users who used 1, also used 2 and 3, And who used 4 didnt use anything else.
that would be much easier if your table were instead:
user_id used_cmd
1 1
1 2
1 3
2 1
2 2
3 4
4 2
4 3
5 1
5 2
Yeah but table is not like that
If it was up to me I'd drop the whole database and go be a bus driver
can you make a new table that is like that?
Probably not 😩
Do you think it's possible? Or shall I find an excuse to tell the manager 🥺
Maybe my laptop charger broke or something if you know better 🤫 🤣
well - putting multiple values in a single row/column makes it hard to do the sort of relational algebra stuff that you're trying to do. It's definitely possible to do it still, it's just a lot more difficult.
Well I think it's because the table was very big and so index was taking a lot of space, that's what I heard anyways.
@radiant elbow oh so I just realized the table is actually like you said lmao
It's just that I was looking at a different view which was joining all values together
ok - if it's laid out like that, then you can just
select a.user_id, a.used_cmd
from the_table as a
inner join (select user_id from the_table where used_cmd=4) as b
on a.user_id = b.user_id
That would find a list of all commands used by anyone who has used command 4, for instance.
@radiant elbow thanks a lot that should get me through this, is not exactly as I need but is something I can work with.
What you have there a non-normalized table
And those are really bad
Whoever designed it didn't design it well
Hey i have a database which stores the player cash. is there a way to add everyones cash up together with MySQL Syntax?
select sum(cash) from the_table
@radiant elbow Got it thanks appreciate it 🙂 Is there a way to UPDATE ALL?
cant seem to find anything about updating all on google. only updating individual fields
what would you want "update all" to do?
My MongoDB keeps timing out, can someone help?
@radiant elbow Sorry for the late reply. update everyones cash on the DB 🙂
to the same value? By the same amount? Something else entirely?
by a custom value. so i can run a command like !giveallcash 100 say
update the_table set cash = cash + 100
oh ofcourse. thanks dude. okay now ive got a really awkward question 😄
Okay so i have a weird question here. i have a bounty system in game. say someone has a bounty on their head for $130 i need to spawn them that cash. To spawn cash in game it comes in $10, $50 or $100 is there a formula or a way to work out how to spawn them that cash with those 3 values? I literally have no idea how to do this one.
you mean how many hundreds, how many fifties, and how many tens to give them?
To spawn the cash i write it to a DB and then a second bot checks the DB and then spawns it. which is logged in on a second PC
@radiant elbow basically yeah
brb coldy 5 mins
!e ```py
amount = 150
hundreds = 0
fifties = 0
tens = 0
while amount >= 100:
hundreds += 1
amount -= 100
while amount >= 50:
fifties += 1
amount -= 50
while amount >= 10:
tens += 1
amount -= 10
print(hundreds, fifties, tens)
@radiant elbow :white_check_mark: Your eval job has completed with return code 0.
1 1 0
@radiant elbow dude your the bomb. i owe you a beer 😄 id of never figured that. brb 2 mins
there's more succinct ways to do it, but that's probably the most readable.
!e One of the more succinct ways would be:
amount = 370
hundreds, amount = divmod(amount, 100)
fifties, amount = divmod(amount, 50)
tens = amount // 10
print(f"{hundreds=} {fifties=} {tens=}")
@radiant elbow :white_check_mark: Your eval job has completed with return code 0.
hundreds=3 fifties=1 tens=2
@radiant elbow awesome dude ill have a play around. appreciate it 🙂
divmod is a builtin function where divmod(x, y) returns (x // y, x % y)
the number of times y evenly divides x, and the amount left over.
so the spawn code is say #spawnitem BP_Cash_10 i need to write this into a DB which equals the amount. id put that into an if statement and loop through and then insert into the DB?
if the denominations matter, then the DB needs to store the number of hundreds, fifties, and tens that each player has, and you'd figure out the amount of each that you're adding, and then you'd do update the_table set hundreds = hundreds + ?, fifties = fifties + ?, tens = tens + ? and pass (hundreds, fifties, tens) as parameters to the statement.
Here is the DB structure so under "ScumCode" it would write
BP_Cash_100
BP_Cash_100
BP_Cash_50
BP_Cash_10
BP_Cash_10
if the amount is 270
I don't think I understand this structure at all - there's one row in the database per bill that the user has? So if they've got 270 in cash that's 5 separate rows in the database?
@radiant elbow yeah it writes it all in the same row then my other code will just split the string and loop through and spawn the items
because some items have like 10 codes to spawn their guns and packages etc
How do I get the reason which is Test using the underlined user ID?
@radiant elbow Does this look right? py hundredspawn = "BPC_Cash_100" fiftyspawn = "BPC_Cash_50" tenspawns = "BPC_Cash_10" result = "" for hundreds in divmod(amount, 100): result += hundredspawn + os.linesep for fiftys in divmod(amount, 50): result += fiftyspawn + os.linesep for tens in divmod(amount, 10): result += tenspawns + os.linesep
or have i made hard work out of an easy situation? 😄
Code: ```py
@commands.command()
async def warn(self, ctx, user: discord.Member=None, *, reason=None):
cluster = MongoClient('MongoDB URL')
collection = cluster.Moderation.warns
if reason is None:
await ctx.send(":no_entry_sign: You have to provide a reason.")
else:
try:
embed = discord.Embed(timestamp=ctx.message.created_at, title=f"[Warned] {ctx.guild.name}", description=f"**Type:** Warn\n**Reason:**{reason}", color=0xFC6700)
embed.set_footer(text="Numix", icon_url=self.config.logo)
await user.send(embed=embed)
warn = {"_id":user.id, {f"{ctx.guild.id}":{f"{ctx.message.id}":reason}}}
collection.insert_one(warn)
await ctx.send(f"{self.s} {user.name}#{user.discriminator} warned *User was notified*")
except discord.Forbidden:
warn = {"_id":user.id, {f"{ctx.guild.id}":{f"{ctx.message.id}":reason}}}
collection.insert_one(warn)
await ctx.send(f"{self.s} {user.name}#{user.discriminator} warned *User was not notified*")```
Error:py Command raised an exception: DuplicateKeyError: E11000 duplicate key error collection: Moderation.warns index: _id_ dup key: { _id: 791553406266245121 }, full error: {'index': 0, 'code': 11000, 'keyPattern': {'_id': 1}, 'keyValue': {'_id': 791553406266245121}, 'errmsg': 'E11000 duplicate key error collection: Moderation.warns index: _id_ dup key: { _id: 791553406266245121 }'}
That's not right, it would be:
hundredspawn = "BPC_Cash_100" + os.linesep
fiftyspawn = "BPC_Cash_50" + os.linesep
tenspawns = "BPC_Cash_10" + os.linesep
result = ""
hundreds, amount = divmod(amount, 100)
result += hundreds * ("BPC_Cash_100" + os.linesep)
fifties, amount = divmod(amount, 50)
result += fifties * ("BPC_Cash_50" + os.linesep)
tens, amount = divmod(amount, 10)
result += tens * ("BPC_Cash_10" + os.linesep)
How do I get the warning reason which is Test?
No it was a confusion. It was normalised but I sent wrong table
I sent a view
Does anyone know the syntax for a many to many query? To get data from both sides?
You can select from your junction/pivot table, and then have a join for each of your associated tables.
k, right yeah
I thought i had to select from the other table, instead of the connection table.
Alright, then i should not worry ahaha because i only ever use with just single joins
def get(cursor, col):
cursor.execute('SELECT %s FROM table ORDER BY Num DESC', (col, ))
return cursor.fetchone()
whats error in this this not execute?
Placeholders for query parameters don't work with column names.
so how to do this?
SQL.execute('select user_id from Accounts where user_id=%s and server_id=%s', (USER_ID, SERVER_ID) )
its work properly but that one not
for col name how to do this?
@proven arrow help please sorry for ping
The %s placeholder can't be used for column names. You can't have dynamic columns like that.
so any other way?
The easiest way would be to just have seperate queries for them. Otherwise you get into a mess of getting column names, and validating this against the users input, and sanitising user input.
then for each col there is one fun for that
can i use ' ' for SQL queries rather than " "?
In python, '' and "" are exactly the same.
In SQL, I guess it depends
what do you need to use the quotes for? strings or aliases
Whichever than
oh ok
double may be better, because than, you can use single quotes for strings, which is more common in queries
so "SELECT * FROM mytable WHERE name='sloth'"
cuz here:
c = await db.execute("SELECT Weekly-pass FROM inventory WHERE user_id = (?)", (ctx.message.author.id,))
i was thinking of adding " " to enclose the weekly-pass part because that - might be a problem right?
wait
Dali prichash srpski?
:)
yay
Quick question since I'm using DBs for the first time. I'm using sqlite3 and I want to store a dictionary/list, should I use TEXT or BLOB as my datatype?
List in a dictionary, or a list of dictionaries?
Either way, you should break it down to single fields.
That's the main thing you should focus on when creating tables.
You can DM me your dict, and I'll send you what your table(s) should look like
Well, basically I'm making a economy/fun discord bot where users have items on their inventories
So I was going to, for example, when they try selling something, check if they had it on the dict, then remove and proceed on the sale and updating the db entry
Hello, i want to create a data base of products, clients, suppliers, etc for my family business, but i dont have any experience in databases, what would you recommendt to me for starting?
a database, a tutorial for starting...
Hi, is anyone familiar with the term persistance layer?
I need to write a python Enigma simulator that includes persistance layer. As far as I know, It's the layer that holds all opperations of exporting/importing reading/saving files. In my example, It would be a python file that holds reading/saving operations for the main program. Is that correct?
That would be correct yes.
Actually reminds me of my first ever program I made. It was also the German Enigma, with some advanced features. Although mine was done in Java.
Wonderful. That's exactly what I'm doing;))
Do you mind, If I send you documentation that I'm going to make, so you could look it up if it's correct?
I'm sure it will take me couple of days, I still need to finish my program first
That's fine if you want. Although my knowledge of how it works, with all the reflectors, rotors and stuff probably needs refreshing as it was done a very long time ago 😅.
I still got the jar file and source code somewhere on my laptop, and I can take a look.
No worries, it will send you a single paged introduction of enigma's anatomy with pictures;))
If you could just briefly look the documentation up, that would be great. Can I add you??
Of course If you have time for that
I found the feature list from my documentation. I remember the pain of this 😅
And yeah sure you can message me I can take a look some time when I'm free.
Yours look little bit more advanced than mine. I will use only 3 non-changeable rotors and 3 changeable reflectors, also I need to implement exporting/importing enigma settings and ciphered text. Also I did a simple GUI;))
Thanks a lot!
I'm running into a problem again. I have a dict, which I iterate through. But whenever I try to do this:
for param in values.keys():
p = param.split(" ")[0].lower()
self.cursor.execute("UPDATE cmds SET %s = %s WHERE id = %s", (p, values[param], cmd_id))
self.db.commit()
I get an error, that says it cannot set '<p>' = <value>. And I believe that the cause of that are the '', that surround the <p>. Is there any way to let the db know, to not see them as a string?
The column name can't be used as a parameter in parameterised queries.
Well that sucks
what the hell do you use instead of "on conflic" since sqlite3 doesnt have it on my version (shouldnt update because its on my vps)
what do you want to do if there's a conflict?
update, but already got it, using COALESCE now, lovely