#databases
1 messages · Page 130 of 1
The key is supposed to be unique but a record already has that key value
hey can someone one tell me how to get data returned based on the timestamp? like i want to get data returned based on the day or month or year
this is one of the Colom in sql datetime timestamp with time zone NOT NULL
how certain are you it actually closed?
thank you so much but if i say actor_id is primary key, it gives error and can not insert
i added movies like fury, curious case benjamin and one more, now there are 3 brad pitt
i removed primary key as actor_id
so i can i insert only one time actor_id
if i say actor_id primary key not null, it gives uniquekey.violation error
i need to set condition like IF EXIST ignore
i googled in intenret, i could not find example
well, you can make a try except clause wherein you try to insert duplicate values and if it already exists in table, you just pass
primary key must be enabled for this
or unique constraint
cursor.execute("""INSERT INTO movies (imdb_id, full_title ) VALUES (%s, %s )""", (movie_event["id"], movie_event["fullTitle"] ),)
it is my code
where i need to say except
ok i enabled it.
hey can someone one tell me how to get data returned based on the timestamp? like i want to get data returned based on the day or month or year
this is one of the Colom in sql
datetime timestamp with time zone NOT NULL
pls help
I fixed it, I had some sql code running in the background which I forgot about. Thanks for the good explanation about database is locked.
Someone who has experience with both , mongo and sql which is better
thanks
@commands.command()
async def premium(self, ctx, member: discord.Member.id):
members = str(member)
db = sqlite3.connect('./cogs/database/premium.sqlite')
cursor = db.cursor()
cursor.execute("INSERT INTO premium(User_IDs) VALUES(?)", members)
cursor.fetchone()
is this the right way to insert mentioned user id in table?
anyone ?
MERN stack ppl would also agree lol
kinda discord.py, but how can u get pandas to send only what is in column "test"
my excel sheet
for example:
i want pandas to send
Destination: n/a
Distance: n/a
etc
my code:
@bot.command()
async def data(ctx, date: str):
open(Log)
df = pd.read_excel(Log)
data = df.set_index(date, drop = False)
dembed=discord.Embed(description = f'{data}', color=0x6b1aea)
await ctx.send(embed=dembed)
managed to do this, so far so good (at this point i am documenting my progress as no is active on here lol)
Just want the NaNs? .values
i also want it to send what i said here
i want it to send the column "test" and the unnamed column (in place of the numbers)
So the index & the column... One sec
Its just data["test"] you're after without the dtype line I suppose?
ya
.to_string() on that
so
df2 = df[[f'{date}']].to_string()
await ctx.send(df2)
```?
Shouldnt need the extra brackets, but should be what youre after
alright, thanks
ill try it and let u know how it works
i want to keep test at the top and remove the number, but that ended up doing the opposite, hmmm
Can you print df there see where the text went.
i wanna keep the destination, distance, start, end, and difficulty too
for some context, i am making a discord bot that logs data from group bike rides
So on the read add index_col=0, or you can pick 2 columns
ill try that thanks
data = df.set_index(index_col=0, date, drop = False)
date is now giving me the error: Positional argument cannot appear after keyword argumentsPylance
Its for the line above that one
df = pd.read_excel(Log)
``` the read line?
(what its complaining about is youve but the arg data after a kwarg)
yea (Log, index_col=0)
ohhh ok
thanks
perfect, it worked thanks 🙂
now to figure out how to write with pandas to the excel sheet through discord 💀
thanks again
CREATE TABLE user_preferences(
user_id BIGINT,
guild_id BIGINT,
PRIMARY KEY (user_id, guild_id)
);
How could I create a primary key like this using flask sql alchemy?
@bot.command()
async def log(ctx, date: str, destination: str, distance: float, start: str, end: str, difficulty: float):
open(Log)
df = pd.DataFrame
df = pd.read_excel(Log)
df.write[f'{date}'] = f'{destination}', f'{distance}', f'{start}', f'{end}', f'{difficulty}'
await ctx.send(f'New biking data logged')
print('Documented')
how do i use pandas to write to an existing excel file? obviously df.write doesnt work
date is the new column, and i want everything after to be a new row in the column
I'm trying to make a discord bot store a channel ID once you run a command for later use in the code how would I go about doing so using the SQLite system
any recommendations for an async ORM?
@burnt turret orm
assuming you're talking of https://github.com/encode/orm?
erm well generally you cant have two primary keys like that, but you can make them behave similarly by setting them to be unqiue and indexed i think it is a kwags for sqlachemy types
Yerp
alright thanks, on a quick search this and tortoise, gino were the ones which came up
why would you recommend this one over the others?
actually scratch gino off that it seems to only support postgres right now
Litterally never used the others but orm generally is pretty robhst being built ontop of alchemy core and also supports asyncpg where as i think most will jse aiopg which is pretty slow
oh alright, skimming through tortoise it seems to be using asyncpg too; but i'll use orm anyways
thanks :D
You can have 2 primary keys like that
I know you can its not necessarily what you should do though
Why not?
They are modelling user_preferences so i am assuming this is a many to many relation. In which case this is how it can be done and is perfectly fine.
Generally i find it ends up with people creating many to many relationships instead of properly normalizing the data
Well what is wrong with many to many?
They're incredibly in-efficient mostly
A user can have many preferences, each preference can belong to many users. That is what a many to many is.
No they are not, in what sense do you say this.
Generally it can lead to data duplication, if you want a like many-to-many relationship then you should do so with a linking table so its one-to-many to the linking table
generally this is a pet peeve of mine though
if you have a many-to-many relationship generally you can have a linking table and reduce data duplication
Well that is what they have here. And that is how many to many is done. Via the junction table.
If they're making strictly a linking table then that's fine i might just be miss interpreting what they're doing
Hmm, well their table is misleading to be fair as it has no mention of preference in the columns and instead has a column for a guild. But many to many as a relation is not inefficient, when done properly like you mention with the linking table.
Hi, so currently i have query like this
async with await cursor.execute("SELECT * from prods where store=? and (cat1 = ? or cat2 = ?)", (store, cat, cat)):
data = await cursor.fetchall()
so this is not an issue becausw currently is working but is there a better way to write this because i must currently pass in two values for cat
Yes you can. You can check if a value is in a list of columns using IN
Select * from prods where store = ? and ? IN (cat1, cat2)
I don't use it 🤷♂️ You tell me if it's good. if it does what you want and makes it easy to build your API then yeah.
query works thanks
and yes for me it was easy to make my api. made my life easier but ok thanks
uhh
yo how you do this with your status?
offtopic but - pypresence
alright thanks
can you search by indexes?
Hmm, I think this will work.
Is there anyway to check the resulting SQL command for db.create_all()?
Hello, I am having a debate on whether this is correct to do or not:
I have a "users" table that amongst other columns contains a column called "MessageCollection", the message collection column holds a foreign key for a row in the "messagecollections" table, this table holds the following columns: id and messageIDs, the messageIDs is a serialised list that contains multiple IDs of rows in the messages table. Is that the best way to store the ids or am I missing something?
that is how it looks
I will be serialising the list using json and json.dumps()
self.Item_Name = "Test"
qty = 1
users_col.update_one("_id":f"{fm}x{to}", {"$inc":{f"inventory.{self.Item_Name}": amount:qty}, upsert=True)
@burnt turret
what was the error here again?
can't increment right?
what is amount here? asking because the syntax there looks a bit odd
So im trying to connect to my postgresql database but it gives me this error:
raised an error: OperationalError: FATAL: Peer authentication failed for user "MyUserName"
@autumn epoch can you show how you are connecting to the datbase and what module you are using
I am using postgresql and this is the connecting code:
conn = psycopg2.connect("dbname='users' user='username' password='password'")
try providing the hostname
Where would I get that?
add host='localhost' to the connection string
doesnt really matter
Ok
New error @proven arrow :
ror: OperationalError: could not translate host name "host=localhost" to address: Name or service not known
what platform are you on?
try host=127.0.0.1
Shouldnt I also add the port
you can
So 5432
yes if that is what its running on
Same error
peer error or hostname error?
Hostname error
It worked on my macbook
The postgresql might not of been setup properly
can you connect to postgres via cli on your server?
Wdym cli?
A terminal
But to postgres from your server
{_id: f"{fm}x{to}"},
{$inc: {f"inventory.$[elem].amount": qty},
{arrayFilters: [{elem.name: self.Item_Name}], upsert: 1}
)
``` @torn sphinx you want to be using mongo's arrayFilter option here - you can see the example from https://docs.mongodb.com/manual/reference/method/db.collection.update/#update-specific-elements-of-an-array-of-documents
thing is, i've written that in a way that (i think) would be working on mongo shell, ~~and not pymongo itself. i can come back in a while and try explaining this to you and help you convert this to something that works in pymongo, because i'd have to refer it's docs again, if that's fine?~~
```sql
user_col.update_one({"_id": f"{fm}x{to}"}, {"$inc": {"inventory.$[elem].amount": qty}, array_filters=[{"elem.name": self.Item_Name}], upsert=True)
``` i _think_ that's what it would look like in pymongo
Or you can try turning off peer connection from your postgres settings, but even with that on it should work. But I would first make sure your postgres service is running before you go troubleshooting with that or before you modify any more python code.
I might try doing a fresh reinstall of postgresql
can someone please give an article so i can learn databases?
i need a large database of caracal pictures
I use json as a database
Something like Postgres should work fairly well for that
Anyone have experience using python + ssis / visual studio
What are the advantages of ORM?
from assets.imports import *
class Premium(commands.Cog):
def __init__(self, bot):
self.client = bot
db = sqlite3.connect('./cogs/database/premium.sqlite')
cursor = db.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS post(
User_IDs INTEGER
)
''')
@commands.Cog.listener()
async def on_ready(self):
print("premium Cog has been loaded\n-----")
@commands.command()
async def premium(self, ctx, member: discord.Member.id):
members = str(member)
db = sqlite3.connect('./cogs/database/premium.sqlite')
cursor = db.cursor()
cursor.execute("INSERT INTO premium(User_IDs) VALUES(?)", members)
cursor.fetchone()
def setup(bot):
bot.add_cog(Premium(bot))
I want to add temporary data to this for 30 days and create a loop which checks every 24h
@fallow silo They can make development a lot easier and you can have complex SQL queries with little SQL knowledge. They automate a lot of the stuff for you, and can save you time from writing queries.
Thanks a lot. For Instance, I want to write complex queries and Can ORM support this problems? Can I write raw queries with ORM.
Yes they generally should allow you to write raw queries as well
can anyone help me with making new columns in an existing excel file?
how can I set up a database for storing warns for my discord bot?
What do you already know about databases or how confident are you with using them?
Do you know how they work on a basic level like there is tables and rows? I dont really know of any good beginner guides but a quick google search should return some. Although there are different kinds of databases, and the one you would want to look into is a relational database (or commonly known as an SQL database). This is basically just a database where you have tables. Each table can have many columns/rows. Just like a spreadsheet. And to interact with the database you use a query language called SQL (its easy to understand so dont worry you dont have to learn a new language.) I could find this video on khan academy which should make it easy to understand. https://www.khanacademy.org/computing/computer-programming/sql/sql-basics/v/welcome-to-sql
ik about tables and rows, I know what SQL is, i just don't know how to use SQL and how to make a database
thanks for the resources
In that case that link should cover how to use sql.
Hey guys, can someone validate if my sql code is syntax correct?
im pretty bad at it
@proven arrow but quick question, how do I use sql within python?
With what database?
@proven arrow explain?
what database would be best for storing warns of a user?
i want to actively modify it
discord warns?
yes
How many times will you be writing or reading from the database?
And do you want a server based database or file based? Server meaning the database needs to be installed and run on a server, and file would require no installation of anything and sits as a single file on your pc.
i can't say a set number
You can decide that whenever, but here is an example of how it can be with sqlite that comes installed with python
writing whenever they meet any criteria that is considered as a warn, and reading constantly
which is better?
!eval
import sqlite3
db = sqlite3.connect(":memory:")# make connection
# create db
db.execute("CREATE TABLE warns (id INTEGER PRIMARY KEY, user_id INTEGER, moderator_id INTEGER, reason TEXT)")
# insert 2 rows
db.execute("insert into warns values (1, 777, 123, 'reason a')")
db.execute("insert into warns (id, user_id, moderator_id, reason) values (?, ?, ?, ?)", (2, 567, 123, 'reason b'))
#fetch data
cursor = db.execute("SELECT * FROM warns")
rows = cursor.fetchall()
print(rows)
cursor.close()
db.close()
You are not allowed to use that command here. Please use the #bot-commands channel instead.
Right im not sure why it doesnt work here, but thats a simple way of how you can make a table, insert data, and select. @torn sphinx
thanks!
I updated it to show how you can pass variable parameters to it as well
A server based is normally more powerful and will be able to write more data, handle more users. Its important if you will be writing a lot to it.
You can read this when to use sqlite or when not, https://www.sqlite.org/whentouse.html
ok
im planning to switch to azure anyway
sqlite is for smaller databases?
No not always depends what your doing. That link explains it well. But most importantly, sqlite can only have a single writer at any given time. That means you cant have multiple processes/users writing data to it at the same time. So if you have a lot of requests at a time to write then you may run into issues.
And it lacks some features which a server based db has, but that you probably will never use.
Its quite common when you want embedded databases as well. Like I recently developed an mobile app, and I used sqlite as the database for on device storage, and for serving data from the api.
Could I potentially make a CRUD application with Python/Flask look like this? Or would I be better off just learning React
https://static.vaadin.com/directory/user1373/screenshot/file2102953444183057054_1530141595507ScreenShot2018-06-28at2.16.26.png
Do you mean if that is possible to do without react? Of course
But would be insanely annoying? 👀
You might get full page reloads but that's fine. Or use Ajax for it. Better to ask in #web-development
Why would it be annoying?
Truuuee
Does anyone know how you could retrieve duplicate entries? Right now I’m using SQLite and it’s only retrieving the first entry it finds.
Not sure, just my fear. I've never seen a nice flask app lol
So if I’m searching for a users warnings, it just gets the first one it finds
Thanks @proven arrow
Front end makes it nice. Flask is back end
Can you show code?
yeah one second
it said that writing takes milliseconds and you could take turns writing, so......
on the sqlite page
databaseData = [database.MRP_Blacklist_Data.DiscUsername, database.MRP_Blacklist_Data.DiscID, database.MRP_Blacklist_Data.Gamertag, database.MRP_Blacklist_Data.BannedFrom, database.MRP_Blacklist_Data.KnownAlts, database.MRP_Blacklist_Data.ReasonforBan, database.MRP_Blacklist_Data.DateofIncident, database.MRP_Blacklist_Data.TypeofBan, database.MRP_Blacklist_Data.DatetheBanEnds]
for data in databaseData:
try:
q: database.MRP_Blacklist_Data = database.MRP_Blacklist_Data.select().where(data == string).get()
except:
continue
else:
dataFound = True
break
Yeah generally it's fine. It will usually wait as well if something else is writing.
ok
I’m using peewee if that helps
I'm not sure what you're using @lavish narwhal but maybe there's a function analogous to fetchall() in psycopg2: https://www.psycopg.org/docs/cursor.html#fetchall
I meant to make it look nice it's the html and CSS or js that does that, which would be frontend.
Ah ah, gotcha
Using this to manage my SQLite database
have you tried just executing this query on the db outside your code? should it be returning multiple lines? and is this string variable appropriate for all the queries? @lavish narwhal
This is the first time I'm seeing peewee so nothing is really jumping out at me right now
Other question are you just overwriting the same variable each time?
q: database.MRP_Blacklist_Data = database.MRP_Blacklist_Data.select().where(data == string).get()
also also q: <var> = <expr> is this a python3 thing I don't know about?
@river sand sorry for the late response. But yeah, I’ve gotten multiple responses outside of the code and apparently in the example that’s how I defined the variable q
I don't think this is the problem but do you know what it's supposed to do?
this doesn't seem like it does anything
but also going back to the quickstart page, it specifies get only returns a single record
@lavish narwhal
it looks like without the get operator, the query returns an iterable
So using the second example should bring back multiple records right?
Ah!
Looks like I didn’t surf through the docs deeply
Thanks! I got an idea now on how I can resolve the issue.
Sounds good
Also for prosperity in case I was not the only one confused. this is 'enforcing' a type basically
https://www.blog.pythonlibrary.org/2017/01/12/new-in-python-syntax-for-variable-annotations/
@proven arrow any way of inserting multiple lines into a value?
can anyone help me with pandas and openpyxl?
async def follow(self, ctx, tag):
db = sqlite3.connect('./cogs/database/post.sqlite')
cursor = db.cursor()
cursor.execute(f"SELECT TAG_NAME FROM post WHERE TAG_NAME = '{tag}'")
result = cursor.fetchone()
if not result:
await ctx.send("No one created this tag, you can be the first")
return
try:
db = sqlite3.connect('./cogs/database/users.sqlite')
author_id = str(ctx.message.author.id)
cursor = db.cursor()
user_sql = "INSERT INTO users(AUTHOR_ID, TAG_NAME) VALUES(?,?)"
values = (author_id, tag)
cursor.execute(user_sql, values)
db.commit()
results = cursor.fetchone()
if results is not None:
await ctx.send(f"You're already following {tag}")
return
await ctx.send(f"Followed {tag}")
except discord.Forbidden:
await ctx.send("This tag doesn't exist anymore")
why this always overwrite table??
please help me
Does anybody know how to fix django.db.utils.DatabaseError?
Is there a way to use input(f" type function like how print works?
...what is the error
and how did you get it
i have a question regarding asyncpg and dpy
i made a async function that returns a connection pool in main.py , where the bot is initiated
and i access the pool in the cogs by - pool = await function()
and then do pool.fetch or pool.execute etc
the problem im facing is that as i load more cogs, and execute some db based commands in discord
i get an error - too many connection pools for the user - ...
so , is there a better way of doing this?
what you should be doing is making a single connection pool in your main.py, and assigning it to a bot variable
oh
after which it'll be accessible in all cogs as self.bot.<name of variable you set>
you could directly do bot.pool = asyncpg...
nice
thanks a lot
@burnt turret one more thing
i can put the pool in the init method right?
i don't remember how pools are made, but if it requires an await you probably won't be able to
one sec
aight
that's just assigning a variable
i thought you were asking how you'd make the pool itself in an init
nah, i have done that in main.py
@burnt turret new problem, how to access the pool outside of a cog, in a separate py file?
you can just import your instance of the bot there too
hmm i tried that
and?
it'll work if you get the import statement right
hmm , i will try
hi is anyone familiar with using python shelve as database?
is it possible to store pictures in shelve?
hello, from what I know there is a library in JavaScript called "sequelize" which allows programmers to r/w the db without having to remember the SQL commands. (https://sequelize.org/master/)
Is there an equivalent library in python?
Thats called an ORM. You can look up python orms.
tysm
sqlalchemy
Yeah but I went for encode/orm since it supports async which is what I forgot to mention
:incoming_envelope: :ok_hand: applied mute to @torn sphinx until 2021-01-26 15:11 (9 minutes and 59 seconds) (reason: discord_emojis rule: sent 49 emojis in 10s).
Oof
im testing it out rn
it works! thank you
but upserting didn't seem to work so I created an if-else statement to check if item doesn't exists, I just $push if it doesn't
instead of concatenation, how do you insert a value from a variable in psql
@river sand
@commands.command()
async def DBget2(self, ctx, *, string: str):
try:
database.db.connect(reuse_if_open=True)
except:
await ctx.send("ERROR: Error Code 1")
return
dataFound = False
databaseData = [database.MRP_Blacklist_Data.DiscUsername, database.MRP_Blacklist_Data.DiscID, database.MRP_Blacklist_Data.Gamertag, database.MRP_Blacklist_Data.BannedFrom, database.MRP_Blacklist_Data.KnownAlts, database.MRP_Blacklist_Data.ReasonforBan, database.MRP_Blacklist_Data.DateofIncident, database.MRP_Blacklist_Data.TypeofBan, database.MRP_Blacklist_Data.DatetheBanEnds]
for data in databaseData:
query = database.MRP_Blacklist_Data.select().where(data = string).get()
for person in query:
await ctx.send(person.DiscUsername)
Trying it this way but im getting a keyword error. Isn't this the right way of fetching all the matching records? 🤔
can .format() lead to a sql injection??
ahh sheesh it can
so much for textbooks suggesting .format()
yeah one second
ah alright 👍 i was a little afraid that wouldn't work haha i've had to use these features of mongo only very rarely
TypeError: where() got an unexpected keyword argument 'data'
Normally that error doesn't pop up.
This was my old way old way of doing it
(without an error)
databaseData = [database.MRP_Blacklist_Data.DiscUsername, database.MRP_Blacklist_Data.DiscID, database.MRP_Blacklist_Data.Gamertag, database.MRP_Blacklist_Data.BannedFrom, database.MRP_Blacklist_Data.KnownAlts, database.MRP_Blacklist_Data.ReasonforBan, database.MRP_Blacklist_Data.DateofIncident, database.MRP_Blacklist_Data.TypeofBan, database.MRP_Blacklist_Data.DatetheBanEnds]
for data in databaseData:
try:
q: database.MRP_Blacklist_Data = database.MRP_Blacklist_Data.select().where(data == string).get()
except:
continue
else:
dataFound = True
break
Only difference is that its in a nested for loop
- data == string it looks like you have a single = in the last snippet
Hm.
- according to the quickstart there's no .get()
you're creating an iterable to loop over so you should be using query = database.MRP_Blacklist_Data.select().where(data == string)
then for x in query: do whatever
alright let me test it out

It just rapid sent stuff 🤔
@commands.command()
async def DBget2(self, ctx, *, string: str):
try:
database.db.connect(reuse_if_open=True)
except:
await ctx.send("ERROR: Error Code 1")
return
dataFound = False
databaseData = [database.MRP_Blacklist_Data.DiscUsername, database.MRP_Blacklist_Data.DiscID, database.MRP_Blacklist_Data.Gamertag, database.MRP_Blacklist_Data.BannedFrom, database.MRP_Blacklist_Data.KnownAlts, database.MRP_Blacklist_Data.ReasonforBan, database.MRP_Blacklist_Data.DateofIncident, database.MRP_Blacklist_Data.TypeofBan, database.MRP_Blacklist_Data.DatetheBanEnds]
for data in databaseData:
query = database.MRP_Blacklist_Data.select().where(data == string)
for person in query:
await ctx.send(person.DiscUsername)
This was the old version of the command
https://gyazo.com/60aac691d32eb74c5ce9cbbe801c78df
But the intended solution was to show the duplicate records (so there should technically be 2 responses sent out as there are 2 records named stuff)
I'd recommend putting in a few breakpoints and seeing what the variables are around that query if you're not getting the results you expect
I've already tried using it with some breakpoints but no luck with that either.
hey guys
idk if my question should be here ... but I want to know how to store a dictionary in a csv file and then be able to read it and create the same dictionary from it
hey I got a certain key that is referenced as a foriegn key in a lot of other tables. any way to drop that key in one table, and have it get removed in all the others with it?
preferably in one statement
Some one recently mentioned an asynchronous sqlite package for python in the #discord-bots but I can't remember the name of it. Can someone please point me in the right direction?
You'll have to drop seperately
It's probably Aiosqlite
anyone know why this won't work
select * from question where question.quizname, question.quizowner in (select quizname, quizowner from quiz_keys where quiz_key=4269);
figured it out
I've also found some things online that say sqlite has an official extension to make sqlite "asynchronous" but really it just runs the database writes on a separate thread. Which is the better one to use?
And thanks for your help Pilot
can anyone help me with postgresql
im kinda having trouble with it not recognizing it in path
nvm windows doing weird stuff
depends on what you want to do with it
Store data in it
im using postgreSQL for a multipurpose discord bot, what do you recommend: a big table with everything in it, or small tables for each section of the bot like economy, level system, inventory
it would be kinda weird to just create the data for the user with its id in every single table
also is there some sort of documentation to use for postgreSQL with python
i dont seem to find any
and ive been searching for a while now
usually u would like to have a table for each class
Does anyone have any suggestions for how to model an "availability schedule" for a user, similar to a square site booking calendar. Listing time slots of different lengths for each day, available/booked, , and storing them efficiently for each user? Eventually I plan to fetch/set this info over a Djano REST API i am creating but have been stuck on it a couple weeks now
what library should i use to connect to a sql database?
@torn sphinx for discord, aiosqlite
im already using postgresql
which is good ig
it also has this async thing support with another library
but for now i want to connect to it
o nvm asyncpg does that
cool
yes, use asyncpg
maybe you would wanna have an event class with a start and end date, including time and a field participants and calculate the booking status for each user based on that information. not sure how to model an available block but that could be a special type of event.
Pasting large amounts of code
If your code is too long to fit in a codeblock in discord, you can paste your code here:
https://paste.pydis.com/
After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.
Hey @slender isle!
It looks like you tried to attach a Python file - please use a code-pasting service such as https://paste.pythondiscord.com
your db name is dbname in mongodb?
just make sure to add the proper dbname
and it might work
hey
can anyone tell me why im getting an error when running this
mycursor.execute("CREATE TABLE LoginTbl (UserID int IDENTITY(1,1)PRIMARY KEY, Username VARCHAR(20), Pword VARCHAR(20))")
@coarse ether thanks i will test
@slender isle is it working
1 minute
where should ı look at it
Let i look
@coarse ether its like this
@coarse ether what should i do now
error is this
in mongoclient change dbname to socialmedia
but where is mongoclient
in google
or in code
i will chance
?
@coarse ether
Heyo how do u guys know where a website is taking its data from, like for example covid19.who
@slender isle send code via text
what database are you using?
sqlite
Sqlite doesnt have identity for auto increment
They have the wrong syntax if they are using sqlite
DOes anyone know why I getting this error? 
conn.execute(f'''INSERT INTO prefixes (id, prefix) VALUES ({ctx.guild.id}, "{prefix}")''')
sqlite3.IntegrityError: UNIQUE constraint failed: prefixes.id```
oh
understand
so i need INSERT INTO prefixes (prefix) VALUES ("{prefix}") WHERE id == {ctx.guild.id}?
it's always in the table
There's no way of knowing for sure. But some governments have made public apis to get this data
Ok
Hey @karmic parcel!
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:
from datetime import datetime
from termcolor import colored
cluster = MongoClient("mongodb+srv:Yusuf_Mert:<Murat1299>@socialmedia.knkvp.mongodb.net/<dbname>?retryWrites=true&w=majority")
db = cluster["socialmedia"]["messaging"]
all = db.find({})
date = datetime.now().strftime("%x")
for messages in all:
try:
if date != messages["date"]:
print(colored(f"Today - {messages['time']}", 'red'))
else:
print(colored(f"{messages['date']} - {messages['time']}", 'red'))
print (colored("From: ",'green'), messages['id'])
print(colored("Message: ", 'green'), messages['message'])
print("--------------------")
except:
pass
person = input("Name: ")
message = input("Message: ")
time = datetime.now().strftime("%X")
msg = {"id" : person, "message":message, "date":date, "time":time}
db.insert_one(msg)```
@coarse ether
whats the problem
what is error?
it crashes on migration.. I am using MongoDB with Djongo
This
so problem is in how you connect to database
@torn sphinx ab türk müsün
this line you need fix
How can i fix it
from datetime import datetime
from termcolor import colored
cluster = MongoClient("mongodb+srv:Yusuf_Mert:<Murat1299>@socialmedia.knkvp.mongodb.net/socialmedia?retryWrites=true&w=majority")
db = cluster["socialmedia"]["messaging"]
all = db.find({})
date = datetime.now().strftime("%x")
for messages in all:
try:
if date != messages["date"]:
print(colored(f"Today - {messages['time']}", 'red'))
else:
print(colored(f"{messages['date']} - {messages['time']}", 'red'))
print (colored("From: ",'green'), messages['id'])
print(colored("Message: ", 'green'), messages['message'])
print("--------------------")
except:
pass
person = input("Name: ")
message = input("Message: ")
time = datetime.now().strftime("%X")
msg = {"id" : person, "message":message, "date":date, "time":time}
db.insert_one(msg)```
replace this with your code
Ok
in this line mongodb+srv:Yusuf_Mert:<Murat1299>@socialmedia.knkvp.mongodb.net/<dbname>?retryWrites=true&w=majority you had to edit this to this mongodb+srv:Yusuf_Mert:<Murat1299>@socialmedia.knkvp.mongodb.net/socialmedia?retryWrites=true&w=majority
see <dbname>
@slender isle did it work?
it happesns on migration...... I tried reseting the DB but had no luck. I think its super simple problem but imm new to Django and Mongo can't seem to figure out how to fix it .
@karmic parcel can you even use mongo with django?
your error is trying to do sql statement which not work with mongo
how do I connect my pymongo code to motor?
what do I have to import and what do I have to type and what do I have to connect?
yes you can through djongo package
hm I haven't worked with Djongo, sorry
but why do you want to use NoSQL with Django?
no paticular reason. It was working fine until recently when i added new app
no
We've gone from spamming social media to making one...
Thanks (for non copyright music):
Colorful Flowers by Tokyo Music Walker https://soundcloud.com/user-356546060
Creative Commons — Attribution 3.0 Unported — CC BY 3.0
Free Download / Stream: https://bit.ly/al-colorful-flowers
Mu...
i look at this
@coarse ether
but
can you write this codes and send me
because i have error
and it cant fix
@coarse ether
@torn sphinx
can you help me
@torn sphinx can you send me your discord server
@slender isle replace that line with this mongodb+srv://Yusuf_Mert:<Murat1299>@socialmedia.knkvp.mongodb.net/socialmedia?retryWrites=true&w=majority
and it will work
just noticed now that you had missed mongodb+srv://
first of all install dnspython
and add the above line
for me it worked
you need the package dnspython and the problem is with this mongodb+srv:Yusuf_Mert:<Murat1299>@socialmedia.knkvp.mongodb.net/socialmedia?retryWrites=true&w=majority edit this to mongodb+srv://Yusuf_Mert:<Murat1299>@socialmedia.knkvp.mongodb.net/socialmedia?retryWrites=true&w=majority
Heya! Anyone on right now have experience with setting up Motor for use with MongoDB?
db = await aiosqlite.connect(...)
cursor = await db.execute('SELECT * FROM some_table')
row = await cursor.fetchone()
rows = await cursor.fetchall()
await cursor.close()
await db.close()``` I wrote this, following an article. Can someone explain what it does?
it defines all your connection parameters to an asynchronous db
What does the ... do in the ()
that is your connection url
so i need to change that?
it is just that if you are connecting to mongodb atlas then your username and password will be seen
and hence the code is written with ...
yes you need to change that
which db are you using
where do i get my connection url
aiosqlite
replace it with "file.db" and create a file.db
ask your question
The ...?
yes
ok
I'm trying to set up the connection parameters in a cog but I'm not sure about exact syntax to convert to a self.variable that I can access
db = await aiosqlite.connect('file.db') So what i have now is this
and create the file file.db
unless I just do something like self.db = connectioninfo
just run the code it might only create
alr
if any errors send the image here.
db = await aiosqlite.connect('file.db')
cursor = await db.execute('SELECT * FROM some_table')
row = await cursor.fetchone()
rows = await cursor.fetchall()
await cursor.close()
await db.close()``` I got an error: `await` outside function, but the article never said to put it in a function
guessing this is dpy
i'd recommend you make only one instance of AsyncIOMotorClient for your entire bot - in your main bot file and assign it to a bot variable
loaddb() async {
db = await aiosqlite.connect('file.db')
cursor = await db.execute('SELECT * FROM some_table')
row = await cursor.fetchone()
rows = await cursor.fetchall()
await cursor.close()
await db.close()
}
loaddb()
run this
ok
python right
yes
after this it will be accessible in all cogs as self.bot.<db variable>, and in other files as bot.<db variable> (without the <> that's just a placeholder)
loaddb() async { Invalid syntax this line, at the async
yes sorry bout that js syntax
db = await aiosqlite.connect('file.db')
cursor = await db.execute('SELECT * FROM some_table')
row = await cursor.fetchone()
rows = await cursor.fetchall()
await cursor.close()
await db.close()
await load()
alr thx lemme try
make sure of formatting
so I define it similar to how I did word interval here:
client = commands.Bot(command_prefix='?')
client.word_interval = 7
just with a database or something variable?
you'd need to await load there, but you can only await coroutines inside other async functions 
yes,
client.db = AsyncIOMotorClient(...
yeah i just got error imma put await
thanks edited
and now it says that an await is outside the function
yeah you can't await outside an async function though
still await outside the function
this has become a basic #async-and-concurrency question now more than dbs, but i think you have to do loop.run_until_complete on load
loop.run_until_completion load()?
that doesnt look right..
@burnt turret ello?
hello*
ok
see this url https://stackoverflow.com/questions/40143289/why-do-most-asyncio-examples-use-loop-run-until-complete
I was going through the Python documentation for asyncio and I'm wondering why most examples use loop.run_until_complete() as opposed to Asyncio.ensure_future().
For example: https://docs.python.o...
or you can use asyncio.run(load())
It says loop is not defined
use asyncio.run(load())
you'd need bot.loop.run_until_complete(load) then
alr
assuming your instance of commands.Bot is called bot
yes
TypeError: An asyncio.Future, a coroutine or an awaitable is required error
🤔 i'm not really sure anymore, move to #discord-bots and show the code, this has gone off-topic for this channel
alr
@indigo smelt see this link https://stackoverflow.com/questions/59481105/typeerror-an-asyncio-future-a-coroutine-or-an-awaitable-is-required
ok
loop=asyncio.get_event_loop()
loop.run_until_complete(load())``` if this still does not work move to [#discord-bots](/guild/267624335836053506/channel/343944376055103488/)
i mean, get_event_loop will be getting the same loop as bot.loop, the difference is the load() instead of load - my bad there
?
sqlite3.OperationalError: no such table: some_table Alr new error
make the table
Anand if you have a moment - what is the best practice for setting DB connections and making calls if I plan to have numerous collections each taking different data?
the problem is you should create a table in the db
table or list
table
cursor = await db.execute('SELECT * FROM some_table')
error comes here
some_table is not defined
that is it
ok
want me to give a sample table
db
I can quickly create one via django
and send the file to you
table = newTable()``` This is how you create a table if I am not mistaken?
wait
yes
cursor = await db.execute('CREATE TABLE IF NOT EXISTS some_table')
edit the line to this
ok
that way it will create the table
now edit the line to ```cursor = await db.execute('SELECT * FROM some_table')
alr
It says there is no table
sqlite3.OperationalError: no such table: some_table
is file.db present
shouldn't the script create it?
yes did it create it
i don't know how can i check
which editor are you using??
an IDLE
send your project directory image from files.
you need to specify columns as well
so what do I need to do.?
exactly that lmao you need to specify the columns as well
in that statement you're only telling to create a table but not telling what the columns of the table are
https://sqlbolt.com check this site out, it'll teach you the basics of SQL
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
so what columns do i need to add 🤔
only you know, you make the columns you need
aiosqlite is a database driver module, which uses sqlite as a database. sqlite and other relational databases uses sql as the query language
so, you need to know sql, if you want to use sqlite - and you're using sqlite, through aiosqlite
ok
@burnt turret Are there any videos etc that would be useful in understanding better the workflow for DB interactions? Relatively new to it, I've done some DB work but mostly with Django where Django did most of the work
I have questions but I don't want to barrage you with simple questions for answers a simple video could provide
is this specific to mongodb?
actually whatever db it is i wouldn't know about any good videos for it - i was taught mysql and mongodb at school, so i haven't really learnt much from other sources than school and documentation for these
i wouldn't mind really haha, you can ask here anytime and i'll try to explain to the best of my knowledge
Well, I want to create a series of methods to connect/access DB but without experience/knowledge of how/why I would want to do certain things I'm finding it hard to think ahead, I've got my entire script neatly compartmentalized into classes/cogs, I want to create just a few basic methods to access DB and then call those methods depending on what I need without having to rewrite the same lines of code every time I call DB lol
I feel like a bumbling idiot haha
🤔 let me think if i've seen anything good for mongodb
there's probably a freecodecamp video on it, they're generally good and explain in detail
this feels like something you'd use an ORM for but i don't know if there are any for mongo 🤔
stands for an Object Relational Mapping, it's what you're familiar with in django
using classes to represent the database tables
so i've never used this so i cannot vouch for it but apparently mongodb has something called ODMs instead (the D stands for document in this case)
i see two on the documentation - https://umongo.readthedocs.io/en/latest/ and https://motorengine.readthedocs.io/en/latest/ (these both seem to be asynchronous)
will that work with Motor?
oh I was already planning on using Motor
convenient that that is the second you had suggested haha
oh motor isn't the same as motor engine
motor engine will be similar to what you were used to doing in django (with the use of classes) i assume, while motor is closer to actual mongodb syntax
So I can remove the regular motor code I had written and uninstall the package in favor of this one you think?
i wouldn't vouch that hard for it haha i've never used it - i was just presenting you with the options, especially because you aim to try and create something like that anyways (even though on a smaller scale)
well I mean, the amount of code I had implemented is not exactly a dealbreaker haha, I had like 15 lines tops so far
:p
I'll give it a try, appreciate the advice Anand!
It seems like MotorEngine uses Tornado, I saw that when I was reading the Motor docs that there was a distinction between that and asyncio so I'm not sure here, let me glance back at it
oh right yeah, motor does seem to make a difference between tornado and asyncio, but i don't really know the difference either, having only used asyncio myself
@burnt turret heeeelllppp
ask your question, someone will answer if they can
Can you send me all codes without your db name
@coarse ether
from datetime import datetime
from termcolor import colored
cluster = MongoClient("<dbname>")
db = cluster["socialmedia"]["messaging"]
all = db.find({})
date = datetime.now().strftime("%x")
for messages in all:
try:
if date != messages["date"]:
print(colored(f"Today - {messages['time']}", 'red'))
else:
print(colored(f"{messages['date']} - {messages['time']}", 'red'))
print (colored("From: ",'green'), messages['id'])
print(colored("Message: ", 'green'), messages['message'])
print("--------------------")
except:
pass
person = input("Name: ")
message = input("Message: ")
time = datetime.now().strftime("%X")
msg = {"id" : person, "message":message, "date":date, "time":time}
db.insert_one(msg)```
Thank you
it was just the same name wait I will send the code for your db
mongodb+srv://Yusuf_Mert:<Murat1299>@socialmedia.knkvp.mongodb.net/socialmedia?retryWrites=true&w=majority add this to <dbname>
it must work
Can you test is it works with my db
if anyone can help in #help-kiwi it would be appreciated
My mind is in a pretzel right now. I am trying to create a many to many relational database. I created the two separate tables but now need to create a joining table. Do I add information to that table the same way I would any other normal table?
Anyone with experience in setting up/using μMongo / MotorEngine for Async MongoDB connection in dpy? I'm trying to set it up and am kind of overwhelmed, hoping to find someone to bounce occasional questions off in implementation
import os
import sqlite3
conn = sqlite3.connect(os.path.abspath("source/data/py-deposit.db"))
db = sqlite3.Cursor(conn)
sqlite3.OperationalError: unable to open database file
can anyone help me? im trying to connect to sqlite database but it will always say that the path is invalid, the only path I’m able to use is the relative path, but I want to be able to execute the script from the parent directory
@polar sandal Yes you would add in the same way. Although to make the relationship you only need to add data to the joining table. Assuming the values you reference, exist in the other tables.
@jolly trench you need to use ../ to refer to parent directory.
conn = sqlite3.connect(os.path.abspath("../py-deposit.db")) should work
K I'll try it out later
is there a way to enter a set of values into a table only once ?
Alright thank you. That definitely clears things up for me
Can you explain further? If you want a column to only have unique values then you can add the unique constraint to it.
ooh right so if i dont want an error i cant just put it under try and except right? because it raises and error when i used unique constraint
okay it worked thanks
Well that is one way. But you can also have SQL ignore any errors when your try to insert.
just bumping this request in hopes someone sees it who has experience in either implementation 🙂
best way to master SQL? anyone have any good resources?
How proficient are you at the moment? The best way would be to just practice on different problems.
Well yeah because you can learn SQL, but the advanced queries or scenarios will only come when you are faced with certain problems, which you may not come across whilst going through tutorials.
For beginner take a look at this site which is good for getting comfortable with using SQL. https://sqlbolt.com/
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
If you want to take a look at some advanced queries then you can have a look at http://www.artfulsoftware.com/infotree/queries.php?w=378
It shows show common advanced problems that you might come across
Hi all I got a strange thing with sqlalchemy, If someone got an idea..
toimport = db.query(Csvfiles).filter(Csvfiles.parsed == False).all()
is translated to :
WHERE csvfiles.parsed = 0
this is the expected behaviour, but
toimport = db.query(Csvfiles).filter(Csvfiles.parsed is False).all()
is translated to :
WHERE 0 = 1
does someone knows why.. because the second sentence is more "pep8"..
I use a sqllite.
answer.. https://stackoverflow.com/questions/18998010/flake8-complains-on-boolean-comparison-in-filter-clause
is anyone here good with mysql in python?
@pearl adder working on an sqlite3 project right now, i could try and help
i also need help, i have some tables, but i need to store per-guild per-user data. how would i do this in a database?
i am storing pictures that relate to a user, so i need to be able to store like 100+ pictures per-user
can you have mutliple cursors in mqsql because it seems to crash everytime i do
for that i'm not sure, currently i'm just making a high-level system to easier edit my database with functions n classes
how can I delete data from sqlite after amount of time from saving it?
- save data
- save timestamp of when data was saved
- check every now and then if it's been 60 seconds or so
- if so delete data
- ???
- profit
I did all that steps but idrk how it was supposed to delete after specified time
@tasks.loop(hours=24)
async def check_for_membership(self):
db = sqlite3.connect('./cogs/database/premium.sqlite')
cursor = db.cursor()
cursor.execute(f"SELECT * FROM premium")
current_date = datetime.now()
for row in cursor:
member = self.client.fetch_user(int(row[0]))
member_id = row[1]
date = datetime.strptime(row[2], '%Y-%m-%d %H:%M:%S.%f')
if current_date.date() - date.date():
print(f"Premium Ended for {member}")
cursor.execute(f"DELETE FROM premium WHERE User_IDs='{member_id}'")
db.commit()
I want it to auto delete any data after 30 days but Idk how or where to put "30"
You are not allowed to use that command here. Please use the #bot-commands channel instead.
ok bot
how to get unix time
then
just check if the current unix time is whatever 30 * 86400 is above the old one
thx
where's your query
it is returning the correct document, yes?
just it's giving you all of the fields and you only want some of them?
So you need to specify what fields you want specifically using a projection
inventorydoc = settings.col.find({"serverid":message.guild.id,"playerdata.userid":message.author.id}, {'playerdata': 1})
try that
it should return the playerdata field, and its subfields
yes
just started learning postgres super happy about it
hey guys im trying to get an xlsx file using an online link
heres the link
and heres the code `import requests
import xlrd
url = 'https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwj_9_fVwb7uAhXTNcAKHXkXAtQQFjAAegQIARAC&url=https%3A%2F%2Fwww.rki.de%2FDE%2FContent%2FInfAZ%2FN%2FNeuartiges_Coronavirus%2FDaten%2FFallzahlen_Kum_Tab.xlsx%3F__blob%3DpublicationFile&usg=AOvVaw356d2ETTlb6cb1E4GaZI0c'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0'}
page = requests.get(url, headers=headers)
workbook = xlrd.open_workbook(file_contents=page.content)
worksheet = workbook.sheet_by_index(0)`
it raises the error Unsupported format, or corrupt file: Expected BOF record; found b'<html la'
how can i fix that?
guys how do i script a database diagram in sql server management studio with the datatypes
I am making a python app with google cloud MySQL. It all worked fine, but for some reason at some point it just stopped working and gave me this error:
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'IP' (10060)
When I try to connect to it from the cloud shell on https://console.cloud.google.com/, it works, so I think it's because google cloud SQL is blocking connections from my IP. But after I allowed connections from my IP and 0.0.0.0/0, it still gave me the same error.
Any help will be appreciated.
Google Cloud Platform lets you build, deploy, and scale applications, websites, and services on the same infrastructure as Google.
Anyone familiar with MongoDB (and PyMongo)?
I need to insert a list of documents into a collection.
I am using: ins = database.collection_name.insert_many(json.loads(json_util.dumps(pythonListOfDictionaries)), ordered=False)
However, some document's IDs will already be present into DB, in such case, it should update the already-present documents.
Therefore, it seems that that update_many with upsert=True is the correct function
But update_many asks for a "filter" in the parameter, I don't need any filter, just insert the documents and update if any doc is already there
Not sure how to use update_many
Anyone can suggest the way?
@unique dove How can Mongo know if it should insert or update if it does not know if it exist or not? Because, when you insert a entry to MongoDB, duplicates are allowed per default. There are no unique indexes except _id which is generated by default (for mongodb to keep track of stuff..)
So, you need to pass a filter= on a field to check if it should update or insert the entry.
@restive pilot I didn't mention it, but every entry in the list of dictionaries has a "_id"
I am currently using insert_many and try/except/pass block, so that if a doc exists, it doesn't insert it, if it doesn't exist, it inserts it. The try/except/pass block under which insert_many is, is to avoid the python script throwing an exception in case it finds duplicates.
This works, but now I want to update the code so that if some docs in the list already exist (_id), it should just update them, if not, just add them
update_many works differently (for example upsert starts coming into play, and the required "filter" also)
@unique dove I've written some example code for you:
https://pastebin.com/rGq26eWp
Basically, create many ReplaceOne (replace with updateOne if you need to keep existing data) operations and perform them in bulk using bulk_write
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
@restive pilot Wow thanks, so much new (to me) stuff there
So, the principle is that you need to filter on something for the database to understand what you want to update
How efficient is this on big collections compared to my current method (that works only partly, doesn't update, insert_many with unique indexes to avoid duplicates and try/except/pass)
bulk_write is more efficient as all the operations will be sent as one command for the server to process instead of many individuals.
I see
One nice thing with bulk_write is that you mix replaceone, updateone and so forth in the same process 😄
But really, shouldn't be that much of a difference compared to updatemany
Regarding line 27: {"_id": book["_id"]}
I am using a custom index ("IDD")
If I create the index in mongodb, can I just replace that with {"IDD": book["IDD"]} and expect the same performance as with _id ?
Yes! As mentioned _id is the unique field in MongoDB, I'm using that as a filter key 🙂
You probably want to indexIDD in your DB to make sure things go blazing fast
Regarding "But really, shouldn't be that much of a difference compared to updatemany"
Not sure I understand this part, does it mean that it could have been also done with update_many?
Yes, but then you are limted to update operations only (and insert if it does not exist if you pass upsert=true)
With bulk_write you can mix and match all the operations
i need a help
Thanks @restive pilot !
Np! 🙂
I managed to solve it - I didn't realize I was using a vpn, which didn't allow the connection for some reason
hey guys so i have a downloadable link right here that when u click on it it automatically download the .xls file. How can i open and save this file somewhere with python?
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwj_9_fVwb7uAhXTNcAKHXkXAtQQFjAAegQIARAC&url=https%3A%2F%2Fwww.rki.de%2FDE%2FContent%2FInfAZ%2FN%2FNeuartiges_Coronavirus%2FDaten%2FFallzahlen_Kum_Tab.xlsx%3F__blob%3DpublicationFile&usg=AOvVaw356d2ETTlb6cb1E4GaZI0c heres the link when i copy the link
why is it not working
Try pip instead of pip3
it's still not working
Make sure python is in path @kindred crow
why is this showing every time i open mysql?
you mean why is it asking for a password
I am having difficulty understanding how to set the following up.
and even a harder time trying to explain what I'm trying to do.
long story short, imagine a table called desk and several tables each representing a device - phone, fax, printer, laptop.
In python you can have a list called devices and reference the class object item to include all three different types of those devices.
In sql, I'm having a hard time understanding how to reference multiple tables to one table called devices.
Could you point me in the right direction on how to achieve this?
no above that
the image is just a visualization of the flow not a literal setup
db=c.connect(host='localhost',user='root',database='abc',passwd='//pass//')
mc=db.cursor()
mc.execute('show tables')
for i in mc:
print(i)```
im trying to run this-
but
why is it not working?
Dunno if this is the BEST place for this question, but would it be a better idea to use dictionaries/lists, or an XML file for handling storage of multiple-user's input (all input comes from discord)
how do i output just one field from mongodb document?
Like, if i have multiple fields in a document, how do i output just one single chosen field?
Or how do i find the number of elements when i go through Cursor object with for
Select one field or get one field?
Selecting is passing a second object to the find .find() method with fields you want.
E.g .find({},{title:1})
Le' me try
You can add .count after .find() to get the number of documents returned by the query
It's all in the docs honestly
https://docs.mongodb.com/manual/tutorial/query-documents/
When you say reference do you mean link the tables?
yes. I suppose that is what I'm trying to do.
You can achieve that through a foreign key column.
Which is basically a column you can add in your table so that it points to another row in another table, creating a link.
I'm using SQLAlchemy. I have a config file where Base, engine, and session are defined. In my models file, i import Base and session from the config file. I use the imported session to make class methods like this:
@classmethod
def find_by_name(cls, search):
return session.query(cls).filter(cls.name == search).first()```
In my unittest file, I want to use a different database (i.e. sqlite in memory), and make a session based on that. I'm not able to figure out how to make one session for regular use, and a different one for testing, and then to import the correct one into my models file.
I could add session as an argument to the class method, but I feel like there should be a better way.
Im using asyncpg with my pgSQL db, should i .close connection each time i have finished adding, deleting or updating the data in the tables? or just leave it active to be re used later on?
i've seen you ask questions before about using this for a discord bot so im gonna assume this is also for the same purpose - you should make a single connection pool and use that instead of a connection, and not closing it after every operation
Hey all
I wanted to learn how to use database ?
How can I do it?
Which what databse I should begin?
depends, what type of data do you want to store?
how can i delete rowid via varible eg. ```python
delete = input("type your number to delete")
and use the delete to delete the data
my code ```python
delete = input("type the number you want to delete? \n\n")
c.execute("DELETE FROM password WHERE rowid='(delete)';")
print("successfully deleted ",delete)
anybody got a solution
i use sql
ok 😢
its been 35m
what database is this?
so what is happening now is that you're trying to delete a row where the rowid is literally the string delete
but you want it to have the value of the variable delete,
how do i do that
basically you are supposed to keep a placeholder value there, and the delete is passed as a separate argument
i have tried to do: ```py
delete = input("type the number you want to delete? \n\n")
c.execute("DELETE FROM password WHERE rowid='(?)';",(delete))
print("successfully deleted ",delete)
i'm not sure what the placeholder is for sqlite but it might be something like
c.execute("DELETE FROM password WHERE rowid=?;", (delete,))
yep that's almost right, but you won't have to add those single quotes yourself
moreover what went wrong there is that doing (delete) doesn't make it a tuple
but the execute function wants it's second parameter to be a tuple - so to make a single element tuple we write (delete,)
!e
a = (10)
b = (10,)
print(type(a), type(b))
You are not allowed to use that command here. Please use the #bot-commands channel instead.
i always forget, but that was supposed to demonstrate that the first one without the comma wouldn't be a tuple but the second one would be
i'm guessing the rowid column type in your database is an integer
input() function gives you a string
cant i just add int(input())
yes that's what you have to do
here comes the problem with that ``` c.execute("DELETE FROM password WHERE rowid=?;",(delete))
ValueError: parameters are of unsupported type
oh yeah fogot to edit the code
I'm using psql, how can I define an autoincrement non-unique id?
non-unique?
to autoincrement you'd be using something like SERIAL
yes, I tried this, but doesn't work @burnt turret
CREATE TABLE IF NOT EXISTS report(
server_id integer,
report_id serial primary key (server_id, report_id);
so report_id would have a different autoincrement value for each server
and start from 0 for each server
🤔 i'm not very clear on what you're trying to do, you want to start the report_id from 0 for each server?
yes, and then increment it by 1 independently, for each submitted report for specific servers
i don't really know how you'd do that, maybe look into GENERATED columns or something
also, i don't see how report_id could be a primary key in that case as the column could have multiple rows of the same value
did you commit to the database?
what database?
sqlite @burnt turret
you use INSERT to add data to a database, while you use SELECTs to get data from it
https://sqlbolt.com/ , it is a series of interactive lessons that you can try right in your browser, to quickly get started with SQL.
https://pgexercises.com/ is also an interactive online tutorial, but specifically for learning PostgreSQL.
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
i have two tables, each table has a category column.
Both tables is storing information on products at a specific location store. How i can get all different categories from these tables and remove duplicates?
so result should give me only categories where there is a product for it in a store
thanks
didnt know about connection pools, i guess i can just add connections in every file i have thats part of the discord bot
it'd be better to initialize one connection pool in your main bot file, and assign it to a bot variable
after which itll be accessible in all your cogs as well
is that a thing?
why would i make that up haha
i mean i never heard of that
and i just searched that
and there is nothing
is it in the discord.py documentation?
Need to keep track of a variable between functions? No problem!
⚠️ Careful what you name it though, else you might overwrite something ⚠️
Just add it to your commands.Bot or discord.Client instance like so:
bot = commands.Bot(...)
bot.my_variable = 0
async def foo():
bot.my_variable += 1
# In a cog
@commands.command()
async def counter(self,ctx):
await ctx.send("Current Counter is at {}".format(ctx.bot.my_variable))
This also allows you to access this from other cogs/extensions/functions. Anywhere you have access to the bot instance
i'm not sure where it is in the documentation
is it as simple as that?
yes
that will be really useful
how in sqlite3 names the dict or list type?
How can I use derby database in python?
userlist = users_col.find({"_id":{"$regex":f"^{myvar}"}}).limit(10).where()
can you help me set up $where, I want to get users who has value x in their inventory (inventory is an array of dicts)
@burnt turret 🥺👉👈
:incoming_envelope: :ok_hand: applied mute to @torn sphinx until 2021-01-29 19:16 (9 minutes and 59 seconds) (reason: duplicates rule: sent 4 duplicated messages in 10s).
🤔 can you show a general inventory?
I just did that now testing it rn
i'm about to head to bed rn haha i'll try my best to help
thx 
show this, and explain in a little more detail what you're trying to achieve
i haven't used $where much, so i could try thinking of some alternate way
documentation seems to say you could be giving it javascript code too to match which looks pretty useful but i don't really know well how to use it
https://docs.mongodb.com/guides/server/read_queries/#procedure
https://docs.mongodb.com/manual/reference/method/db.collection.find/index.html
and check out dot notation or nested form if you want to query a document with an array
i'm not the one who asked the question here
it's a really fast key-value store?
I know what . does like user.inventory
but what does .$ do
like user.$.inventory
OperationFailure: unknown operator: inventory, full error: {'operationTime': Timestamp(1611949079, 1), 'ok': 0.0, 'errmsg': 'unknown operator: inventory', 'code': 2, 'codeName': 'BadValue', '$clusterTime': {'clusterTime': Timestamp(1611949079, 1), 'signature':}}
why does this raise this error
how do I count how many documents contain a query with motor(mongodb)
@torn sphinx the $ sign is reserved for special operators :
https://docs.mongodb.com/manual/reference/operator/query/
e.g $elemMatch for query:
https://docs.mongodb.com/manual/reference/operator/query/elemMatch/index.html
so you can query multiple conditions
I have this query: users_col.find({"_id":{"$regex":f"^{myvar}"}}).limit(10).sort()
I want to sort with a dict key value (descending)
an example inventory:
inv = [
{"name:":"example", "amount":5},
{"name:":"example2", "amount":2},
{"name:":"example3", "amount":0} # these dicts are examples of an item object
]
I want to sort by amount of "example" item
what should I do
I've been using sqlite3 for a while now since my discord.py bots have only ever needed to store text basically, but now I'm wanting to store datetime and I was wondering what the best option would be for a simple python db to use?
I know that sqlite can store datetime as a text type, but I thought I should maybe try something different that actually supports datetime
it's always good to do new stuff, but I do wonder why you would need the db to support that, surely you can use it as a string using strXtime etc?
Yeah, I just thought I should maybe learn to use a db that supports the data type datetime, but I could also just store it as a string
I'll try the string way first and see how it goes, just seemed a bit messy changing it to text and back
it can be not messy if you have a function that does all the ugly stuff in the background when you call it. But yeah I get what you mean, even that code itself would be a bit messy. I am also a noob, I first set up my project on mysql, and now I rebuilt it with mongodb
yeah I was thinking of just having a method somewhere to do that for me haha, thanks
Also, I often open my connection at the start of my file, but should I only be starting a connection when a specific method needs it?
I have multiple files that will use the database, and each of them starts a connection with a cursor and I wondered if it was bad practice
I've used 'with' for opening json files, usually something like:
with open("my_file.json", 'r') as f:
token = json.load(f)['TEST']
I'm guessing you would just open a connection at the start of each method?
so i think here you would use something like
with conn.cursor() as cur:
cur.execute(query)
fetch = cur.fetchall()
print(fetch)
return fetch
this is the code I used for mysql
tbh not sure how exactly this would work in sqlite
in sqlite I usually open in at the start of the file like
conn = sqlite3.connect("evo_db.db")
c = conn.cursor()
then use c to execute SQL
so yeah it would be the same I think
that is, your first line
conn =
and then with conn.corsur() as c:
so should I ever close my connection?
that does the open and close for you on a per need basis
ohhh right
as it does with files
I'll give it a try later on
It's just because some of my methods have timers, so they may have a connection open for 1 minute, and I was wondering if I would be able to have another method access the db while the other one was still connected
that's a question indeed. what does google say about it?
Ohh that's really useful to know then, thanks!
How can I remove data entries where I remove the older entry and keep the newer entry? I’m using autoincrement for ids.
@solemn root try giving a bit more details
I want to replace data in a table through variables. Ex. (Autoincremented id, user_cash, user_token) = (1, 45, 677344363535264534) and replace that with (1, myPurse, author.id), so the data consistently has 1 entry per user and not thousands of individual logs recording each interaction
I can’t figure out how to have the database remove the oldest entry if there are 2 entries by the same user, and to remove the oldest one. (Ultimately what I want to do)
Ideally something like this:
|1|45|55796434697575657|
|2|57|46975756575579643|
•
•
•
Replace(...)
|1|76|55796434697575657|
|2|106|46975756575579643|
•
•
•```
currently get either deleting very first data entry (w/o a second one to prompt deletion) or TypeError from Replace function
e.g
user_data = [{"id": 1, "cash": 1337, "token":1111111}]
user_data[0]["cash"] = 1338
print (user_data[0])
but it highly depends on ur database 😛 What type of database are you working with? E.g mongodb which is document based?
sqlite3
Also, don't focus on removing and inserting, focus on updating by index instead
@solemn root
Looks like you need some introduction to DB since you don't understand the fact that you are supposed update an entry instead of deletion and insertion again
E.g some quick reading should do it: https://www.ibm.com/cloud/learn/relational-databases#toc-what-is-a--8q6isCrS
So Sqlite3 is a RDBMS, so things are stored in a table using rows and columns.
So, the for example the id column would hold a unique value that can't be changed, the next column user_cash (in ur example) is a integer (signed) that holds digits. so, whenever you want to update the user_cash column (change it) you first find the row that is unique for the user by the id column and then you get the data for from the row. When you have the data you change it.
Once you changed the data, you go again and update the database with the changes based where the row is equal to the id column
@solemn root maybe this can help you get a more understanding of how things works with SQL: https://zetcode.com/db/sqlite/
Hi all, have any of you guys built a database from scratch?
I am looking for a course that could guide me through the whole process cause I am teaching myself database.
@restive pilot How can I set user_cash = myPurse ? myPurse is another variable I have which controls user_cash, but I have to write my execute lines like this:
crsr.execute("""UPDATE mytable SET user_cash = myPurse WHERE (not sure what expression I need here to grab any unique id) """)
read what I wrote again. It's a simple thing. Have you used Excel before?
yes
Excel is pretty much the same thing, to find a row to update you first find the column and then the row matching that value you want to update
but multiple rows can have the same value, but you know that id column is unique right
so id column can't have two values of the same, so use that column to find the data. once you have the data you probably have a dict{} which is a key-value pair. Update the value in that dict and then grab the id from id dict, and execute a UPDATE-statement WHERE ID = to the ID from the dict that you got earlier when you first grabbed the data
@solemn root https://zetcode.com/db/sqlite/datamanipulation/
I mean, I could give you the answer, but then you wouldn't understand how a RDBMS work and once you need more complex statement, you would have issues again.
everyone starts somewhere! 🙂
why is python3.9 unable to locate mysql module, I have installed connector but it's showing notfounderror. However if I check from the terminal its ok but when I use pycharm it's not
pls help
same with me I've installed mysql server but at time of connecting my database in my python program it says bad handshake error. May be because of version difference. but in netbeans I'm using wamp server and that mysql database I'm trying to connect with my python program.
I've no clue what I'm doing!?
Someone can help me with the mongodb database?
hmmm
bot.con = await asyncio.create_pool(database="Evelone", user="postgres", password="admin")
AttributeError: module 'asyncio' has no attribute 'create_pool'
async def createDbPool():
bot.con = await asyncio.create_pool(database="Evelone", user="postgres", password="admin")
bot.loop.run_until_complete(createDbPool())
bot.run("Token123")
you're thinking of asyncpg.create_pool
or whatever db driver module you're using, but not asyncio
xd thanks
@hasty juniper im sure u meant asyncpg
ye
Ok
I don't understand!
motor is a driver for async and mongodb


