#databases
1 messages · Page 77 of 1
it depends, the mongo shell rounds your number in some case and you have to wrap it in a string
why would it round my number
i’m not 100% sure myself
WILD
that is SO WEIRD
thanks for the article I tried searching it and couldn't find anything probably because it was a hard issue to describe
i’m not huge fan of Document Storage
this might make me never use it in the future ever
LOL
yeah this is super inconvenient wtf
I want to save my specific number not a rounded number this is so wild
Hi I made a post on Code Review (since it's too long to post here) about some postgresql stuff if anyone can check it out (i don't have high hopes of it being answered there due to the really low amount of sql questions in there) https://codereview.stackexchange.com/questions/232537/storing-multiple-translations-for-retrieval
quick question:
I am going to scrape several website (20-40) and extract the same kind of information from all of them.
There will be about 10 columns of data saved in the MySQL database.
Would it be best to create a different table for each site or would you just have 'site name' as a column in one large table?
how about sites table and content with site_id column
so it is used as a sort of look up
?
get the ID from one table and look for it in the data table?
thinking about it, that is a good idea as I want to display the data on a website so having multiple tables will complicate queries
This is quite a theoretical question, but I have an (enhanced) entity relationship diagram with like a 3-way relatiionship. A river connects to a lake at a county border, where river, lake, and county are all entities. Is a 3-way relationship like that allowed, or do I need to resolve it in some way?
You can do that with an intermediate table, which is also a recommended way to do a two-way relationship
That is, table1 is Country, table2 is River, table3 is Lake, Table4 has three columns: country, river, lake
Hi
I am making a psw manager app
with qt and pyside2
should I have any consideration when choosing what db to use?
everything I have already made in my backend I made it with sqlite3, and I want to store the passwords as a string that has been encrypted with fernet
and in live time in the ui, when the user has pressed a button decrypt them all
SQLite stores data as raw text, but has basic security features in that only authorized users may access the database. That basically means it’s as strong as the security level enforced by the user.
So it will be the same that storing the passwords in a word file
I think I couldn't hope for anything else
i know there's at least one sqlite extension for encrypting the database
dunno how easy it would be to get working with python though
Hello. I'm looking to store product information in a database and I figure the best way is to store each product's set of info in a JSON object. Is there a best practice for this?
@noble kite
... store each product's set of info in a JSON object. Is there a best practice for this?
I would recommend treating the top level (0th) of the JSON object as the tables, the 1st level as the attributes, and the 2nd level as the values. For example: ```json
{
"Products": {
"ProductID": [0, 1, 2, 3, ...],
"ProductName": ["Product1", "Product2", ...],
...
},
"Vendors": {
...
},
"Other_Table": {
...
},
...
}
Ah excellent!
Hey guys, I'm facing a weird issue where when I pass an array of Object IDs arr = [3, 4, 1] and then query them in SQLAlchemy using Object.query.filter(Object.id.in_(arr)).all() it returns the queries but in the [1, 3, 4] order
Is there a way to mitigate this?
function getuserinfo($connection, $db, $uid, $row)
{
$stmt = $connection->prepare("SELECT * FROM $db WHERE UID=$uid");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row["amount"] > 0;
}
``` im trying to port this function to python does the fetch work the same as the mysql connector fetchall ?
fetchall would return an iterable of rows
You're looking for fetchone(), perhaps?
Hi like to ask a question on recommendation on how and which program is good for creating blockchain
hey with sqlalchemy automap is there a way to limit the reflection to specific tables to speed things up?
hello
can someone help me add tables for this code
async def score(ctx, s1=None, s2=None):
try:
s1 = int(s1)
s2 = int(s2)
except:
return
if s1 > s2:
await ctx.send("Team 1 wins!")
elif s2 > s1:
await cts.send("Team 2 wins!")
else:
await ctx.send("Its a draw!")```
Can you be a little more specific plz
yes
1sec
@tawny sail
i want to create a table to go into the database
select current games
update the wins/losses
and delete the current games
So u probably wanna have a table called games, columns can be players, wins, loses and anything else u need?
Oh
Which db
sqlite3
whats what?
It's a library which let's you connect to a database
why would i use that when i already have sqlite3?
Okay I got something else
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 u...
This tutorial will answer everything
Then what's the issue
Have u learnt how to connect to a dB and run sql commands via python?
Then u wanna do that first
i mean my friend helped me
do the other cmd
!start
it reaches into the DB gets players and adds them
to games
Watch the tutorial and learn how to connect to dB and do stuff with python
alright well, ill try again i guess i just dont understand it
Well u don't wanna rely on ur friend teaching and keeping u with just half knowledge. It better to learn the basics of sql and also sqlite3 lib, the video above
wanted 2 see if someone could help me create it
tbh its the last cmd on the bot that i need to work on
and its finished
Yea I get it but ur gonna benefit a lot more if u can learn stuff too
Rather then me giving solution :)
you're right I know. id rather have the solution though if you wanna help me on your free time let me know cuz i know i wont understand it thanks anyways
Well the thing is ur not ready to learn, if u say u won't understand then it means u don't want to, if that's the case....
!mute 294308151749246983 5d This community is here to provide help to people who want to learn python. Our members are not to be used as someone to do your bidding. Nor do we allow recruitment of any kind here for the purpose of you getting people to do things for you, with or without pay. I suggest you seriously reconsider why you're here.
:incoming_envelope: :ok_hand: applied mute to @robust tundra until 2019-11-25 07:00.
@regal rapids commentary on something that's already had been solved through moderative action isn't constructive or on topic to the channel. please avoid it.
thank you
How exactly foreign key usage impacts a database except binding entries together? Does it save space or make it process queries faster?
it prevents you from having data that falls out of sync (i.e. if you deleted the entry in the parent table it can automatically delete it, and it can prevent you from adding an entry that doesn't have a valid parent id)
for query performance it may be a good idea to create an index
Okay, I'll read about database indices. Thanks
I'm wondering if there's a way to dynamically input data into columns in sqlite3. Specifically, I want my update function to take in kwargs and update the db only for ones that are passed in. Is there a way to do this without just using a list of conditionals?
What exactly is your kwargs
Im writing a discord bot and I'm saving muterole id, modrole id, log channel id, and modlog channel id in the guild table. So I want to be able to update as many values are passed in.
def update_guild(self, guild_id, **kwargs):
self.c.execute('UPDATE Guild SET muterole_id=?, modrole_id=?, log_channel_id=?, modlog_channel_id=? WHERE guild_id=?', (guild_id))
self.conn.commit()
self.logger.log(level=logging.INFO, msg=f'Updated guild {guild_id} in database')```
dont use **kwargs
also this is blocking
use async
use optional args for your function
I suggest postgres via asyncpg library
Hi guys im working on a knowledge data base which has many documents about machine information and also information about how to fix troubleshoots which are connected with machine information. The Sources come from help desk and engineers. So a lot of unstructred data and structured data which i need to combine at the end for some ai models.
Midorina I'm a little confused. Aren't kwargs optional? Also not sure what you mean by blocking.
er, kwargs or not, you still need to detect the presence or absence of the arguments
I made a way to do it but I'm not sure if it protects against injection
something like this
def update_guild(self, guild_id, **kwargs):
dynamic_update = ','.join(f'{name}=?' for name in kwargs.keys())
query = f'update guild set {dynamic_update} where guild_id=?'
self.c.execute(query, (*kwargs.values(), guild_id))```
...how is your function being used that you need to worry about injection
are you constructing kwargs directly from user input in commands?
nah
or are you just doing update_guild(guild_id, muterole_id=whatever) one place, different field another place, etc
you don't really have to worry about injection unless it's from user input, though you could make the function more robust by checking to make sure all of the keys are valid column names
functions that call this will have static kws specific to that function
i will say the usual way to do this kind of thing
is to use an ORM and fetch the whole row as an object, make the changes locally, then save the whole row back
like there's a reason you don't see this kind of thing often
why is it that people do that more?
mainly because it's considered better to leave dynamic sql building to libraries designed to do it
ah
async with ORM, how and what libs to use? Preference sqlalchemy. Tried Databases but I just keep going in circles.
@hollow wadi Are you saying, "how do I query my database from my code"?
With flask-sqlalchemy, can I make it so whenever I try to create a child foreign key entry I also create the parent entry if it doesn't exist?
Right now I catch the integrity error and create parent in there, but maybe there's more elegant solution
Like this
@app.route('/react/', methods=['POST'])
def create_react():
data = json.loads(request.data)
if not data or not 'message_id' in data:
abort(400)
try:
react = Reaction(data)
db.session.add(react)
db.session.commit()
return 'OK', 201
except IntegrityError as e: # Foreign key violation?
db.session.rollback()
error = str(e.orig)
if 'guild' in error and 'is not present' in error:
guild = Guild(data['guild_id'])
db.session.add(guild)
db.session.commit()
create_msg()
return 'OK', 201```
which database engine you are using
Postgres
well you can do something like first insert a guild with with on conflict do nothing statement
and then insert reaction
Thought about that too, but couldn't figure out what would be better. I think it's less likely for parent not to be there, so I decided to catch the exception instead of trying to insert parent each time I'm inserting child. But since I'm not really experienced with this yet, I thought that there could be some relationship magic that can create a parent entry automatically
I have 2 servers with different hardware, 1 query with same execution plan, no disk reads and two different execution times
I'm interested in seeing what exactly is the bottleneck on the server that has slower time for this query
I'm guessing it's either cpu/ram
any idea how to find out which it is?
Might help if you list database software, OS and overview of hardware
@torn sphinx run vmstat -n -w -S m 1 | tee ~/$HOSTNAME.vmstat.dat when doing queries and analyse the output. I have written a python script to make graphs out of such data, if you search around you can find it
cpu is sy + us and ram is swpd + buff + free. But don't forget to monitor si so and bi bo as well, which is swap and general i/o.
assuming linux of course, but you didn't specify.
Hi there! Is anyone familiar with mongoDB?
i'm using mysql.connector, from python
should i do cursor = db.cursor() each time i try to read or write sth, or i can do it only once?
or could i initialise it only in my data parser class in the __init__ and each time you try to obj.read or obj.write it just uses self.cursor without redifining it
Doing a web application which should save documents, photos, audio, text (unstructured data) and also structured data. I thought that i save the videos and audio in a blob storage (azure) others should be raw. My approach is to implement this on mariaDB, anybody got some exp regarding this?
hi I was working with flask sqlalchemy on heroku...can u give me a hint on how to create tables in webapp......I currently have to create table manually before deploying app or it creashes
what do you mean with "in webapp"? you want to run migrations from your application itself?
while it's running?
I'm having trouble to set up my DB with Flask-SQLAlchemy, I'm messing up implementing my design.
I'm basically trying to link the classes User (which is also linked to Flask-Login through UserMixin) to Student, Teacher and Class.
Both Student and Teacher inherit from Users, a User can be a Teacher and/or a Student (but not both when associated with a single Class), each Class has Class.students and Class.teachers, it should be possible to access User.classes (which joins every class the User teaches and/or studies) and it should be possible to retrieve a list of Class objects separately using User.teaches and User.studies.
It seems like I have to use both Many-to-Many relationships, Joined Tables Inheritance + Polymorphic and Association Tables. Is anybody in here up for a help or can recommend a good guide for such a complicated (at least for me) design?
I'm trying to get each bit right, but I'm messing it in the very beginning.
class StudentAssociation(db.Model):
__tablename__ = 'students_x_classes_association_table'
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
class_id = db.Column(db.Integer, db.ForeignKey('class.id'), primary_key=True)
class_ = db.relationship('Class', back_populates='students')
student = db.relationship('User', back_populates='studies')
class TeacherAssociation(db.Model):
__tablename__ = 'teacher_x_classes_association_table'
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
class_id = db.Column(db.Integer, db.ForeignKey('class.id'), primary_key=True)
class_ = db.relationship('Class', back_populates='teachers')
teacher = db.relationship('User', back_populates='teaches')
# Clas definition
class User(db.Model, UserMixin):
# Table information
__tablename__ = 'user'
# Mappers
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(250), unique=True, index=True)
# Polymorfic
role = db.Column(db.String(240))
# Relationship
studies = db.relationship(StudentAssociation, back_populates='student')
teaches = db.relationship(TeacherAssociation, back_populates='teacher')
__mapper_args__ = {
'polymorphic_identity':'user',
'polymorphic_on':role
}
class Student(User):
__tablename__ = 'student'
__mapper_args__ = {
'polymorphic_identity':'student',
}
class Teacher(User):
__tablename__ = 'teacher'
__mapper_args__ = {
'polymorphic_identity':'teacher',
}
class Class(db.Model):
# Table information
__tablename__ = 'class'
# Mappers
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20))
description = db.Column(db.String(240))
students = db.relationship(StudentAssociation, back_populates="class_")
teachers = db.relationship(TeacherAssociation, back_populates="class_")
if the first line of the codeblock is ```python it'll also highlight the code for you making it better to read

I'm also not quite sure how to implement the role to vary between [], ['teacher','student'], ['teacher'] and ['student'] depending on the user :P
Thank you for the tips about formatting here :)
you're welcome.
i'll have to be clear, i don't use sqlalchemy personally, so im unsure on syntax for building the models you require. however i can at least give a brief idea as to how i'd approach it and explain kinda why
It'd be awesome if you could share your thoughts on it
are your association tables to handle the cases of many-to-many entries?
Yes, I'm actually using an Association Object, but using an Association table is an option according to https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html#association-object
okay, so it's an orm construct
rather than a real table
the idea though plays similar to my own thoughts
to give you an idea
your structure overall is heading in the right direction
as long as the schema supports the relationships without much hassle, your models should be able to retrieve related data through it's relationships
Hm, what's the name of the topic which covers what each of those different types of links mean?
I'm actually a civil engineer, so I'm not used to this diagram notation
they're just generalised relationship indicators in that model view
a line is coming from one side and goes towards the end of the line with the dot
and a solid line defines an identifying relationship, which means the foreign key that's related to the other table is also the primary key of the current table
so you'll see user has two relationships
one with teacher and one with student
both are identifying, which means you're limited to creating a PK that is the same value of an existing user ID and relates to an existing user
so there's a clear direct relationship to query student or teacher related tables through it
the dotted lines are just normal non-identifying foreign keys
so user and teacher table have a one-to-one relationship since it's identifying
and teacher to assigned class doesn't have that limit, so you can have multiple teachers in the assigned class
overall this subject would just be called something like "sql relationships"
i'm sure you'll be able to find a lot of resources on the subject
hey
i have a website here
with text that you can edit
but i want the text a user will edit in there to turn into a different color or will be underlined
for example
when the user adds additional letters, those letter need to turn into different color or need to be underlined.
any ideas on how i can do that?
to make this possible (Making the text on my webpage editable), i came here in the first place.
so i'l be able to pull this of too if its possible. I just need some advice and steps
@plain radish I'll keep digging how to implement it using SQLAlchemy, seems like there's a lot of constructs to do it that I'm not quite certain how to properly use. I appreciate the explanation, your time and effort
no probs
it helps to get a ensure you establish the sql-side of things first anyhow
once that's done you can find out later how to do equivalents with an ORM
makes searching a lot easier
also helps with connecting what a construct in an ORM is doing on the SQL side
since sometimes it can be super confusing dealing with some abstraction of a something unknown
yes, and when i submit the changes, the color needs to stay changed
oof
got a document or tutorial which cover those like stuff
uh
@torn sphinx did you mix up the channels
#web-development might be what you were after?
it has to do with both
i'm not sure how it's related to databases
i want to store the submitted data with the colors in the databse
You should ask in the webdev the formatting part and then when it comes to storing it, you should ask that part here
It's something that I've learned recently called separation of concerns
I think
ohkay
Pasting large amounts of code
If your code is too long to fit in a codeblock in discord, you can paste your code here:
https://paste.pydis.com/
After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.
hey im working with sqlite3. what im doing is selecting a collumb from the database, and using a try except if that collumn does not exist. I need to have an exception raised if the collumn doesnt exist. although the way im doing this is an injection vulnerability. i could pass it in as a proper parameter but the exception is not raised if i do it that way. anybody know a way around this?
# this is insecure but it works and is what im doing rn
c.execute(f'SELECT {"asdfasdfsadf"} FROM clans WHERE user_id = ?;', (id,))
Traceback (most recent call last):
File "<input>", line 1, in <module>
sqlite3.OperationalError: no such column: asdfasdfsadf
# this is secure but doesnt raise a "sqlite3.OperationalError: no such column" exception
c.execute('SELECT ? FROM clans WHERE user_id = ?;', ('asdfasdfsadf', id,))```
i need the exception raised somehow. or some way to know if the collumn exists or not. i also dont want some long voo doo check magic, this needs to be increadably quick.
Hi guys,
Anyone here use SQLalchemy?, I have a situation where I want to update 300 records at once, and if that record doesn't exist, insert it into the db.
I'm sure there must be a way of doing this, maybe i'm over complicating it. Thought merge might be the answer.
@runic badger your second version returns the literal string, not the value of the column
where does your column name come from? there's not really a way to do what you're asking
if you want to find out what columns a table contains you can do select * from [table name] then keep the list of columns around and just use that
anyway most database designs just use a fixed set of columns, the whole thing you're doing is very unusual
ik it is, essentialy what its actualy supposed to be for is for a chat bot with gameifacation. the collumns are supposed to be the clans. but a user may have a custom clan that is not in the database, and so it needs to be treated differently
and im gonna add an update soon for users to set there custom klans themselves so i need to make sure its secure
and yes, ik that if they make their clan "user_id" it will brake. ill remembet to filter for that
hmm i think ill just skip the exception loop and just use the hardcoded list of real clans to see if its custom or not. if not then i can assume its safe and not a rhat (becayse why would by own clans be rhats) if it is not found in the list and is custom it skips the sql and does what i want it to do.
you should just have a row for each clan
with a table joining rows and clans together
you're gonna have to do that anyway for the custom clans, so why treat real clans specially
there are 24 rows. 1st is the user id and the others are how much xp they have contributed to each clan. i need to keep a history of all xp commited per clan per user. so each new inserted row is a users record of there xp added to that clan
thats why i have it setup that way
and just tested it, checking if there clan is in the known safe and non custom clan list works great. idk why i didnt think about this before
import mysql.connector
config = {
'user': 'root',
'host': 'localhost',
'database': 'shoppy',
'raise_on_warnings': True
}
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor(buffered=True)
query = ("SELECT * FROM `items`")
print(cursor.execute(query))
cursor.close()
cnx.close()
``` I am trying to run this but it always returns None 
U missed a line dude
Results = cursor.fetchall()```
After
`cursor.execute()`
And then print results
@frozen fossil
ooh yeah
@tawny sail can u help me plz?
i have this code
sql1 = (f"INSERT INTO mons(user_id, Level, Name, HPiv, CCiv, CCDEFiv, FRiv, FRDEFiv, MGC, SPD, num) VALUES(?,?,?,?,?,?,?,?,?,?,?)")
val1 = (str(ctx.author.id), 5, arg, hpiv, CCiv, CCDEFiv, FRiv, FRDEFiv, MGC, SPD, result[1]+1)
conn.execute(sql1, val1)
sql = (f"UPDATE usr(user_id, total)")
val = (str(ctx.author.id), result[1] + 1)
c.execute(sql, val)```
i keep getting val1 = (str(ctx.author.id), 5, arg, hpiv, CCiv, CCDEFiv, FRiv, FRDEFiv, MGC, SPD, result[1]+1) TypeError: 'builtin_function_or_method' object is not subscriptable
whats result?
o
ty i forgot the parenthesis
im blind lol
fixed but i got another error
Command raised an exception: TypeError: can only concatenate str (not "int") to str```
@primal valve do u know why im getting this?
c.execute(f"SELECT user_id, total FROM usr WHERE user_id = '{ctx.author.id}'")
result = c.fetchone()
if result is None:
await ctx.send("You Haven't Started yet!")
ivs = 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
hpiv = random.choice(ivs)
CCiv = random.choice(ivs)
CCDEFiv = random.choice(ivs)
FRiv = random.choice(ivs)
FRDEFiv = random.choice(ivs)
MGC = random.choice(ivs)
SPD = random.choice(ivs)
sql1 = (f"INSERT INTO mons(user_id, Level, Name, HPiv, CCiv, CCDEFiv, FRiv, FRDEFiv, MGC, SPD, num) VALUES(?,?,?,?,?,?,?,?,?,?,?)")
val1 = (str(ctx.author.id), 5, str(arg), int(hpiv), int(CCiv), int(CCDEFiv), int(FRiv), int(FRDEFiv), int(MGC), int(SPD), int(result[1]+1))
conn.execute(sql1, val1)
sql = (f"UPDATE usr(user_id, total)")
val = (str(ctx.author.id), result[1] + 1)
c.execute(sql, val)```
thats the full block
where exactly?
val1 = (str(ctx.author.id), 5, str(arg), int(hpiv), int(CCiv), int(CCDEFiv), int(FRiv), int(FRDEFiv), int(MGC), int(SPD), int(result[1]+1))
TypeError: can only concatenate str (not "int") to str```
yea
but youre doing result[1]+1
oof
that wont work if result[i] is a string
oooooof
ty im so stupid
and
yet another error
🤦
c.execute(sql, val)
sqlite3.OperationalError: near "(": syntax error```
???????
my syntax looks gud to me
@primal valve sry for the troubleeeeeeeeeeeeeeeee ik im getting annoying
yes
sql1 = (f"INSERT INTO mons(user_id, Level, Name, HPiv, CCiv, CCDEFiv, FRiv, FRDEFiv, MGC, SPD, num) VALUES(?,?,?,?,?,?,?,?,?,?,?)")
val1 = (str(ctx.author.id), 5, str(arg), int(hpiv), int(CCiv), int(CCDEFiv), int(FRiv), int(FRDEFiv), int(MGC), int(SPD), int(total+1))
conn.execute(sql1, val1)
sql = (f"UPDATE usr(user_id, total)")
val = (str(ctx.author.id), total + 1)
c.execute(sql, val)
culorz = [0x9750C7, 0x000066, 0xA200FF, 0x0008FF]
embed = discord.Embed(color=random.choice(culorz), timestamp=message.created_at, title=f"{ctx.author[:-5]} Has Tammed A:", description=f"{arg}!")
await ctx.send(embed=embed)```
?????????????
;;;;;;;;;------;;;;;;;;;
my mysql query returns [(2,)] how would i just get the 2 as a int and not as a list
oof
is there any way to prevent stuff like this?
same id but different word should be okay, but same id and same word shouldnt
but same word and different id should be okay too
hello, maybe someone can provide me best database for big projects?
i was using mysql, but i think its old nowadays
if you want to have two columns in combination to be unique, you should create a multi column unique constraint
IMO it's postgresql, but if you want to use the exact same interface, MariaDB is the modern equivalent to mysql
@pale knoll mysql is great.
@pale knoll
Postgresql is excellent for big projects, it has a lot of features and is blazing fast
Anybody know PHP/MySQL ?
https://raizo.de/p/6YOCp.png i want to check if the item exists (with a id as input) and if it exists it should give me the whole row(name price stock etc) 
You can do a select() filtering by id, and see if the result set is empty
Depends on how you're accessing the DB
umm with mysql.connector
If youre using fetchall() for example, it would return an empty list
so if empty it returns False ?
yes
hmm thats great
There's a pitfall which is if you don't convert to list (which fetchall() does), you could be checking against a generator and that wouldn't behave as expected, but I'm sure you're fine in that regard

