#databases
1 messages · Page 150 of 1
You can use ODM, it will care about field names
Help me please, here is the code:
@client.command(aliases = ['balance', 'cash'])
async def bal(ctx, member: discord.Member = None):
if member is None:
await ctx.send(embed = discord.Embed(
description = f"""Баланс пользователя **{ctx.author}** составляет **{cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[0]} 🍃**"""
))
else:
await ctx.send(embed = discord.Embed(
description = f"""Баланс пользователя **{member}** составляет **{cursor.execute('SELECT cash FROM users WHERE id = {}'.format(member.id)).fetchone()[0]} 🍃**"""
))```
**Error:**
```Command raised an exception: TypeError: 'NoneType' object is not subscriptable```
You're trying to subscript ([0]) the result of fetchone. When fetchone can't find a row, it returns None.
Try not to put so much stuff into an f-string. Extract the expression into a variable.
I have already worked, the code is working completely, I just forgot to include intents
That's good. By the way, don't format queries like that, it's prone to SQL injection. You should use the formatting tools provided by your database adapter:
cursor.execute("SELECT cash FROM users WHERE id = ?", (ctx.author.id,))
Thanks I'll know)
@welcome.command()
@commands.has_permissions(manage_channels=True)
@commands.guild_only()
async def channel(ctx):
db = sqlite3.connect(db_path)
cursor = db.cursor()
cursor.execute("SELECT channel_id FROM welcome WHERE guild_id = ?",(ctx.guild.id,))
result = cursor.fetchone()
print (result)
if result is None:
sql = ("INSERT INTO welcome(guild_id, channel_id) VALUES(?,?)",(ctx.guild.id, ctx.channel.id))
await ctx.send(f"Welcome channel has been set to {ctx.channel.mention}")
elif result is not None:
sql = ("UPDATE welcome SET channel_id = ? WHERE guild_id = ?",(ctx.channel.id, ctx.guild.id))
await ctx.send(f"Welcome channel has been updated to {ctx.channel.mention}")
cursor.execute(sql)
db.commit()
cursor.close()
db.close()
Ignoring exception in command welcome channel:
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\25dch\github\pythonbot\CalcyBot.py", line 1161, in channel
cursor.execute(sql)
TypeError: argument 1 must be str, not tuple
```any idea why im getting that error?
nvm fixed it
you can do cursor.execute(*sql) instead
Whats up folks
would you recommend mysql with python for web development or another dbms? if so which library would you use to go with it
for regular db, user info, content etc
small to mid size deployment
management (backups, compatability) and security in mind
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
free, fun, interactive
is there an async postgresql driver? im using psycopg2 for now but it is not async
!pypi asyncpg
@crimson valley
thanks
I need help with this postgres, im trying to check if a value is in a row called id (which is a bigint). And if so, do something.
IF EXISTS (SELECT 1 FROM spotify_api.access_tokens WHERE id = 579646098704957460) THEN
raise notice 'yes'
end IF;
but this doesnt seem to work
Hi
Can anyone tell me
How to make database
How to edit it with sql
And is ms access good for database
what is your use case
How can I store pdf files submited through POST form into a PostgreSQL database? And what data type should I use? BYTE?
Some people would argue that you should not store such files in the database, and instead just store the path to the file which points to its location on a standard filesystem. Which path you take, and what data type to use depends on what you will do with these files, and so without knowing more about your requirements its hard to give a definitive answer. But you can read this to help you decide, as it goes through the different options, https://wiki.postgresql.org/wiki/BinaryFilesInDB
Yeah, a path to the specific file would also be a valid option. Thanks, Imma give it a read
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'username'@'localhost' (using password: YES)
how to fix this error
what kind of error is this
@setroster_command.error
async def setroster_command_error(self, ctx, exc):
if isinstance(exc, ChannelNotFound):
await ctx.send("Channel not found.")
elif isinstance(exc, IntegrityError):
await ctx.send("You are not able to have more than one active roster.")
else:
raise
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IntegrityError: UNIQUE constraint failed: roster.GuildID
On IntegrityError the bot should Send the message instead of raising the error. Does somebody knows why the error is raising?
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'username'@'localhost' (using password: YES)
how to fix this error
because d.py wraps the error inside a CommandInvokeError (you can see it in the error message discord.ext.commands.errors.CommandInvokeError: Command...), and so those isinstance checks fail
you want to get the original error from the commandinvoke error before doing those checks, like so
exc = getattr(exc, "original", exc)
Can anyone tell me if this is a valid join in PostgreSQL? What type of join would it be considered as?
It’s an inner join.
Thanks, and I'm guessing this is a valid statement?
How do you know it's an inner join if it doesn't specify that?
sorry if that's a stupid question
How do I fix?
Because it’s Sql standard. The word join and inner join are the same. You can write inner join if you want it to be more readable or clear when you have other join types.
ah, TIL! Thank you!
@client.command(aliases = ['deposit'])
async def dep(ctx, amount = None):
if amount is None:
embed = discord.Embed(
description = f"**{ctx.author.mention} введите сумму которую хотите перевести в банк**"
)
await ctx.send(embed = embed)
elif amount == 'all':
cursor.execute(f"UPDATE users SET cash = {0} WHERE id = {ctx.author.id}")
conn.commit()
cursor.execute(f"UPDATE users SET bank = bank + {int(amount)} WHERE id = {ctx.author.id}")
conn.commit()
embed = discord.Embed(
description = f"Вы успешно перевели себе на банк все коины"
)
await ctx.send(embed = embed)
elif amount < 1:
embed = discord.Embed(
description = f"**{ctx.author.mention} введите сумму больше 0**"
)
await ctx.send(embed = embed)
else:
cursor.execute(f"UPDATE users SET cash = cash - {int(amount)} WHERE id = {ctx.author.id}")
conn.commit()
cursor.execute(f"UPDATE users SET bank = bank + {int(amount)} WHERe id = {ctx.author.id}")
conn.commit()
embed = discord.Embed(
description = f"Вы успешно перевели себе на банк {amount} коинов"
)
await ctx.send(embed = embed)```
**help, the problem is in the line where there is "all"**
**Error:**
```Command raised an exception: ValueError: invalid literal for int() with base 10: 'all'```
thx
Why int(amount) when amount is equal to "all"?
Run INSERT query
INSERT INTO tbl_name (col1, col2) VALUES (15, 2);
where will i put my database name in which i want to put information
Instead of tbl_name
ok
Start with some databases tutorial
and is it possible to store files in database
Yes but you shouldn't do it
@prisma girderwhy??
It's slow and ineffective
https://sqlbolt.com/ Here is a tutorial on how to use SQL syntax
@prisma girdercan you tell me how to store a python file in a sql database
You can use BLOB type for example
https://dev.mysql.com/doc/refman/8.0/en/blob.html
What are you asking for? How to connect with MySQL database from Python?
@prisma girderyep
@prisma girderand which module should i use to give those commands
shall i use mysql connector
Idk, I am using SQLAlchemy but you can use mysql-connector-python 
!pypi mysql-connector-python
!pypi SQLAlchemy
@prisma girderwhich one is better
they're not the same thing
SQLAlchemy is an ORM
internally it uses mysql connector
for MYSQL
If you don’t have any experience with SQL you shouldn’t use SQLAlchemy and ORMs
what is orm
but it also has several other connectors it uses for other databases
Object Relational Mapping
which module is best for database management
@prisma girder Can you use sqlalchemy ORM with some kind of async or asyncpg implementation?
yes actually
they added full support since alchemy 1.4
Oh super cool
in your connection uri
you need to do postgresql+asyncpg://... though
so you tell it the driver to use
I have been exploring SQlalchemy ORM, because before I used psycopg2
I feel the ORM part is kind of nice, and I'm also exploring async so I was wondering if it can be used together
Since if I understand correcly, the methods used within async code have to be async as well for it to have any benefit
yeah
I never tried to do that yet 
with alchemy pretty much everything stays the same except the engine and sessions really. all the query generation stays the same
just have a lil read through the asyncio docs and it shows pretty much the only changes needed
ty for the tip
I just tried to replicate the Synopsis - ORM example but I get a runtime error (Event loop is closed)
Traceback (most recent call last):
File "C:\Users\Mike\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 116, in __del__
self.close()
File "C:\Users\Mike\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\Mike\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 719, in call_soon
self._check_closed()
File "C:\Users\Mike\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 508, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed```
err show code
oh wait
yeah the error is because asyncio.run abruptly closes and removes tasks when it's done
rather than asyncio.run
use
loop = asyncio.get_event_loop()
loop.run_until_complete(foo())```
that way asyncpg can do its cleanup etc...
!paste for the next time it'll be over 2000
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.
Thank you 🙂
I've got a list of flattened lists, wanting to create a list of dicts so that the row header elements correspond to each row element. What would be the method that involves the least labour?
[
[
"ID",
"Name",
"Description",
"Price",
"Stock",
"Keywords",
"Category 1",
"Category 2",
"Category 3",
"Image URL"
],
[
"001",
"Eggs",
"2 eggs fried in a pan",
"1.00",
"Unlimited",
"eggs, breakfast",
"Poultry",
"Breakfast",
"Protein",
""
],
[
"002",
"Bacon",
"Crispy bacon fried in a pan",
"1.00",
"Unlimited",
"bacon, breakfast",
"Protein",
"Breakfast",
"Protein",
""
],
[
"003",
"Sausages",
"Pan fried beef sausages",
"2.00",
"Unlimited",
"sausages, breakfast",
"Protein",
"Breakfast",
"Protein",
""
],
[
"004",
"Donut",
"Sweet donut",
"0.50",
"Unlimited",
"donut, sweet",
"Sweets",
"Deserts",
"",
""
]
]
I've got a list of flattened lists, wanting to create a list of dicts so that the row header elements correspond to each row element. What would be the method that involves the least labour?
is db.commit necessary for every command? (MySQL)
can i just add it at the end of my code?
@torn sphinx Please stick to channel topics. This is the databases topical channel, you can try out bot commands in #bot-commands
with db.cursor() as cursor:
# do something
db.commit()
Is this how a transaction looks like?
This isn't really related to #databases, but you can do it relatively easily with a dictcomp. Something like:
dct_lst = [{k:v for k,v in zip(main_lst[0], sublst)} for sublst in main_lst[1:] ]
ahh cheers mate thanks for that
In PostgreSQL, how can I organize the table by id?(I made a column named id which is serial, but when I update one value in the first row it pushes it down)
You mean you want to always keep it sorted by the ID, or only do it manually each time?
the former might be solved by a btreeindex: https://stackoverflow.com/questions/47932676/how-to-store-data-in-postgresql-already-sorted
I'll check it out, thanks
Mhm, there isn't really an answer there, the solution was to make what I already did with an id...
I still don't know how to fix that?
you mean auto increment?
Rows in a relational db are not sorted. If you want them ordered, then the only way to do is by using the order by statement in your query. If you don’t specify an order by, then the database can return it however it likes.
How will I do that
Do I need to do that every time I update?
ORDER BY id ASC
I just told you database doesn’t store ordered rows.
I have an id row(serial)
@client.command()
async def setS(ctx, statusarg = None):
if statusarg is None:
embed = discord.Embed(
description = f"**{ctx.author.mention} введите текст!**"
)
await ctx.send(embed = embed)
else:
cursor.execute(f"UPDATE users SET status = status + {statusarg} WHERE id = {ctx.author.id}")
conn.commit()
await ctx.send("Успешно!")```
**I want to make a command to change the status in the profile in the database, but something goes wrong**
What's the error
when I decide to use this command, I write 2 argument and it gives an error: ret = await coro(*args, **kwargs) File "C:\Users\Администратор\Desktop\MyServerBot\index.py", line 188, in setS cursor.execute(f"UPDATE users SET status = status + {statusarg} WHERE id = {ctx.author.id}") sqlite3.OperationalError: no such column: test
Some libraries like MySQL have autocommit. https://mysqlclient.readthedocs.io/MySQLdb.html?highlight=Autocommit#MySQLdb.connections.Connection.autocommit
it's happening because the statusarg you're passing in isn't enclosed within quotes in the query (strings should be enclosed withing quotes).
probably the best move here would be to just not use f-strings for SQL queries
how do i get database ping/latency?
Get the current time, make some fetch get the time afterwards
And than simple math
ah
start = time.time()
#Do some db action
end = time.time()
ended_in_ms = round((end - start) * 1000)```
ah tq
Hi i'm using sqlalchemy, and I'm trying to make "conditional relationship" like this :
class GuildConfiguration(Base):
__tablename__ = "guild_configuration"
id: int = Column('id', ForeignKey('guild.id'), primary_key=True)
prefix: str = Column('prefix', String, nullable=False, default="!")
commands_config: = relationship(
'FeatureConfiguration', lazy='selectin',
primaryjoin="and_(GuildConfiguration.id == foreign(FeatureConfiguration.guild_id), FeatureConfiguration.feature_type == 'command')"
)
modules_config: = relationship(
'FeatureConfiguration', lazy='selectin',
primaryjoin="and_(GuildConfiguration.id == foreign(FeatureConfiguration.guild_id), FeatureConfiguration.feature_type == 'module')"
)
guild = relationship('Guild', back_populates='configuration', lazy='selectin', uselist=False)
class FeatureConfiguration(Base):
__tablename__ = "feature_configuration"
id = Column('id', Integer, primary_key=True, autoincrement=True)
guild_id = Column("guild_id", ForeignKey('guild_configuration.id'))
feature_type: str = Column('feature_type', String, nullable=False) # command | module
But I get this warning :
sys:1: SAWarning: relationship 'GuildConfiguration.modules_config' will copy column guild_configuration.id to column feature_configuration.guild_id, which conflicts with relationship(s): 'GuildConfiguration.commands_config' (copies guild_configuration.id to feature_configuration.guild_id). If this is not the intention, consider if these relationships should be linked with back_populates, or if viewonly=True should be applied to one or more if they are read-only. For the less common case that foreign key constraints are partially overlapping, the orm.foreign() annotation can be used to isolate the columns that should be written towards. The 'overlaps' parameter may be used to remove this warning.
If anyone can help me ?
How do I get a specific data from an Array in MongoDB?
I have this code :
def get_user_data(guild_id: int, user_id: int):
results = users.find({"_id": guild_id})
for x in results:
if x["_id"] == guild_id:
result = x
else:
continue
for user in result["members"]:
print(user)
That's supposed to get the data of a specific user, and this user is in an array as shown in the image. when I print the data I get :
{'843273420799737876': {'items': [], 'balance': 0}}
{'510736807999307786': {'items': [], 'balance': 0}}
{'771829098137255976': {'items': [], 'balance': 0}}
{'797947530104668190': {'items': [], 'balance': 0}}
None
I am planning on creating a search engine with python, have been doing research for a while. I am planning to scrape the web with broad crawlers in parallel (using scrapy), and I am planning to use django for the api (rest framework). Django haystack works well with full-text searches, I would have to tokenize and optimize the crawled data before storing it in my database (haystack document with template). However, django doesn't have native support for nosql. Obviously I cannot use relational databases for writing chunks of data and keeping it fresh at the same time.
What are my options?
Any suggestions?
Not sure if this is a good place to ask this question. I've been dabbling with Discord for some months and still mostly can't understand how it works, but... I'm trying to form the question from a more narrow perspective this time.
Background: I have an old and complicated database application, though it's just personal information with a few thousand records in two primary tables (of books and authors). There's one complete version of the system in dBase and a separate Web-based front-end using PERL/CGI (under JavaScript). (It actually started on a typewriter, but one time I even programmed it with PL/C, a version of PL/I.) I'd like to port the entire thing to Python, possibly as a smartphone app. I've taken a number of Python classes, including a cumbersome SQL interface class and a fairly complicated version of Asteroids (in an unusual browser-resident subset of event-driven Python).
However the easy approach for me in my declining years is to just start with working code for a similar purpose and massage it to my twisted new purposes. (That's how the PERL got into the system, starting with a little personal contacts database.) Does that ring any bells? Someone have advice to point me at the right place to find a suitable Python source to start with?
Is there a way to store a discord embed in a database?
You'd store the contents of the embed
And how do I do that
And also how can I convert the stored contents of thr embed back to the embed whenever it is called sometimes in tbe future
You'd store all the fields of the embeds?
The title, description, url etc are all plain strings that can be stored
Make some columns, like title, description And more if you'd like. When someone creates an embed, insert values to these columns. When someone wants to call it, fetch them
Damn you anand you are fast
Oh I see
Ah ye ye..., some other servers adviced me to make a dict and save it in the db lol
Mongodb vs postgresql?
what is recommended database for economy bot other than sqlite
sqlite is simple to use, however postgresql or mysql provide much better functionality, but are significantly more complicated to use
@burnt turret I mean as in I dont need to select blah from black where blah and instead do something like bot.pool.dosomething
Oh right
You could maybe write a few functions that do queries that you use often I'd assume
But if you don't like writing SQL queries, you could check out using an ORM
!pypi orm
This is a pretty good async one
hmmm ok ok
so is this just like a module that turns my database into objects that then can do stuff
it lets you interact with your database with an object-oriented interface (I hope that definition is right)
oooh yes yes i think ill use an ORM
So you'd be writing classes and calling methods, and the ORM will convert it into SQL for you
I made an API for mongo that does the same and it was a real time saver
hmmm ok ok i understand now
thanks anand
Hi anyone knows how to connect database in pycharm?
who me?
yes
what database r u trying to connect to, it's pretty simple
MySQL
after adding my id and pass, I apply and run it. But shows error after running
is the mysql server actually running?
looks like your login details are incorrect
do you have a working database URL with all the auth in it that you use to connect normally?
but I am using the credential since long it can't incorrect
No
Can I create new user name and password here only somehow?

is it possible to store a list of string in an sqlite database
yes generally you want to make a relation between them, but just dumping the list to a string with JSON works
generally no
you can try make a new user and pass Ig
Ok thanks
How would I select a integer from a postgres database which is the highest in the column?
This is my entrypoint.sh for a docker based container. It runs everytime docker-compose is started so even the alembic migrations run everytime even if there is no change in the DB Schema. Is there any way to automate it by making revisions run only when there is a change and ignore the statement otherwise ?
pip install requests
alembic revision --autogenerate
alembic upgrade head
uvicorn app.server:app --reload --host 0.0.0.0 --port 5000
PostgreSQL sounds like the best choice for economy bot
plus, since you already used SQLite, you can make easy transition
the language is more or less the same, just with some additional things
Hello, I have an issue while importing a csv to a table in postgresql database using DBeaver. While the imported csv's columns appear correctly the data wouldn't al all. I get an empty table. Could anyone please help me fix this?
https://www.postgresql.org/docs/8.2/tutorial-agg.html very first example
I forgot to delete, got it working
Using the Max() thing
yes
alright, ty
I use it for economy, it's easy, very easy
So i am creating multiple payment methods for my site. Paypal, card, adyen, stripe. A concern i have is how to create the tables for this? Should i make one table for all gateways, or multiple tables for each specific gateway.
@jaunty galleon
Depends on what data you want to store in those tables.
If for each payment gateway you find that you need to store a lot of unique fields then, the multiple table inheritence method would work better. For this you can have a transactions table where you store the fields shared across all gateways, and then individual tables for each payment gateway, and each gateway table would have a foreign key for transaction_id, that references the transactions table.
If you find that the fields you store are common across all methods, then you can store in a single table for all payment gateways.
It is possible to do with both approaches. I have worked on applications that have had multiple gateways using both approaches. And there is nothing wrong with either approach. Its just a matter of choosing the one that suits your data best.
@trim lintel And for one recent project we did it with polymorphic structure, so it works better with the ORM we use. Note the payable type/id.
transactions table:
- id
- payable_type
- payable_id
- amount
buynet_payments:
- id
- columns unique for buynet
paypal_payments:
- id
- columns unique for paypal
mobile_payments:
- id
- columns unique for mobile
Can someone show me how to manually start a postgresql databse
does anyone know the exception sqlalchemy raises when you try to insert duplicate data in a database where the field has to be unique?
It'll likely be a programming error
ProgrammingError wraps all exception raised by the driver. The driver is what will handle a unqiue constraint failure from the db in all likelyhood
easiest way is to test
thanks for the suggestion
I... I don't know anymore 😭
createdb: error: could not connect to database template1: FATAL: password authentication failed for user "User"
This keeps happening... but I'm unsure as to why
PostgreSQL
Everytime... This is the 4th time in the past 2 years that I'm trying to learn this again... and I always get stuck at setting up postgreSQL...
Every... single... time 😟
Im assuming you made a new user then?
I don't know how...
All I did was do the setup from the wizard...
And then tried to use createdb
try do
createdb -d postgres -U postgres
actually scrap the -d bit
createdb -U postgres test i think it is
I didn't get an error. And it seemingly accepted the password
by default the only user on the DB is postgres
and by default it doesnt have a password
you should now have a database called test made
so what was the password the wizard required about then?
what password wizard?
When I ran the installation wizard for postgres, one of the things it asked for was a password... along with the port, and the locale
:o
I havent setup postgres via the wizard for a while so i cant say for certain
Alright. Thanks again
The hardest part for me was to connect to PostgreSQL locally(stupid me had to go to elephant sql) but it's really all about SQL syntax
Well, if you used JSON until now, MognoDB is probably the best option to get started with, as it's uses JSON format.
python manage.py migrate
File "D:\Programs\code\Python\webdev\everycheese\.venv\lib\site-packages\psycopg2\__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: fe_sendauth: no password supplied
Now what is this about 😟
what does you code look like?
I'd recommend just telling it to connect via a url rather than the builder tool psycopg2 gives
Trying to learn Django 😟
Using Wedge of Django as a guide...
or using sqlite db backend for now till you want to deploy
then use docker
https://docs.docker.com/samples/django/ has a pretty simple compose setup
I'm just tryna learn the basics...
THe book makes it seem so easy...
but now I'm just running into error after error >(
once you install docker it makes life much easier with this sorta thing
Right...
It's not like the book is old or anything...
so why is all this so different...
heyyy im new to pymongo and i need bit help with this
i need to know how to match the _id and then read out the cash and bank
penezenka_cislo= collection.find_one({"cash": []})
I wanna try PostgreSql
You got some examples here
i just need to know how to read numbers
dct = COLLECTION.find_one({"_id": the id})
That'll.return a dict object
like {"cash": what here?}
So:
data = COLLECTION.find_one({"_id": 506874709607186432})
print(data['cash'])
Data would be:
{"_id": 506874709607186432, "cash": 150, "banka": 350}```
Sqlite or postgre? which is easier
By my opinion, PostgreSQL
alr
It's record object return is amazing
sqlite is the easiest to setup
but postgres has much more power behind it and advantages in the long run
Yes nice, at first you'll be frustrated, but than everything will go as planned
alright, i’m gonna sleep now i’ll start working tmrw when i wake up. will try making some basic commands like prefix for each server. bye
Bye.
I think set works for that as well
query = {"_id": 506874709607186432}
data = {"$set": {"new_key": "hi"}}
await COLLECTION.update_one(query, data)
Wdym? You want to increase the number?
No, there is $inc way
query = {"_id": 506874709607186432}
data = {"$inc": {"cash": 2}}
await COLLECTION.update_one(query, data)
ohhhh
thats nice
and btw idk why but i somehow broke if i dont have a data to user, it should make one)
Wdym?
jmeno = str(ctx.guild.id)
collection = db[jmeno]
if not member:
member=ctx.author
data = collection.find_one({"_id": member.id})
penezenka_cislo= (data["cash"])
banka_cislo= (data["banka"])
if data is None:
id = member.id
post = {"_id": id, "cash": 150, "banka": 350}
collection.insert_one(post)
await ctx.channel.send('Právě sis vytvořil ůčet, hůrá!')
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'NoneType' object is not subscriptable
What line raises the error?
Well, if it is None, you are doing None['cash']
!e
print(None['cash'])```
Happened to the best of us
forgot that the cash data wasnt existing
and it cant get to the part when it makes the data
Well no, it couldn't find a doc with that member id
no its ok
@commands.command()
async def penize(self,ctx, member: discord.member = None):
jmeno = str(ctx.guild.id)
collection = db[jmeno]
if not member:
member=ctx.author
data = collection.find_one({"_id": member.id})
if data is None:
id = member.id
post = {"_id": id, "cash": 150, "banka": 350}
collection.insert_one(post)
await ctx.channel.send('Právě sis vytvořil ůčet, hůrá!')
else:
penezenka_cislo = (data["cash"])
banka_cislo = (data["banka"])
embed = discord.Embed(title=f"Peníze pana {member}")
embed.add_field(name="Peněženka", value=f"{penezenka_cislo}")
embed.add_field(name="Peněženka", value=f"{banka_cislo}")
await ctx.send(embed=embed)
my problem was the cask and bank
but this works just fine
thx so much <333
I'm using MongoDB and I have this code :
def get_user_data(guild_id: int, user_id: int):
results = users.find({"_id": guild_id})
for x in results:
if x["_id"] == guild_id:
result = x["members"]
else:
continue
data = result
return data
That should get a certain user from the list members. When i print the whole data i get this :
[{'843273420799737876': {'items': [], 'balance': 0}}, {'510736807999307786': {'items': [], 'balance': 0}}, {'771829098137255976': {'items': [], 'balance': 0}}, {'797947530104668190': {'items': [], 'balance': 0}}]
But what I want it to print is only the specific user's data. I tried doing result[str(user_id)] and result[{str(user_id)}] but none of that worked. Can someone help me? And also I get that error :
TypeError: list indices must be integers or slices, not str
one thing that concerns me is that guild_id is an int but your ids in the list seem to be strings
perhaps you need .find({"_id": str(guild_id)})?
yes you are right
i will change this
well, that's not the problem im having. I want to get the member data, not the guild
the guild data is already gotten
ah, I see
hmm, can't you use MongoDB for filtering users out of guilds, too? anyway, you seem to be comparing to guild_id
should i do
for x in data:
if x.startswith(str(user_id)):
user_data = x
?
instead of getting the key str(user_id) from the dict
i have multiple collections under one database, users, settings and shop
the guilds' members are saved in the users collection
excuse me if im mistaking tho, i'm pretty new to mongo 😅
@hexed estuary
so members is a list of dicts
oh
that's a very weird organisation you have here
the only way you can get the data of the user is by iterating over that entire list, searching for the right ID
consider instead making members itself a dict, so that looking up the data for a user is as simple as members[user_id]
The array is actually fine, if you'd make the user ID a value and not a key
Then mongodb would be able to directly query for it
The problem now is that you've kept the member ID as the key, so mongodb can't directly query for it and you need to iterate over the entire thing
I'm talking about members being an array, and each member's dictionary being something like
member_id : <id>,
items: ...,
balance: ...
Can someone help? I am using tinydb and have this error:
update()
File "E:\.Code\Python\SmallerProjects\Project14.py", line 13, in update
db.update({'age': 26}, User.name == 'Tim')
NameError: name 'User' is not defined```
This is the code:
```py
from tinydb import TinyDB, Query
from time import sleep
db = TinyDB("database.json")
def insert():
db.insert({'name': 'Tim', 'age': 26})
def clearDataBase():
db.truncate()
def update():
db.update({'age': 26}, User.name == 'Tim')
insert()
update()
print(db.all())
answered = False
while answered == False:
answer = input("Do you want to clear the database?\n\nType 'yes' if you wnat to, else type 'no'")
answer = answer.lower()
if answer == "yes":
clearDataBase()
answered = True
elif answer == "no":
print("The database has no been cleared!")
sleep(2)
answered = True
else:
print("You put in an invalid answer, you will be asked again.")
sleep(1)
Where dod you define User?
def update():
db.update({'age': 26}, User.name == 'Tim')
what args does db.update take?
you trying to treat dot notation like structs?
I mean technically possible to implement (I'm still ironing out the globals() details) but... you've still gotta implement it if you want to use it
db = TinyDB("\database.json")
What do I put so that the file is created in the same folder that the program is in?
i wouldnt need db.commit everytime i update values in my database if i have autocommit enabled right
That's what autocommit does yes
im new to the sql stuff, why is this a syntax error? ```py
self.sqlcommand(f"""
INSERT INTO VALUES accounts (
{self.email},
{self.password},
{self.username},
{self.creation_time}
);
""")
oooh wait
it has to be in quotes
And don't use f-strings, use the placeholder of your lib. See #databases message to know more.
thank you, i discovered this bug like a minute ago where there was a ' " ' in the password
How do I do so that the bot translate the whole balance to the bank with SQLite?
How do i use this in TinyDB?
db.search(User.name.exists())
hi guys
question
how do you store Your data if You don't want to make "server" ?
@fringe sentinel what kind of data you want to store
Is it user data or program data
user
If its sensitive data like password then you can use cloud services to store your data. But if it's not sensitive then store in users local device
lets say i want to gather info to gui from table or whatever then user do something or change value and push back info to modify it
use local database in this case if you don't want to save user data
first thoughts was to make "local" warehouse system
Ya that will be fine in your case
heyy small question, Im using mongoDB, how do i decrase numbers?
what numbers ?
{"$inc": {"cash": cash}}- i know how to add but how do i decrase
how to fix this
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '142.54.191.90:3306' (110 Connection timed out)
add negative amount lol
oh
like {"$inc": {"cash": -cash}}
yeah, you'd $inc a negative number to decrement
oki imma try that one
myquery = collection.find_one({"_id": ctx.author.id})
vyber = {"$inc": {"cash": {kolik}}}
vyber_banka = {"$inc": {"banka": {-kolik}}}
collection.update_many(myquery, vyber, vyber_banka, upsert=True)
why this isnt working?
bruh
comma
what is that?
hi
anyone here
im trying to get update statistics from azure data warehouse
is the modified date in sys tables enough
show the query
thats a column
not a table
oh, im wrong sorry mate
lets definetley check out the query
Hello, anyone knows how i can check if a id exist in my MongoDB database that i then can store into a variable? for now the following code is just not working because after a reboot of the bot python thinks there is nothing in the variable and thats what i need to fix. any solutions?
heyyy try looking for it:
if data is None:
post = {somestuff here}
collection.insert_one(post)
data = collection.find_one({"_id": username})
.
is this correct?
@bot.command(name="ban")
@commands.has_permissions(ban_members=True)
async def banuser(ctx, member: discord.Member, *, reason=None):
userdata = {"_id": member.id, "Username": member.display_name}
data = collection.find_one({"_id": member.id})
if data is None:
post = {userdata}
collection.insert_one(post)
print(member.id)
if member.id not in userdata:
print("Not in userdata")
bannedusers.insert_one(userdata)
elif member.id in bannedusers:
await ctx.send(f"Trying to ban the User {member.display_name}...")
try:
await member.ban(reason=reason)
await ctx.send("Banned this user Sucessfully")
except:
await ctx.send("error")
@torn sphinx
well should be fine, but why is there the if member.id not in?
or like whats the poin? Make a banned user database right?
yea
noow with the code the bot is just not responsing
okkk, well if you ban someone make for them the {"_id": member.id, "Username": member.display_name}, and if you unban them, delete the thing
that would be easier
hmm im not sure what exaclty you mean...
like make doc for them if they get banned
somewhat like this
then when they get unbanned, delete that
do you got a examplecode for that? I feel like my brain is dead now and i need to wait a bit to do better work again....
well i dont have that ://
im trying to make a economy sys and i have some problems too xD
oh okey...
collection.delete_one()
but there is simple delete one command
btw @jaunty galleon can u help me a bit?
If I'll know, sure
idk what im doing wrong, Im using pymongo and im trying to overwrite 2 values at the same time
vyber_banka = {"$inc": {"banka": kolik_banka}}
vyber = {"$inc": {"cash": kolik_cash}}
collection.update_one(data,vyber_banka)
collection.update_one(data, vyber)
it overwrite just the first update
Weird
Send data
@bot.command(name="ban")
if ctx and member ctx.author.guild_permissions.ban_members:
async def banuser(ctx, member: discord.Member, *, reason=None):
userdata = {"_id": member.id, "Username": member.display_name}
data = collection.find_one({"_id": member.id})
if data is None:
post = {userdata}
collection.insert_one(post)
print(member.id)
if member.id not in userdata:
print("Not in userdata")
bannedusers.insert_one(userdata)
elif member.id in bannedusers:
await ctx.send(f"Trying to ban the User {member.display_name}...")
try:
await member.ban(reason=reason)
await ctx.send("Banned this user Sucessfully")
except:
await ctx.send("error")
My idea
still not changed anything
heyo! just asking, how can i make it so the bot does:
Format: Discord ID | IGN | End Date | <ping>
('311383238373277697', 'NoNameWrath', '2021-06-15') @hard phoenix
('244212458670129152', 'Foetation', '2021-08-14') @forest forge
('789896054048686100', 'Wh1teLightning', '2021-06-23') @gleaming igloo
('444009219499229184', 'Robominer116', '2021-05-26') @open ocean
('448121879161602048', 'MindBlow42', '2022-05-18') @winter parrot```
but rn the bot does this:
```python
Format: Discord ID | IGN | End Date | <ping>
('311383238373277697', 'NoNameWrath', '2021-06-15') @hard phoenix
('244212458670129152', 'Foetation', '2021-08-14') @hard phoenix
('789896054048686100', 'Wh1teLightning', '2021-06-23') @hard phoenix
('444009219499229184', 'Robominer116', '2021-05-26') @hard phoenix
('448121879161602048', 'MindBlow42', '2022-05-18') @hard phoenix```
(if u dont see the difference, its basically, the numbers in discord ID should match with the <ping> so it would ping the right person)
this is sqlite3 btw
Hi guys. I need a bit of nudging to implement a feature. So, I'd like to check if a database exist in my flask app when I start the app by running python app.py. If it does not I'd like to create the database as well as create a default user with a password on the fly when the database is created. How do I go about this?
await self.bot.primedb.execute("create table if not exists userstrikes (caseid int autoincrement, guildid int, userid int, reason str, dateissued str, reminders int)")
hey im getting a syntax error near autoincremennt, can someone point it to me?
sqlite3.OperationalError: near "autoincrement": syntax error
wait to be able to use autoincrement in sql i need to have the field be a primary key?
elif amount == 'all':
cursor.execute(f"UPDATE users SET cash = {0} WHERE id = {ctx.author.id}")
cursor.execute(f"""UPDATE users SET bank = bank + {cursor.execute(f"SELECT cash FROM users WHERE id = {ctx.author.id}").fetchone()[0]} WHERE id = {ctx.author.id}""")
conn.commit()```
**how to make the bot select the entire user's balance and send this balance to the bank?**
SELECT * FROM users WHERE id = the id
And don't use f-strings, use the placeholder of your lib. See #databases message to know more.
TypeError: index 'xp' cannot be applied to Cursor instances
so....
maybe it sound horrible but....
I got a discord bot that I run on a free server which has 208 mb ram and....
I run SQLite db in it. how can I make it be fast?
it is kinda very slow
Why does my MySQL database show connection timed out??
Im new to databasing and need some help making a simple one can anyone help me?
How complicated is your form? If you can save form submissions as json, then you may not need a relational DB at all.
it might be hard to get python to perform well in a resource constrained environment. however sqlite itself is probably not the limiting factor
I'm using motor to interact with a mongodb database
What's the difference between each() and to_list()?
Hello there
o/
I have a problem
elaborate
I am trying to make a MySql database application with the Flask module. What happens is that Flask as such recognizes it, but when downloading and importing Flask MySQL it says that there is no module with that name.
These guts are so different. each is to iterate over the query results and run callback on each item. to_list is to present query results as list (instead of async generator)
what is a callback?
A custom function, you provide there. Take a look: https://motor.readthedocs.io/en/stable/api-asyncio/cursors.html?highlight=Each#motor.motor_asyncio.AsyncIOMotorCursor.each
Thanks
SELECT * FROM reactionroles WHERE gid=%s AND cid=%s AND mid=%s AND reaction=%s OR role=%s
Does the or here works like just WHERE role=%s or is it like WHERE gid=%s AND cid=%s AND mid=%s AND role=%s?
You mean if it's like:
if i == "hi" or "hii":```
In PostgreSQL, doing WHERE i = ($1) AND z = ($2) works, so it should be fine
Or did you mean something completly different?
Hmm, i dont quite no how to say what I mean, lemme google real quick
if i == "i" and i == "blah" or i == "iii":```?
Yea like that
So yeah, I am pretty sure after a or statement it'll try to detect a new True
It's hard to explain lol
yea ik haha
so I have to put gid=%s AND cid=%s AND mid=%s also after the or?
because I need to check that too for the role
I am pretty sure you do need, but not sure...
np
@flat wind
I need help with postgresql in python. this query coordinate_select = "SELECT fun_id, c1x, c1y, c2x, c2y, c3x, c3y FROM graphics_items_sorted WHERE fun_id = %s AND device_type = %s;" device_id = 'CAR-TV-102T' device_type = 'track_vector'
returns nothing when I use it like this: coordinate = cursor.execute(coordinate_select, (device_id, device_type)) and I know the values I am passing are in the database. What am I doing wrong?
@spiral knoll Well I get this error, and I was using a debugger. Traceback (most recent call last):
File "/home/carl/OneDrive/Chicago_database_experiments/database_query_test.py", line 19, in <module>
coordinate = cursor.execute("SELECT * FROM graphics_items_sorted WHERE fun_id=%s AND device_type=%s", (str(device_id), str(device_type))).fetchall()
AttributeError: 'NoneType' object has no attribute 'fetchall'
@ivory parcel I was for question on sqlite as well, encryption. I made some progress with sqlite, what would you like to ask?
i created a DB with SQLite
that i have 4 columns
names, gender, agr and ID
i wanna insert the data column by column
i will send a pic of what i got
im using a PySimpleGUI interface to insert the values
do you know how can solve this problem?
what is a blob in sqlite
are you usying psycopg?
The method returns None. If a query was executed, the returned values can be retrieved using fetch*() methods.
have you tried your sql query in the console?
open your sql console and execute the query as you would in your python script (use the actual values instead of the placeholders) and see if you get the response you expected.
Yes. I fixed it.
If I were to receive massive XML files from a vendor (why?!) and it was the only format they could send
What tools or processes would you use to get it to something more user friendly for process automation and reporting?
It’s nested XML data with 3-5 main branches that are basically tables
I'm trying to update data of only one dictionary inside a list of dictionaries, but when i use this code it changes the data of the whole list to only the dictionary given
def add_money(guild_id: int, user_id: int):
results = users.find({"_id": guild_id})
amt = get_amt(guild_id)
for x in results:
result = x
members = result["members"]
for x in members:
if x["user"]["id"] == user_id:
users.update_one(result, {
"$set": {
"members": {
"user": {
"id": user_id,
"balance": x["user"]["balance"]+amt,
"items": x["user"]["items"]
}
}
}
})
How do I update the value of a specific dictionary only?
would you recommend using mongodb for storing trillions of documents?
My use case is to do broad web crawling in parallel and continuously store web pages.
I am using django, and I see some libraries giving good support for mongodb.
As for other nosql databases, the support isn't that great.
please ping me when help, thanks!
Also, please let me know of any alternatives if you suggest any.
Yes, I wouldn't have trillions of documents when I start off, but
in my use case, documents never stop growing.
Also, I need to periodically update crawled pages.
Trillions is a big number. What is your use-case? Usually that big amount of data is stored as files and only some metadata is stored to the database.
import pymongo
from pymongo import MongoClient
cluster = MongoClient("mongodb+srv://<user>:<pwd>@cluster0.yqqvw.mongodb.net/Discord?retryWrites=true&w=majority")
db = cluster["Discord"]
collection = db["prefix"]
post1 = {"_id": "1", "prefix": "!"}
collection.insert_one(post1)
Hey @loud crane!
Uh-oh! It looks like your message got zapped by our spam filter. We currently don't allow .txt attachments, so here are some tips to help you travel safely:
• If you attempted to send a message longer than 2000 characters, try shortening your message to fit within the character limit or use a pasting service (see below)
• If you tried to show someone your code, you can use codeblocks
(run !code-blocks in #bot-commands for more information) or use a pasting service like:
Traceback (most recent call last):
File "D:\python projects\main.py", line 9, in <module>
collection.insert_one(post1)
File "D:\python projects\venv\lib\site-packages\pymongo\collection.py", line 698, in insert_one
self._insert(document,
File "D:\python projects\venv\lib\site-packages\pymongo\collection.py", line 613, in _insert
return self._insert_one(
File "D:\python projects\venv\lib\site-packages\pymongo\collection.py", line 602, in _insert_one
self.__database.client._retryable_write(
File "D:\python projects\venv\lib\site-packages\pymongo\mongo_client.py", line 1497, in _retryable_write
with self._tmp_session(session) as s:
File "C:\Program Files\Python39\lib\contextlib.py", line 117, in __enter__
return next(self.gen)
File "D:\python projects\venv\lib\site-packages\pymongo\mongo_client.py", line 1829, in _tmp_session
s = self._ensure_session(session)
File "D:\python projects\venv\lib\site-packages\pymongo\mongo_client.py", line 1816, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "D:\python projects\venv\lib\site-packages\pymongo\mongo_client.py", line 1766, in __start_session
server_session = self._get_server_session()
File "D:\python projects\venv\lib\site-packages\pymongo\mongo_client.py", line 1802, in _get_server_session
return self._topology.get_server_session()
File "D:\python projects\venv\lib\site-packages\pymongo\topology.py", line 501, in get_server_session
self._select_servers_loop(
File "D:\python projects\venv\lib\site-packages\pymongo\topology.py", line 215, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError:
How to fix this error
I am building something like a web search engine.
The web crawler constantly needs to crawl and save metadata of webpages
I wouldn't start with trillions
but, I just wanted to know if mongodb is a smooth option long-term
or else, I can do something similar to discord- start with mongodb because we have excellent support for it in django (3rd party)
and then move onto something like cassandra when mongodb cannot scale
the main problem is locking, I guess?
@slow sand
I also found papers specifically advising not to use relational databases
whats your take on this?
.
Locks are not a problem, I'd say. If this is a problem - something should be changed in the schema design. About the relational vs nosql databases - this holywar will not end never 🙂
I'd suggest to start first of all. You will change your schema many times during the development process. It is impossible to predict everything in the early stage. Probably you will need more than one database. Something like document db for the metadata + append-only fast timeseries for the append logs. and etc.
I would choose mongodb for this. It s very flexible - will fit the changing requirements well.
thanks for the response!
because i dont know any database languages, can I use Django's orm feature in the development of my discord bot, i mean, is it possible to use django to update and fetch data from my database?
ping me if you know
lxml or etree? I just had to integrate with an xml-only API at work and managed to get by just with etree
hello, i'm using django's ORM, I have a question, I tried to write a query into phpmyadmin, and it works like i've expected. However, when I'm trying to use Django ORM like x.objects.related_select('y'), Because my query join multiples tables, my request doesn't work and even if I try to use .raw(), the output doesn't work...; Is anyone can help me
there must be really strong use case to make that claim, if it's not there then it's not worth reading. If you are working with django then stick to postgresql, use json type column for non-trivial schema issues. start looking for options once you start really growing beyond few 100mn rows per table.
look for jespen database tests. mongodb specifically has failed it bunch of times. It's not a reliable store
It is possible to access your bot database with Django, but I don't know precisely how to setup the models. You could also use discord-ext-ipc to create an API to your bot.
I'm trying to delete some duplicates in a postgres db using SQLAlchemy. I'm following the example here:
https://stackoverflow.com/a/63764238
I can't get mine to work. The difference is I have an additional filter.
q = session.query(func.min(Price.id)).filter(
Price.listing_id == Listing.id,
Listing.page_id == Page.id,
Page.scrape_id == self.id,
Price.processing_level ==
processing_level).group_by(Price.listing_id)
aliased = alias(q)
q2 = session.query(Price).filter(
~Price.id.in_(aliased))
print(q2.count())
The following works, but I feel like this isn't the right way to do it:
q = session.query(func.min(Price.id)).filter(
Price.listing_id == Listing.id,
Listing.page_id == Page.id,
Page.scrape_id == self.id,
Price.processing_level ==
processing_level).group_by(Price.listing_id)
price_ids = q.all()
price_ids = [row[0] for row in price_ids]
q2 = session.query(Price).filter(
~Price.id.in_(price_ids))
print(q2.count())
Error message when I try to run the first example:
LINE 4: WHERE price.id NOT IN (SELECT min(price.id) AS min_1
^
I need to create a currency system for my discord bot, I dont want to learn any other languages for now, for now i just need something with which I can implement my currency system
so should I learn psycopg2 directly or postgres first
will I understand psycopg2 without learning postgres
you'd need to know SQL to be able to use postgres
psycopg2 is just a module for interacting with the postgres database
you'd be writing SQL queries anyways, so that would be what you should learn first
https://sqlbolt.com i'd recommend this for learning SQL
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
bruh it will take a lot of time
:<
do i have to learn sql then postgres and then psycopg?
but thats true i am not understanding anything
about postgres and psyopg2
doesnt mattter how hard i try
no, learning the basics of SQL is only a matter of hours
learning SQL is the only major part in there
really?
using the modules are pretty easy
yes. try out the tutorial I linked.
DOes Python work with Fauna
hi there
Anyone familiar with cx_oracle?
my issue: I need to be able to deploy that lib + what's needed for it to work (oracle instant client) on an AWS lambda, but the size is way tooo much (150+MB). Anyone knows how to make it lighter, or any alternative that is easier to set up without external files?
Please ping me if you answer, I might miss the answer otherwise.
whats a good database if i want it to simply save a guild id + a channel id and then have the option to delete the chanel id associated with a certain guild id
if that's all you need, no reason not to use a simple relational database like sqlite
Bit of a specialised question, but does anyone know how I can use PyCharms "Language Injection" to syntax highlight SQL strings with aiosqlite? It appears to have built-in support by default for sqlite3, but not aiosqlite. Thanks in advance!
@simple kelp https://www.jetbrains.com/help/pycharm/language-injections-settings.html#toolbar it looks doable, and sounds like a great idea
i should try configuring this myself
I've read that already, but it doesn't provide any detailed info on how to actually write custom rules.
can someone explain how to use local host db in mongo
using motor driver*
that is the right channel
its dead tho hehe
@burnt turret can u help?
What did you try? What is the problem with motor?
No difference at all from the application side. Just set the right url. The simplest way to run mongo is to use docker. Here there are a few examples: https://hub.docker.com/_/mongo/
MongoDB document databases provide high availability and easy scalability.
i want the db in my host server and i cant access to a lot of things
Hello, I'm not sure if this is the right spot for this, but I'm attempting to host a web-app online, and am using a MySQL database that's done through Google Cloud Platform's SQL. I have connected to it via Flask-SQLAlchemy, but I am having trouble actually creating the tables.
When running locally, I can just do as follows, but with my understanding, this code is not run when being deployed through Google App Engine.
if __name__ == "__main__":
db.create_all()
app.run(host='127.0.0.1', port=5000, debug=True)
The error that I'm getting through GCP is ProgrammingError: (1146, "Table 'file_info.file_indices' doesn't exist") so I'm assuming that the db.create_all() code hasn't run. The issue is, I'm not quite sure how I can run it, as it appears that I only need this to be run once in the Flask app.
Here is the dabase model:
class FileIndices(db.Model):
_id = db.Column(db.Integer, primary_key=True)
user = db.Column('user', db.String(80), unique=False, nullable=False)
filename = db.Column('filename', db.String(200), unique=False, nullable=False)
file_info = db.Column('file_info', db.String(500), unique=False, nullable=True)
file_type = db.Column('file_type', db.String(20), unique=False, nullable=False)
summary_generated = db.Column('summary_generated', db.Boolean, unique=False, nullable=False)
transcript_generated = db.Column('transcript_generated', db.Boolean, unique=False, nullable=True)
def __init__(self, user, filename, file_info, file_type, summary_generated, transcript_generated):
self.user = user
self.filename = filename
self.file_info = file_info
self.file_type = file_type
self.summary_generated = summary_generated
self.transcript_generated = transcript_generated
thanks for the reply!
https://paste.pythondiscord.com/papijihere.sql Anyone knows why it spams this error in the terminal?
code: https://paste.pythondiscord.com/vajucuruku.sql
hello how can i count items from a csv file (iris.csv) to plot it
Using what library to read the csv?
Quick question, is this formatting OK? (4 space indents, but 2 space indents for SQL string)
So use 4 space indents in the string too?
yes
it depends on how u like it
its ok
i have way large table creation methods
just live wit it lol
ok, thanks
(Also I must say that PyCharm's string language injection is godly)
I just wish the injection rules worked properly
can you guys help me with how can I send data from an existing form website to SQL with python?
I am using mysql.connector to connect to the server all working properly.
the question is how to send data through
👌
You may have to count it using something pandas. Not sure if you can do it with directly matplot but best to ask in #data-science-and-ml for better answer @zenith scarab
Does the form already have an endpoint where it posts to the server?
I just trying to figure it out so not yet
Do you mean figure the sql part or the whole process of submitting the form?
this is where I am
the python file is working
but how can I send data from the placeholder to sql?
I am sure it is super simple but I am new with python
I thought you were using some sort of framework for this.
can I do it without frameworks?
The idea of how it should work is, your sever/app has an endpoint where it can receive data. Then you give this route to the HTML form through the action attribute and so whenever you submit the form your severs endpoint will receive whatever data was submitted through the form as a request.
Yeah you should be able to although I’ve never done that with python. Try seeing the answer in this https://stackoverflow.com/questions/15965646/posting-html-form-values-to-python-script
I have created html form with text box and button
enter ur search keyword
My requirement is to pass the text box value in to my test.py code, is there any way to do it.
Please suggest me how do it.
I did use pandas and it worked now
Sure it would make it easier. But which you choose like Flask, FastApi, django depends on what you need. Try to look at the different options you have then pick one.
There is code examples here, https://dev.mysql.com/doc/connector-python/en/connector-python-examples.html
Can somone tell me how can I check what are the tables I have in my postgresql database
So, I have the following question:
I have a postgresql data volume (Docker) which is being shared with other developers in my team, but the thing is, whenever I push something into our repo, even tho the data volume is being shared, the DB tables are empty and the DB is not created at all, as it should be. With this in mind I have two questions, actually:
- How can the tables be already created inside my Docker image?
- How can the table data I have in the image running locally on my machine, be shared with the other developers?
Maybe you could use psql, open a shell and write \dt+, I think, don't quite recall the command
In other way, you could open a connection to port 5432 (default) and execute a query using a cursor, by doing SELECT * FROM information_schema.tables
okay thanks
You probably should not be pushing the volume to git.
-
Have a migration file or some script they can run to create tables locally.
-
First of all why do other developers need your data? If it’s to save time from having them populate their database with test data, then consider creating some sort of seeder which runs and populates the db with data.
They're supposed to due to FE testing purposes
I admit it's quite strange pushing volumes to git
So then make a seeder/script that populates the db.
The thing is: this "data" should be actual data from an API
Do you have any factories or faker libs that can easily create data for you? If so might make the process a little easier.
Nop, at least not at the moment tho
My app populates the DB with data retrieved from an API
Then, VueJS has a listener for making requests to the DB
Guess I'll try it out
Anyone got experience with using mysql-connector-python for writing large amounts of data?
If i connect my database in @tasks.loop() will it stay connect after initial connect or do i have to connect it after some time.
I am using postgresql
Will I mean when my discord bot launches it will run the function under @task.loop()
which will then add self.connection object
hey everyone! Can somebody tell me some free course (MOOC) about postgreSQL? I´m trying to learn databases...
Look at the pinned messages here
thanks!
Thank anand
async def limit_set(self, ctx, new_limit: int):
try:
info = antilimit.find_one(
{'guild_id': ctx.guild.id}
)
blimit = info['ban_limit']
blimit[0] = new_limit
antilimit.update_one(
{'guild_id': ctx.guild.id},
{
'$set': {
'ban_limit': blimit
}
}
)
returns - TypeError: 'int' object does not support item assignment
anyone know a fix? also idk if this would go in #discord-bots
Send full error
@torn sphinx
Yeah I didn't understand lol
what line does this happen on?
Damn anand is here problem fixed 
wdym
which line does that error happen on
It is me and saiv’s question
the blimit[0] one
ah right right
wait a minute i'm a little confused: what exactly do you want the function to do?
to change the number that is said and put it in the db
you want to set the ban_limit in the database to new_limit, or do you want to increment the value by new_limit
so you just want to set it right? why are you even getting the old value from the db in the first place?
you can just remove all that logic
ok
ok
We want the limit to save to the db, and the bot to see that that’s the limit
After said limit bans it would punish the member
yes, and the bot gets the number from the database right?
Yes
so updating it in your database means your bot "sees" that new limit too?
i don't understand why you are retrieving the old data in this case, when you just need to update
I belive we got it to work
yes
thank you
Works
and I didn't realize it was trying to read old data
God mobile sucks
guys
Hello Guys, how would a follower/following table look a like? what would be the relation between the tables and the user?
well it's a multi to multi relation, so you need an in between table to connect them all by their unique identifier
for "find_one", would it be only used for an array or does it not matter?
What do you mean for an array?
so if im trying to get my program to find something from the database would using "find_one" work even if what its trying to find isn't an array and is just one thing ?
an array that would be added into the collection ^^
nvm
findOne() - if query matches, first document is returned, otherwise null.
find() - no matter the number of documents matched, a cursor is returned, never null.
find_one returns dict object for the first document it finds by the query
You would need to create a Follow table.
CREATE TABLE followers (
follower_id, -- user_id of the the follower
followed_id, -- user_id of the the user being followed
PRIMARY KEY(follower_id, followed_id)
);
You have a composite primary key, in "followers" table and make sure that each of those columns has a foreign key referencing the user id in your users table.
And, example queries would be:
Get number of followers for a user:
SELECT COUNT(*) FROM followers WHERE followed_id = ?
Get all users following someone:
SELECT follower_id FROM followers WHERE followed_id = ?
I see, thank you very much for your help. That will really help
Hey,
I'm working with a peewee sqlite database, and I've encountered some very weird behavior and I can't seem to figure out why or how this is happening.
So I have this function. It creates a new ListItem and stores it to the database.
class List(Model):
# snip
def append(self, value):
new_item = ListItem(parent=self)
new_item.set_value(value)
new_item.save()
print("Value from object:", new_item.value)
print("Value from queried object: ", ListItem.select().where(ListItem.id == new_item.id).get().value)
>>>
Value from object: 1
Value from queried object: <class 'peewee.CharField'>
the ListItem has a property value, when reading directly off it it works. But when I query the object and then try to read off it (after saving, notice new_item.save()) it has the peewee.CharField type as a value.
Does somebody know what is happening here? And can they explain :?? All help is appreciated
what is a ListItem? is that a peewee thing?
hey guys i am stuck, can someone help me? Please ping me
**ERROR: **
Traceback (most recent call last):
File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "W:\SupportBot\bot.py", line 104, in ticket
await create_ticket_db(userid, issue)
File "W:\SupportBot\bot.py", line 65, in create_ticket_db
sql.execute("INSERT INTO SupportTickets (UserId, Issue) VALUES (?, ?)", (userid, issue))
sqlite3.IntegrityError: UNIQUE constraint failed: SupportTickets.UserId
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IntegrityError: UNIQUE constraint failed: SupportTickets.UserId```
**CODE:**
`# CREATE TICKET IN DATABASE
async def create_ticket_db(userid, issue):
# sql.execute("INSERT INTO SupportTickets (UserId, Issue) VALUES (" + str(userid) + "," + issue +")")
sql.execute("INSERT INTO SupportTickets (UserId, Issue) VALUES (?, ?)", (userid, issue))
conn.commit()`
Your error is trying to say, when you created your table you specified a unique constraint on the UserId column. Which means the same value can not exist more than once in that column. So you get this error because your inserting data that is not unique, and the user id already exists in that column.
And im not sure why you decided to add a unique constraint to the users field, as it would mean a user can't create more than one support ticket which is strange to see.
Error:
Traceback (most recent call last):
File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "W:\SupportBot\bot.py", line 106, in ticket
channelname = "ticket-" + str(await get_support_ticket_id(userid))
File "W:\SupportBot\bot.py", line 54, in get_support_ticket_id
sql.execute("SELECT TicketId FROM SupportTickets WHERE UserId = " + userid)
TypeError: can only concatenate str (not "int") to str
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Jakob\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: can only concatenate str (not "int") to str```
there is no unique data doubled
- Do you read errors? It explains it quite clearly.
can only concatenate str (not "int") to str - Make queries using the parameterised query like you did for the insert to avoid such errors.
i used that but idk how to make that differently
Its not about being doubled, your entering the same user id value.
Because your entering it twice lol like i already said
async def create_ticket_db(userid, issue):
sql.execute("INSERT INTO SupportTickets (UserId, Issue) VALUES (?, ?)", (str(userid), issue))
conn.commit()```
That doesn't tell me anything of where the function is called or how
@bot.command()
async def ticket(ctx, *, issue = None):
userid = ctx.message.author.id
if ctx.author != bot.user:
# CHECK IF TICKET IS EMPTY
if issue == None:
# ERROR - NO TICKET IS EMPTY
await ctx.send("Der Inhalt deines Tickets sollte nicht leer sein: ticket <grund>")
return
# Create Support-Ticket
await create_ticket_db(userid, issue)
# CREATE SUPPORT-TICKET-CHANNEL
channelname = "ticket-" + str(await get_support_ticket_id(userid))
await ctx.guild.create_text_channel(channelname)
# set channel permission
# send embed ticket message
else:
return```
thats the code
anyone want a nuke bot
@green raptor See the code and error it raises #bot-commands message
Theres only one reason why such an error can happen and ive explained it to you. So you'll have to figure out where its calling that code twice or where its inserting multiple times.
look at my status
😉
how can I format the string so it looks like sql text?
its very hard to read if it doesnt use idfferent colors
its pycharm btw
Say could we use databases to come up with a specific algorithm
The query is a python string. The IDE doesn't know its a database query so you cant.
Use multi line strings if you want to make it more readable
how does this guy do it?
Hes using a different IDE.
You can try looking at your IDE settings for similar thing
so its possible but not for pycharm?
pycharm can definitely do sql syntax highlighting
not in community version , https://www.jetbrains.com/pycharm/features/editions_comparison_matrix.html
yep just saw it
does vs support it for free?
ahhh yeah ok
You can try it, i never used that.
I think I should switch to visual studio :o
Extension for Visual Studio - Adds basic SQL syntax highlighting (keywords, functions and variables) to string literals.
its says yes
db = sqlite3.connect('bury.sqlite')
cursor = db.cursor()
sql1 = f"SELECT ground FROM buried WHERE guild_id = {ctx.message.guild.id}"
cursor.execute(sql1)
deleted = random.choice(cursor.fetchall()[0])
db.close()
the command for my discord bot breaks when i change the above SQL to the below SQL... why?
db = sqlite3.connect('bury.sqlite')
cursor = db.cursor()
sql1 = f"SELECT ground FROM buried WHERE guild_id = ?"
val = ctx.message.guild.id
cursor.execute(sql1, val)
deleted = random.choice(cursor.fetchall()[0])
db.close()
Hi is it okay to ask about openpyxl here?
how do i loop through users to reset their invites in a text based db file?
without using a for loop
bc that takes too long
How else would you go through everyone of them?
The slowest part is probably resetting an invite.
You can do several network calls concurrently
If you are using async way, you can do it with asyncio.gather - https://docs.python.org/3/library/asyncio-task.html#running-tasks-concurrently
actually i found out a way, my only concern now is making a leaderboard command
get_balance = f"SELECT balance FROM ID WHERE name=(%s)" #:(((((
val = (token_user, )
balance_getting = mycursor.execute(get_balance, val)
balance = mycursor.fetchall()
mydb.commit()
i have this code but the balance is only coming 0 even though it's 10
('d26783af0421f917a8aac69d59125231', '10')]
Hello. How can i increment a value in a table ?
I tried this but it doesn't seem to work
c.execute(f"SELECT warns FROM Moderation WHERE user_id = {user.id} AND guild_id = {ctx.guild.id}")
warnings = c.fetchone()
amount = warnings[0]
c.execute(f"UPDATE Moderation SET warns = {amount} + 1 WHERE user_id = {user.id} AND guild_id = {ctx.guild.id}")
warns = warns + {amount} perhaps?
or if incrementing by 1
warns = warns + 1
remove the {amount} if just increment by 1
ok that leaves me with this
# c.execute(f"SELECT warns FROM Moderation WHERE user_id = {user.id} AND guild_id = {ctx.guild.id}")
c.execute(f"UPDATE Moderation SET warns = warns + 1 WHERE user_id = {user.id} AND guild_id = {ctx.guild.id}")
Correct ?
Ok seems to be working. thanks mate
this is my first try to design a database
any hints?
data base guys where are you :d
is user_id in table XP the same as in user settings? if so then it should be a foreign key in XP as well
It keeps saying no such column
c.execute(f"INSERT INTO Moderation(user_name, user_id, guild_id, warns) VALUES ({user.display_name}, {user.id}, {ctx.guild.id}, 1)")
It thinks user.display_name is column idk why
There's a better way to do that
Smth like this
c.execute("INSERT INTO moderation(user_name, user_id, guild_id, warns) VALUES (?,?,?,?)" ,(user.display_name, user.id, ctx.guild.id, 1) )
Try this one
I just typed it roughly so tell me if you get an error
There are some complications when using those expressions
If you want you can read the documentation
this is my csv dataset anyone knows how can I remove entire lines? for example just remove the parameter of year and remove year from every single example
you want to delete a column ?
do you know how to do it then?
well, i hope ur using pandas
when u make a dataframe object, let's say df
you can say :
del df['year']
why does it have to be independent from server? does this mean there can be entries in server(XP) without a valid user_id? if yes, why? if no then current design should be improved. Relying on weak references is a big no-no
I also see a lot of columns being strings. this should be avoided. If you have definite options for possible values for a column then either use ENUM or INT with enum mapped in application layer. even after proper indexing, lookup on INT will be way faster than that of strings
I see tysm
is it a permament command that deletes it forever or do i need to reuse it everytime I want to interact with df?
it just updates ur df object for the session
I would like to have something like the following table in redis.
host name
back queue
stanford.edu
23
microsoft.com
17
As far as I know, the best way to implement this is to use redis hashes (...
is there a way to remove all of these in one command?
if you know you only want to read certain column names, you can use the usecols option in read_csv https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html#pandas.read_csv
in your case, if you want to omit certain column names, you can put those column names in a list or set and use a function:
invalid_names = {'prestige', 'widowed', 'reg16'}
def is_valid_column(colname):
return colname not in invalid_names
df = pd.read_csv('data.csv', usecols=is_valid_column)
also in the future please post your code as text, not as a screenshot
!code-block
Here's how to format Python code on Discord:
```py
print('Hello world!')
```
These are backticks, not quotes. Check this out if you can't find the backtick key.
if you really need to delete the columns after the data is loaded, you can use
invalid_names = ['prestige', 'widowed', 'reg16']
df.drop(invalid_names, axis='columns', inplace=True)
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.drop.html
also this is not really a "databases" questions - pandas questions are better asked in a regular help channel (see #❓|how-to-get-help ) or #data-science-and-ml
Why does python with MySql suck? its just too much of a headache ..
Are there any other options available to store my python application's data and retrieve when needed...(Except SQL please, i hate it already)
pls ping if u answer
What have you been having difficulty with?
Its so unreliable, like using connecter i've trying inserting data, the script runs but it aint even inserting it
Code's completely fine
Never had any issues of that kind; are you sure you've been commiting the changes as well?
and i always have to check my sql back to make sure everything's working
yeah i've been, its just i need somthing better and more reliable than a database(if there is)
mysql and mysql connector python are used widely by many thousands of developers. they definitely do have their difficulties and annoyances, but something like "the data isn't inserted when i thought it should be inserted" is usually a matter of user error
that said, personally i much prefer using postgresql and its associated python libraries (psycopg2, aiopg, asyncpg). i think postgres is a better "default" choice for most projects.
Aiomysql not being actively maintained is a real bummer 
yeah its annoying sometimes..
will definitely checkout postgres
also - if you share the code that you are struggling with, someone here might be able to help.
thanks! the community is relly helpful
Hello there, I created two Tables:
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
usertype_id = Column(Integer, ForeignKey('usertype.id'))
usertype = relationship("Usertype", back_populates="users")
name = Column(String(20), nullable=False,unique=True)
password = Column(String(60)) #wird vom Dekorator aus password.py genommen
eMail = Column(String, unique=True, nullable=False)
# Parent für User --> ein Ustertype hat n User
class Usertype(Base):
__tablename__ = 'usertype'
id = Column(Integer, primary_key=True)
type = Column(String)
users = relationship("User")
The idea is, that each user has a usertype with different possibilities on the webapp.
I'm not sure how to add the foreinkey in my form to add the data. Do I just add a number from wich I know what usertype she represent or can I call the Usertype class direct?
ok so I should make booleans to 0s and 1s?
is there a way to sort columns (like if my columns are now 1-2-3 and I want them to be 2-1-3 and is there a way to remove an unnamed column without naming it?
No. I was saying only about strings. Booleans, Datetime, jsonb or other binary columns are good as they are. Boolean is actually just a single byte, replacing that with integer would be expensive
but sqlite doesnt support booleans
@tribal cargo in general you should state what library you are using, it looks like django but you shouldn't make people guess
via 0 & 1 it does
@real cliff @torn sphinx sqlite entries aren't fixed-size anyway, use 0 and 1 for booleans. it will use an integer as small as 1 byte https://www.sqlite.org/datatype3.html
yep taht's what I thought
does mySQL support bools?
oh. Generally if you are using something like django then it will take care of it internally and replace boolean with smallest number. If you are using it raw then you don't really have option. Also, AFAIK, sqlite doesn't care about types that much anyway
i hope you at least read the long explanation i gave before. all columns have names, but the names aren't always "user friendly" (e.g. if you didn't give the column a name explicitly). you can reorder columns in various ways, but it depends on what you're trying to do. usually column ordering isn't that important anyway except when displaying the results.
ok
then I think I will stick to mysql with 0s and 1s
But I also guess that I dont have to care about file sizes since its a pretty small project and I have a heavy serv for it
nevertheless, its good to get used to efficient solution
Hello I'm a beginner in SQL and I'm doing a lab in SQL injection. I'm wondering when you send a querry like this one : "SELECT * FROM T_Client WHERE utilisateur = « Harold » AND password = «1234»" does it return a boolean ? Because a way to bypass a login form is to state that '1'='1'. Or does WHERE have a special behaviour when putting a True statement?
is there a way to have an if-else in sqlite? like i have a table with userid, character, numberofcharacters, and what im trying to do is if character not in the database, insert userid, character, numberofcharacter else numberofcharacters += 1 where userid and character = (random)
SELECT * means "select all rows", so this query will return every row for which the WHERE condition is true
not all databases have a native boolean type, sometimes it's just 1 and 0
i'd suggest you to setup a sandbox db and quickly recreate basic schemas to try these things out. It will help you more than just yes/no answer from our side 🙂
in fact i think most databases don't even let you get a "return value" from AND and OR
you'd probably have to do this on the python side. some databases like postgres have special syntax for these kinds of queries, but i don't know if sqlite does. it won't be a general-purpose if-else.
on the other hand, '1'='1' wouldn't work no? it should be a' OR 1=1; -- so that when this is injected in the login query it remains a valid query that will return something
!e ```python
utilisateur = "a' OR 1=1;"
query = f"SELECT * FROM T_Client WHERE utilisateur='{utilisateur!s}'"
print(query)
@harsh pulsar :white_check_mark: Your eval job has completed with return code 0.
SELECT * FROM T_Client WHERE utilisateur='a' OR 1=1;'
sql injection works because people don't do input escaping properly, not because sql itself has some special behavior when you append OR to a query
Thanks for the clarifications !
yeah, but one has to be aware or be able to speculate what will be the actual query after injection
look into insert + "on conflict update". that's basically what you want so no need for an if-else here
and hence using OR and -- at the end helps. -- in sql is comment, which helps you discard everything if any in the application code for that query
thank you!
It looks like you need upsert. https://www.sqlite.org/lang_UPSERT.html
I'm using Flask and sqlalchemy
i've tried doing an on conflict update, but i get a ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint error
for context
insert into userchars (userid,charobtained,charcount) values (?,?,?) on conflict(userid,charobtained) do update set charcount = charcount + 1',(ctx.author.id,obtained,1)
create table if not exists userchars (userid int, charobtained text, charcount int)
is there something im missing?
im using (userid,charobtained) as a primary key
you havent actually marked it as a primary key or unique though
Although you may treat it like it is
SQL doesnt treat it like it is unless it's marked
create table if not exists userchars (userid int PRIMARY KEY, charobtained text UNIQUE, charcount int)
is what you would need to have to be able to use that upsert statement
can i do create table if not exists userchars (userid int PRIMARY KEY, charobtained text UNIQUE, charcount int PRIMARY KEY(userid, charobtained)) instead?
Not really, that statement doesnt make much sense
mb fixed
why do you want PRIMARY KEY(userid, charobtained) instead of just having the constraint next to the column
PRIMARY KEY marks that column as being the primary key
UNIQUE adds the unique constraint
i don't think charobtained will be unique, like
one sec
this is what the table looks like, there are multiple same userid and multiple same charobtained
so i dont think either would qualify for a primary key/unique on their own? i could be wrong tho
i think the composite primary key makes perfect sense here
ah, never mind... i see the issue
you'd need a unique constraint over userid, charobtained, and charcount
actually yeah i think the primary key is what you want
CREATE TABLE userchars (
userid int,
charobtained text,
charcount int,
PRIMARY KEY (userid, charobtained)
);
INSERT INTO userchars (userid, charobtained, charcount)
VALUES (1234, 'john', 1);
INSERT INTO userchars (userid, charobtained)
VALUES (1234, 'john')
ON CONFLICT DO UPDATE SET charcount=charcount+1;
is this not what you want?
A question that came up at work just now:
database version: MariaDB 10.3.29
say we have a table like this
key | val | overwriteable
-------------------
1 | 5 | 1
2 | 8 | 0
4 | 9 | 1
Now the issue is, i want to do bulk inserts which should only replace certain rows.
say, if i do a insert like this
INSERT INTO table (key, val) VALUES (2, 9), (3, 6), (4, 5)
This exact query would fail due to a duplicate key
how would i write a query which can insert new rows (3, 6), overwrite rows that have overwriteable set to 1 (4, 5) and do nothing to rows where overwriteable is 0 (2, 8)
I imagine something like a conditional ON DUPLICATE KEY UPDATE, but i dont think thats possible.
Some more context: a row which is not overwritable would be quite rare, so one possiblitly i thought of is selecting the non-overwritable rows, then overwriting them anyways, and writing back the non-overwritable ones after to reset them to before the insert.
yeah i think thats what im looking for, but i get a syntax error near "update" insert into userchars (userid,charobtained,charcount) values (?,?,?) on conflict do update set charcount = charcount + 1'
@upper basin i did too actually, i probably misunderstood something in the sqlite docs
or maybe it needs some compiled extension
well rip i think ill just try to do it within python itself
@smoky radish does this do what you want? https://www.db-fiddle.com/f/x8sqphp26fbEUrKE3EkANS/0
An online SQL database playground for testing, debugging and sharing SQL snippets.
YES thank you
i did rewrite it as a IF instead, since thats easier to read IMO
ON DUPLICATE KEY UPDATE val= IF(overwritable = 1, Values(val), val);
Hey, im trying to store a raw dict inside of a sqlite database. I've tried making the data type blob but im getting the "Error binding parameter - of unsupported type"
Is there even a way to do this? or is there some type of work around i can do.
Who know a PyMongo?
My code is not connecting to the database
I connected everything, but the data does not appear in the database
Send your question and someone that knows will help
Hello all,
I'm trying to set up a couple functions for a discord bot, all of which with the need to interact with the same database (some functions will add data to it, others use the data). As such, this means that I'll have asynchronous functions, running in parallel, and a high chance of them sending requests to the database at the same time.
As such, would you recommend using sqlite or mysql (I would go with sqlite, since it is given as simpler to use, but doesn't use a separate server, and I'm afraid I could lose data if two functions attempt to write to the same table simultaneously).
Any advice from those more experienced than me ?
I don't think sqlite is vulnerable to problems with simultaneous writes; I think it would just block the second connection until the first finishes.
(that doesn't mean it's intended for concurrent access much, but at least it shouldn't be corrupted if you do)
It implements ACID so your good on that part.
yeah, AFAIK sqlite allows concurrent reads, but not (write + something)
It does support concurrent writes with WAL but not nearly as efficient
Hey! So I have a question. Basically, I have a table of names, and I want to insert name when my application runs. Here's an example, When a user has a name, say John, and I insert that name, I do not want to reinsert John when the application restarts. However, if John changed his name to Sam, and I recorded that, and he changed his name back to John while my application was not running, I would want to reinsert John back into the table. Creating a unique constraint on the name wouldn't do it. I'm hoping for a more efficient solution than selecting from the table and comparing the current name with the name that has the largest timestamp. (latest inserted name)
convert the dict to a string, with the json.dumps function from the JSON module
so now you can put this string into a TEXT type column
when you retrieve the data, it'll still be a string of course, so to make it a dict you use the json.loads function
I actually just stored it as a string and just used json.loads to load it back in
That's what I said :p
so yeah pretty much the same 😂
If I try to delete a tag which is not present in a table what am I suppose to get
My command
"""DELETE FROM tags WHERE LOWER(TAGS) = %s"""
I am using postgresql
Hmm
I have a question regarding Postgres using Heroku. I have a database hosted on the Heroku postgres add and I can establish a connection to it, but I can't seem to insert data into it or even run queries, just connect. DOes anyone know what can be the cause for it?
which database is best for python ?
it depends on the application you want to make, not python
whats best for webdev?
again, it depends on the application you want to make. i'd suggest doing some research on what would best suit your needs
am new to databases so i want to start with something, i dont have any project idea now
ok, personally i'd say start with sqlite since it comes with built in support in python
isit suitable for large scale projects?
read. the research part
ok, thx
You will most likely want a relational database for that. If your app has some sort of data access layer with the right level of abstractions, then it will be easy to change the database you use later on, so you might want to keep that when your creating your project.
so if we can design in a way that changing databases wont be a prblm,right?
Exactly
And its quite common that people will use sqlite for local dev and on production they will switch to something else, with the same codebase
thx
So I am using asyncpg. The following code outputs this. How can I loop over each record?
warnings = await self.client.conn.fetch("SELECT * FROM moderation WHERE type = $1 AND userid = $2 AND serverid = $3", "Warn", str(user.id), str(ctx.guild.id))
print(warnings)
[<Record id=2 type='Warn' reason=None userid='562901935267774480' serverid='12345678' date=None>, <Record id=3 type='Warn' reason='test' userid='562901935267774480' serverid='12345678' date=datetime.date(2021, 5, 21)>]
For example, If I wanted to print it to the terminal like this:
ID = 2, Reason = None
ID = 3, Reason = test
...
...
They are just objects, so you can loop over them as you would do with anything else and access that value for each of them
So like warnings[0] would output the first record?
Yes first record object, which would be <Record id=2 type='Warn' reason=None userid='562901935267774480' serverid='12345678' date=None>
And so each of those is one of these https://magicstack.github.io/asyncpg/current/api/index.html#record-objects
Or have a read of this, #databases message maybe that helps
ok that is interesting, could I use this ( warnings[0] ) to get reason?
actually wait I think I figured that out
thanks :))
i need some help setting up a database. basically, i need to store userid, multiple objects the user has, and the amount of objects each user has. the only thing i can think of is a table with create table userinventory( userid int, object text, amount int, primary key(userid, object) . Is there a better/more efficient way of doing this?
Is each object only allowed to be assigned to a single user or can the same object be given to more users?
they can be given to multiple users
theres a set pool of objects, say [a,b,c,d], anyone can have any amount of these
Ok then what you have is almost correct. Except 2 things.
Do you already have a items table?
nope
Oh
Ok then.
So you need to make an items table. In this you just store details of the item like id, name, price, ...
Then in your new user_inventory table you have 2 columns (user_id, item_id). Both of these are primary keys, and they both are foreign keys as well to their respective tables. This is basically how you achieve a many-to-many relationship which is what you need here.
the item has no details, its just a name
And you don't need to store the amount because it will be redundant, and you should only store this when you have performance problems.
As the saying goes, A man with one watch always knows the time. A man with two watches is never sure.
So then just store the id and name, but you still need its own table.
guys , how can I fix flask_sqlalchemy import ? if I have installed it but I could not import to my script.
pip show flask_sqlalchemy
Name: Flask-SQLAlchemy
Version: 2.5.1
Summary: Adds SQLAlchemy support to your Flask application.
Home-page: https://github.com/pallets/flask-sqlalchemy
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD-3-Clause
Location: c:\users\polcs\appdata\local\programs\python\python39\lib\site-packages
Requires: SQLAlchemy, Flask
Required-by:
sorry, i don't understand what you mean by this?
It is weird cuz I can import it if I am in other directories except this one that I am working in
like, all i need to store is objects a person has and the number of objects it has
in dict form itd look like {user: {"objects": {"A": 9, "B": 1, "C": 0}}}
In short its because it is a violation of database normalisation rules. You can look into data redundancy as well if you want to read more about it.
If you want to count the number of objects a user has use the COUNT() function provided by the database. Why do you need to calculate the count yourself? Let the database do it. And this way you have a single source of truth. If you store the count seperatly you can never guarantee the value you store is always correct.
Hey, I have this warnings system for my discord bot that I am creating. The warn command works fine it's just the warning command that I am having trouble with. I am trying to format the command so it looks cleaner but when I run the command the bot doesn't respond and it doesn't give me an error.
Code 👇
https://paste.pythondiscord.com/ilulimuluf.py
I am using MongoDB and PyMongo
What doesn't work exactly?
