#databases
1 messages · Page 119 of 1
@ashen lintel My post above still stands. You don't need the quotes around %s
So far it looks like:
SELECT title
FROM tblFilms
WHERE genre = " "
ORDER BY filmID DESC
%s is a placeholder, it implies the appropriate quotations so you don't have to
if i dont put " ' " i get an error
yes
int
CREATE TABLE IF NOT EXISTS vrp_users(
id INTEGER AUTO_INCREMENT,```
%s > %d
?
@pseudo summit
yes
the error its still there
:o
and the error is?
@marble osprey sorry, trying to help str first, then I can try to help you too
@marble osprey Im assuming you already know how to connect to the database and execute queries. Then you can do something like this.
cursor = connection.cursor()
cursor.execute("SELECT title FROM tblFilms WHERE genre= ? ORDER BY filmID DESC", (user_input,))
result = cursor.fetchone()
The ? is a placeholder and makes this a parameterised query. So the ? will be substituted by the parameter user_input.
fetchone() method will get the result from that query.
@marble osprey Im assuming you already know how to connect to the database and execute queries. Then you can do something like this.
cursor = connection.cursor() cursor.execute("SELECT title FROM tblFilms WHERE genre= ? ORDER BY filmID DESC", (user_input,)) result = cursor.fetchone()The
?is a placeholder and makes this a parameterised query. So the?will be substituted by the parameteruser_input.
fetchone()method will get the result from that query.
@proven arrow thanks bro
@ashen lintel I think you still have quotes around your %d?
thanks
bro
look
=))
it works
:))
@ashen lintel I think you still have quotes around your %d?
@pseudo summit thanks to you too :))
That is insecure so you should avoid doing that. @ashen lintel
What you had before was the correct approach.
😅
look at printgenre the rest is irrelevant
Can you fix that to work for me
I tried and it got stuck on cursor.execute not being an attribute of a built in function
Then when i removed that bit it displayed nothing so
You said you wanted to take in user input? Where is the input?
Well you should try paste code here or in a paste service if its too long, otherwise i just have to type it out again
And show the code you tried, that didnt work
I already deleted it
It got stuck on the cursor.execute bit with the bottom three rows and didn't print anything without them
Your error regarding the built in function is because of your third line
You have connection.cursor when instead it should be connection.cursor() since cursor is a function so your missing the brackets. Anyways you should avoid using global cursor like that.
def printgenre(user_input):
conn = sqlite3.connect("MyFilms.db")
cursor = conn.cursor()
sql = "SELECT title FROM tblFilms WHERE genre= ? ORDER BY filmID DESC"
for row in cursor.execute(sql, (user_input,)):
print(row)
Thats kind of how it should be
Sorry
Most of this is me editing examples my school has given me
So i need to put an input named user_input into my function?
When you call the function, it will ask you to provide an parameter for that function which corresponds to the user_input variable. So it would be the value of your Genre.
Something like, printgenre("Horror")
So do I need to add something because right now it's bringing up an error
Missing 1 required positional argument: 'user_input'
How did you use the function?
In a menu?
But how do you call it in your code?
So then why ask for it?
Well I want to pass something into user input that is what I've been tryong to do the whole time
Get it to ask for a genre then print all the films in the genre
Then that should be your question, and what you should have tried to solve first. How is the application being ran? If terminal, then use input() function
Hi so I was using pickledb and was wondering how to use lappend
I'm a complete novice
Alot of what you're saying is not coming across to me at all
I'm running it in python¿
Then you might be better off asking your question in #python-discussion or in a help channel #❓|how-to-get-help since your question on how to take user input is not database related. If you say your a novice, and this seems too much, then i would recommend breaking things down and solve the problem that way, rather than attempting it as a whole. Your first question should be how to take in user input. Once you understand that, then you can proceed forward into your next task, which is using that function. @marble osprey
What is supposed to happen with the code you gave me?
Regarding the user_input bit
It answers your original question, on how user input can be used for the database query.
But right now it doesn't do anything because there is no input
There is nothing being passed into it
I just want to know what to write to make an input that passes a value into there
I already mentioned you can use the input() function to take your input.
https://docs.python.org/3/library/functions.html#input
https://www.w3schools.com/python/ref_func_input.asp
I know how an input works
I just need to know how to get said input to pass it's value to user_input
In printgenre
The example shows pretty clearly, in the w3schools link.
user_input = input("Enter here: ")
printgenre(user_input)
I got it
@client.command()
async def setchannel(ctx, channel:discord.TextChannel):
idforserver = ctx.guild
channelid = channel.id
conn = await aiosqlite.connect('servers.db')
try:
await conn.execute("INSERT INTO main VALUES (?, ?)", (idforserver, channelid))
except:
await ctx.send("Oh, No a error has happend!")
return
im using aiosqlite would this work for storing a channel id and a server id in a db
heres my db layout
async def create_db(client):
await client.wait_until_ready()
conn = await aiosqlite.connect('servers.db')
await conn.cursor()
await conn.execute("""CREATE TABLE main (
serverid integer PRIMARY KEY,
channelid integer
)""")
await conn.commit()
await conn.close()
client.loop.create_task(create_db(client))
nvm i got it
Apparently I have a syntax error at or near user:
INSERT INTO event_log(uuid,function_name,team,user,event_type,event_data,date) VALUES($1,$2,$3,$4,$5,$6,$7)
But I'm not entirely sure what that syntax error is
Can anyone maybe point out what it is?
anyone here?
i want the a variable to be used for comparison but in all 4 statement
including name,german_name,japanese_name,french_name
how to do it
does anyone know where I do the pg_dump command for postgresql?
can u help me
I tried it in cmd but it says pg_dumb is not a valid command

@toxic mist each ? in your query has to have an accompanying argument in the list of parameters. So where you have just a, you'd say a,a,a,a
oh, I think that might be because the 2nd argument (where you have a now) needs to be a tuple. E.g., (a,a,a,a)
ok, it's going to be hard to help you with only screenshots of the code
I understand you're probably using your phone because you don't have a choice, but it's still going to be hard to do anything
ee i did got a pc recently
but making sql table is easier on mobile
coz i have an app for ez editing
so i am making simple programs on it and will run bot on pc
anyway, in general, posting screenshots of code and errors is going to make it hard to help you.
Has anyone tested sqlalchemy v1.4?.0
Has anyone tested sqlalchemy v1.4?.0
@scenic zinc ooh it's OUT?
I heard it supports async
I tried it in cmd but it says pg_dumb is not a valid command
@silk vortex You can find the executable from wherever your postgres installation is. Should be in the bin folder.
I would like there to be a redirect if both records are already in the database
server_ip_addr = get_server_data["address"]["ip"]
server_ip_port = get_server_data["address"]["port"]
cur.execute("SELECT ip_addr,ip_port FROM servers WHERE ip_addr=%s,ip_port=%s", (server_ip_addr,server_ip_port,))
check_ip = cur.fetchall()
if check_ip:
return jsonify({"message": "Serwer z takim adresem ip jest już na liście."}), 400
@silk vortex You can find the executable from wherever your postgres installation is. Should be in the bin folder.
@proven arrow how do I find where I installed my postgresql? I don't remember where I installed it.
Check in program files
@proven arrow Hey, any suggestion for sql ?
I meant, I have never used sql and right now, I am using python here, I meant my entire project is written in python and some bash... so is it okay if I first do sql and then look for python related libraries for sql or sqlite ? or should I go with python library of sql ?
SQL is the query language that allows you to communicate and do operations on the Database. So you would definitely need to be familiar with that first before you pick your python related libraries (although sqlite module comes with python, so no need to look far). Regarding SQL you have two options. Either learn basic SQL, or if you don't want to write raw SQL queries you can use an ORM (you can look them up, a common one people use with Python is SQLAlchemy), which will do the queries for you. However, this might be an overkill for your project where you only might need a few handful queries in which case writing raw queries will be a better choice.
SQL is pretty high level, and very human readable, so its not too difficult to learn as well. You may want to take a look at this website which takes you through it through the browser, https://sqlbolt.com/. Maybe this is a good project for you to learn some basic SQL? So give it a try.
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
thanks again for the help, Although I was using this website -> https://mystery.knightlab.com/walkthrough.html but official ones seems a better choice. The one i mentioned has implemented a mystery game which you've to solve via using sql commands.
Yes, I don't want to overkill or like implementing some features that might use high cpu usage or consume time/space.. I want to store the song links with their names, considering as a playlist of each user.
like you said last night, JSON format is not good to modify things such as select a song that all the user have in their playlist (similar songs). This might take little extra computation in python coz first you've to check the DictObj of user1, then 2, 3 4.... and so on.. then you have to extract the similar {key: value} presented in all users playlist... yeah SQL might gives me a better access and efficient too.
Thanks, I'll start the basics first, and If i stuck anywhere then I'll let you know, is that okay?
Hi everyone, I am trying to insert some raw json data into a psql table. I just want to insert all of the data in one column called json. I am using psycopg2. My code was working a few months ago and successfully inserting the data into the table but coming back to it today, it is not inserting the data into the table. I get no error msg but the data is not there. Am I going about it the right way???
@proven arrow ingest is the name of the schema in which the table is located. I will try without it
Oh ok, well Schema name is fine there then
ok great
maybe I have not connected to the db properly then and have the wrong credentials if the code is looking ok
@proven arrow I can't find the files. I went to the apps and features settings and it shows postgresql but I can't find the postgresql folder on my computer.
nvm
I found it
this is what it looks like
I don't have a bins folder
@scenic zinc ooh it's OUT?
@shell ocean well 1.4.0b1 is out. So far I prefer using it over encode/databases. I like its native support for async.
can i get help for asyncpg integration with dpy?
so this is a basic function that creates a table and adds values to it
async def add_new_value_to_db(name):
db_connection = await asyncpg.connect(host=host,
password=password,
user=user,
port=port,
dsn=dsn,
database=database)
try:
await db_connection.execute("CREATE TABLE IF NOT EXISTS BOTGAMEDB(Name TEXT(30) PRIMARY KEY,POINTS INT(10) NOT NULL,DEFAULT POINTS = 500)")
except:
pass
try:
await db_connection.execute(f'INSERT INTO BOTGAMEDB VALUES("{name}",500)')
await db_connection.commit()
except:
pass
await db_connection.close()
where host, password etc are the values given in heroku site
this does not work though
the bot command that calls the function is :
@bot.command()
async def register(ctx, member: discord.Member):
await ctx.send(member.name)
await add_new_value_to_db(member.name)
ping me :))
@weak tinsel Your create table syntax is wrong
oh?
i know mysql
so i thought it is the same in postgre 😅
what is the correct syntax? @proven arrow
CREATE TABLE IF NOT EXISTS BOTGAMEDB(
Name TEXT PRIMARY KEY,
POINTS INT NOT NULL DEFAULT 500
)```
Your default was incorrect and the text/int dont take length in brackets.
oh
ok so if this is indeed correct
will my function work?
as in will it connect to heroku and store data?
And use parameterised query for your insert. 1) its safer and 2) what you have currently wont work since you double quote the name value (it expects single quotes)
Also i don't think asyncpg has a method called commit() under connection. I don't remember on top of my head, so you may want to double check their docs, since it may commit itself.
hmm
i remember there being a commit()
but i will check again
so you are saying that it auto-commits?
Yes (but double check documentation)
hello
so i changed it into parameterised query form
and tried with and without commit()
the table is created but values are not inserted
async def add_new_value_to_db(name):
db_connection = await asyncpg.connect(host=host,
password=password,
user=user,
port=port,
dsn=dsn,
database=database)
try:
await db_connection.execute("CREATE TABLE IF NOT EXISTS BOTGAMEDB(Name TEXT PRIMARY KEY,POINTS INT NOT NULL DEFAULT 500)")
except:
pass
try:
await db_connection.execute("INSERT INTO BOTGAMEDB VALUES('{}',500)").format(name)
# await db_connection.commit()
except:
pass
await db_connection.close()```
@weak tinsel Because your using format on execute. And that is still unsafe. Someone can still inject something like name = "';DROP TABLE BOTGAMEDB;--'"
await db_connection.execute("INSERT INTO BOTGAMEDB VALUES($1,500)", name)
Thats two variables there, see again. One is $1, other is 500. That 500 is what you had, i just copied it over.
hey guys a quick question. I am trying to connect to a mysql server using python (mysql.connector) on windows and I keep getting this error. (10060 A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)
Create a student table and insert data. Implement the following SQL commands on the student table: ALTER table to add new attributes / modify data type / drop attribute UPDATE table to modify data ORDER By to display data in ascending / descending order DELETE to remove tuple(s) GROUP BY and find the min, max, sum, count and average..
is this for homework @pallid badger
Nope! @torn sphinx
I've created student table and also inserted few data.
You may see it here!
@proven arrow I figured it out. Thanks for the assistance
How to fill data in email one!
Also how to use ORDER By to display data in ascending / descending order DELETE to remove tuple(s) GROUP BY and find the min, max, sum, count and average.
@torn sphinx
I also asked there 10 hours ago but no one gave answer there!
Actually it's from my project.
Teacher has given few python and sql work.
I've done all python one but stuck in sql one!
hi, I am using SQlite 3 for my database. How do I turn values from a table into a list so that I can use the list in a for-loop? Or, how can I use those values in a for-loop? I am wanting it to loop through all the values for 1 value. Or, how do I get every single value from all the values that are in the table?
What are the alleged virtues of mongodb?
MongoDB doesn't work and sucks because of that
@rough hearth as a general question? or for a particular use case
@brazen charm general question. I'm using it for a project and I don't understand why the extra steps are worth it.
overall i find it pretty meh
a simple docker container and basic db setup is fine but after that its pretty pointless especially after i got to grips with the Scylla monster
how to i make a steady connection with my postgresql DB so i dont have to reconnect every command so it only connects when the code is run. i am using asyncpg . ping me so i see it faster pls
(this is for a discord.py bot)
Just store the connection in a variable and reuse it?
Just store the connection in a variable and reuse it?
@earnest parcel i need to use await and await cant be outside of a def
You can use asyncio.create_task to run the coroutine
im using asyncpg
I know
And you can use asyncio.create_task yo run the coroutine
Since asyncpg uses asyncio
Ok?
sorry im not really experience with the DB stuff
i have it connecting per command rn
import asyncio
class Db:
async def connect():
self.conn = await asyncpg.connect(...)
db = Db()
asyncio.create_task(db.connect())
async def do_something():
await db.conn.fetch(...) #do stuff
That's a minimal example
so question where wuld i put all my commads tehn
You can use asyncio.Event to ensure the database is connected and asyncio.Lock to ensure exclusive access to the conn
so question where wuld i put all my commads tehn
How you implement this into your code base is up to you
TypeError: connect() takes 0 positional arguments but 1 was given```
self has a red line
Just add the self argument
async def connect(self):
oh ok
asyncio.create_task(db.connect())
File "C:\Program Files (x86)\Python38-32\lib\asyncio\tasks.py", line 381, in create_task
loop = events.get_running_loop()
RuntimeError: no running event loop
sys:1: RuntimeWarning: coroutine 'Db.connect' was never awaited``` when i add awair it doesnt work
await*
Are you using discord.py?
yes
async def connect(self):
self.conn = await asyncpg.create_pool("postgresql://**************@db-postgresql-nyc1-********-do-user-*************-0.b.db.***************8.com:********/****8?***88=*******8")
this seems to work
do i have to close it?
You can just use bot (or whatever you named your bot instance).loop.create_task(db.connect())
oh ok
And no you don't have to close it.
db.connect()
db isnt defined
Well what does your code look like now?
async def connect(self):
self.conn = await asyncpg.create_pool("8888888888888")
client.loop.create_task(db.connect("88888888888888"))
What happened to the class?
class Db:
async def connect(self):
self.conn = await asyncpg.connect(1111111111111111111)
db = Db()
asyncio.create_task(db.connect())
async def do_something():
await db.conn.fetch(...) #do stuff
client.loop.create_task(db.connect("1111111111111111111111"))
class Db:
async def connect():
self.conn = await asyncpg.connect("credentials")
db = Db()
client.loop.create_task(db.connect())
ohhhh ok
do i put the loop at teh eend of my code?
i have users table, and guilds table. User table has FK guild_id, and this is the PK of guilds table. Guild table also has FK owner_id, which is a user_id and PK from users table. How i can get all guilds, and the owner of that guild from users?
class Db:
async def connect(self):
self.conn = await asyncpg.connect("creds")
db = Db()
@client.command()
async def coolbeans(ctx):
await conn.execute
client.loop.create_task(db.connect())``` it says conn is not defineed
anyone? smokey you know?
db.conn
db.conn
@earnest parcel You sir are a god of life it works thank you sm
who else uses asqlite?
i have users table, and guilds table. User table has FK
guild_id, and this is the PK of guilds table. Guild table also has FKowner_id, which is a user_id and PK from users table. How i can get all guilds, and the owner of that guild from users?
@torn sphinx Its hard to give an exact answer without seeing your table structure but you can useJOINfor this.
SELECT * FROM guilds_table
INNER JOIN users_table
ON guilds_table.user_id = users_table.user_id;
Generally something like this to make the join. You can replace table names with your actual table names, and change the * for whatever columns you require from the respective tables. @torn sphinx
ok well let me try, and sorry for not give table. It was very long, and i didnt know how to format correctly here with the lines and stuff.
who else uses asqlite?
@torn sphinx Just ask your question.
I dont understand what you mean?
I meant, who else uses the asqlite module that was created by Danny?
pretty easy to understand
Whos danny?
Y0u Don't knOw who he is???!!
He's the creator of discord.py!!!
Oh yeah, sorry, some people don't know who the creator of discord.py is, I'm sorry for that
Ok, well i didnt know they are also the creator of aiosqlite.
hello again !
async def fetch_db_value(name):
db_connection = await asyncpg.connect(host=host,
password=password,
user=user,
port=port,
dsn=dsn,
database=database)
await db_connection.execute("SELECT * FROM BOTGAMEDB")
amount_fetched = db_connection.fetchall()
await db_connection.close()
return amount_fetched```
whats the postgre syntax for fetching all results?
because this doesnt work
@weak tinsel You can fetch directly. And its fetch() to get all rows.
oh
data = await db_connection.fetch("SELECT * FROM BOTGAMEDB")
Will give you a list of Record objects. https://magicstack.github.io/asyncpg/current/api/index.html#record-objects
With lots of foreign keys and other constraints just looking at your tables can be super overwhelming @rough hearth
With diagram, assuming it's interactive you can isolate specific relay
^ this
Of course if you work with 30-40 tables you likely can live without any
I also find that if your DB structure is reflecting some real-life system then often diagrams are useful for helping people without domain knowledge to quickly visualise the system
🤷♂️ Didn't have that kind of experience, but can see how it can be
who else uses asqlite?
@torn sphinx
I do, sqlite is great
Still getting this error... Even both Apache and Mysql both are now active.. Can some one please help
none of those things are python
That being said, the UI says the httpd is on port 4433
Or 8080
does anyone know how to create connection pools with asyncpg module?
Or 8080
@jaunty yew you mean on browser?
Yeah xampp says apache is bound to 4433 and 8080
Like @jolly acorn said or also http://localhost:8080
When I click on Admin button which is there on Apache it say it's working..
But, when I am trying what you mentioned it's throwing error
Any idea what should I do?
Write some python and stop using phpmyadmin
Write some python and stop using phpmyadmin
@jolly acorn
I actually need to work on backend.. So, that's
the reason..
MySQL comes with a native management tool
Or use a python migrations library
Or HeidiSQL.. or debug why php isn't working
Or use a python migrations library
@jolly acorn not sure what aare you saying. But sound good.. I am using VS code..
Mysql Workbench, better than phpmyadmin
@dreamy cedar What happens if you click on the admin button next to MySQL?
Or you might want to try MAMP, an alternative to xammp. Sometimes xampp breaks with things like UAC enabled, (that's what I've experienced anyways).
hi, iim new with working in databases, i'm making a database for my discord bot, but i dont know if what i've written is right
thinghs i've written:
'''connet to database'''
con = sqlite3.connect('users.db')
cur = con.cursor()
'''create table'''
cur.execute('CREATE TABLE IF NOT EXISTS mod(id INTGER PRIMARY KEY, warns TEXT, reasons TEXT)')
'''select user and import user'''
cur.execute('SELECT * FROM mod WHERE id= ?', (user.id,))
mod = cur.fetchone()
nwarn = mod[1]
reasons = mod[2]
'''insert values in user'''
cur.execute('INSERT INTO users(id, warns, reasons) VALUES(?,?,?)',(user.id, nwarn, reasons))
'''close database'''
con.commit()
con.close()
tell me if i'm doing good
@grave eagle Your creating a table called mod but you are inserting into a table called users. Maybe just change the name in insert to mod (which is what i think you meant to put anyways).
Other than that, it looks fine and it should work. Although you may want to consider normalising the data, if you want to have more tables in the future.
Hi, I have sucessfully connected to a db using psycopg2 but when I use environment variables (host=os.environ['HOST'],) to connect to the db I get this error:
raise KeyError(key) from None
KeyError: 'HOST'
Does anyone know how I can connect using environment variables. I believe my code is right but there might be a configuration issue but I don't know where to look. My env variables are set in intellij.
@proven arrow yes x,x i misstyped
what do you mean by "normalising the data", it's the 1st time i use sqlite/databases in general
@grave eagle TLDR; In short its just a way of trying to make the database more efficient. So basically instead of storing data in multiple places, you store it in a single place. Essentially each table should store specific/related data and avoid storing nothing more.
More importantly its a way how we can avoid data redundancy and help enforce data integrity. Data redundancy is just when the same piece of data is stored in multiple places. As an example in your case you stored user_id as a column, and now say you had another table called giveaways, where you also had to store the user_id. Thats fine, however what if the user_id changed? It would mean you got to update it in both the tables now. This is data redundancy which you should try and avoid by having a single source of truth (i.e. store it in a single place, and make reference to this, so if it needs changing its changed in a single location, where the changes are reflected everywhere).
If your thinking then how to link the tables together if you were to split the data up, you can link them through Primary and Foreign Keys. I wouldn't worry about it too much, just understand the concept and why its important, and everything else will fall in place. No system is perfect, however these are just ways to make your life easier, and your systems more efficient. You will probably fine more information on the internet that may help, but this is what i could think of on top of my head.
Hey everyone,
I just installed mariaDB on my manjaro distro.. had to follow a medium article to install it and run it (default port = 3306)
Now, I'm trying to login as a root user but whenever I execute the command
mysql -u root -p and hit enter when password prompt was appeared, don't know why but it was showing this error :
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Although at the time of installation, i passed mysql value to the flag --user here :
# mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
so, does it mean, only this user mysql has an access to it ? even though i tried passing other names to this flag but didn't work.. so yeah, this user is dedicated to mysqld service ?
nvm, running that command with sudo did the task haha
Hello everyone! I have been learning about ORMs (focused on SQLAlchemy) and I created my first attempt at a database using that tool. Would anyone be able to take a look at my ERD (https://github.com/Majestic95/BellBankLMS/blob/master/BBLMS - ERD.JPG) and my short code block (https://github.com/Majestic95/BellBankLMS/blob/master/Database_test.py) to critique the syntax or structure of the code? I want to make sure I'm creating one-to-many and many-to-many relationships correctly Thank you to anyone helping!
how do i find an empty row in an excel file using openpyxl? to fill info with.
@icy fable Diagram doesnt look right
having some issues inserting something into mysql db
pymysql.err.DataError: (1366, "Incorrect string value: '\\xE2\\x80\\x8BThe...' for column 'content' at row 1")
i did some research and \\xE2\\x80\\x8B corresponds to zero width utf8 character
some preliminary googling showed that i should switch the character encoding of my table to utf8
idk what the implications of that would be to my information
@proven arrow Lol could I bother you for what looks wrong about it? Also, did the syntax make sense in the code block? Thanks for taking a look at these, I appreciate it
Guys I need some help querying PostgreSQL. I have a user table with uuid column. However I just cannot select using it in a where statement no matter what I try
ERROR: column "020c5727-9ccf-4f6a-a846-246f8580830a" does not exist
LINE 1: SELECT * FROM users WHERE uuid = "020c5727-9ccf-4f6a-a846-24...
^
SQL state: 42703
Character: 34
the full query SELECT * FROM users WHERE uuid = "020c5727-9ccf-4f6a-a846-246f8580830a"
@icy fable For many-to-many: The arrows should be the other way around (many should point to the pivot table). Also on the pivot table you only have a Not Null constraint, so your missing the others like PKs
Regarding the other side, shouldnt it be: A curriculum has many modules, and each module can have many quizzes? But thats up to you on how you see it (you know your design/intentions better)
Module Table - (maybe rename the FK so its called curriculum_id, since that is what it should be referencing too anyways)
Quiz table - It has no FK field, even though you have a 1-M relationship on it.
Curriculum table - I dont see the purpose of having the curriculum_id/quiz_id/module_id columns.
As a hint: If Table A, has a one to many relationship with Table B, then the foreign key fields should be in Table B.
The child has the FK, so the "Many" side has FK over the "One" side.
@dusky plaza use single quotes around your value, not double
@proven arrow Can't believe that was the error lol. Thanks
Can't believe that was the error lol. Thanks
Double quotes for identifiers
plis halp lufthansa plane man
@glass gorge Ive just finished work. Home time now 😛
But utf-8 is fine
:/
It looks like you are inserting bytes to me
i mean should i be managing this in my python code
or should i just convert the table to utf 8
ALTER TABLE roll_call MODIFY content VARCHAR(10000) CHARACTER SET utf8;
well converting table is easier so try that first
currently the character set is Latin1
but I would definitely handle that inside the code
ALTER TABLE roll_call MODIFY content VARCHAR(10000) CHARACTER SET utf8;
i ran this and im waiting for it to process, it's taking a long time for a table with basically nothing in it
it's been almost an hour and it hasn't stopped processing
kill the process and try running it with debugger this time, might work
kill the process and try running it with debugger this time, might work
@umbral osprey is that done in themy.cnf
try that
@torn sphinx
I do, sqlite is great
@jaunty yew aSQlite is a coroutine version of SQlite 3, not another alias for SQlite 3 lol
how do i print all teh names of the tables in my db
@client.command()
async def tables(ctx):
tables = await db.conn.execute('''SELECT * FROM ''')
print(tables)```
what would i add to print the tables?
im using postgresql
@torn sphinx try this, #databases message
yeah idk
@torn sphinx try this, #databases message
@proven arrow it printsSELECT 3
try that
@umbral osprey im stuck
You want fetch instead of execute
ohhh
then you might have to run your software as a admin so it gets more ram and cores dedicated to it
if that doesnt work, might either be your software, or your code is somehow looping
all i can think of
its run as root
and my pymysql config has utf encoding already
def db_connection_content():
return pymysql.connect(host=os.environ.get('HOST'),
user=os.environ.get('PYMSQL_USER'),
password=os.environ.get('PASSWORD'),
db='content',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)```
hmm i may be trying to debug under root and not the admin account
then maybey your software couldnt handle it, if you have another software, try that, doesnt work i dont know anymore
i ment your computer
i see
is it overheated
i mean i dont know alot about software and thier cpacity
np
I want to use mongodb (or potentially similar) as a database for my discord bot using the discord.py library. It seems like my two options are pymongo or motor, however, motor says it is the one to use with asyncio (what discord.py uses). Does anyone who has some experience have any recommendations?
generally the recommended db is Postgres with asyncpg
because:
- Its the most performant DB
- It has the most performant async driver
- it has the most correct and sane driver
- most if not all of the data you'll encounter with your bot will be relational and therefore suited to SQL
Motor used to be really really bad overall
not as bad now that they finally dropped py2 support
but still pretty awkward in places and slow
but mongo in general is like that
From my research I'm pretty sure a NoSQL db would be best for my situation, as I intend on adding new things regularly as well as the fact that I potentially will have different data for each user...
I am creating a reputation system in my discord bot
I want it to be cross server
where should I start
postgresql or mysql, or Maria?
I was under the impression that with SQL you sort of needed to have a really good idea of the data you planned to store so you could create your db based on that and that if you wanted to add new fields it was a pain/takes a while?
it's fairly simple and quick to add new columns
also I suggest using an orm like sqlalchemy
you can do powerfull stuff, but you can also store plain json files if you wish
This post dives into patterns that most data scientists/analysts/engineers have seen: ways DAGs grow, and how that leads to a compounding level of effort to maintain data systems. We’ve found this vocabulary very helpful in our own work---hope you do, too!
https://greatexpectations.io/blog/deeper-wider-thicker/
Introductory note: this blog post is a refresh of a talk that James and I gave at Strata back in 2017. Why recap a 3-year-old conference talk? Well, the core ideas have aged well, we’ve never actually put them into writing before, and we’ve learned some new things in the meant...
I'll take a look
All my user data atm is just in a few json files if that adds anything
Hey
I need some help with sqlalchemy
I have multiple instances of a model, I want to sum a specific column from all the instances
How do I do that?
How do I transfer ownership of a postgresql table? I tried ALTER TABLE muterole OWNER to Slio but no work
@slow river https://stackoverflow.com/questions/11830980/sqlalchemy-simple-example-of-sum-average-min-max
@slow river Is your code open-source? I'm using Python and SQLite and I'd love to learn more from someone who is far more experienced than me!
hey all. Does anyone have a recommendation for a SQL database manager? Just have a few large SQL databases I just want to tidy up and search through etc - dont need network capability or anything I'm just looking for a good one 🙂 I tried postgreSQL and it's far too confusing I think
@winged cargo Take a look at JetBrains' DataGrip if that is something you like
https://www.jetbrains.com/datagrip/
sweet I shall
i dont need any advanced SQL programming features it's litterally just combining db's. removing duplicates, etc
I find the above really good for any work with databases no matter if it is small or big. Its just a personal preference.
Hello
yeah im just pretty new to SQL dbs and it seems like it's just full of such advanced packages if you just google haha
im not a sysop in a ftse100 tech company managing thousands of complex dbs haha
my previous experience was MS ACCESS, then i dove into postgre and i was like oh god this doesnt look right 😄
so jetbrains datagrip is not free, is there a more open source alternative?
not going to dump £70 on it
Heard about HeidiSQL from a couple people as well
HeidiSQL is a free and powerful client for MariaDB, MySQL, Microsoft SQL Server, PostgreSQL and SQLite
oh wicked
so is SQL just a type of database yeah?
or is it a file format
well thats kinda the same thing
SQL stands for structured query language. Its used for databases. You could call it a programming language.
Hey guys, someone with SqlAlchemy experience here?
@cunning copper I don't have a ton, but am actively trying to learn. What's your question?
thanks @icy fable , was about when you've to prform queries, but you don't have entities, because you are just connecting to an database and has to extract some data
I saw a lot of tutorial, but always with the models :/
@main gazelle thanks so much appreciate it
@main gazelle so HeidiSQL requires a SQL server in place. So not a real use to me but I appreciate it. I'll go hunt some more 🙂
Sorry that I couldnt be of too much help
no worries, some times it helps others reading this 🙂
if anyone else can recommend a local SQL database manager (dont need any client/server stuff) I'd appreciate it!
@winged cargo Which database are you using?
Hi all, someone knows a good/link/tuto for sqlalchemy many to many queries ?
I had two table, linked by a many many relation assoc table
I simply try go get name for table 2 where id in table is X
@icy fable actualy I think it is necessary to declare the database, this from sqlalchemy doc:
When using the ORM, the configurational process starts by describing the database tables we’ll be dealing with, and then by defining our own classes which will be mapped to those tables. In modern SQLAlchemy, these two tasks are usually performed together, using a system known as Declarative, which allows us to create classes that include directives to describe the actual database table they will be mapped to.
to be clear, yes I had declared the database in sqlalchemy.
like this : https://pastebin.com/QXvExe5g
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.
@winged cargo Which database are you using?
@proven arrow I've got some SQL files
and some other large files csv files i'd like to add to the db
What is the proper syntax in SQLAlchemy to create a new Object (also a new entry into the database table) when the Object's required attribute is user input? Is it new_user = User(username=?, pd=?, date_added=? WHERE (userval1, userval2, userval3))
@winged cargo SQL is just the query language to interact with the Database. I meant what database as in sqlite, postgres, MySQL, was those sql files created with?
@winged cargo SQL is just the query language to interact with the Database. I meant what database as in sqlite, postgres, MySQL, was those sql files created with?
@proven arrow I don't actually know...
@proven arrow MySQL 10.13
just looked
Ok well then MySQL is a client/server database, which you were trying to avoid right?
yeah i dont need that, just need something local on my PC you know?
yeah im guessing using localhost - was just wondering if there was a bit less faff way
to me setting up a client/server relationship on my localhost seems a tad overkill
and there must be a more simple way
but i've never used SQL before so perhasp that is me being naïve?
most DB's are server based
if you dont want that because its only a small project look at SQLite
which is just a single file instead
it's about 250gb
What is it you are trying to do with the database?
If you want something easy to setup then go with something like downloading MAMP or Xampp. This comes with phpmyadmin, which allows you to interact and manage the database over the web. Things like importing and stuff are pretty straightforward as well, and gui is quite simple.
It's easy to setup as well (just a single install), and you can start/close from the admin panel.
sweet i'll have a look
ref the other comment on SQLite
what is classed as a small db
im looking at 250-500gb
Maybe medium or crossing just over small.
But still small considering some have many millions of rows.
sqlite can go upto what? 256TB now or something dumb
ok thanks
Something like that I read. But whoever hits that in sqlite
but size wise you probably want something that can crunch the data a bit better like Postgres
okay, so now i get that DBs are mostly client/server
should i just boot up postgres and use it like that?
probably
i havent used SQL so has it got a nice GUI interface?
pgadmin
its a lil daunting at first because it gives you so much info
but its pretty simple
Yep, was just going to say that.
sweet - thanks a lot
ill check it out
appreciate the help, never done databases before :)!
so it seems like because my db is a mysql db i need to convert it to a postgreSQL db
ahhh hmm
maybe i should just download mySQL?
depends what you wanna use
i personally wouldnt use mysql because it provides nothing but downsides compared to postgres but yeah
if it already exists as a full mysql just roll with mysql
unless you plan on using it for a long time ig
do you recommend a good way to convert then?
or will it take ages
up for recommendations 🙂
found that - will check it out
uhhh quick question
when i set the character set and collation to utf8
do i have to do that by column, or can i just do it at the end for the whole table
Which dB?
mysql
oh?
yeah i tried to upload some stuff and it couldnt encode zero width spaces
the char set was latin1
so i tried to alter the table to utf8
and it took a really long time, so I was told to drop the table and reupload data, after i create a new table with the right encoding
Mysql is know for hating anything unicode
im about to quit programming and become a monk
i tried
it proved too difficult for me
the installation was lame
but yeah
still didn't answer the original question @brazen charm
Please ping me on reply
I am using the discord.py library, and making a game in python. Players interact with it through a discord bot so it is basically a text/command based game. But I want to host my bot on a vps and would like to set up a proper database server/system rather than what I have currently which is just a few json files that store user data as well as game data (eg. weapon stats and stuff like that). Currently to change user data I load the json file, make the change then overwrite the file - this is not a great way of doing it and most likely will not be able to cope if the player base grows. So I would need a database that supports a python API and that works with the async branch of python. I would really appreciate any advice anyone has. Here is an example of some user data:
Hey 🙂
cursor.execute("SELECT PlayerName from players WHERE Online = 'Yes' ORDER BY PlayerName ASC")
data = cursor.fetchall()
if not data:
print(f"No players are online no bounty has been set.")
else:
victim = data
print(victim)```
it prints like this [('EntropYToucH',), ('Poochington',), ('Tanyol',), ('suvipetraa',)] is there a way to select it without the brackets and commas. or should i just str replace them to remove ?
print (victime.PlayerName) gives what ?
@grim pier
in fact .. for row in data:
print (row.PlayerName)
try that
okay ill give it a try 🙂
just comes as row isnt defined
.... non...
else:
victim = data
print(victim)
change it to :
else:
for row in data:
print (row[0])
That works yes but only selects the first player
Instead of printing all of them online
😛
you have done data[0] ?? past your code please..
i did not tell you tat..
i hit backspace by accident
what is the output now ?
The code above works but only prints the first name 😛
try that.. I can't believe it :
print(data) for row in data: print(row[0]) print(row)
WAIT ..
no data is an list .. and you have inside the list a tuple
[] -> list in python... kind of array
(X,X) the tuple
the first print data should print the object..
that's what the " for " is 🙂
for row in data
will iterate all tuple in a variable named row.
typically
are you sure they're are still in your db 🙂 ... the first print data should print all of them.
in the test i asked for before the for loop.
so the print data still print [('EntropYToucH',), ('Poochington',), ('Tanyol',), ('suvipetraa',)]
sorry it work here 🙂
hmmm weird
can't help more .. i did not have a msql here 🙂
Ill have a play around
Thanks dude 🙂 will do
what is the formatting when using google sheets api to only import the data from the cell instead of the whole cell details, e.g. ( <Cell R3C2 'Name1 Name2'> )
Is sent whereas i want it to just output ( Name1 Name2 )
I'm not very sure, but I think the avg function works on an entire column, but after the WHERE you only have access to individual rows (so it can't compute the average of the entire column)
ok that makes sense, thanks
that would be the reason you would have to sub-query the avg part of the code
Yeah
If I were to transfer my code from sqlite3 to aiosqlite, how much would I have to change if I just imported aiosqlite as sqlite3
Like would I just be awaiting everything, or are there massive changes
idk this is dumb question but just a doubt ,cant i use msaccess instead of postgresql becoz mine is a simple bot?And if yes would there be any problems due to that?
they both are database isn t it?
Anyone free? 🙂
sqlite3.OperationalError: no such table: bounty
The table is created though :/
elif data:
randombounty = random.choice(data[0])
print(f'Bounty has automatically been set against: {randombounty} Bounty will be active for 6 hours.')
now = datetime.now()
time = now.strftime("%H:%M")
sql = sqlite3.connect(r'databases\bounty.db')
cursor.execute(f"INSERT INTO bounty (victimname, playername, timeset)"
f"VALUES ('{randombounty}','TEC 1 Corp','{time}')")
sql.commit()
log(green("bounty database updated"))```
Where are you getting the cursor from for that execute statement?
Hello, i'm trying to count every occurrence of a value in each column, for each unique value in the final column, but it always returns an empty list, the only thing that returns data is a "SELECT * FROM NewData", merely trying "SELECT * FROM NewData WHERE 'External ID' = 'external ID 1'" returns an empty list, any help or advice would be appreciated
@sharp dragon your column name has a space, so use the backtick character ` instead of the single quotes around the column name
You can also double quote the column name as well, or also use [] around the column name.
@proven arrow omg thank you so much!, ive never seen in SQL before, it works, but what exactly does do?
wait it does something in discord too, i mean (`)
ahah yeah discord uses it for code blocks, i had the same issue trying to show you
but in sql its for quoting identifiers (although different databases use different characters for this.)
Usually you use it when your identifier (something you create like table name, column name) has a special character or one of the reserved words, so itll treat it as a string
ohh, thank you, ive been breaking my head for so long on this
Anyone can help me here :
MariaDB [(none)]> CREATE DATABASE mydb1;
ERROR 1044 (42000): Access denied for user 'hi-man'@'localhost' to database 'mydb1'
I've granted all the permissions to hi-man on mydb but it never allows me create a database, why ?
how did you grant the perms?
I did this -> GRANT ALL PRIVILEGES ON mydb.* TO 'hi-man'@'localhost';
did you also create the user?
yeah i did
Actually i followed the arch wiki
$ mysql -u root -p
MariaDB> CREATE USER 'hi-man'@'localhost' IDENTIFIED BY 'some_pass';
MariaDB> GRANT ALL PRIVILEGES ON mydb.* TO 'hi-man'@'localhost';
MariaDB> FLUSH PRIVILEGES;
MariaDB> quit
You have db specific priveleges, and not access to create more dbs.
Check the rights with SHOW GRANTS;
means, I can access the database, but not create anymore ?
not with that user,
GRANT ALL PRIVILEGES ON mydb.* TO hi-man@localhost`
ohh okay okay... please wait
ERROR 1045 (28000): Access denied for user 'hi-man'@'localhost' (using password: YES)
I got this error @proven arrow
Do it with the root user
can you tell me what's the difference?
Oh wait, is it like, i can't alter or set any preferences with user hi-man ?
Well to do something with hi-man you need the privs
can anyone help?i am making a discord bot and i wanted postgreysql so which all components should i install from the installer?
@proven arrow Thanks sir, It worked now 🙂
but you know what other changes occured, before creating any other database I had only 1 database i.e., information_schema but now it's showing two others including the one I wanted to create
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| ronin_playlist |
+--------------------+
what other databases are used for ?
@torn sphinx What are the options? I think you can install all of them except for the stackbuilder option which is not always necessary
@steady lava those are part of the database
only stack builder not needed?
Yeah i guess you can leave that out, unless you need it?
what is it used for ?
those are part of the database
the one which i just created ?
pgadmin is the GUI interface so you can access the db through the browser,
ok
thanks for the help
so there is only password and no username?
i am new to sql
the one which i just created ?
No i meant the other 3.
mysql - is the system database
information_schema - has the database metadata
performance_schema - is for monitoring certain things
You should just leave them as they are.
@torn sphinx default username is postgres
ok
ohhh okay okay got it now... they have nothing to do with ronin_playlist ..
Great, Thanks for the help 🙂
what is port should i leave it as default?
default is 3306 i guess (for mariaDB)
mine starts with 5 tho
Yes
thanks for helping me setup
if i am using a DB like MongoDB, then where to download the DB in order to take data from it? am using repl.it for hosting my bot
pls ping me if u know
What is the proper syntax in SQLAlchemy to create a new Object (also a new entry into the database table) when the Object's required attribute is user input? Is it
new_user = User(username=?, pd=?, date_added=? WHERE (userval1, userval2, userval3))
@icy fable I use this way and works, like thisclient = self.clientModel(username=_username, email=_email, password=generate_password_hash(_password) ) self.db.session.add(client) self.db.session.commit()
If you want check my repo, could be more clear: https://github.com/acpn/brasilprev-challenge
getting this error when i am trying to install pip install mysql through command prompt
Sure give me one sec
try pip3 install whatever your trying to install
@sage scaffold
Same error
Sure..
you need
Microsoft Visual C++ 14
with buildtools
as it seems
Didn't understand what that means.. What seems hard to solve
..
Okay sure.. Please let me try
sure!
@sage scaffold ohh i need 6 GB data..
i need some help
am new to databases and stuff
and i wanna know how to host a mysql db online for free so that i can access data from it
@knotty gyro Either you can find a free host, or if you already have a server for your application, you could run it on that.
Also if your application is local on your PC, then you can also run MySQL on your pc too
What about it?
Am getting that the host ain't listening to TCP/IP on the port and ip
It's IPv4
0.0.0.0
And some port
Is your postgres server running?
Yes
How do you know?
-_- cz I started the server myself
I will show the pic
In 2 min
Oh wait
I got the problem
psycopg2 connection timed out (0x0000274C/10060)
This problem am getting
Pls ping me if u know
Postgres
Double check your connection details, they are probably incorrect
So i have this query,
select count(*) as aggregate from products where status in (0, 'NULL') and name like '%Tommy Hilfiger Mens Jacket%'
Double check your connection details, they are probably incorrect
@proven arrow those are correct
Wait
Should I change the node port to 5432?
So this should get me results where, status is 0 or is null, but it is not, instead just give me 0 results. It works if status is 0, but not if null
Should I change the node port to 5432?
if thats what its running on then yeah
Oh ok
generally if on you local pc, then it should be on localhost over that port, so you only specify the host as localhost.
@torn sphinx Because thats not how null is evaluated.
.... FROM `products` WHERE `dormant` = 0 OR `dormant` IS NULL AND `name` LIKE ....
is how you should check it
ohhh
should i use asyncpg or pygscop2 for discord bots?
Second one
why?in discord.py one person said asyncpg. whats the difference between them?
It's different modules for different databases
The second one is for postgres
Libraries
so if i install em both would any problems occur?
i have postgres
BTW
but the problem is asynpg or psycopg
those are different libraries for different db
but asyncpg is also there for postgrey
there is a github page can i link it here?
?
oh okay
you should very much not use psycopg2 for bots
asyncpg is the async lib
its also faster than psycopg2 by alot
ok thanks i installed asyncpg rn
@brazen charm am also making dpy bot, so I should use asyncpg instead of the other?
yes
yes... Just google it
Hmm ok
anyone can tell why it didn't add the songs from the 1st Id (or say index) ?
Also, I've set Auto_Increment when defining Id (Primary key), and later I set Values to only Title column.. is this causing a problem ?
@steady lava it's because you never told it to insert into the first three rows.
Your first insert you are inserting three values which the database adds to the rows 1-3. The second insert is just carrying on from row 3. You would have to do UPDATE to add to the previous three rows, instead of insert.
@proven arrow ohh yes you were right, and I guess when it didn't recieve any value, it just set it to NULL.
Now, I've used the update operation like this :
UPDATE song_info SET Path = ("/Music/LP/Pushing\ Me\ Away.mp4") WHERE Id=1;
and I had to do it three times, isn't there any other approach where I can set all the column values at once? like a WHERE condition ?
just remove the where
it will automatically override all rows to that if it doesnt have a where
just like DELETE does
I've 3 different values or say three different song links to store
You can either insert and on duplicate update, or use update with a case statement.
Yeah but more like an if else
ohh that's nice.. lemme check it out
Thanks for the help 😄
Also when you insert for the first time you can specify all the values then, in case you wasn't aware.
Does anyone have any recommended code examples or tutorials/guides on SQLAlchemy? I have some nuanced questions that the documentation glosses over very briefly about program architecture (ex. Where do I place 'create_engine()' or 'declarative_base()'? Do they go into the same .py file as my table declarations?)
Any SQLAlchemy code examples or programs would be very much appreciated!
Hello people.
I'm trying to pull data out of this mongo db. But I think it has invalid dates in them?
Because when I'm iterating through the cursor,
I'm getting hit with the
InvalidBSON: year -1 is out of range
Any suggestions on what to do?
Is SQLAlchemy pretty dead?
I still use it, and plenty of apps use that as their ORM of choice, it's pretty mature
speaking of the beast, I'm running into trouble with it right now
I have this declarative base https://paste.pythondiscord.com/virecinoro.py
Which I use this way https://paste.pythondiscord.com/aralacoqok.py
but when I do alembic revision --autogenerate -m "message"
I get sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'bases' and 'players'
this is my player model
there's no foreign keys
I've been using this documentation page to get it working and it doesn't https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/mixins.html
anyone able to help ?
I'm trying to do a pg_dump command but it says access denied. Does anyone know why this is happening?
pg_dump.exe -U postgres EXO > C://Desktop this is the command I did
I just tried doing the same command in my admin account and it still says access denied
did you run the command shell as an administrator ?
That might help
oh I didn't see
the path should be C:\Users\Username\Desktop
the root of the drive usually is protected on windows
@silk vortex
oh I already got it
but I need help with something else
I did the psql command in my vps
but how do I check if the database got successfully restored?
@hybrid horizon
no idea about that though sorry
Traceback (most recent call last):
File "main.py", line 11, in <module>
loop.run_until_complete(run())
File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "main.py", line 5, in run
conn = await asyncpg.connect(user='pterodactyl', password='ZPWgpMN4hETqjXAV',
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/connection.py", line 1718, in connect
return await connect_utils._connect(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 674, in _connect
raise last_error
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 663, in _connect
con = await _connect_addr(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 632, in _connect_addr
tr, pr = await asyncio.wait_for(
File "/usr/lib/python3.8/asyncio/tasks.py", line 490, in wait_for
raise exceptions.TimeoutError()
asyncio.exceptions.TimeoutError
am getting this error
pls ping me if u know
it works but is there a way to get rid of the ( and ,
Any ideas anyone? 😛
Any ideas anyone? 😛
@grim pierlistcomprehension
@shell ocean Would it just be better to str.replace them?
h
@grim pier they aren't strings...
It's a list right?
cursor.fetchall returns by default, a list of tuples
What?
The point is, those () and , are from the tuple
They aren't part of any string
so you can't replace them
Not without some transformation...
If you know that your query always returns just 1 column (or you only ever care about the first column returned) then you can use a list-comp as @shell ocean says
cursor.execute("SELECT expirytime from bounty")
data = cursor.fetchall()
if data:
data = [row[0] for row in data]
print(data)
In MongoDB, am I missing something or is there no easy way to have one document per day in mongodb?
like i mean i could do something like _id = "2020-10-10" to have a unique document for each day
but then i cant query against datetime objects obviously
In general i would want to write daily statistics to a document in a collection but i should later be able to find for example the statistics of the last n days
nvm i figured out that you can use $toDate and this works well for me (https://docs.mongodb.com/manual/reference/operator/aggregation/toDate/)
im trying to add items in a db transaction (sqlalchemy) that have a unique constraint on their name, so its flipping out when i commit because i have a whole bunch of dups
is there any way i can access pending inserts so i can check for existing?
Guys, pls help I use MongoDB and I need the user to be entered into 1 collection for each guild separately, but it gives
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: ecodb.colldb index: _id_ dup key: { _id: 337954786190295050 }, full error: {'index': 0, 'code': 11000, 'keyPattern': {'_id': 1}, 'keyValue': {'_id': 337954786190295050}, 'errmsg': 'E11000 duplicate key error collection: ecodb.colldb index: _id_ dup key: { _id: 337954786190295050 }'}
elif not self.db.find_one({"_id": message.author.id, "guild_id": message.guild.id}):
await message.add_reaction(':white_check_mark:')
post = {
"_id": message.author.id,
"guild_id": message.guild.id,
"guild_name": message.guild,
"name": str(message.author),
"balance": 100,
"xp": 0,
"lvl": 1,
"minvoice" : 1,
"messages" : 1,
"reports" : 0,
"rep" : 0,
"timely": 0
}
if not self.db.find_one({"_id": message.author.id, "guild_id": message.guild.id}):
self.db.insert_one(post)
Please tell me, I really need to, but I can't solve the problem for the second day
Here is the structure of the database, it cannot add the same only with another guild and I cannot understand why, but I am writing self.db.find_one({"_id": message.author.id, "guild_id": message.guild.id})
@velvet ember so the problem is that mongo does not support duplicates
And you have the user ID as the primary key on what it lists everything by
yes, but I don't know how else to do it
also you're using pymongo right?
yes
which is kinda a bad choice, since discord.py is async and database requests can take some time and will cause massive blocking
motor (async driver for mongo) is almost the same but async
meaning you have no more blocking
also for the error you're having, you need to turn your logic around
since you cannot store the user multiple times
I'll try to do
you can either have a list within the user record with all guilds they are part of
or
you can have a list in the guild record with all members they have
databases are relational
so you have like 1 entry per user
and 1 entry per guild
and you put the main value (in this case the id) in one and it makes it relational
it links the 2 entries together
that's what databases are all about
relational
ok, got it, thank you!
I've just started with the aysyncpg lib and its docs, and I have this little example program:
>>> import asyncpg
>>> import asyncio
>>> async def run():
... con = await asyncpg.connect(user='postgres')
... types = await con.fetch('SELECT * FROM pg_type')
... print(types)
...
>>> asyncio.get_event_loop().run_until_complete(run())
If I want to place and use e.g. function run() in a class, where do I put the asyncio.get_event_loop() code in the class?
Or must I have 2 functions, one with my query called __run and then another 'public' function called run, with asyncio.get_event_loop().run_until_complete(__run())?
>>> table_name = 'user2_playlist'
>>> c.execute('CREATE TABLE ?', table_name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: near "?": syntax error
Anyone can help me here? It is mentioned in python's sqlite docs that, whenever we need to set the value explicitly then always use this ? as a placeholder for values..
but when I did it for table name then it's showing this error... should I use f strings instead of using ? for table name ?
do we need to cursor.close() and db.close() everytime we are finished with stuff?
like in def func
@torn sphinx You should close the cursor once done using it, but the database connection is generally global and is only closed when your application closes. Or you can use pooling. But this might differ for your use case.
@steady lava Not for table names. Only values.
Also why do you need to create dynamic table names?
Looks like you can better design your schema
Can someone help me with some PL/SQL code
PL/SQL is python?
well yeah
huh didn't know that
It's just oracle SQL
DECLARE
v_numbercredits students.current_credits%TYPE;
v_surname students.last_name%TYPE;
v_comment VARCHAR(2);
BEGIN
SELECT current_credits
INTO v_numbercredits
FROM students
WHERE v_surname = 'Smith';
IF v_numbercredits > 0 AND v_numbercredits < 5 THEN
v_comment :='Not enough credits';
ELSIF v_numbercredits >5 AND v_numbercredits < 10 THEN
v_comment := 'Credits need to be improved';
ELSIF v_numbercredits > 10 AND v_numbercredits < 15 THEN
v_comment :='Credits Completed';
ELSIF v_numbercredits = NULL OR v_surname = NULL THEN
v_comment :='Student smith does not exist';
END IF;
dbms_output.put_line(v_comment);
END;
/ ```
I'm writing a PL/SQL block that will check the current_credits for a Student who’s surname is Smith.
If the credits are > 0 but < 5 display out “Not Enough Credits”
If the credits are > 5 and < 10 display out “Credits need to be improved”
If the credits are > 10 and < 15 display out “Credits Completed”;
And ensuring the code can handle a NULL being returned. If a NULL is returned display out “Student Smith Does not Exist” to the user.
So can anyone help me with it please?
@proven arrow i have 2 files running at one time so if i am done with the db stuff in 1 file do i have to close in that only or smt else
What database are you using?
Also if the 2 files are part of the same application, then you can still refer to a single connection.
It's just oracle SQL
the real beast, unfortunately ive never had the chance to experience it
cogs for dpy
You can close sqlite connections since they are cheap, so should be fine if you close in both files.
okii
closing db when we are done with sqlite stuff right?
and closing cursor when you are done using the cursor in a def function?
@proven arrow
Close the cursor when your done with it.
with cursor.close(), and since you mentioned its for a bot, make sure to use aiosqlite module
Hello, anyone know how to store boolean in mysql?
@torn sphinx just useTINYINT(1). Thats what mariadb does as well internally
So then 0 is false, 1 true.
Its the async module for sqlite. Since your code is async you dont want to be nuking your bot each time you make a DB call now do you. https://pypi.org/project/aiosqlite/
you just got to await calls
the SQLite3 db insterts data as only rows?
like closing db and cursor when done with "sqlite stuff" in a SINGLE file
every relational db inserts as rows @knotty gyro
@torn sphinx again close the cursor. and keep a single connection.
From python or sql?
so it requires server starting and stuff???
Can anyone help me with my question? Does anyone know oracle SQL?
c.execute('SELECT * FROM table_name')
data = c.fetchall()
@fair crescent just ask yr question here and if anyone would know, then he/she would tell 🙂
Well I did I was just asking again since I saw more people typing
oh ok
Lufthansa has already said he/she doesn't know much about oracle SQL so I didn't want bother him/her
so it requires server starting and stuff???
@torn sphinx
so it requires server starting and stuff???
@torn sphinx What do you mean?
Yeah sqlite is file based db so it doesnt
in aiosqlite
aiosqlite is just an asynchronous interface for it. See the link i sent. Explains it.
And no it doesnt require a server
soo it doesnt requires cursors
as i can see in that doc
cursor = db.cursor()
😕
I dont understand what your asking?
like making a cursor
cursor = db.cursor() - for sqlite3
??????? - for aiosqlite
what should be for aiosqlite
because in the docs
cursor = await db.execute('SELECT * FROM some_table')```
Well you are not reading the docs properly i guess. Because it does say Helper to create a cursor and execute the given query. https://aiosqlite.omnilib.dev/en/latest/api.html#aiosqlite.Connection.execute
And the examples show of this as well, by storing it in the variable cursor
just use
TINYINT(1). Thats what mariadb does as well internally
@proven arrow oh right thanks, did not know this was like this.
Can anyone even tell me what are the errors in this PL/SQL code
DECLARE
v_Radius NUMBER := 2;
BEGIN
LOOP
dbms.output.put_line('The AREA of the circle is '|| v_Radius * v_Radius ||);
EXIT WHEN v_Radius > 10;
END LOOP;```
hi please wanna ask abt mysql.connector , what does cursor.execute return on success ??
what I wanna do exactly is like
if thestatement executed succesfully :
smtng
else :
smntng
ValueError: no active connection```
mhm?
i didnt event close the db
wt
oh i am nto awaiting it
xd
@dreamy pebble Itself it doesn't return anything. Instead you should be fetching the results and checking if results are present or not. Alternatively use try catch to get any errors, or you also have cursor.rowcount that will give you rows returned by the select, or other DML statements like (Insert, Update).
thaaank yaa buddy appreciate it <3
Itself it doesn't return anything. Instead you should be fetching the results and checking if results are present or not. Alternatively use try catch to get any errors, or you also have
cursor.rowcountthat will give you rows returned by the select, or other DML statements like (Insert, Update).
here is my code async with aiosqlite.connect("./database.db") as db: query = """ SELECT RANK() OVER(ORDER BY xp DESC) AS rank, userguild, xp, level FROM level;""" async with db.execute(query) as cursor2: level22 = await cursor2.fetchall() print(level22) exp = '1' intexp = int(exp) multiplier = '3' intmultiplier = int(multiplier) #1st place if level22[0] is not None: user1 = ctx.guild.get_member(user_id=level22[0][1]) userlvl1 = level22[0][3] userxp1 = level22[0][2] levelxp1 = userlvl1 + intexp lvl_end1 = int(levelxp1) ** intmultiplier #2nd place if level22[1] is not None: user2 = ctx.guild.get_member(user_id=level22[1][1]) userlvl2 = level22[1][3] userxp2 = level22[1][2] levelxp2 = userlvl2 + intexp lvl_end2 = int(levelxp2) ** intmultiplier #3rd place if level22[2] is not None: user3 = ctx.guild.get_member(user_id=level22[2][1]) userlvl3 = level22[2][3] userxp3 = level22[2][2] levelxp3 = userlvl3 + intexp lvl_end3 = int(levelxp3) ** intmultiplier I am trying to fix
list index out of range
and make it so if there are less than 3 people it will still work
is there a way to do that without errors
ah i think i found the channel im looiking for
just started using sqllite for database and im trying to make a simple table
i feel like im doing everyting right but only 4 / 6 fields that im trying to create actually get made, could someone explain what im doing wrong that would be great
@sinful condor Can you not make a loop and loop X times where X is the length of the results from the db?
@weary warren Show your queries/code
@proven arrow would that work with what I am trying to do when it is a differnet user every time and I am trying to se if parts of a database exist or not
In a loop you wont have to check, since you only looping how many elements the database returns you
So if it gave you 2 rows, you loop 2 times.
And you can cut down all that extra code as well. Just make a dict which you append to from within the loop
um I dont really know how to make what you mean can you send an example
The only None check you would do is once, where you check if the database actually returned something. If it did proceed with the loop else no.
results = whatever_your_db_returns
if not results:
# no results returned from db
else:
my_data = {}
for index, value in enumerate(results):
my_data[index] = {}
my_data[index]['userlvl'] = results[index][3]
my_data[index]['userxp'] = results[index][2]
# ... store whatever else you want inside the subdict here
@sinful condor Something like that.
my_data =
{
0: {
'userlvl': 1,
'userxp': 2
},
1: {
'userlvl': 4,
'userxp': 5
}
...
}
End result will be like something like this.
Best database format for a small business (analysis will be done using Pandas)
I'm looking to replace excel with Pandas. Pandas is great for analyzing data, not so good for inputting new data (to my knowledge). That's why I'm looking for a good DB format which makes it easy to view, append, modify.
The type of data I will be keeping in this DB is: product dimensions, logistics costs, sales projections, actual sales, etc.
Has anyone come across a blog post of someone who has done this? Does anyone have any suggestions for an easy to use database formats for this type of thing?
sounds like SQL would work well for this sort of thing
would recommend checking out Postgres
would recommend checking out Postgres
@brazen charm thanks for the response. Will check it out. Just looking for easy input and reading of the DB when needed