#databases
1 messages · Page 146 of 1
my condolences for using db2
lol
k
index.html
app is my home page i.e. index.html
and I want to add another page by connecting a link from home page
But I'm stuck I don't know how to add it?
- How to add two diiffn links at the same page
2.If I add new link then how do I connect to forms.html
this is better suited for #web-development
Hello so I've an issue
My Discord Bot is on Heroku, so my files are on my Github
But, when I try to wrote a data for saving it, it didn't save it because he has to be commiting by someone
So how can make the bot editing a file on my Github and save it ?
For example somone has an amount of money
I've a .txt file with every saved current money for all users. (like a file with an id by user) So I use the open method of Python (like reading and wrote/create). The Bot since he's on Heroku can actually read and write, but if it's not saved on Github, it'll read the not edited version of the file
With Heroku you just copy all of the files and run whatever, that is why it does not edit the files on GitHub (it simply copied the files from there). I don't think GitHub allows you to do that automatically.
Oh ok
I guess it's not a very big issue, but when the bot will restart or want to create a new file, it's not saving that
So yeah that's a king disturbing ^^'
I don't think heroku supports something like that, personally I haven't used it so no idea.
You can try Linode or digital ocean
hey, how do i create a table in sqlite with python? I need some help, couldnt find good solution online
my table:
CREATE TABLE "test" ( "Message-ID" TEXT UNIQUE, "Emoji" TEXT UNIQUE, "Role" TEXT UNIQUE )
my current code:
async def create_serverroles_table(): sql.execute("CREATE TABLE "test" ("Message-ID" TEXT UNIQUE, "Emoji" INTEGER UNIQUE, Role" TEXT UNIQUE);") conn.commit()
Heroku's file system is ephemeral, you should be using a proper database solution they offer like heroku postgres
Oh I didn't know about the Heroku Postgress
I'll try to get more information about that
Hey, is this the right way to setup a MySQL search query:
host = "my_host_ip_was_pasted_here"
user = "my_username_was_pasted_here"
password = "my_password_was_put_here"
database = "personal_stats"
Anyone know why this won't work? I have the variables defined in the .env file and it connects to the database. My HWID is also in the db
import subprocess, requests, time, os, pymongo
from dotenv import load_dotenv
load_dotenv()
hwid = subprocess.check_output('wmic csproduct get uuid').decode().split('\n')[1].strip()
mongo = pymongo.MongoClient(os.environ["MONGO_DB_URL"].replace("<password>", os.environ["MONGO_DB_PASSWORD"]))
db = mongo.get_database("hwid").get_collection("HwidKeys")
def HwidKeys(ctx):
return db.find_one({"hwid"})
try:
if hwid in db:
pass
else:
print('[ERROR] HWID Not in database')
print(f'HWID: {hwid}')
time.sleep(5)
os._exit()
except:
print('[ERROR] Failed to connect to database')
time.sleep(5)
os._exit()
print("Welcome, we've found you in our database!")
input()
i tried this, can someone help me:
async def create_serverroles_table(serverid): serverid = str(serverid) sql.execute(f"""CREATE TABLE {serverid} ( first text, last text, te text )""") conn.commit()
error:
`Ignoring exception in command lol:
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:\Pybot\QCommunity\bot2.py", line 118, in lol
await create_serverroles_table(we)
File "W:\Pybot\QCommunity\bot2.py", line 27, in create_serverroles_table
sql.execute(f"""CREATE TABLE {serverid} (
sqlite3.OperationalError: near "823228011477860352": syntax error
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: OperationalError: near "823228011477860352": syntax error
`
how to find duplicates in pymongo
A database is a place you can store and access data
server IDs are most likely not valid table names
code
def delete_data():
# '''This function will delete data from database '''
con = mysql.connector.connect(host="localhost", user="root", password="private@12345", database="project")
cur = con.cursor()
cur.execute("delete from student where rollno = %s", Roll_no.get())
con.commit()
fetch_data()
con.close()
error:-
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\GoLu\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
return self.func(*args)
File "E:\discord\discord py\db\main.py", line 166, in delete_data
cur.execute("delete from student where rollno = %s", Roll_no.get())
File "C:\Users\GoLu\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor.py", line 568, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Users\GoLu\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 686, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Users\GoLu\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 573, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1
what is the problem?
should i change it to server-ID ?
You shouldn't have a table per server, you should have one table, maybe called guilds, with columns like guild_id, first, last and te
guild_id should probably be a primary key
Nah, that wouldn't allow multiple rows for the same guild
@rain plank
serverid = "ReactRole-Server-" + str(ctx.guild.id)
async def create_serverroles_table(serverid): serverid = str(serverid) sql.execute("CREATE TABLE " + serverid + "(Message-ID text, Emoji text, Role text)") conn.commit()
error:
`Ignoring exception in command lol:
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:\Pybot\QCommunity\bot2.py", line 114, in lol
await create_serverroles_table(we)
File "W:\Pybot\QCommunity\bot2.py", line 27, in create_serverroles_table
sql.execute("CREATE TABLE " + serverid + "(Message-ID text, Emoji text, Role text)")
sqlite3.OperationalError: near "-": syntax error
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: OperationalError: near "-": syntax error
`
idk whats wrong there
can anyone help me 🤔
The table should be called serverroles or something like that, and it should have four columns: message_id, emoji, role, serverid
Is messageid unique and cant be same on two servers?@rain plank
message ids are always unique, in fact, all IDs ("snowflakes") in discord are unique. Like literal snowflakes, they are always unique
I am trying to save information from a sqlite table to a csv. I have the db connected and configured to receive information from entry on form i made with tkinter. However, everytime I click my save to excel button instead of just appending the most recent entry to the csv file it reappends the whole table under the existing info.
Pls
why I am getting error?
because it's invalid PgSQL
why where that syntax error
PostgreSQL != MySQL
what's wrong
your CHARACTER SET ...
Ok so what to change
just delete it
@rain plank ty bud i will do one table
Was stupid idea to make so many tables
Didnt know they are unique!
Thanks i learned something important today <3
you're missing the parameters of the Datetime type
postgres allows several varients
Which one?
date, timestamp?
that depends on what you're doing tbh
read what the description of each type
and see which is most suited to you
!close
Opening files
The built-in function open() is one of several ways to open files on your computer. It accepts many different parameters, so this tag will only go over two of them (file and mode). For more extensive documentation on all these parameters, consult the official documentation. The object returned from this function is a file object or stream, for which the full documentation can be found here.
See also:
• !tags with for information on context managers
• !tags pathlib for an alternative way of opening files
• !tags seek for information on changing your position in a file
The file parameter
This should be a path-like object denoting the name or path (absolute or relative) to the file you want to open.
An absolute path is the full path from your root directory to the file you want to open. Generally this is the option you should choose so it doesn't matter what directory you're in when you execute your module.
See !tags relative-path for more information on relative paths.
The mode parameter
This is an optional string that specifies the mode in which the file should be opened. There's not enough room to discuss them all, but listed below are some of the more confusing modes.
'r+' Opens for reading and writing (file must already exist)
'w+' Opens for reading and writing and truncates (can create files)
'x' Creates file and opens for writing (file must not already exist)
'x+' Creates file and opens for reading and writing (file must not already exist)
'a+' Opens file for reading and writing at end of file (can create files)
what data types do you have for your table?
Hello their, i use sqlalchemy for my db but i have a problem, when i'm connecting on my flask page, it save my data but it say that: duplicate key value violates unique constraint "users_pkey"
I would like to check it but it doesn't work... Can you help me?
# TODO: Rework as scheduler
def db_updater(self):
for u in self.s.query(Users).all():
self.s.merge(
Users(discriminator=u[0], email=u[1],
id=u[2], name=u[3], verified=u[4])
)
When i run my code it return that: TypeError: 'Users' object is not subscriptable
Can someone help me?
Are you sure that you have mapping for Users class?
Hum, i don't know how ^^'
You should check it because it could be a reason 
How can i do that?
I have single file database.py with all tables definitions and mappings
I don't know your project
I have a single file too @prisma girder
Here you have documentation: https://docs.sqlalchemy.org/en/13/orm/mapping_styles.html
Are you using declarative or classical mappings?
Hum sorry what's declarative mappings?
declarative
Can you show whole error output (with backtrace)?
I think error come here
My model:
Base = declarative_base()
class Users(Base):
__tablename__ = 'users'
discriminator = Column(String(250), nullable=False, unique=True)
email = Column(String(250), nullable=False, unique=True)
id = Column(BigInteger, primary_key=True)
name = Column(String(250), nullable=False, unique=True)
role = Column(SmallInteger, default=USER.USER)
status = Column(SmallInteger, default=USER.NEW)
verified = Column(Boolean, default=False, nullable=False)
Okay, I expected some console output 😂
Hey @golden warren!
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:
Do you have Base.metadata.create_all(engine)?
Yes
Hmm, hard to say what is going on 
Have you any idea to check rows value?
I mean that I like to debug interactively (with access to code) 🙂 How about echo option?
Do you have any output of working queries?
Is this class works somewhere?
Yes my class work but not that function.
Same for queries, it work
Strange
Since it's breaking at the first one after your original select argument I'd say it's just a syntax error
Try a line break after "SELECT"
@glossy flume
it didnt break for 'NAME_OF_SCHOOL'
Right because it's on the same line
make 'SELECT' on its own line and put all the columns and aliases in one big block
weird
what's the data type of saftey_icon?
idk why that would matter but it might
I'm really just guessing
lol
VARCHAR
My hunch is that it has something to do with that block of arguments rather than with 'safety_icon' itself
Try putting a different argument as the second one in the list
or try putting safety_icon first
same errors 😢
weird
Delete the table
jk
ill try this
same error! wtff
lemme see what you did
What does it say when you click 'learn more about this error'?
seems to check out tho
you're sure that column didn't get removed from your database somehow?
lol
Can you refresh the view and it's still there?
its in the database
it's all in one table?
yea, would you like me to send you the csv file?
no, I'm really not sure how to fix it I'm just troubleshooting based on what it says in the docs you linked
lol
" " s solved it
why doesn't the first one need it?
I'm making a bot using discord.py. I need to store the same few attributes of data for a few hundred people, and I'm also going to be creating a web portal (Django) that will be used to manually modify entries in the database
Should I use JSON for this, or MongoDB?
Or, something better than either of those, if applicable
Things might have changed, but when I last used Django I don't think it had the greatest MongoDB support
Yeah, about that
Should I be worried about how the database is updated?
What happens if the bot tries to update the database at the same time as a user on the web portal
That's what transactions are for
I know very little about web development
I found the Django documentation for transactions though
Thanks
That's more of a database thing than a web-dev thing
data1 = collection.find({},{"discordId":user_data['id'],"guilds":[{'id':guild_id, "owner"}]})
how can i know the "owner" information without define it?
how to filter date
I want to get all the table names that contain a specific column. I want to check the value of that specific column too. How do i do this?
db = sqlite3.connect('databases/tags.db')
c = db.cursor()
c.execute(f"SELECT * FROM sqlite_master WHERE column = {column_name}")
data = c.fetchall()
This is what I've come up so far and yes it doesn't work. How do I get the table names and the column values?
How can I check my mysql database table is empty?
SELECT EXISTS (SELECT 1 FROM table); EXISTS returns 1 if the subquery returns any rows, otherwise 0
Hi. Can aiopg also handle mariadb / mysql?
In SQLAlchemy if I have a self-referential table like:
class TreeNode(Base):
__tablename__ = 'tree'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('tree.id'))
name = Column(String(50), nullable=False)
children = relationship(
'TreeNode', cascade="all",
backref=backref("parent", remote_side='TreeNode.id')
)
Is there a nice way to get the full tree of a given TreeNode? Or perhaps depth information?
Perhaps something which would allow:
node = session.query(TreeNode).first()
node.full_tree()
# returns string like name of node > name of child > name of grandchild etc etc
node.depth()
# returns how deep in a tree this node is
if db.execute('''SELECT link_articulo FROM articulos WHERE link_articulo = ?''', (url)) == 0:```
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 77 supplied.
sqlite is taking my parameter url (it's a string) and thinks that every char is a different binding
I have a list of rows. I want to insert them all into a sqlite database, and, if there are rows the primary key of which is already in the database, then this row should just be ignored (the current value in the database left as-is).
This, I believe, can be done with a INSERT OR IGNORE INTO statement. But I have another requirement - I want to get back, for each row, whether that insertion succeeded. How can I do that?
can somebody help me with an Entity–relationship model
Inserting how? Executemany function?
hello i need help in mysql
I'm sure this is common, mssql server local getting actively refused to connect. What should I also be doing? Tried like 4 different mysql modules all throw the refused it error
Group is a keyword so don’t use that for column name
ohh thank you very much
i am new to mysql , so i dont know
If you really want to then you can escape it, but best to avoid using those kind of names .
ohh ok
you r using which os ?
This is on a windows box fyi
I have a mac os for work, and windows at home
But doesn’t really matter, it’ll be same everywhere
Is the server running?
Yes
How can you be sure?
u installed mysql through the terminal ?
on mac ?
On Mac it’s with docker
I'm logged into the sql manager studio. Services show it running
Ok so are you using sql server or mySql?
They are two different things, in your question you mentioned both
i am using pop os (ubuntu based ) so i installed mysql server thru the terminal
sudo apt install mysql-server
What?
i installed mysql-server
I was answering question of another user, not you
ohh ok
😔 sry
@bot.listen()
async def on_message(message):
liczbaa = 300
liczba += 1
print(liczba)
``` How to save accidents worked in replit?
@cosmic wave wrong channel?
how do i delete a specific id from a db with tinydb (yes laugh all you want, i only need to store maximum 5 numbers per id, unless you have a better one for me to use)
current db looks like {"_default": {"1": {"id": "781", "walksanator": "2", "test": "2", "test2": "1"}}}
and i want to remove the ['_default']['1']
well since all the information is test/dummy data
anyone got another db i could use (offline please)
that allows me to store a id and a username:choice dictionary in it
any specific reason you want to use document oriented DB rather than SQL @harsh dew ?
it looks like your data structure is dictionary with well defined and not nested structure, at least in the specific example you show
Hey! This is what I'm doing with a discord bot database but I can't get it to list each task which is a column name in the database. Due to there being multiple columns I can't figure out how to do it.
tasks = await self.client.db.fetchrow("SELECT task FROM todo WHERE userid = $1", ctx.author.id)
join_tasks ="\n".join([f"**{num}.** `{tasks}`" for num, taskthing in enumerate(tasks, start=1)])
I want to get every value of task from the database and put it into join_tasks basically.
beacuse everyone says "dont use json DBs"
so that is the reason you want to use one?
and i think it will be something like
{781:{"walksanator":"2", "test":"2","test2":"1"}}
so just use a json db
i am using tinydb right now
which is a json db
yeah
but do you have a specific reason you want to use document oriented (json) DB over relational/sql?
{781:{"walksanator":"2", "test":"2","test2":"1"},{454:{"walksanator":"4", "test":"1","test":"4"}}}
that would be a example of a filled out db
so it shouldn't be hard to write my own simple db system
well, everyithng depends on your goals I think
i am baisically storing votes
because simply put it is
{voting_id:{username:vote_option,username:vote_option,username:vote_option}}
Yeah, I mean what's you end goal?
If you want to try to write your own data managemnt systems, that's cool. But if it is just a building block of a bigger app than I would opt for using SQL
If I have a postgres column with the datetime data type can I insert a string like '2021-01-02' or how does the data have to be formatted?
My question is can I use a string
ISO 8601 Data elements and interchange formats – Information interchange – Representation of dates and times is an international standard covering the exchange of date- and time-related data. It is maintained by the Geneva-based International Organization for Standardization (ISO) and was first published in 1988 with updates in 1991, 2000, 2004 ...
try string, if that doesn't work import datatime
builtin library
Yeah I imported datetime and it's giving me a string as an output that's why I was wondering
if you print the datatime object it will output something readable, but it's not a string
Ok I looked again and I'm actually casting it to a string so that I can concatenate it to another string
Ok sorry trying to figure this out, hoping someone has a lot of postgres experience. I'm getting a lot of time series data and the time values for each one is represented in the UNIX msec format. So I'm using datetime to convert the milliseconds to a date, and then adding the date to a string to be inserted into a postgres database
Is this the best way to do it or is there something I can do like just insert the msec value into the database and then postgres converts it for me?
I'm auto-generating a lot of tables and auto-inserting a lot of data so I was trying to just get everything correct during the insert operation
@bright hound There is a helper function to_timestamp() to which you can pass the unix timestamp, and will convert it for you when it inserts.
And postgres is quite flexible in the way it accepts the format for the date. You can enter it using the standard ISO format which someone linked above or see this link for other formats, https://www.postgresql.org/docs/13/datatype-datetime.html#DATATYPE-DATETIME-INPUT
How i can teach json for discord.py
What do you mean?
Hmm, good question. The solution I ended up using is:
def insert_games(games: List[Game]) -> List[bool]:
res = []
con = sqlite3.connect(database_path)
with con: # transaction
for game in games:
exists = con.execute("SELECT game_id from games where game_id=?", (game.game_id,)).fetchone()
if not exists:
con.execute("insert into games(game_id, game) values (?,?)",
(game.game_id, game))
res.append(True)
else:
res.append(False)
con.close()
return res
- performing a
SELECTbefore everyINSERTto check whether the row should be inserted. I suppose what I want to know now is what the most performant solution for the same result would be.
(Game is a complicated nested class that via adapters and converters gets (de)serialized to and from TEXT.)
Hey, I know this query is sorta messy and doesn't work at the moment. but I have two questions. One, is there a way to inject column names without having to format the string? Two, I'm getting the error asyncpg.exceptions.DatatypeMismatchError: argument of AND must be type boolean, not type double precision Where am I going wrong?
query = """
INSERT INTO userstatus (user_id, last_changed)
VALUES ($1, (SELECT EXTRACT(epoch from NOW())))
ON CONFLICT (user_id)
DO UPDATE SET {0} = userstatus.{0} + (SELECT EXTRACT(epoch from NOW()) - userstatus.last_changed)
AND last_changed = (SELECT EXTRACT(epoch from NOW()))
WHERE userstatus.user_id = $1;
""".format(bstatus)
would it be better to just pass the current unix timestamp as a parameter? which one is faster?
What are you trying to achieve with the AND?
You're trying to set multiple columns when there's a conflict?
Hello! Yes exactly. I replaced the AND with , and got no errors
about injecting however, the formatted string gives me the shivers, but I couldn't think of another way
Yeah, it needs to be a comma.
In my experience, they don't allow using variables as column names. I've used and seen others use string formatting
It's acceptable if you're taking the column names from a constant rather than from user input
Yeah the column name is preset by another function.
Lastly, is it faster to use the epoch from now() timestamp or just insert time.time() as a parameter?
You can avoid the extra select query and use insert or ignore, and then check the cursor.lastrowid or cursor.rowcount attributes that can tell you if it was successful or not. https://www.python.org/dev/peps/pep-0249/#cursor-attributes
However, generally for large lists the most performant is to use executemany, rather than execute in a for loop. Although in this case the attributes I mentioned before won’t be available. So you have to check manually, if those rows were inserted.
No idea, but I don't imagine it's a significant difference, especially at a low volume of queries.
If you want to know you'd have to benchmark it.
Yeah thats the thing. I'll try timing them both later. but I plan to be inserting this query quite a bit. Basically this will batch insert user status changes into the db on a 2 second interval.
So if the number of users go up.. then it could make a difference.
if self.status_batch:
async with self.batch_lock:
for data in self.status_batch.items():
user_id = data[1]["user_id"]
bstatus = data[1]["bstatus"]
query = """
INSERT INTO userstatus (user_id, last_changed)
VALUES ($1, (SELECT EXTRACT(epoch from NOW())))
ON CONFLICT (user_id)
DO UPDATE SET {0} = userstatus.{0} + (SELECT EXTRACT(epoch from NOW()) - userstatus.last_changed),
last_changed = (SELECT EXTRACT(epoch from NOW()))
WHERE userstatus.user_id = $1;
""".format(bstatus)
await self.bot.cxn.execute(
query,
user_id
)
self.status_batch.clear()
would it be better to use an executemany statement and a generator instead of iterating through the "data"
I imagine that it would be better performance, but that's not something I've benchmarked.
yes it will give you better performance, especially if your inserting in bulk since less round trips to the server
Alrighty I'll change it. Is there a better way to bulk insert? generally iterating through all the data seems taxing no?
What do you mean? Executemany wouldn't be called inside a loop
My bad. Got confused there for a second
Sorry for ping, but one quick question. Since I'm basing the query based off of the values in the "data". How could I executemany? Wouldn't I always have to define the query string before the list?
You pass the list of data into the executemany function, with the query.
executemany(“query”, list_of_data)
You can define the query string wherever you like, as long as it reaches that function
This way Each tuple from the list of data would get passed to the query string.
right but the issue I’m having is that the query string depends on the data
each iteration would insert into a different column
depending on the data
Here I format the string to insert into the right columns
Right then you can’t use that
yeah unfortunate. I suppose I could insert into all of the columns.. but insert null into specific ones? but that might make it worse
Not sure what your working on here, but null columns like that are a hint of table structure that could be better designe d
yeah good point. So here’s what I thought of to track status times. When the user changes status, I insert the time they changed and their previous status.
the length of time they had their previous status
and on first insert, I just insert the time stamp
and the times per status go into the 4 columns. online offline idle dnd
Am I missing a much better way?
here’s my table structure
+-------------+-----------------------------+----------------+-------------+
| column_name | data_type | column_default | is_nullable |
+-------------+-----------------------------+----------------+-------------+
| user_id | bigint | None | NO |
| online | real | 0 | NO |
| idle | real | 0 | NO |
| dnd | real | 0 | NO |
| past | real | None | YES |
| startdate | timestamp without time zone | None | YES |
+-------------+-----------------------------+----------------+-------------+
Yeah can’t read any code you send on mobile, 😃
But how many users does your program have, or do you expect
How will this data you store be used later on?
currently 350k. Hopefully much more in the future
status usage commands. I’ll turn the data into graphs with PIL
I see, so what’s the purpose of this. I still don’t quite understand.
And If I’m correct you want to store the users current status, and details of their past status?
yes
So then why do you have columns for each status type?
I want to show user online times while providing some details on what percentage of the online time was on which status
So to show that dnd was say 20 percent of the online time.
I see so each of those columns for status acts as a counter then
I haven’t made the graphs or made it pretty but this basically
yes
all the times are in seconds
Ok then this structure you have currently seems fine.
Alrighty. So not worth trying to rewrite it so i can use executemany?
I mean with I use batches and inserting per iteration seems like it just defeats the whole purpose
Yeah you can still use executemany, just if nothing incremented for a status then it’ll be 0.
If that makes sense
Alright good luck.