for result in conn.execute(SQL):
print(result)
a = conn.execute(sql)
if a: # always true
pass
vs
a = conn.execute(SQL).fetchall()
if a: # True if there were results
(working with mysql.connector)
could someone explain me what's the point of cnx.cursor ? I know that basically, we'll do all the operations from it, to fetch and write data. But when should I initialise a new cursor? Only at the beginning of my script? Or each time i interact with db i re initialise the cursor?
Or only when i change the table i'm working with?
# select if item exists
query = "SELECT * FROM `items` WHERE ID = %s"
val = (id)
exists = cursor.execute(query, val).fetchall()
if exists:
# get whole row
else:
await ctx.send("Item could not be found please contact the database administrator.")
``` alright i think this is it
Yes, but keep in mind exists would be [ .the whole row. ] (a one item list with the whole row inside
SELECT id FROM ...
i'd be greatful if someone helps me out on https://discordapp.com/channels/267624335836053506/342318764227821568/647875685175525376
@dire cedar in my expierence i only use one cursor all the time
even if switching tables
but i dont know if thats a good idea
A cursor is a single "View" into queries and results
If you have multiple cursors, you can perform multiple queries "at the same time", although multiple connections is more common and more performant
SELECT id FROM `items` WHERE ID = %s
```?
yes
ah ok
presumably your items table has an id column
You can do SELECT id, name, address FROM items to pick which columns are returned
well i use sql for my discord bot in my bot commands, and my bot events. My bot being asyncio based.
Do you advise me to use several cursors, one new each time i'm on another command/event, or only at the script beginning?
I believe it returns a row inside a list--so you can do things like row.id or row.name
I would suggest a connection pool
what is that?
pool = mysql.connector.pooling.MySQLConnectionPool(pool_name = "mypool",
pool_size = 3,
**dbconfig)
connection = pool.get_connection()
This allows you to reuse connections, but you will need to get a cursor every time as well
You'd initialize the pool when your bot starts, and whenever you need a connection grab one, and create a cursor and do querying
That way you can have multiple simultaneous connections for async usage
You might also want to look into https://github.com/aio-libs/aiomysql
oh i may need that too probaly
oh my god, thanks so much
@rich trout ```
SELECT itemname, price, stock FROM `items´ WHERE ID = 1
mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'shoppy.items´ where id = 1' doesn't exist
i am an idiot
sorry for the ping
i should use 1% of my brainpower to atleast read my querys
oh that too
[('golden_apples', 4.0, 20)]
hmm
@rich trout with the async module u linked me, would i have to initialise the cursor each coroutine?
yes
and using pool?
You would call this every time:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 42;")
print(cur.description)
(r,) = await cur.fetchone()
assert r == 42
Although I think that can inline to
async with pool.acquire() as conn, conn.cursor() as cur:
or just async with pool.acquire().cursor() as cur:
also how would i sanitize input since any user can use that command
pass things in as parameters
are pool.acquire and conn.cursor coroutines?
cursor.execute("INSERT INTO employees "
"(first_name, last_name, hire_date, gender, birth_date) "
"VALUES (%s, %s, %s, %s, %s)", ('Geert', 'Vanderkelen', tomorrow, 'M', date(1977, 6, 14)))
They are async context managers
Should be where id = %s, no?
yes
but could i do sth like:
class Data:
def __init__(self):
self.cursor = pool.acquire().cursor()
async def read(self, params):
await self.cursor.execute(...)
...
async def write(self, params):
await self.cursor.execute(...)
...
You could, yes, but that would cause your cursors to potentially interweave
but that dosent sanitize input, no?
result sets are stored in the cursor
well, i create one Data object per coroutine ONLY
The %s syntax does sanitize input
In that case maybe? but it's really best practice to use the context managers
oooh thats awesome
In that case maybe?
Wdym?
but it's really best practice to use the context managers
yeah i understand that context managers with aenter property or sth are best practise. But one thing I absolutly want is being able to control the data of a same function with my custom class. Else it's an absolute pain to redifine everything such as cursors, the pre defined arguments in "WHERE" clause (that i would parse as initiliasing the Data obj), etc...
==============
Altho i wanna be sure that doing this class won't cause me issues such as interferences with results or stuff like that, IF i initialise a new Data object per coroutine / code block of a same goal.
@rich trout
Cursors maintain their result sets until you clean them up manually, and don't always last too long (since they're specific to a connection, and if the connection dies they break IIRC), so it's usually best to create new cursors whenever you do "a new task"
If you're comfortable maintaining the proper workflow so that they work, then they will work, but it's unneccessarily risky
so ur saying that if i keep my initial intention and don't start making exception of its usage here and there, then it is optimised.
But as a whole its risky cuz i might do it at some point.
right, and there is a chance that you do something that causes an error to appear inconsistently, and there's nothing so painful to debug as something like that
@rich trout i see, could ya give me an example of somethign i do that would cause an error to appear inconsistently, besides using my class out if its function.
knowing that my object can only .write and .read in a same table
d = Data()
await d.read()
sleep(200)
# Put laptop to sleep and move it here
await d.write() # Exception happens here
or:
d = Data()
read, written = asyncio.gather(d.read(), d.write("data")) # exception: data.write() call results showed up inside d.read()'s execute call
this can happen because when d.read() await's into cursor.execute(), it's possible for asyncio to schedule the d.write() next, before getting the results, and then d.write()'s result-get, of d.reads() query result inside d.write() next
i seee
well, technically i can't really do what i said, cuz http://prntscr.com/q145vm
coroutine
unless i make the object asynchronously initiliased but at this point it doesn't have any sense any more
@dire cedar im having a hard time running mysql with discord.py can you show me a snippet of your class?
I'm working with Flask + SQLAlchemy
I have two relationships on my models.py looking like this:
class User(db.Model, UserMixin):
#....
# Relationship
studies = db.relationship("Class", secondary=students_association, back_populates='students')
teaches = db.relationship("Class", secondary=teachers_association, back_populates='teachers')
Then, I wanted to create a user.classes retrieving the result of the joined user.studies and user.teaches.
Since I currently couldn't find my way through its implementation using relationships, my workaround was just like that:
class User(db.Model, UserMixin):
#....
@hybrid_property
def classes(self):
return(self.studies + self.teaches)
Is that a good workaround or should I try something else? Obviously I can't assign or delete user.classes values by doing this.
P.S: I also added a similar @hybrid_property on my Class object such as
class Class(db.Model):
#....
@hybrid_property
def members(self):
return(self.students + self.teachers)
So I can access class.members
@rich trout are you still online?
Still about, yes
@rich trout can you help me with this class https://gist.github.com/7ada27d70bddea211823a69ac483e222
im getting a func error
and im pretty sure its not discord
What's the full error message?
let me get it real quick
embed.set_footer(text= {id})
looks rather suspicious as well
I believe this should be @commands.Command without the ()
But note how that's in python, so you'd probably want it to be text=id or similar
oh yeah im a dumbo
https://raizo.de/p/9U0R5.png ayy im a dumbo ig,btw what was suspicious ?
the text={id}
That creates a set from the contents of ID, but that function parameter takes a string
well okay
sql = "INSERT INTO `items` (`itemname`, `price`, `stock`, `id`) VALUES (%s, %s, %s, %s)"
val = (itemname, price, stock, id)
cursor.execute(sql, val)
``` this executes but dosent actually insert into the table 
why not use f strings
because it dosent help me ¯_(ツ)_/¯
query_1 = "UPDATE cats SET money = money - $2 WHERE owner_id = $1"
query_2 = "UPDATE cats SET money = money + $2 WHERE owner_id = $1"
await self.bot.db.execute(query_1, ctx.author.id, amount)
await self.bot.db.execute(query_2, member.id, amount)
how can i make this in one line?
i was thinking about variables or something
did you forget to .commit()
i dont have to do this, if its to me
asyncpg is commiting everything after functions
You might want to look into executemany(), but I was actually responding to raizo up there, my discord may have missed a few messages
ye discord is having troubles atm
@rich trout Do i need to commit if i have buffer on?
I don't see why not
@rich trout do i commit after every excute
every time you want your current transaction to be saved to the database
so if i'd fetch or select i wouldnt need that?
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: near "FROM": syntax error```
and
```css
c.execute(f"SELECT selected WHERE user_id = '{ctx.author.id}' FROM usr")
sqlite3.OperationalError: near "FROM": syntax error```
db = sqlite3.connect('mons.sqlite')
main = sqlite3.connect('users.sqlite')
conn = main.cursor()
c = main.cursor()
c.execute(f"SELECT selected WHERE user_id = '{ctx.author.id}' FROM usr")
c_result = c.fetchone()
if c_result == None:
await ctx.send("You Have to Start First!")
select = str(c_result[0])
conn.execute(f"SELECT Level, Name, HPiv, CCiv, CCDEFiv, FRiv, FRDEFiv, MGC, SPD, Total WHERE user_id = '{ctx.author.id}' and num = '{select}' FROM usr")
result = conn.fetchone()
cursor = db.cursor()
cursor.execute(f"SELECT image, hp, CC, CC_DEF, FR, FR_DEF, MGC, SPD FROM mons WHERE name = '{select}' FROM mons")
cur_result = cursor.fetchone()```
@frozen fossil help?? maybe~~ and i die for a ping~~
what is selected
hmm ok
@frozen fossil nope, not for that. Only for changes
well in the db its registered as text
ayy ty
but im using it in the code as a int
@fossil leaf well comment some lines and check what line it is
what's the problem?
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: near "FROM": syntax error
thats his problem
ew f strings
1 sec
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: near "FROM": syntax error
and
c.execute(f"SELECT selected WHERE user_id = '{ctx.author.id}' FROM usr") sqlite3.OperationalError: near "FROM": syntax error
db = sqlite3.connect('mons.sqlite')
main = sqlite3.connect('users.sqlite')
conn = main.cursor()
c = main.cursor()
c.execute(f"SELECT selected WHERE user_id = '{ctx.author.id}' FROM usr")
c_result = c.fetchone()
if c_result == None:
await ctx.send("You Have to Start First!")
select = str(c_result[0])
conn.execute(f"SELECT Level, Name, HPiv, CCiv, CCDEFiv, FRiv, FRDEFiv, MGC, SPD, Total WHERE user_id = '{ctx.author.id}' and num = '{select}' FROM usr")
result = conn.fetchone()
cursor = db.cursor()
cursor.execute(f"SELECT image, hp, CC, CC_DEF, FR, FR_DEF, MGC, SPD FROM mons WHERE name = '{select}' FROM mons")
cur_result = cursor.fetchone()```
thats my errors and cde
@fringe tiger do you know what im doing wrong?
using f strings instead of placeholders..but that's not directly related to your error
lemme see
try to reverse it
first FROM then WHERE
ok
equery = "SELECT * FROM `items` WHERE ID = %s"
row = cursor.execute(query, id).fetchall()
``` this runs fine in mysql itself when replacing %s with a int but not in py 
same error
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1
im not even inputting info im just trying to get info ;-;
WAHHHHHHHHHHHHHHHHHHHHHHHH
I THOUGHT MOVINGFROM JSON SO SQLITE WAS A GUD IDEA
😫
y
maybe this?
c.execute("SELECT selected FROM usr WHERE user_id = ?", (ctx.author.id,))
(ctx.author.id,)
c.execute("SELECT selected FROM usr WHERE user_id = ?", (ctx.author.id))
^```
it needs to be tuple
IndentationError: unindent does not match any outer indentation level```
you forgot ,
o
same error
o nvm
my indent was wrong
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: near "FROM": syntax error
same fucked uperror
usr is your table name?
ohwait
its from the other oe this time
conn.execute(f"SELECT Level, Name, HPiv, CCiv, CCDEFiv, FRiv, FRDEFiv, MGC, SPD, Total WHERE user_id = '{ctx.author.id}' and num = '{select}' FROM usr")```
ty
hmmmmmm im sorry im mega dumb how do i addmultiple/
(ctx.author.id1, ctx.author.id2, ctx.author.id3)
oof
and for each argument there needs to be a ? placeholder
inside the string
it goes in order
ty
@fringe tiger you have a clue whats going on at my snippet?
where
equery = "SELECT * FROM `items` WHERE ID = %s"
row = cursor.execute(query, id).fetchall()
Hm I don't think you need ` it's just items but that doesn't seem to be problem. This is mysql? I used %s too for placeholder, now depending on what lib/db you're using it might be different
mysql.connector
id is just a int?
try row = cursor.execute(query, (id,)).fetchall()
I'm just randomly guessing
yes
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1
oh i think i know
i tried query = ("SELECT id FROM items WHERE id = %s", id) but that gives me a tuple rror
hm I'm bad at this stuff idk why I'm helping gonna make it even worse
but query is just a string
you made it a tuple
how the hell am i making this a tuple
("SELECT id FROM items WHERE id = %s", id)
depends how you pass that querry
i fetchall
no what I meant
query = "SELECT PREFIX FROM GUILDS WHERE GUILD_ID=%s"
row = fetch_helper(query, guild_id)
is not the same as
query = ("SELECT PREFIX FROM GUILDS WHERE GUILD_ID=%s", guild_id)
row = fetch_helper(query, guild_id)
if i pass it like ```py
query = "SELECT id FROM items WHERE id = %s"
exists = cursor.execute(query, id).fetchall
exactly like that?
yes
what about ()
prob won't help but what about
exists = cursor.execute(query, (id,)).fetchall()
oke then your sql definitelly doens't like that %s
it worked for insert

# insertion into database
sql = "INSERT INTO items (itemname, price, stock, id) VALUES (%s, %s, %s, %s)"
val = (itemname, price, stock, id)
cursor.execute(sql, val)
cnx.commit()
await ctx.send(f"Record inserted.")
``` that works
will do 
@frozen fossil
exists = cursor.execute(query, (id,)).fetchall()
note the trailing comma in (id,), that must be present
Pretty sure I tried that but I’ll do that when I’m going to wake up
yep
@rich trout thanks a whole bunch for ur help yesterday
@frozen fossil so it works?
indeed
lol
if a select fails does it return None ?
what? @torn sphinx
@client.event
async def on_member_join(member):
with open ('users.json', 'r') as f:
users = json.load(f)
await update_data(users, member):
with open ('users.json', 'w') as f:
json.dump(users, f)
@client.event
async def on_message(message):
with open ('users.json', 'r') as f:
users = json.load(f)
await update_data(users, message.author):
await add_experience(users, message.author, 5):
await level_up(users, message.author, message.channel):
with open ('users.json', 'w') as f:
json.dump(users, f)
async def update_data(users, user):
if not user.id in users:
users[user.id] = {}
users[user.id]['experience'] = 0
users[user.id]['level'] = 1
async def add_experience(users, user, exp):
users[user.id]['experience'] += exp
async def level_up(users, user, channel):
experience = users[user.id]['experience']
lvl_start = [user.id]['level']
lvl_end = int(experience ** (1/4))
if lvl_start < lvl_end:
await channel.send('{} has Leveled to {}'.format(user.mention, lvl_end))
users[user.id]['level'] = lvl_end
could anyone help me
you are not following python formating rules
your problem is not at line 16, it's before that
go look up python pass
also using json to store data like that is not database problem and asking for issues
also, why is your indent level sooo off? What IDE are you using?
#....
with open('users.json', 'w') as f:
#...
@torn sphinx You added a whitespace after open
I think he copied pasted chunks of code @minor ruin , that's why the identation is odd
edug
await add_experience(users, message.author, 5):
await level_up(users, message.author, message.channel):```
do you see any issues because I do
@rich trout sorry to bother but could u explain me again why (id,) works and not id
i do believe it'd be similar as to how sqlite works with prepping its statements, it requires a tuple
eg. a sqlite3 statement cursor.execute('SELECT id FROM items WHERE id = ?', (id,))
i don't know the technicality behind why a tuple is required, but the docs i do believe stated it's for security reasons
@frozen fossil
@cinder sierra so not only id should be a tuple but every of my values?
yes
alright
say you wanted another parameter with the WHERE, like uhh, a name
exists = cursor.execute(query, (id, name)).fetchall()
that'd be all, just adding it to the tuple
if that's what you mean that is
oh yea
can somebody help me with foreign keys i dont get them
A foreign key is basically a primary key from another table
yes
It's used to ensure data integrity
it just says okay connect model1 with id of something with a model2 with id that is equal to the foreign key of the model1
so like inner joining databases right? @void otter
yh
sure
so user with id e.g. 4 is in a relationship with post with id7
@lofty summit Yeah i got that but would i reference the primary key
yes
What's the point of a managed database if you can just use is in your code directly and not pay a bunch of money?
Just copying from digitaloceans ad:
- Automated failover
- Automated and flexible scaling
- Backups
- Experienced and prompt maintenance
No chances of "I oopsed my database and lost all my clients data" or "I ran across a bug in the database server code and the database crashed, and now the app doesn't work"
These bugs do exist--some enterprise databases have issues with very high read/write combined loads, which can lead to timeouts halfway through an insertion query. If that timeout happens, the database shuts off. With a managed database this isn't an issue. Generally, if you need a managed database you should already be looking to pay for one; the idea being that managing your own database is difficult and expensive and paying someone else to do it is worth the money.
Pasting large amounts of code
If your code is too long to fit in a codeblock in discord, you can paste your code here:
https://paste.pydis.com/
After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.
Remove the try except block or print error to get a better idea
Not sure about the 15th line, need some education on that 😅
Hi there! I have a question regarding an SQLalchemy issue. Someone could please help me?
This is a many-to-many relation issue. I have difficulties to add data in it.
Hey everyone! Was wondering if anyone knows any good async MongoDB library, motor seems bit outdated tho :<
I keep getting Authentication errors when I try to load something on Heroku, I know it's bad but it's the only thing I have, so it keeps saying that it can't connect for this one file when it's looking for a col, but it works for my other code when i host it myself.
Ping me if someone responds, thanks
I get this error:
await cursor.execute(command,variables)
File "C:\Users\alexis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiomysql\cursors.py", line 239, in execute
await self._query(query)
File "C:\Users\alexis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiomysql\cursors.py", line 457, in _query
await conn.query(q)
File "C:\Users\alexis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiomysql\connection.py", line 428, in query
await self._read_query_result(unbuffered=unbuffered)
File "C:\Users\alexis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiomysql\connection.py", line 622, in _read_query_result
await result.read()
File "C:\Users\alexis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiomysql\connection.py", line 1105, in read
first_packet = await self.connection._read_packet()
File "C:\Users\alexis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiomysql\connection.py", line 593, in _read_packet
packet.check_error()
File "C:\Users\alexis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "C:\Users\alexis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; try restarting transaction')```
as i just try to delete an entry in the table
as I "DELETE FROM table_name WHERE column=value AND column2=value2"
but it happens with deletes only
not updates, not inserts
any clue?
i'd assume something is holding a lock open. you can try running the delete directly from the mysql client to ensure it's not related to your code
I have a python script that keeps submitting data to a MySQL database.
I have a TIMESTAMP column at the end I want to update with the current timestamp ONLY if a specific value in the row has changed.
Do I need to use a trigger for that? How do I detect if one value has changed?
I think I just figured it out
Now this probably isn't right but I think I'm on the right track:
BEGIN
IF NEW.price <> OLD.price
SET NEW.LastChanged = TIMESTAMP()
END```
BEGIN
IF (NEW.price <> OLD.price, NEW.LastChanged = TIMESTAMP(), NEW.LastChanged = OLD.LastChanged)
END```
is that better?
BEGIN
IF NEW.Price <> OLD.Price THEN
SET NEW.LastPriceChange = TIMESTAMP;
ELSE
SET NEW.LastPriceChange = OLD.LastPriceChange;
END IF;
END```
This didn't give me any errors, going to test
Hay, i have 2 tables that have both the same columns (id) and in one table i want to get the row based on the id of the other table could somebody please tell me how to do that
I am totally shit with databases and I think a JOIN of some soft would be best but the way I'd do it would be something like:
SELECT * FROM table1 WHERE table1.id in (SELECT id FROM table2 WHERE some conditions)```
Does this work?
return databaseCursor.fetchall(databaseCursor.execute(statement))
unrelated to the previous discussion
um you could just return the fetchall after executing..
so i cant keep it in one line?
i dont think so
okk thx
you can try
i think thats when you return a object
for i in getStatement("SELECT name FROM data WHERE x ='x'"):
r.append(i)
and get statement is
global databaseCursor
statementReturn = databaseCursor.execute(statement)
return databaseCursor.fetchall(statementReturn)
🤦♂️
I often do things like:
query = "SELECT blah blah blah"
cursor.execute(query)
data = cursor.fetchall()
return data```
I use the MySQLdb lib btw
databaseCursor.execute(statement)
data = databaseCursor.fetchall()
return data
still doesnt work
can you post your full code that creates the cursor and connects to thedata base etc
(blank out passwords etc obvs.)
I use sqlite so no pass lol
databaseConnection = sqlite.connect('Data.db')
databaseCursor = databaseConnection.cursor()
def getStatement(statement):
global databaseCursor
databaseCursor.execute(statement)
data = databaseCursor.fetchall()
return data
for i in getStatement("SELECT smth FROM dataInDB WHERE x = 'f'"):
roles.append(i)
not error
but when I print, it says <function x.<locals>.x at 0x0121E610>
it gives me a pointer
I'd go along the lines of
def getStatement(statement):
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
cursor.execute(statement)
data = cursor.fetchall()
conn.close()
return data
data = getStatement(statement)
for i in data:
print(i)```
so I cant keep everything in one line huh
I am not a Python officionado or anything but I think trying to do that is unpythonic
and softa fround upon, and makes your code look weird
but honestly I'm just some noob guy
ooo I just noticed I never closed my database connection up there
not every time, no
not as far as I'm aware
but in my example every time you called getStatement a new connection was opened and never closed
after a while I'm sure that would cause an issue lol
its not a connextion issue. Its insead of giving me the value, it spits out the pointer
thats my prob
what does your print command look like?
<function x.<locals>.x at 0x0121E610>
if that is printed it is like that looks like you are just printing a function
not what it returns
datas = []
global databaseCursor
databaseCursor.execute(statement)
data = databaseCursor.fetchall()
for i in data:
datas.append(i)
return datas
the indentation is messed up
sorry, I no longer follow you. If you're not printing it and you get that message I'm not sure what it is you're trying to do
Ok here is in what I try to use this
from discord.ext import commands
from discord.ext.commands.core import Command
import sqlite3 as sqlite
databaseConnection = sqlite.connect('Data.db')
databaseCursor = databaseConnection.cursor()
def getStatement(statement):
datas = []
global databaseCursor
databaseCursor.execute(statement)
data = databaseCursor.fetchall()
for i in data:
datas.append(i)
return datas
def hasAdminRole():
async def predicate():
roles = []
dataStatement = getStatement("SELECT adminRoles FROM serverData WHERE server='server'")
for i in dataStatement:
roles.append(i)
return roles
return commands.has_role(predicate)
@commands.command()
@hasAdminRole()
async def a(ctx, arg):
await ctx.send(arg)
your predicate is supposed to return True or False, depending on whether your check passes
not return a list of roles
Ok got it thx
Hello! I have a big problem! I can not access my server database in the pgadmin4 postgresql!!
I have changed from windows 7 to Ubuntu,I can still take/write to the database,but I can not log in from browser,I can not even restart my password in postgresql website because it is broken..can someone help me?
Anyone? ;-;
Hay, i have 2 tables that have both the same columns (id) and in one table i want to get the row based on the id of the other table could somebody please tell me how to do that
SELECT FROM table_2 WHERE ID = ( SELECT id FROM table_1 WHERE criteria=search)
You can also do a JOIN WHERE table1.column1 = table2.column_whatever
depends on your exact goal
replace = with IN in the first option to match many
@rich trout well hard to explain but i get a input that is a id and table 1 has the id in a column and i wanna get the row with the exact id so i can get the item name
@frozen fossil that would be select from table1 where id = ?, and then pass in the id
@rich trout problem is i have many of the ids
i can't tell by the way you're describing it buy you might want a join. if you can give two example tables it'd be easier to help
Ah, this can be done but looks slightly awkward
lemme open my project
cursor.execute("SELECT * FROM table1 WHERE id IN (?)", ([1, 2, 3],))
There is a possibility this doesn't work--it appears that back in 2012 the sqlite bindings did not support passing lists. I think it does now but the documentation is not specific. If that's the case, you can use executemany() across your ids
Meh, worse case is doing this:
cursor.execute(f"SELECT * FROM table1 WHERE id IN ({', '.join('?'*len(ids))})", ids)
@rich trout so wait cursor.execute("SELECT item_name FROM table1 WHERE id IN (?)", ([1, 2, 3],)) would only get me the name of all the items right?
in a
tuple?
That's a good question, I don't remember. You might end up with a list of rows that only have name, but I think you'll get a tuple of names only
You'd need to do fetchall() of course
@rich trout heyo buddy, i'm pinging u cuz you basically introduced me to aiomysql, i've been using your snippet of code for each interaction with db http://prntscr.com/q3jr1r and sometimes, espcailly when i do a lot of operations on the db on a short amount of time, i get this error
pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; try restarting transaction')
Could ya help me out on this?
https://discordapp.com/channels/267624335836053506/342318764227821568/649499152731865108
i know i could increase a variable about this lock wait timeout, but i prefer avoid ALL the errors that could follow, even given a higher timeout
i've been debugging a lot, and errors have been occuring on await cursor.commit() or await cursor.execute()
The issue is that you're performing too many operations in too short a time on the database. depending on your serialization level, each call to execute() will reserve the items in the database it needs to operate on
If it goes too long and things are still locked, it throws that error
I'm curious as to what exactly you're doing to do so many operations in a short amount of time. Perhaps executemany() could be of use? It's designed to avoid these kinds of issues
yeah i have thought of this and i guess it can fix the issue. But i'm concerned about the future proof aspect. Basically if they spam commands, i mean send a lot at the same time, many events trigger etcetra... when i'll release this to public how will the interaction with db go
it will be exactly the same as what i'm experiencing without executemany right
knowing that its for a guild with more than 7k members and they all are eager to run many commands storing data
@rich trout
well and its not a question of putting ratelimits to commands
Probably, although I do suspect it will be less intensive. Regardless, that is where you start to get into dealing with how to optimize your database queries and usage--are they accessing more rows than they need to? Should some of these commands come with a builtin ratelimit so if they're spammed they don't work too fast, etc
its just as a whole that i'm concerned
With 7k members I don't think you could expect anything more than a few hundred commands a second, even if they're all going at it
Without executemany I suspect you're over that threshhold
Perhaps looking into serialization level could help if things get quite hairy, but beyond that I'm not so familiar--too many read/writes in a second to the point you get lock errors is first a matter of cleaning up your code, then implementing optimized queries and local ratelimits, and then it's time to look into database config, optimization, and sharding, and I have very little experience with the last three. Regardless, I think you're fine as long as your code is clean and doesn't do too many queries--especially ones that hit a lot of rows
Alexis, you may consider decoupling command handler from listener
have listener shove something into RabbitMQ or like system and handler read off of it
@minor ruin i'm not entirely sure of having understood what u just said, but could ya perhaos re explain that seems interesting
Alexis, microservice it
instead of one single discord process where you are constantly worrying about async, split it out
to use bank teller example, you have a line with slow grandma, teenager, normal people, grandma gets to bank teller and lo and behold, she doesn't have required materials so instead of waiting, you set her to side and move on to next person in line.
I'm not sure due to Python GIL if you can multi thread it
well, multi thread in a way that's useful
what I'm talking about is multiple processes that are working in unison
do you have one or two commands that have a ton of SQL calls and crunching?
see about moving that, make you make flask calls or something else
not really
its just that there are some events being constantly called
like on_message
eventing still might help depending
You may consider batching those, adding them to a queue until there's enough and then executing queries that do many of them at once
anyone using Python and MongoDB? I am looking to switch from MySQL to MongoDB
Quicken, I looked into it, how are you going to run the MongoDB servers? They can be much more squirrelly then MySQL/Postgres
So we use MongoDB at work and it runs in Azure. I want to load it on Windows so I can ramp up with my skills.
i'm using pyrebase(package for firebase) and the authentication of a private key works perfectly fine on windows but when i try to do the same on linux, it gives me a RSA key format is not supported error
Quicken, we use Azure as well, have you looked at Cosmos with Mongo?
cursor.execute(f"SELECT user_id, exp, level FROM glevel WHERE guild_id = '{ctx.guild.id}' ORDER BY level DESC, exp DESC")
result = cursor.fetchall()```
I'm not sure what I'm doing wrong but it stops getting results when `level` is over `10` it will get results and order them all right but if the `level` is over `10` it just ignores them
Any help would be appreciated
ping when responded
@torn sphinx is it possible you're saving the level as a String, and therefore '10' comes after '1' in the results?
you should be using parameter-passing, not string formatting, for these things. It'll help avoid that problem
👍
Yeah it was a str making it an int fixed it
Hi I want to join two tables and select only the columns in BOTH, I thought this is what INNER JOIN did but it doesn't work.
tableA has 40 columns, tableB has 39 columns which are also in A. A has an extra column.
I want a join but only the 39 columns in common to show.
SELECT * FROM 'tableA' A INNER JOIN tableB' B USING(Symbol);
doesn't work?
Joins operate on the USING column, so an INNER JOIN selects the columns you specify, appending them together and returning all matching columns
ok so is there a way for me to do what I want?
manually selecting them like "Select A.c1, A.c2 .... A.c39, B.c1, B.c2 ... B.39" is not good enough
my other option was to just delete the column I don't care about but sqlite doesn't support removing a column lol
There is a procedure for deleting columns, it's just awkward
is it recreating the table?>
1. Create new table
2. Copy data
3. Drop old table
4. Rename new into old
I can not access my postgresql admin4 browser databases - I can not see the server list!
Help! All I did is change operating systems,though I can still take data and put it in the database
Sqlite3 sucks! It gets locked no matter what table or file,I want to switch all to postgresql
Really,nobody has had this problem? ;-;
maybe you havent described it well enough
where is you postgres running
how are you accessing it successfully, how are you accessing it when it fails
the list of what type of servers is missing? did you mean table?
All I can say is I have it downloaded on Ubuntu, I run the application,it opens and it asks me my master password (which means I am logged in in some way) and I enter it, there is a 'Server' to the left,I press on the little arrow and there should be thousands of databases,but I can see nothing,it is ALL empty,but somehow I can still access the databases through scripts..
what application
so you can access by running queries directly?
Yeah,I think I understand what you are asking 😂
Yes
I can access the information from the database
Is the app broken or something ,I do not know..
hm
😦
how important is using pgadmin4 for you? would you be willing to try another app?
You mean another database type?
ive never used pgadmin4 so i cant really help much
no
another way to access the database
You mean another app to a
oh
Sure
Why not?
I mean I kinda got used to pgAdmin4 already,BUT better anything than nothing
I am trying to install java,wait ;-;
YES,IT WORKS,THANKS
Except 1 problem 😆
why are BIGINTs weird? there are like 6 pairs of 3 numbers like
111,222,333,444,555,666 (ofc not literally like that)
Oh nevermind
When you double-click on it,it becomes a normal number
thaaaanks @smoky radish ❤️
Have a nice day
@torn sphinx thats just the thousand seperator, its so you can tell the magnitude of the number
it is a member id
one million and thirty three thousand
1,033,000
@rich trout on last thursday you send me a piece of code (cursor.execute("SELECT item_name FROM table1 WHERE id IN (?)", ([1, 2, 3],)) ) , tho lists cant be converted to a mysql type right now i am trying to make this work:
#get all ids
cursor.execute("SELECT id FROM orders WHERE userid=(%s)", (userid,))
ids = cursor.fetchall()
# iterate trough item table
cursor.execute("SELECT itemname FROM items WHERE id IN (%s)", (ids,))
await userobj.send()
You would want to go back and look at my second solution then--the one that includes a short format string within the query string
alright i will try that
ayy tuple cant be converted
is ids supposed to be a list?
List is probably not convertable either
Try something like this:
cursor.execute(f"SELECT * FROM table1 WHERE id IN ({', '.join('?'*len(ids))})", ids)
same thing :/
are you passing in ids as I mentioned there? It's different than the others for a reason
yeah i c+p'd it
cursor.execute(f"SELECT itemname FROM items WHERE id IN ({', '.join('?'*len(ids))})", ids)
lemme check
What that should do is expand into the following:
"SELECT itemname FROM items WHERE id IN (?, ?, ?, ?, ?)", (1, 2, 3, 4)
oh, that's definitely wrong
so what the hell is that
That's a list of tuples, it should be a tuple of ints
You're probably taking a result and appending it, you should be appending result.id or similar
cursor.execute("SELECT id FROM orders WHERE userid = %s", (userid,))
ids = cursor.fetchall()
print(ids)
thats what i am doing
Yeah, ids' is a list of results, and each result has one item (the id)
You'd need to rearrange that so that you have a tuple of id's
@rich trout actually i've been able to fix my stuff a whole bunch.
Indeed i'm sending A LOOOOOT of delete requests to the db in a short amount of time.
So far i've been getting some lock error timeouts...
But the fix to this is actually to make the key (from which i read, update, delete the whole data set) a primary key
cuz really i couldn't optimise more in terms of grouping into executemany, etc
...I had simply assumed it was a primary key. Whoops.
What you're seeing there is indexing
that is, all primary keys have an "index" created for their column. If you do a search for a primary key (SELECT .. WHERE key = thing) it search the index, which is really really fast. Otherwise it needs to loop through the table
You can create manual indexes too, but for your case I suspect making it a primary key was smarter :P
well i didn't know the point of indexes on sql before, since i'm really a beginner in DBs, i've only been using json so far xd
thanks for explanation mate ^^
so basically i'll make my identifiers key primary
@rich trout ((1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)) now i got this
tuples inside a tuple
Are you familiar with list comprehensions?
no i never had to
one of those would be the perfect solution here
something like, tuple(item.id for item in ids)
!listcomps
Do you ever find yourself writing something like:
even_numbers = []
for n in range(20):
if n % 2 == 0:
even_numbers.append(n)
Using list comprehensions can simplify this significantly, and greatly improve code readability. If we rewrite the example above to use list comprehensions, it would look like this:
even_numbers = [n for n in range(20) if n % 2 == 0]
This also works for generators, dicts and sets by using () or {} instead of [].
For more info, see this pythonforbeginners.com post or PEP 202.
Yes, one tuple containing only the ids you want
@rich trout wait wait is that a list with tuples and in the tuples is a empty tuple?
or what the hell am i looking at
You want this format:
ids = (1, 2, 3, 4, 5, 6, 7)
You can use tuple() and a comprehension to do something like this:
!e ```py
a = [(0,)]*6
print(a)
print(tuple(i[0] for i in a))
@rich trout :white_check_mark: Your eval job has completed with return code 0.
001 | [(0,), (0,), (0,), (0,), (0,), (0,)]
002 | (0, 0, 0, 0, 0, 0)
well that was more than enough

@rich trout ? is a placeholer, but dont i need infinite placeholders for that?
since there could be infinite ids
That's what this part is for:
({', '.join('?'*len(ids))})
It inserts N placeholders, depending on the length of ids
however in your case we might just want a join
cursor.execute("SELECT id FROM orders WHERE userid = %s", (userid,))
cursor.execute("SELECT item FROM items WHERE order IN (ids)")
becomes
cursor.execute("SELECT item FROM items WHERE order IN ( SELECT id FROM orders WHERE userid = %s)", (id,))
Nothing, then
ugh

@rich trout wait so just use the command below?
because the last command is not working
What are you trying?
cursor.execute("SELECT itemname FROM items WHERE order IN ( SELECT id FROM orders WHERE userid = %s)", (userid,)) @rich trout
And what's wrong with it?
@rich trout syntax errors
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'order IN ( SELECT id FROM orders
WHERE userid = 452088717008437249)' at line 1
i am







why does god want me to suffer