hello anyone know about python
Hey all, I'm wondering about the best way to integrate a database.
I want a database for my DnD game. I can use Python to generate the data and set parameters, but I don't know how to access the database in a friendly way.
I want to test myself and learn. I'd like other people to be able to use it. I'd like to showcase it in my portfolio.
I've done a bit of HTML and CSS, using Flask (or similar) could "be the front-end" although web access isn't entirely necessary for me, or I could write up something for cmd2. A GUI with tkinter is also possible, but I'm far less confident.
I'd be looking at (as an incredibly rough estimate) around 5 areas of data, each one containing 40 tables, of 30 columns each, containing 5-10k entries. 30-60 million pieces of data seems like an obscene amount, but it might not be far from what is required.
Where do I start/Is there anything I'm missing?
Would there be a best choice out of PostgreSQL, MariaDB or ArangoDB in this scenario?
Is making a front-end just a part of database creation?
Should my front-end be in one of the Python GUIs or HTML, or something else?
Thanks in advance.
I'm trying to insert an image into a sqlite database, but I keep getting the error self.BlackHole.database.execute(query)
sqlite3.OperationalError: near "<": syntax error
Code:
grid = Image.new("RGB", (300, 300), "white")
bytes = io.BytesIO()
grid.save(bytes, "PNG")
bytes.seek(0)
query = f"INSERT INTO PlaceGrids(guild,grid) VALUES({ctx.guild.id},{bytes}) ON CONFLICT(guild) DO UPDATE SET grid = {bytes}"
self.BlackHole.database.execute(query)
hey can anyone tell me how to convert user data into csv file that saves permanently (like when i run this, the data saves in a csv but when i run it a second time the data in the csv file i got the first time disappears and in its place the data entered the second time is saved)
df = pd.DataFrame(columns=["Day", "Date", "time"])
parts = int(input("Enter the number of days: "))
for _ in range(parts):
ed = input("Enter the Day = ")
td = input("Enter the Date = ".format(ed))
ps = input("Enter Time = ".format(ed))
df1 = pd.DataFrame(data=[[ed,td,ps]],columns=["Day", "Date", "Time"])
df = pd.concat([df,df1], axis=0)
df.index = range(len(df.index))
print (df)
data = np.genfromtxt("heatmap.csv", delimiter=",")
plt.imshow(data, cmap='hot', interpolation='nearest')
plt.show()
data = np.genfromtxt("heatmap.csv", delimiter=",", skip_header=True, usecols=range(1, 11))```
mysql can not connect to the server and i have not starter password what should do
plz help
I exported database as JSON from mysql and cannot load it to python via json.loads(...), gettin error decode. But it was EXPORTED from mysql as JSON so why mysql makes it invalid JSON?
what is that chart representing?
and, not enough info to guess really off of just that
db = cluster["test"]
collection = db["test"]
post = {"_id": 0, "name": "Pizza", "price": 50}
collection.insert_one(post)
print("Done!")
I try to run this code in MongoDB, however, the terminal stays quite for a minute then prints out this error :
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 608ab2b89d41f05565a1d100, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('scoopy-shard-00-00.cyrmt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('scoopy-shard-00-01.cyrmt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('scoopy-shard-00-02.cyrmt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>]>
@burnt turret are you able to help? 😄
Have you allowed your IP?
oh right
yes i did
as I said, not enough data to guess; depends on what you're doing with the db after all
Okay, as long as my bot works it's fine I guess 
i will recreate the database
Do you see this when you go in the "Network Access" tab?
ah hold don i did not repalce myFirstDatabase with the database name
@inland imp my database name is Scoopy, right?
The power of optimization 
Go to "Collections"
Yup
You can have multiple databases
For instance, I have three, depending on their use
db = cluster["ScopesTesting"]
collection = db["Data"]
post = {"_id": 0, "name": "Pizza", "price": 50}
collection.insert_one(post)
print("Done!")
now this should be my code, right?
Yup
i run it and its taking some time
you have cluster = MongoClient(...) I guess?
So that would be the right code
cluster = MongoClient(
"mongodb+srv://ScopesCodez:mypasshere@scoopy.cyrmt.mongodb.net/ScopesTesting?retryWrites=true&w=majority")
well, my code is all correct why am i getting the error
Thought you shared the whole link at first 
lol
Still that ReplicaSet thing?
import pymongo
from pymongo import MongoClient
cluster = MongoClient(
"mongodb+srv://ScopesCodez:mypassishere@scoopy.cyrmt.mongodb.net/ScopesTesting?retryWrites=true&w=majority")
db = cluster["ScopesTesting"]
collection = db["Data"]
post = {"_id": 0, "name": "Pizza", "price": 50}
collection.insert_one(post)
print("Done!")
this is my full code
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 608ab4bd94329195d92749ee, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('scoopy-shard-00-00.cyrmt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('scoopy-shard-00-01.cyrmt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('scoopy-shard-00-02.cyrmt.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>]>
thats the error
I can have a lot of different IP adresses so I allowed any IP personally
(Not the safest thing to do though but since idk heroku's IP...)
brb
and how do i do that
okay
Allowing every IP would allow pp to connect to your db though
alr will try now
In case your db link leaks for instance
Nice 👌
time to learn how to READ info
I learnt the basics from this then I was able to read the docs
MongoDB has a lot of powerfull operators
#help-broccoli please
oops @jade swan forgot to mention
pymongo is blocking; you should be using motor
but the usage is pretty much the same; https://gist.github.com/anand2312/840aeb3e98c3d7dbb3db8b757c1a7ace
Oh... yeah. Add MongoDB, Django and Pandas as additional things to consider in my wall of text post 10 hours ago if I can call back to get some guidance on that.
How to fix MySQL connector Error: Too many connections
pymongo is easier to learn than Django
If your datastructure is simple, knowing the basics is enough
It's such a big field of tech, very daunting.
I guess just playing around and trying to get something functional and modular, and well documented, is the way it might have to be.
Cool cool, food for thought: pymongo.
I exported database as JSON from mysql and cannot load it to python via json.loads(...), gettin error decode. But it was EXPORTED from mysql as JSON so why mysql makes it invalid JSON?
blocking what
just making sure, is smallint/int/bigint better for storing numbers in PostgreSQL then text even if no math is being done with them?
I'm storing 0-60 in smallint, and discord ids in bigint
I need sql pages to learn
What's the current de-facto MongoDB library for Python?
pymongo
Thanks! Also saw pymongodb wasn't sure which was better 🙂
Seems like this one is malicious, don't try it at home
Or better yet don't try it at the office where you might get fired
Does anyone know how to approximate how much disk space a database would take up if it was like Ohhh, idk 120,000 tables and 8 billion rows of data with maybe 15-20 columns each?
Welcome to PiProgramming.org, a software development publication dedicated to providing high quality content to the programming community. PiProgramming.org is known for providing insight into advanced software development technologies and concepts. At PiProgramming.org, we really enjoy diving into complex issues and sharing the most valuable fi...
Database disk space calculator ^
I am trying to setup a database schema (POstgresql with sqlalchemy) where I need two columns in one table to be foreign keyed to two columns in another table. How would I do that?
does sqlite have any ways to store values in array?
pretty sure those are called composite keys, but I'm not really good enough at this stuff to explain how it works
Is using an executemany statement faster or slower than casting data as a json array and using jsonb_to_recordset to insert with an execute statement?
Thats possible. There’s example shown here, https://docs.sqlalchemy.org/en/14/core/constraints.html?highlight=check#metadata-foreignkeys
It doesn’t have a data type for arrays. Best you can do is store a serialised array.
is it feasible to store image data (not the image itself) in a database and check new images against database entries to see if they are identical, similar or part of one another?
how would i search this xlsx by the username and be able to print the fullname and phone number in the username's row?
so far i can search the username column:
username = [val for val in df['Username:']]
``` but i want to be able to pull and separate the information from the other column in the corresponding row
What are some reasons I get invalid character errors when trying to cast a list of dictionaries to jsonb? It said invalid character "'" but no ' character was in the dict
file is not a database
recieving this for my .db file
all i have within the file is a table
Not sure what else im suppose to put
While inside the database when i try .dbinfo I recieve
unable to read header
New to sqlalchemy, figured this is a decent place to ask SA help questions?
Hey! So I have this code, ```py
import json
stuff= json.dumps(str([{"guild": "805638877762420786",
"channel": f"{ctx.channel.id}",
"author": f"{ctx.author.id}",
"prefix":"hi",
"command":"hewwo",
"failed":True}]))
await ctx.send(stuff)
query = '''INSERT INTO commands (server_id, channel_id, author_id, timestamp, prefix, command, failed)
SELECT x.guild, x.channel, x.author, x.used, x.prefix, x.command, x.failed
FROM jsonb_to_recordset(CAST($1 as jsonb)) AS
x(guild BIGINT, channel BIGINT, author BIGINT, used TIMESTAMP, prefix TEXT, command TEXT, failed BOOLEAN)'''
await bot.cxn.execute(query, stuff)```
but I get the error
cannot call jsonb_to_recordset on non-array
what am I doing wrong here?
@tasks.loop(seconds = 5.0)
async def tempbancheck(self):
connection: Connection
async with self.bot.pool.acquire() as connection:
async with connection.transaction():
rows = await connection.fetch('SELECT "end" FROM tempmutes')
if rows is not None:
for row in rows:
if row['end'] <= datetime.utcnow():
id = await connection.fetchval('SELECT userid FROM tempbans WHERE "end" = $1', (row['end']) )
user = await self.bot.fetch_user(id)
guild: Guild = self.bot.get_guild(id = 812314425318440961)
await guild.unban(user = user, reason = 'End of ban duration.')
await connection.execute('DELETE FROM "tempbans" WHERE "end" = $1', (row['end']), )```
Id doesn't fetch anythhing, while the table isn't empty
asyncpg, postgres
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.
is this mysql?
the db i mean
nvm mb
Does anyone have any knowledge on salesforce and python?
hey guys, is there anyone who is good at backend database in mysql?
You may just ask the question, that may get you an answer faster
well, actually I'm making a messaging webapp and I want to make a database for the messaging part and stuff...
but, unfortunately I dont know mysql, so I was looking for an aid...
might be helpful with someones help..
I have started learning mysql
For learning SQL generally sqlbolt.com is recommended (it's pinned to the channel) I did it and went to do a project with SQLite3. You could do that too,
SQL is mostly the same yet, they have some syntax and technical differences between the different types
so, in how many days can I learn it in?
I specifically want to learn mysql..
Depends how fast you go through it, I went through SQLBolt in a week or so
Nice
a good way to refresh knowledge even if know
It's great that it's so interactive!!
xD I like learning by practice
exactly my way
I had a course of MySQL in uni, but I am afraid that I forgot most of it after few years
and I think ORM is still not the same)
good way to refresh
I think though, that writing good ORM queries can be more rewarding in spent time to received proffit though
but well, for a deeper understanding, why not to learn hehe Raw SQL
Help?
Any number way related to store numbers is better than text
Because numbers take from 1 to 4 bytes to store them, while text requires 1 byte for its every character! (so 50 char string takes 50 bytes!!!)
Number formats do not require more than 4 bytes even for big values
Plus by storing with proper number way, you allow yourself making better SQL queries
I was doing some benchmarking and swapping text for bigint changed size from 53 -> 48mb, but made queries take a little longer (however that could just be margin of error)
This was a non-primary key however, I haven't tested primary key performance changes
I know one postgresql database that grew to the size of a more than hunderds gigabytes
Was using explain analyse
Yeah, was wondering why performance was to drop tho
gonna swap to bigint anyway, just wondering why
perhaps something related to... db indexes?
consider to compare
how different benchmarks for them
with and without db indexes
I haven't done any indexes, just have a primary key on a different bytea column
this could be important, speed of hashing can be different
lol the bytea is literally storing hashes
and indexes can take different space
for different formats, perhaps
or may be the same
Yeah, I was just doing quick testing during off time in a Computer Science lesson lol
so good SQL trainer
if only we had the same in university
it would make learning much faster
I tried to jump into ORM without knowing SQL but didn't really undestand what I was doing, so the trainer was a great way to learn, I was able to do my db project without ORM. I'll try orm for my next project
No lol, I was off task, I was meant to be doing stupid bad python stuff
I mean https://sqlbolt.com/
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
I am only afraid of doing stupid actions in ORM
like I should not do few.... requests in a row
when they can be combined in a bigger one
that does the same, without extra not needed moves
well, basically there are some anti pattern traps
which we need to avoid in ORM
Have you learned the antipatterns by Uni is there a good resources?
all study materials we were offered in uni
were made by proffesors on their own
so, basically no internet resources
only paper thin books
Alright, no problem
😩
after starting the course I realized
I need to read one more book
now about good pattern and bad anti patterns in SQL
without it my knowledge would be not complete
damm
Currently reading about python testing 😉 it's good
I have currently in queue 3 books about unit testing
hehe lol the same
reading really good book about unit testing
finally started to get it at the normal level
Which one are you reading?
and after that I planned to read few books about my favourite framework Django
I'm reading Python testing with pytest
I am reading
Unit Testing Principles, Practices, and Patterns: Effective ...Book by Vladimir Khorikov
A really super cool one explaining which tests are good, how to write good tests, how to refactor them and making easy to support and etc
really big eye opener
it is written with examples in C#, but I don't care
Unit testing is universal for any language
Testing is a good thing, I want to apply testing when I'm done with the book
it is written with experience of author, which unit tests you should not ever write)
if you want to be efficient
and which you should
which bring more usefulness
how to score the test and to understand if it is useful for your project
Sounds like a good book
I already apply testing
I just was not sure if I did it right
with reading this book, plus one more.
I'll be sure that I write tests in a right way
uh
SELECT *
FROM North_american_cities
WHERE Longitude < (
SELECT Longitude
FROM North_american_cities
WHERE City LIKE "Chi%"
)
ORDER BY Longitude
than further tasks
then more interesting SQL queries
will be there 1-2 A4 lists long queries? 🤔
Will there be what? 
hehe, that's what we finished with in uni ;b
multiple tables, and super long request through multiple tables
with many conditions from different tables
there will be 2 table queries with sorting and conditions but they are not too long
aww
you left me no room to wonder
Uhh damm I spoilered?
Well I may don't remember it right 
Woohoo. It was cool tutorial
I finished
Hehe, one hour to complete it
well, I knew already SQL, I just refreshed knowledge I guess
ORM is still better though
at least if it has proper migrating system
much more flexibility
well, actually raw SQL is more flexible
but ORM is better in term of... moving your product between its versions
Running an MySQL query, if I want to group by twice, do I need to use a sub query?
Example, I group a time column by minutes, and then limit 5 to get the past 5 minutes, then I want to group those 5 records into one record
[Q] TinyDB | JSON:
Cannot read data from .json whom content has sometimes signs like ąół. I tried to encode as utf-8 but didnt help.
Error is: 'charmap' codec can't decode byte 0x81
I have a model called log , and on it i have a relationship defined:
user = relationship('User', primaryjoin='Log.user_id == User.id', backref='logs')
however, when I do a query like log.query.filter(item_id=id).all(), whenever I try to access Log.user.username i get a name error.
What am I misunderstanding about querying/traversing this relationship?
I am new to SA, so there's that.
Hey! Am new to python, and am learning how to make databases for my scripts. I want to store the db into the script's file. So am using yaml, and using the PyYAML package, how can i remove and add certain variables from the yml database file.
The actual error is , NameError: name 'user' is not defined
yaml is not a "database". it is just plain text. the yaml format does not make it possible to modify the data "in place" - you will have to load all the data into your program, modify it, then overwrite the file.
Oh i see
i recommend sqlite instead for pretty much any "light duty" app
Alright! I'll check it out, ty for the help
@wintry verge it would help if you showed the full code for your model, or better yet for your entire application
import json
data1 = {"thing": "ąół"}
data2 = json.loads(json.dumps(data1))
assert data1 == data2
!e ```python
import json
data1 = {"thing": "ąół"}
data2 = json.loads(json.dumps(data1))
assert data1 == data2
@harsh pulsar :warning: Your eval job has completed with return code 0.
[No output]
that works fine ... is the encoding of your file not utf-8?
are you able to share the file?
!e ```python
import json
data = json.loads('''{"thing": "ąół"}''')
print( data )
@harsh pulsar :white_check_mark: Your eval job has completed with return code 0.
{'thing': 'ąół'}
I'm storing a sha256 hash in postgres, and have been using the BYTEA type, is there a better option? (would like to hash in app, simply store in postgres)
@harsh pulsar I need tinyDB solution, try to do db.all() once you point .json db and look at error
i don't know anything about tinydb
the problem might be tinydb itself
but rly handy lightweight db
this does seem useful, kind of like mongo-but-sqlite
anyway, thank you for solution if it was json
the point is I want to deply my app on githuib pages
and I need some db. mysql cannot be there, cos its static
what about sqlite? i havent had unicode issues with sqlite
although, im not sure how you expect to use a database with a static site at all
unless you ship the database itself to the client? seems like a weird design
I plan to place that json file in main directory where app.py will be, and it will read from there
item = InventoryItem.query.get_or_404(item_id)
history = Log.query.filter_by(object_id=item.id)\
.filter_by(content_type_id=9)\
.options(db.joinedload('user'))\
.order_by(Log.id).all()
for log in history:
to_return.append({
dt: log.action_time,
user: log.user.username,
change: log.change_message
})
This is giving me a NameError on user not being on log inside the to_return.append()
Model:
__tablename__ = 'admin_log'
id = Column(Integer, primary_key=True)
action_time = Column(DateTime, nullable=False)
user_id = Column(ForeignKey('auth_user.id'), nullable=False, index=True)
new_user_id = Column(Integer, nullable=True)
content_type_id = Column(ForeignKey('content_type.id'), index=True)
object_id = Column(String)
object_repr = Column(String(200), nullable=False)
action_flag = Column(SmallInteger, nullable=False)
change_message = Column(String, nullable=False)
content_type = relationship(ContentType', primaryjoin='Log.content_type_id == ContentType.id', backref='admin_logs')
user = relationship('AuthUser', primaryjoin='Log.user_id == AuthUser.id', backref='user_logs')
i still dont understand how that will work with a static site
@wintry verge i don't see anything obvious but maybe someone else does
@regal moss my only guess regarding your original issue is that somehow the data was not written to the db correctly
do a SELECT * FROM Movies and check the column name
^^
@harsh pulsar oh and btw, I would store that json db in the same directory, accessable by user, but that database only sortes Herbs and vitamis + description. not passwords etc cos website will not use login not contact forms, nothing, but loops posts from json about vitamins 🙂
just btw, github pages dont support running python iirc
it's litterally just designed to serve raw HTML and some static file assets
why this error?```py
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/runner/CaliberBot/cogs/notes.py", line 29, in note
if (await collection.count_documents({"_id": ctx.author.id}) == 0) or (await collection.find_one({"_id": ctx.author.id})["note"] is None):
TypeError: '_asyncio.Future' object is not subscriptable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: '_asyncio.Future' object is not subscriptable
You should put the await in parenthesis so it gets evaluated first: ```py
(await collection.find_one(...))["..."]
thats true but, it will support Frozen-Flask, my app is based on flask and I will make it frozen-flask, so will work
sure, but you wont be able to change stuff
when it freezes it it takes a set of stuff and makes it into static files
so DB or not you're still essentially hard coding the values etc...
what it wont do is convert things like your DB etc...
true but once data sotored in db, I might in future develop some website with mysql serv on side lets say, and will not have to again type data
or maybe I will host it on heroku, who knows
maybe you should consider rendering the data to a format like json
even if you store it in a database on your server
you mean export data to json from database?
oh ok thanks
Is there an error with how I'm setting up the columns?
from sqlalchemy import (
Column,
DateTime,
Integer,
String,
Table,
ForeignKeyConstraint,
Enum
)
from sqlalchemy.sql import func
from ..db import metadata
import enum
class PermissionsEnum(enum.Enum):
private = 'private'
public = 'public'
images = Table("images", metadata,
Column("id", Integer, primary_key=True),
Column("permissions", Enum(PermissionsEnum), default="public"),
Column("text", String),
Column("characteristics", String),
Column("date_created", DateTime(
timezone=True), server_default=func.now()),
ForeignKeyConstraint(['id'], ['users.id']))
When I went on psql and ran \+d images I only got the id column
I don't think my db configuration is the problem since I have another table that I can read and write to and I've seen that all the columns exist
anyone know the best dbms for storing a large amount of rows into a table? Im creating a discord bot that also stores messages and I will need a lot of rows in the future
@somber isle "large" is probably orders of magnitude bigger than a casual discord bot will store, unless you are planning to store 100s of millions or billions of messages.
well how many MB do you think lets say 1 million messages is?
not enough to care
use sqlite
a sqlite database is a plain file on disk that can be accessed with a python library
sqlite can only handle one entry at a time though, i need an async one
any other database requires a separate server, trust me you dont want to go that route
cause this bot will be in multiple servers
well ive worked with mysql server before, currently taking a dbms class rn
what do you mean "one entry at a time"? you can do arbitrary concurrent reads from sqlite, and writes are fast enough that you won't notice that they aren't async
I was told that sqlite can only allow one write and read at a time
is that not true>
it supports highly concurrent reading
writing not so much, but again writes are very fast on a modern ssd
ok that sounds good. using just the sqlite3 library?
yes
or do i need to make an async version
i believe there is some support for truly concurrent writes too in sqlite but you might be going off-trail with that
also there is an asyncio sqlite library
well my only concern was since the functions within discord.py are async, would the database operations needs to be async too?
they dont need to be async, if they are fast
the event loop will be blocked while the write is executing, but if it's fast you won't notice a difference
ok they should be simple operations anyway just basic write and reads not a lot of hard queries
however if you do need to do a lot of concurrent reading, then yes you should use asyncio for that
Does SQLite3 safely handle concurrent access by multiple processes
reading/writing from the same DB? Are there any platform exceptions to that?
theres definitely some reading to be done if you end up serving a lot of requests
but i think with aiosqlite you will be fine
thanks @harsh pulsar ive actually been working on an API for sqlite3 to make it even easier to use
the best solution is to use Postgres for example
it has no limit to write/read at the same time
there is a dumb work around for Sqlite though
I make writing to RAM stored Sqlite and then dump it to HDD stored Sqlite from time to time ;b
makes 99% of my app time, reading from HDD Sqlite as main one
it is a really dirty solution, but suiting small apps
unclear question
unable to identify what you mean
How to change save data in txt files to save it on database
def load_codes():
f = open("codes.txt")
for line in f.readlines():
codes.append(line.strip())
f.close()
code_to_use = input("Podaj kod aktywacyjny:\n> ")
def use_code():
cmd = 'wmic csproduct get uuid'
uuid = str(subprocess.check_output(cmd))
pos1 = uuid.find("\\n")+2
uuid = uuid[pos1:-15]
uid = []
u = open("uuids.txt", "w+")
for line in u.readlines():
uid.append(line.strip())
try:
codes.remove(code_to_use)
except:
print("Coś poszło nie tak, skontaktuj się z developerem programu!")
input("Kliknij enter by zamknąć program!")
f = open("codes.txt", "w+")
for code in codes:
f.write(code + "\n")
uid.append(uuid)
for uidn in uid:
u.write(uidn + "\n")
u.close()
f.close()
ua.close
load_codes()
cmd = 'wmic csproduct get uuid'
uuid = str(subprocess.check_output(cmd))
pos1 = uuid.find("\\n")+2
uuid = uuid[pos1:-15]
uid = []
u = open("uuids.txt", "w+")
for line in u.readlines():
uid.append(line.strip())
if uuid in uid:
print("Twoje hwid jest już połączone z innym kodem! Jeżeli uważasz że jest to błąd skontaktuj się z developerem.")
input("Naciśnij enter by zakończyć program!")
if code_to_use in codes:
print("Sukces!")
use_code()
else:
print("Kod nieprawidłowy")```
where do i begin with sqlite?
learning SQL is a good first step - https://sqlbolt.com
once you know SQL, using an SQLite (or other SQL) database is going to be pretty simple - https://docs.python.org/3/library/sqlite3.html
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
thanks
You should probably use the answer above as well
It would be a good start to databases
Hello, i need help with my sql query:
I want to fetch certain amount of blogs from my table, ordered by most recent post and joined with it, however with such query it would have to execute subquery for each entry in blogs table
explain analyze
select *
from blogs
left join posts on posts.id = (
select posts.id
from posts
where posts.blog_id = blogs.id
order by created_at desc
limit 1
)
order by posts.created_at desc
limit 5
Is there any way to achieve similar behavior with different query?
Say you have a SQL database, you have tribe names, and the tribe has a stat, e.g. player kills, etc.
If that tribe is recurring in the database, how could you add their stats up and display the sum of all of that?
how do I do something like sql ALTER TABLE ADD CONSTRAINT whatever ON CONFLICT DO something because I have 2 tables which I want to add a foreign key to but they have possible non-present keys from one to the other
I have this query```sql
ALTER TABLE nicknames
ALTER COLUMN guild_id TYPE bigint USING guild_id::bigint,
ALTER COLUMN user_id TYPE bigint USING user_id::bigint,
ADD PRIMARY KEY (guild_id, user_id),
ADD CONSTRAINT fk_guild
FOREIGN KEY (guild_id)
REFERENCES guilds (guild_id)
ON DELETE CASCADE,
ADD CONSTRAINT fk_user
FOREIGN KEY (user_id)
REFERENCES userinfo (user_id)
ON DELETE CASCADE;```and the userinfo table is simply `guild_id::bigint, lang::text, blocked::bool`, so I want to just insert a default row into `userinfo` table if it doesn't already exist
so I use xammp for my Database and I'm trying to use import sqlite3 but I'm not sure how I would connect to the database in xammp with sqlite3
nvm, I'll use mysql instead :)
What is sql used for?
databases
hello everyone!
I have an sql alchemy selection statement that looks like this py statement = select(email_subscriptions).where(email_subscriptions.user_id ==user, email_subscriptions.guild == guild) inside a function and I want to be able to set the table and the column names with parameters, but I don't know how to do it.
I need all of the guides to understand monogdb, I want to get started and use it for discord bot
Anyome got any good one's? I.also.need guide oj how to set up monogdb
Generally I've liked mongodb's own resources - https://university.mongodb.com/courses/M001/about
Learn MongoDB directly from MongoDB University. Free online training courses and tutorials. Register today!
Their docs are really nice too
I have a question about mongodb, it's structed by documents, which are field value. It looks really like dictionary, is it possible to convert it to that?
And collection stors a few of these documents
when you retrieve documents, you'll be able to use them as dictionaries yes
Te guide says mongodb free tier suits to small projects, but I want to use it for a discord bot, will it be ok?
more than sufficient
i mean, are you using their cloud based option? i don't know how much storage that gives you but it should be enough if it's a small bot
when i was using mongodb i was running an instance myself (not using their cloud option)
512 mb
should be enough i think
which is better databse 'MySQL or MongoDB'?
those are two different types of databases, and they can't be compared
because they are the best for different types of situations
you can ask -> is MySql good for situation X, and MngoDB for sutiation N?
I want a database for a discord bot(discord.js)
what do you plan to store in it?
it's a economy bot so user's info, their balance
etc.
basically MongoDB is best to store... large arrays of dictionary like weakly structurized data.
it is very specialized to be not always possible to use it
tutorials name document type of data suiting MongoDB
SQL dbs are usually 'all around' suiting for most of tasks
efficient in their space to have data being stored in minimum available space
and then reconnected through keys with SQL queries
I would say that mongodb is good for non-important data? 🤔
xD, may be.
we thought to use mongoDB to store searching results
which we have no idea what to use for, except for training of neural networks later may be
or some other sort of post processing
Yeah, basically that data is not important 🤔
I wouldn't store something like user information in mongodb
perhaps user comments / news
something which has great amount of characters / unstructurized things
per one record
Relational databases can handle huge amount of characters though
well, all right
go with MySQL basically ;b
it would be safer option
nobody stores users in MongoDB, I think
I would choose postgres over mysql though 🙂
I am actually using postgres too ;b
MariaDB is also an option
open source as well as far as I know
or json!
json is small sized alternative to MongoDB ;b
ohk
JSON is good to store config data ;b
I mean...
some super cheap ass solutions offer instead of databases storing data in files ;b
Wdym by cheap 😅
JSON can be as much as a way to store data in such cases
xD cheap in term of developer laziness to learn better ways
SQLite yeah, better
afaik sqlite is used a lot
I am currently using two SQLites in my project ;b
RAM and HDD one together
writing everything to RAM and dumping to HDD sqlite
avoiding write/read conflicts in this way
and twice faster to write into RAM
probably at a later stage will swap to Postgres
it is proprietary
some people afraid it would become for money only
that's why MaridDB appeared
I would use postgres over mariadb anyway 🤔
I'm not very familiar with either of them but postgres is more sophisticated afaik
I used only once MariaDB, when I was asked to setup Wordpress in VPS
tutorials were for MariaDB
I usually use orms so database doesn't matter that much
me too, me too
But when you would have to write sql queries by hand i'd better be postgres
MongoDB uses JSON format?
MongoDB uses format which looks like python dictionaries
and python dictionaries are almost JSON in terms of easy converting
And what is BSON?
!rtfm asyncpg
it is binary encoding of json
hey people, im looking to create a users database that will format users like:
{
user "user1"
ukey "1234"
upass "4321"
lastlog "1/5/2021"
}
and repeat for each user, and then this form can be used to login with. the last log is also when the user was last logged in.
what is ukey?
Plus storing passwords in plain text is a no-no 🙂
Anyone got a guide for MongoDB with python? I already have a cluster made
each user has a key as part of my program. i already have the generation and storing of the variable done, i just need it to be able to be used with login
What database are you using?
and how should i create a login system if passwords cant be stored in plaintext. im aware its a bad idea, i just dont know what to do about it
Hashing
i dont know at the moment because any tutorial i follow online just seems to break no matter what i do
I would recommend to use any realational database
Like postgres
Plus an orm of your choice
ok well i have no idea how to do anything with databases so this should be fun
a moment, let me find you build it in function to correct it
Anyone got a guide for MongoDB with python? I already have a cluster made
Just google it
!e
import hashlib
salt = 'is salt a secret key?'
print(hashlib.sha512('my secret password'.encode('utf-8') +
salt.encode('utf-8')).hexdigest())
print(hashlib.sha512('password'.encode('utf-8') +
salt.encode('utf-8')).hexdigest())
print(hashlib.sha512('my secret password'.encode('utf-8') +
salt.encode('utf-8')).hexdigest())
print(hashlib.sha512('my secret password'.encode('utf-8') +
'differrent salt'.encode('utf-8')).hexdigest())
@wise goblet :white_check_mark: Your eval job has completed with return code 0.
001 | 10df09f1ff972072e9919b0ab2413988e3d066875049d4a1dd2415a2fde29fc5f55f380b40e7d0eeb78cf055bccbdbbb1894338f49e2dc05ae48bb3d485a68e5
002 | 046ce283f3e41fc7283724db160ec234dbc497528277aade21e12d88c74965692356481c9bc772a9a24d85da2bfd644e0a4396221cfaf17f71c69a9c73495981
003 | 10df09f1ff972072e9919b0ab2413988e3d066875049d4a1dd2415a2fde29fc5f55f380b40e7d0eeb78cf055bccbdbbb1894338f49e2dc05ae48bb3d485a68e5
004 | 63916d424fd1b2f7a697e87a510f3ba10a6c9baa5cd62158d8b64e62190050d88f915ebe4dbb692a8cf7637d2be5d6532e527309aca681b41c2bc8a829cd0fea
here you go
as you can see first and third line were encoded with the same secret key (which is salt)
which allowed to encode 'my secret password' to the same hash
https://www.mongodb.com/blog/post/getting-started-with-python-and-mongodb this one
and there is one realpython one also, can't find the link for that
MongoDB has a native Python driver, PyMongo, and a team of Driver engineers dedicated to making the driver fit to the Python community’s needs. In this article, which is aimed at Python developers who are new to MongoDB, you will learn how to create a free hosted MongoDB database, install PyMongo, the Python Driver, connect to MongoDB and more.
I can't seem to find any way in pymongo to connect the cluster
all you need to store your password as hashes
cool thanks dude that really helps
keep your salt as secret
it is your secret_key basically
which should be never known
technically even if someone would know it
he would not be able to guess passwords
because he would have to.... check billions of combinations
but better be safe than sorry
ah and yes... don't change salt unless you wish old passwords to be useless)
u a welcome
there are more secure ways to handle it though
I know there should exist algorithm....
which can encode two the same passwords 'my secret password' to different hashes
while being able to verify that they are still 'my secret password'
it will be more secure, because.... hackers would not know that two passwords are the same
but I guess, some hash is already better than nothing
this should be more then enough security for what i have planned thanks man
Or use random salt
There's already libraries that provide salt for you
ah and yes... don't change salt unless you wish old passwords to be useless)
ahem
It depends on algorithm
bcrypt uses random salt
argon2 too
So there would be no risk if you leak your salt
!e
# import the hash algorithm
from passlib.hash import pbkdf2_sha256
# generate new salt, and hash a password
hash_ = pbkdf2_sha256.hash("toomanysecrets")
print(hash_)
hash_2 = pbkdf2_sha256.hash("toomanysecrets")
print(hash_2)
# verifying the password
print(pbkdf2_sha256.verify("toomanysecrets", hash_))
print(pbkdf2_sha256.verify("toomanysecrets", hash_2))
print(pbkdf2_sha256.verify("joshua", hash_))
yeah, tested it
output
$pbkdf2-sha256$29000$JuRcy1mrVUqJcc7ZW4vRGg$rqSfPg6WOM8c4tktTB761FQ2fR2.DAvG35n.VM2YSoU
$pbkdf2-sha256$29000$itF6bw1BqPU.B8B4z3mvNQ$05O6g4YVkWvVVXJAn5M/oDTSkvdaQrPRYWZDm3kaVG4
True
True
False
it is exactly what I was speaking about
you see, they generate different hashes
for the same password
while being able to verify both of them
it is indeed more secure
I need a little help with connecting to mongo db, if I have this link format:
mongodb+srv://myusername:<password>
let's say my password is blah, should I do:
mongodb+srv://myusername:blah
or:
mongodb+srv://myusername:<blah>
Because salt is different
thanks, that better
option 1
ah ok
Just so I be sure, how can I get my password?
how are you running mongodb?
more salt to the hash god
if it's on the atlas option, i'm not very sure, but you'd probably have to make user details
https://docs.atlas.mongodb.com/getting-started/ in fact this entire thing should be useful
Oh yeah I just got it I think
one more thing
it says replac the test with the database
How can I get the database name? I know the cluster name is Sandbox
And what is compass
GUI for MongoDB
Do I *need it?
userJohn = {
"user": "user1",
"ukey": "1a2b3c4d",
"uhash": ""
}
userSmith = {
"user": "user2",
"ukey": "4d3c2b1a",
"uhash": ""
}
user = input("User Please")
chosenUser = (f'{"user"}{user}')
print(json.dumps((chosenUser)))```
im clearly stupid but wtf am i doing wrong
it says line 1 is invalid syntax 😐
It's not?
!e
import json
userJohn = {
"user": "user1",
"ukey": "1a2b3c4d",
"uhash": ""
}
userSmith = {
"user": "user2",
"ukey": "4d3c2b1a",
"uhash": ""
}
#user = input("User Please")
chosenUser = 'userSmith'
print(json.dumps((chosenUser)))
@bitter bone :white_check_mark: Your eval job has completed with return code 0.
"userSmith"
you need to eval it
aight it was just my shitty programming then
im completely new to JSON and databases so yeah
i would say to go with the latter, since having eval() in the code is not a good habit 
Json has nothing to do with databases
If you want to use a database then use one 😅
JSON is just slightly cooler way of CSV ;b
@kind kayak Look into an orm, for example sqlalchemy or peewee
isn't that a overkill for this task
It would be a good practice
i just want a basic login system 😢
And i would rather use databse for it than a json
I remember somewhere old joke
inside the brain:
- Oh look! pretty girl is on ten o'clock!
- Oh no, the brain is overloaded, and not functioning anymore!
- Just kick it!
- Yes, sir! kicks the brain. Everything is back to normal, sir!
tried that... just got echos in return
think my head is empty D;
Just reboot 😅
Works for windows
mongodb+srv://m001-student:mypassword@sandbox.hytmj.mongodb.net/test
What should I replace test with?
How do i get the database name?
databases inside MongoDB are what you are creating inside ;b
you can connect without specifying database
in order to create the first database there
Why can't there just be a website with all databases names
is there some kind of way to just create a really simple login thing?
it depends on which framework you are using
import pymongo
from pymongo import MongoClient
from pymongo import mongo_client
print(pymongo.version)
db_link = "My database link"
client = MongoClient(db_link)
db = client.FirstDatabase
people = db.people
name = input("Name? - ")
age = input("Age? - ")
doc = {name: age}
people.insert_one(doc)
print(people.find_one(name))```
It returns None
How can I fix it?
And is it possible to get the entire people data as a dict? Not to find anything in it? and connect with it from somewhere else
i just want people to be "signed in" when they perform actions
it doesnt even have to be secure
so idrc what framework
i just need to know which one 😂
just use Flask I guess then
I have this document, created with this code:
import pymongo
from pymongo import MongoClient
from pymongo import mongo_client
print(pymongo.version)
db_link = "the link needed"
client = MongoClient(db_link)
db = client.FirstDatabase
people = db.people
name = input("Name? - ")
age = input("Age? - ")
doc = {name: age}
people.insert_one(doc)
print(people.find_one(name))```
If I'll do in another program, db.people will t connect me to the document?
man, read tutorial, and find out how to login
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
at the chapter 5, how to make user login is pretty much explained
it it outdated though (but still good!)
flask documentation has more up to date version, but less structurized in a right order (section 'Sessions' in flask doc)
Wdym login? I know how to get in the database
the thing above is for you
https://discordapp.com/channels/267624335836053506/342318764227821568/838328960949157919
Oh
???
uhm, what do you mean by this?
yo just wanted to ask how important it is to use an async library for the database handling?
because i'm making a discord bot and i've always just made normal functions and never had any problems with it
Write a program that will ask the user to input two numbers, and then display the message “They are equal numbers" if they are equal, print "num1 is greater than num2" if the first number is greater than the second number, otherwise print "num1 is less than num2".
i need help?
?? use if statements
yeah and why are u asking this in database channel
x = input('give number')
y = input('give more number')
if x == y:
print('literally the same')
else:
print('literally not the same')
just take a course man
codecademy or something
look for a course that's free or something and start from there
that's how i did it
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
This tutorial is outdated by 4 years
which made some of instructions a little bit incorrect
but that's not a problem in my opinion
it just motivates to find the right updated commands
oh ok
hey dudes Im working on a Crud with django Im having trouble with putting up multiple tables with the stuff Ive been digging up I have only been able to get 1 table for my database
does that make sense?
Why MongoDB is so complicated
uh, not really
weird
I thought it is supposed to be simple
like put to dictionary/ get from dictionary ;b
never tried one though
@burnt turret
Yeah, do you know about the difference between find and findOne? (Or find_one when using pymongo)
I think find returns the entire query?
and find one returns a dict by key and value?
You didn't word that very well; find will return all documents that matches your constraint
Like in your example query in the other channel, if your collection had multiple documents with name set to "nir", findOne will only return one document
pls answer me
how can i get the prefix from the server by guild_id
{
guild_id: "718381882293189182",
prefix: "//"
}
``` i only want the value //
find will return all of them in a Cursor object
What database is this?
mongo
I think i'll do one doc in a collection for a guild
So find will never be None even if no documents matched, it'll always be a Cursor object
find_one as it just returns a single doc can be None if there was no matching documents
collection.find_one({"guild_id": ...})
It'll return the dictionary, so you can just access the "prefix" key to get the prefix
{"guild_id": "guild_id": the_id, "economy": {"member_id": {"wallet": 0, "bank": 0}}, "suggestions_channel": the_channel_id} maybe like that
Oh
hmm
collection:
doc_a:
{"name": "nir", "skill": "bad af"}
doc_b:
{"name": "test", "skill": "better than nir"}
doing:
test = collection.find_one({"name": "nir"})
and than doing:
skill = test['skill']
will it return bad af
oh i got it
{
guild_id:"something",
prefix:"//"
}
``````py
guild = collection.find_one({guild_id:"something"})
prefix = guild['prefix']
```?
Yes
how to write data
look into collection.insert
If I would do:
data = collection.find_one({"name": "nir"}) and it's not there, will it return None?
Yes
If I get full dict object it could really be easier
data = {
guild_id:f"{ctx.guild.id}",
prefix:"//"
}
collection.insert(data)
```?
Would an array field be violiting the first rule of 1NF in database normalisation?
@jaunty galleon You can store the document _id under a member id tho
so all you'd do is find({"_id": member.id})
find_one if only one document needed
from discord.ext.commands.core import guild_only
from discord.invite import PartialInviteGuild
import pymongo
from pymongo import MongoClient
from pymongo import mongo_client
print(pymongo.version)
CLUSTER = MongoClient("The link ya know")
NAMES_DATABASE = CLUSTER["FirstDatabase"]["names_data"]
def tryout():
options_log = ['log-in', 'log in', 'login']
options_sign = ['sign-up', 'sign up', 'signup']
while True:
sign_or_log = input("Sign up or log in?")
if sign_or_log in options_sign:
name = input("What is your name? - ")
check_if_exists = NAMES_DATABASE.find_one({"name": name})
if check_if_exists:
print("This name already exists")
else:
skill = input("What is your skill - ")
ended_dct = ({"name": name, "skill": skill})
NAMES_DATABASE.insert(ended_dct)
elif sign_or_log in options_log:
name = input("What is your saved username? - ")
check_if_exists = NAMES_DATABASE.find_one({"name": name})
if check_if_exists:
skill = check_if_exists.get('skill')
name = check_if_exists.get('name')
print(f'Name: {name} \n Skill: {skill}')
else:
print("This name is not in the database.")
tryout()```
I just made this I am proud of myself
Its just an async driver lol

