#databases
1 messages Β· Page 112 of 1
Which database?
Yes but if you had data in there before it will continue
oh ok
is there any way to "reset" it?
oh ok i figured this out
just need to truncate the table before the data transfer
thanks for the heads up man @proven arrow
Hello guys! Who can tell me. How I can deploy telegram bot if I have the Django administrating. And I need deploy the bot with Django project and DB.
Maybe any documentations?
States.
how do you make expiring documents in pymongo
you can set a TTL on documents, read about it here https://docs.mongodb.com/manual/tutorial/expire-data/
(TTL = time to live)
How do big companies manage Thier Database , they get like 10 million rows a days , how so they still maintaining the speed and efficiency ?
how come pymongo cant find any documents
i tried to uninstall postgress from my unix
i hit this error at the end
When the uninstaller finishes, a Warning notifies you that the data directory and service user account have not been removed
I don't really understand why it wouldn't remove them
I also don't understand why it originally installed postgress in a super user directory instead of a regular one
I am wanting to create a function that allows the user of an app to print a specific text file at any point with typing a key word, any advice or tips?
I am wanting to create a function that allows the user of an app to print a specific text file at any point with typing a key word, any advice or tips?
@novel elbow This doesn't really need a database? Does it? What are you planning?
make a dict inside the function, mapping keywords to filename,
I am working on my final project for college:
The project is a cocktail book
The program has a large amount of cocktails saved as subclasses to a main class of "Cocktail"
The player can search for cocktails with certain ingredients
The player can pull a recipe for any of the cocktails
how many ingredients are you planning for this project?
well to start I will take a few dozen cocktails and input their ingredients
I am considering having a true/false system
Is OOP necessary for the project? as in a requirement?
No I just need to use python
I was thinking just simple lists
wrong channel btw
What channel should I use
fairly new to python
class Cocktail():
def init(self, name, noIngredients, ingredients, noDirections, directions):
self.name = name
self.noIngredients = noIngredients
self.ingredients = ingredients
self.noDirections = noDirections
self.directions = directions
self.serves = serves
negroni = Cocktail('Negroni', 5, [gin, sweetvermouth, campari, ice, orange], 4, ['Add the ingredients together in a cocktail shaker.', 'Stir well with cracked ice.', 'Strain into a glass over cubed ice.', 'Garnish with a twist of orange peel.'])
Hello! I have a question, I have the class/table AbstractZone with:
__mapper_args__ = {"polymorphic_on": discr}
and another one called CommercialZone that extend from the previous one and using:
__mapper_args__ = {"polymorphic_identity": "commercial_zone"}
how can I make AbstractZone sort of like abstract? (AbstractZone has a few relationship that the children should be able to use, like country)
my code
MySQLConnection.mycursor.execute("SELECT cash_amount FROM Money WHERE moneyuser = %s", [str(usercash)])
result = MySQLConnection.mycursor.fetchone()
print(result)
MySQLConnection.mycursor.execute("UPDATE Money SET cash_amount = %s WHERE moneyuser = %s", (int(cashamountdownload) + amountcashadd, str(usercash)))
MySQLConnection.db.commit()
cashprnt1 = int(0 if result is None else result)
cashprnt2 = int(cashprnt1) + amountcashadd
the error:
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'fetchone'
Ignoring exception in command None:
how do i fix?
also copy and pasting screwed up the indents
Is better to use sqlalchemy or psycopg2 or asyncpg π
asyncpg is the best postgre driver but its a async system so ig that depends on if your system is async or not
sqlalchemy uses psycopg2 under the hood anyway @chrome dock
@tepid cradle hey bro i need some help u here?
@jovial notch what's your question?
Is better to use sqlalchemy or psycopg2 or asyncpg π
@chrome dock sqlalchemy uses psycopg2 underneath, so it's not really very different. However, if you want to use an ORM like system, where you don't have to write raw SQL queries, then you use Sqlalchemy. Otherwise stick to psycopg2.
I haven't really used asyncpg.
I was wondering how I could only get the 5 top joiners of the voice channel lowest being 0.
How do you define top joiners?
wym
As in, what do you mean by top joiners? People who have joined most number of times or joined most recently, etc.
Usually it's better to try to resolve a question by understanding the required logic rather than by looking at the code.
Also, it's difficult to understand code without knowing the logic.
So basically you want people ordered by amount in descending order?
deleted message
so what i want is to get only 5 people from the whole column and i want them ordered from highest to lowest being 0
@tepid cradle
Ok
So you can do it in the query itself like this
query = "select * from table order by amount desc limit 5"
How do big companies manage Thier Database , they get like 10 million rows a days , how so they still maintaining the speed and efficiency ?
10 million rows a day and in 1month you db will be slow ?
What magick does they do
In reality 10 million rows isn't that much for a relational dB, but for speed and efficient generally involves giving the DB more ram and cpu allowance, creating index for faster lookup, better disk types e.g ssd over HDD
Defragging etc...
Ig most people just go with the throw more server at it
Caching as well is a big thing
With proper caching you can cut down on dB queries by alot of done right
A big thing is using partitioning for dynamic data https://www.postgresql.org/docs/10/ddl-partitioning.html Or using a column-based db such as Clickhouse https://clickhouse.tech/ - I remember Cloudflare showing how they do it for their analytics platform https://blog.cloudflare.com/http-analytics-for-6m-requests-per-second-using-clickhouse/
ClickHouse is a fast open-source column-oriented database management system that allows generating analytical data reports in real-time using SQL queries
In addition, to the above points, it is also important to design your database correctly. Database tables are not a place where you just dump your data.
People generally underestimate how important it is to structure your database well.
Divide data in tables so that data which will be fetched together, stays together. Design tables in such a way that you have minimal duplication. Create indices on the right columns or set of columns to make lookups faster. Use correct data types to avoid using up unnecessary space and avoid casting of data types in queries.
All these are just as important as any other.
I have a table in a PostgreSQL Db with 11 million rows. Over my local network, fetching about 300 rows takes ~12 ms. And that Db resides on a Raspberry Pi.
Imagine the kind of performance databases can give on enterprise grade SSDs backed by Octa core Xeon processors and 64 gigs of RAM.
I mean i don't see a way to setup the db ram usage ?
Is it automatic ? Means the more ram and the more cpu power my system will have on which the db is running will that make the database faster automatically ?
I have a script that uploads to database every hour. I upload like 500 items which i get from an api. I do every hour in case data from api changes so i need to refresh data in the database.
However if item already exists with PK i get error duplicate entry for this PK.
What can I do to ensure that if the data has changed then just update the value/row that has changed?
I update to the database like this currently. Is MySQL db.
cursor = db_connection.cursor()
statement = "INSERT INTO products (id, name, description, image, price) VALUES (%s, %s, %s, %s, %s)"
cursor.executemany(statement, data_tuple)
db_connection.commit()
await db.execute("UPDATE Count SET Name_Channel_text_channels = ? WHERE Guild_ID = ? WHERE id"
``` i can do that?
The last one is a bit fishy. It's not syntactically incorrect unless id is a boolean column. But id is hardly ever a boolean column. So what is your objective in ending the query with where id?
Everything before that looks correct
@hasty juniper
I have a 6 value in table, I need to sort one value by two values
@hasty juniper it seems you've used the word value to refer to all of value, row, and column.
Can you rephrase with the correct terms so that I can understand what you're trying to do
like a have id_member and guild_id and I need to find a value by two values @tepid cradle
Yes, for that you dvd to specify the id where id = something
The syntax is where id = ? and guild_id = ?
The word where should appear only once, rest should be added with and
Um anyone can help me please? #databases message
@torn sphinx catch the duplicate key error and compare the data. if you believe is the same just run an UPDATE query with the diff?
What if i want to force update each record?
Also to mention i have access to the previous data, and new data in my script before i upload to db
Data is in tuple format.
I'm trying to make sure I understand the terminology around databases. The closure of a set of attributes is all the other attributes you'd get if you follow each arrow out recursively. So does that mean that the minimum candidate key is the smallest set of attributes, the closure of which includes all attributes?
so I think the minimum candidate key would be {A, B, G}+ because following {A, B}+ gets you everywhere except G and F, and G gives you F.
looks like you might only need G and B, since G gives you A
hey all, I've been trying to connect with a local database and pull stuff from it. I've made it connect succesfully as far as I can see but when I try to send specific data or all the data from a table to console it won't work, itll say no such table named dayindicators. While in DB Browser I have a table named dayindicators. I've never worked with databases before so I could be doing something. Anyone got an idea on what the problem could be? Here is the code I have currently: ```py
connection = sqlite3.connect("notsurewhattocallthis.db")
cursor = connection.cursor()
cursor.execute("SELECT * FROM dayindicators")
results = cursor.fetchall()
print(results)
cursor.close()
Hello folks, I could use some help with a simple dict() (python dictionary) function. I'm new to using the module and having a bit of trouble with sorting the values
thanks. I got it to sort using l.sort(reverse=True)
i guess it was converted to a 'list'
can I show you the code?
sure
string = input("Enter any collection of words, and the output will show the number of times each word occurs in the collection.\n")
lower = string.lower()
#print(lower)
lower = string.split()
#print(lower)
#print(lower)
counts = dict()
l=list(counts.items())
i = 0
#word = ######
for word in lower :
if word in counts:
counts[word] += 1
else:
counts[word] = 1
i += 1
l.sort(reverse=True)
num = dict.keys(counts)
#print word counts
print("Total words:")
print("============")
#print index (how many words total)
print(i)
print("Word count")
print("==========")
for key in sorted(dict.keys(counts)) :
print(key , " :: " , counts[key])
commented parts were just testing
you know what would be alot easier to use todo that
they essentially do what you've done there
would defo be worth looking at https://discordapp.com/channels/267624335836053506/723655946672275466/733039892371865621
with Jetbrain's videos
thanks. that's helpful. however, my code that I showed above does exactly what I want EXCEPT it is sorting alphabetically, instead of sorting by number of word frequency
as for the main question/discussion, it .keys() is essentially returning a list of the keys
you're then ordering it alphabetically
i imagined it would be...
when getting caught up in loops and indexes, I sometimes get mixed up
thanks for the chat. I suppose I should look for 'sorting by value of dict'
look at the the Jetbrain's video
they did and example of what youre trying todo
with the different collections but also regular dicts
great, thanks
Just asking to see if its common for databases to have integer primary keys in some tables and uids in another, or is it exclusively (most of the time) one or the other?
hey guys, I'm new to this community π and hoping to help others if I can π , but currently need help/guidence on how I can trigger some code a week before a date+time set on MongoDB
i've looked at MongoDB triggers, but it doesn't look like CRON will help me much
Hey yβall, I was thinking of making a simple gratitude journal. Basically I want to list 3 things or events Iβm grateful for. But I also want to store them somewhere and be able to access them and see the data and the time. Is there a way to do this through python? SQL?
@karmic flicker sure it is completely doable in python
In this Python SQLite tutorial, we will be going over a complete introduction to the sqlite3 built-in module within Python. SQLite allows us to quickly get u...
you could even go a few steps further and create a web interface for it using something like django or flask
Thanks!
Just asking to see if its common for databases to have integer primary keys in some tables and uids in another, or is it exclusively (most of the time) one or the other?
@grand estuary it's entirely up to you, keep whatever primary key you want.
Personally, I believe the primary key should be meaningful, something you'll actually use in queries. A dummy id column which has no meaning is actually useless.
how fetch the data in mongodb
okay so... i keep gettings errors.. Lost connection to MySQL server at 'reading initial communication packet', system error: 0 when ive got port="8080" in my connector, and Can't connect to MySQL server on 'localhost' (61) when i dont have port... the first error comes up after a minute ish of the bot 'running' but the bot doesnt actually run yet..
c.execute("BEGIN TRANSACTION")
c.execute("""UPDATE guildstats
SET ?=?
WHERE guildid=?""",(column_name,1,guildid))
c.execute("COMMIT")```
c.execute("""UPDATE guildstats
sqlite3.OperationalError: near "?": syntax error```
error
why did this happen? pls help
am using sqlite3
@high geyser You can't parametrize a column name like that, I believe.
what
only the values
oh no but then how will I
I need to do that bro
column_name = self.get_column_name(plugin_name=plugin_name)
c.execute("""SELECT * FROM guildstats
WHERE guildid=?""",(guildid,))
result = c.fetchall()
if result != []:
c.execute("BEGIN TRANSACTION")
c.execute("""UPDATE guildstats
SET ?=?
WHERE guildid=?""",(column_name,1,guildid))
c.execute("COMMIT")```
see?
like that
If you really trust the source of column_name (i.e. it does not come from the user), you might just interpolate it. But maybe there is a better solution.
With f-strings, for example.
I haven't used sqlite that much (if that's sqlite), maybe there's some built-in transaction stuff in the adapter.
c.execute(f"""UPDATE guildstats
SET {}={}
WHERE guildid={}""",(column_name,1,guildid))```
like this?
no, just substitute the column name
f"""UPDATE guildstats set {column_name}=? WHERE guildid=?""", (1, guildid)
But I really doubt there's no better way to do what you want.
If column_name is somehow coming from user input, don't do that, of course
Also (and now for something completely different), you shouldn't do blocking calls (without running them if your program is async. There's probably an async adapter for your DBMS.
If you're using SQLite, there's aiosqlite
If you're using PostgreSQL, there's asyncpg
Right now -- yes
If there really is no safer solution, I would mark that with a TODO comment
# TODO: ...
or
# HACK: ...
i am new to databases
how do i store and make databases?
i want to store id of users
like if someone is afk i want to store that
when someone runs the afk command
@high geyser is your query resolved?
When someone tries to parameterize column names, I generally say two things to them
- You don't really need to do that. You think you do, but there's almost always a better solution
- If you absolutely have to do it, then a database has something called an information schema (at least, that's what it's called in Postgresql), which has a list of all column names. After getting the user input, first check that the column name is there in the information schema, then join them using f-string. Proceed to pass parameters the usual way
yes thanks bro
can I explain what I want in detail like is there any alternative? @tepid cradle
Sure
so Ive got a command called enable where py @config.command() async def enable(self,ctx,*,plugin:str): print(f"plugin is {plugin}") existing_status = self.check_plugin_status(plugin=plugin,guildid=ctx.guild.id) if existing_status != 1: await self.enable_plugin(plugin_name=plugin,guildid=ctx.guild.id) embed=discord.Embed() embed.colour=discord.Colour.blurple() embed.description = f"Successfully enabled plugin `{plugin}`" await ctx.send(embed=embed) else: embed=discord.Embed() embed.colour=discord.Colour.red() embed.description = f"Plugin `{plugin}` has already been enabled." await ctx.send(embed=embed)
I get the plugin name from the user
this is in a class called Tags
class Tags(commands.Cog):
def __init__(self, client):
self.client = client
self.id = 744149940082835577
self.plugins = [("nsfw filter","nsfw_filter",0)]```
in this function py def check_plugin_status(self,plugin:str,guildid:int): column_name = self.get_column_name(plugin) c.execute(f"""SELECT {column_name} FROM guildstats WHERE guildid = ?""",(guildid,)) result = c.fetchone() print(f"result is {result}.") if result is None: for (pluginname,_,status) in self.plugins: if pluginname.lower() == plugin: return status else: return result[0]
in the tags class
I have self.plugins= a tuple
the tuple contains the following:
(the plugin's name, the plugin's column in the database which needs to be updated, the default value of the plugin like hwether it is set to true or false
so now in that function
I need to match the column with the plugin name
and then get the query
so what am doing is the best practice or are there any alternatives? @tepid cradle
Can you show this table's schema?
Like, either the create table command or a screenshot of the first couple of rows and all columns
@tepid cradle
See if you can design it like this :
id guild plugin active
1 abc some plugin true
2 xyz another plugin false
@high geyser
So?
Guilds can also have multiple rows
That's how tables are actually designed, that's the best practice
okay thanks Ill look into it. It would take a while to change the code though
Yes. But that's the usual way of designing database tables. Because you may want to add more and more plugins over time. You can't keep modifying the table's schema every time.
anyone know why get a null verein_id 1? i have datas in line 1 like msv duisburg etc.
Hi, I'm creating an API with FastAPI and I'm using postgresql as DB. I'm creating new postgres schemas for new clients dynamically. I want alembic auto migration to work on for all schemas if for example I add a new model the table should be created in all the schemas. I found https://gist.github.com/h4/fc9b6d350544ff66491308b535762fee this works for any updates to the model columns but not with new models i,e if I change column to json from text alembic will detect the change and update all schemas likewise but if I add new model alembic won't detect it for all schemas it will just detect it for public schema only can anyone help?
Setup alembic to work properly with PostgreSQL schemas - env.py
anyone got any idea how to get this kind of thing to work https://paste.pythondiscord.com/iyubonukih.py trying to use sqlite3 with discord.py
Hello guys I would like to develop a version control of exe files
Does someone have any idea?
Traceback (most recent call last):
File "drawFeatures.py", line 7, in <module>
cv2.error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:3929: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
do u know the solution guys
dont forget to mention my name
@tepid cradle I changed the db design now because I got the time. thanks bro
it helped me a lot
@plush vector can you explain what you're doing? Creating a new schema for each client doesn't sound like a good idea
@tepid cradle I am storing data of a new client (organization) in a different schema
I have to separate data of each organization via schemas
Is that a design you've created or is that a requirement you've received from someone?
thats a requirement
Ok. Then not much you can do. But that's usually not how databases are designed. Data for all users is generally kept in a single database. Databases are split for micro-services or based on how the application is designed
actually clients are different organizations each schema will store data pertaining to an organization
so what I want is that if I add a new model alembic should automatically generate a script that will create the table across all schemas
actually clients are different organizations each schema will store data pertaining to an organization
@plush vector yeah, that's what databases do. There is no need to create separate database for each client, unless the database is hosted on the client side, i.e., your application will be deployed on the client's own servers.
I have no idea about FastAPI or Alembic. Was only discussing from a design perspective.
Yeah from the design prespect I also had suggested that we should only use one database but it didn't fit well in their head
Hi everybody, hope you could give me some advice. I am currently using SQLite to save real-time data obtained from a websocket connection, do you think there could be done some improvement using other Relational DB Management System, for example, PostgreSQL or any other. It's a ton of data being saved for like 6 hours every day.
still not working
Define not working? What doesn't work? Show any error you are facing if so.
Also question is not entirely related to #databases
Do I need to install mysql on my pc for the mysql-connector-python package to work?
Yes
MySQL is the actual database system
The python connector package is what allows you to interact with it through the python language
No then you don't need to install the database locally
You would just put in the database connection details in python.
Ah
I pip installed it and whenever I try to import it, it says ModuleNotFoundError: No module named 'mysql.connector'; 'mysql' is not a package
Did you install the correct version for your python version and environment?
pip install mysql-connector-python
Yeah
I created a venv
i went into it and pip install mysql-connector-python
then when i tried to run the file
which is just a test file that only connects to it
and it gives me that error
if you enter the command in your environment pip show mysql-connector-python what output do you get?
And also make sure to not name your file mysql.py
oh
thats the problem
haha i didnt know that bothered it
yup it works now
Thanks!
idk why people dislike sqlite3, I love it.
@torn sphinx, I have only used sqlite and it's been fine for me, just was interested in listening to some opinions, pros and cons
SQLite has limited datatypes for a start
Is using the Automap capability of SQLAlchemy a sensible idea?
It seems excessive to manually map a pre-existing DB but at the same time I feel like there must be drawbacks to doing it automatically?
How can i make a searchbar with pymongo
Like postgres has WHERE item LIKE something
sql = """SELECT name, id
FROM tag_lookup
WHERE location_id=$1 AND name % $2
ORDER BY similarity(name, $2) DESC
LIMIT 100;
"""
results = await ctx.db.fetch(sql, ctx.guild.id, query)``` how do I write this statement in sqlite? pls help me there is no similarity function
pls ping me when help
Hi everybody, hope you could give me some advice. I am currently using SQLite to save real-time data obtained from a websocket connection, do you think there could be done some improvement using other Relational DB Management System, for example, PostgreSQL or any other. It's a ton of data being saved for like 6 hours every day.
@inland canopy SQLite is also and RDBMS. But yes, write performance would be much better on PostgreSQL, especially if you have multiple threads/scripts writing to the db at the same time. SQLite isn't really built for concurrent write operations.
If you have more writes than reads them I'll suggest you to use MongoDB.
idk why people dislike sqlite3, I love it.
@torn sphinx I don't think anyone dislikes sqlite. It has its uses. It's just not built for hosted web applications with multiple simultaneous users. If your application is self contained, where the db will reside on the client machine, then sqlite is the best option for such an application. For instance, Firefox uses sqlite to store it's data on your computer, so do many other applications.
If you have more writes than reads them I'll suggest you to use MongoDB.
@torn sphinx any specific reason for this suggestion?
How can i make a searchbar with pymongo
Like postgres has WHERE item LIKE something
@marsh tinsel I'm not well versed with Mongo, but you could use regex
{field: {$regex: 'pattern', $options: 'i'}}
Actually a better answer would be: Use LSM Tree based DBMS if you have large writes.
[postgres]
Hello, let's say I have a vote table votes with user id uid and count of votes.
I expect the table to be frequently polled with getting top 50 most voted users, eg select * from votes order by count limit 50
It would make sense for me to have a "view" that gets automatically updated with this query everytime the counts change (can go up one, or down one).
I might me mixing views with indexes but I've made myself a simple view for this, though the question is then, is the view automatically updated ? thanks
Essentially creating a view is like storing the underlying query in the database itself
So instead of writing the query in your program, you just call that view
The query is then run in real-time
So there's no performance benefit of creating a view
have just stumbled upon some SO answer which says exactly this, thanks a lot anyway:)
You might want to look at materialized view
Or just create an index on the main table, as you said so yourself
will have to try both (to wrap my head around what I actually need) ty
anyone familiar with postgis around here?
it's more to do with geometries, geojsons
anyone familiar with postgis around here?
@valid thistle what about?
I have something like this to get length of all posts by in users_posts table:
total_member_posts = Posts.query.order_by(desc(Posts.time)).all()
I have two issues:
-> By using .all() I am actually loading each and every single post and later I am using len(total_member_posts) to get total number of posts
-> And second, I want to filter this in a way that the current_user's posts don't get displayed, I have another variable for displaying current_user's posts but that's not what I want this variable to do
Any help?
Well, maybe use filter then
hey guys just arrived here
Well, maybe use filter then
@torn sphinx that's the problem, I don't know what to pass to filter_by()
What uniquely identifies the user?
@client.command(name='welcomesetup')
async def welcomesetup(message: discord.Message, arg1 = str, arg2 = str, arg3 = str):
async with aiosqlite.connect(...) as database:
await database.execute(f"INSERT INTO Welcomes VALUES ({arg1}, {arg2}, {arg3});", (message.guild.id, message.channel.id, message.content[:-6]))
await database.commit()
await database.close
await client.send(f"Your welcome channel has been set to `{message.channel}` with the message of: {message.content[:-6]}")
how far off am i
What uniquely identifies the user?
@torn sphinx current_user.username
Show full code @torn sphinx
def create_table():
cur.exute('CREATE TABLE IF NOT EXISTS Welcomes')
@client.command(name='welcomesetup')
async def welcomesetup(ctx,):
async with aiosqlite.connect(...) as database:
await database.execute(f"INSERT INTO Welcomes VALUES (?, ?, ?);", (ctx.message.guild.id, ctx.message.channel.id, ctx.message.content[:-6]))
await database.commit()
await database.close
await ctx.send(f"Your welcome channel has been set to `{ctx.message.channel}` with the message of: {ctx.message.content[:-6]}")
@hazy mango
cur.exute should be cur.execute
And your creation isn't valid either I don't think
You have to specify what columns and type of said columns should be in the table
As well as commit the creation of the table which you aren't currently doing
how do i do that..
username__ne
@torn sphinx what?
@hazy mango in my use case what kind of setup would i want?
regarding the database
filter(username__ne=username)
Well...
Did you do this on queryset returned from all?
It's basically remove posts from that particular user.
Can you reiterate your problem?
if anyone can answer why aiosqlite cant read the datebase but sqlite can
welcomes is a table.
it shows up on sqlite but not aiosqlite
Can you reiterate your problem?
@torn sphinx I want to grab all posts from posts table except posts of the current_user.username (current user)
nvm
Hello i want to make a todo application and can't figure out what Database scheme should i take
I thinked of
username TEXT
todo_title TEXT
todo_description TEXT
done BOOL
but i wanted to bruteforce/ pen test the following application with 1 million user
Each user can make 20 todos a day , with the following scheme i can definitely claim my Database is getting slow after 5-6 days which is 20 million Γ 5 = 100 million rows
I also thought of using List as the todo_description and title so that it will be 1 row a day
But due to online search and many pals over here suggesting me to not use list in db. I can't take my dicision
Can anyone help me in making a fast / reliable database table scheme , to make my task more efficient
@torn sphinx I want to grab all posts from posts table except posts of the current_user.username (current user)
@golden shadow yeah
Ok
ne doesn't exist. Don't know from where did I pick up that.
Use exclude
all().exclude(username=***)
you mean like this @torn sphinx :
Posts.query.all().exclude(username=current_user.username) ?
because that just didn't work
@golden shadow in general if something "didn't work" you need to explain what exactly didn't work about it. was there an error? were the results not what you expected?
Oh jesus
Don't show passive aggression against me.
@torn sphinx Quote aggression parts on my reply
@golden shadow in general if something "didn't work" you need to explain what exactly didn't work about it. was there an error? were the results not what you expected?
@harsh pulsar I assume you didn't follow my question from beginning
@grim lotus i dont understand your question. the schema you described is fine, but it sounds like your application needs a rate limit or some other way to prevent someone from spamming the database. you might also need to periodically re-index the username field
I'm working on an asynchronous project and I'm using asyncpg (Asynchronous Postgres library), I know I'll need a lot of tables for it and I'm not sure how to scale it.
Right now I'm using functions to easily set and access these stored values and right now I'm just using functions like:
def get_default_role(guild_id):
pass
def set_default_role(guild_id):
pass
...
While these work great for smaller projects, it's getting really messy since I already have about 40 of these and I know that I'll be adding more.
I'd like to separate them into something more convenient. I was thinking to make 1 class per db table with its functions contained there, but I'm not at all sure how to do that. Initially, I wanted to simply use SQLAlchemy's ORM but that isn't supported for asyncpg and so I'm not sure how to proceed.
It isn't that important but here's my code (I removed most of these access function to keep it cleaner):
https://paste.pythondiscord.com/ebadudijuf.py
if anyone can answer why aiosqlite cant read the datebase but sqlite can
@torn sphinx
postgresSQL for windows
@velvet coyote what?
and its postgreSQL not postgresSQL
ye
@tropic kayak i went to the website and then I went to wndose section and then now I am stuck here:
In this video we will see How to Install PostgreSQL & pgAdmin 4 on Windows 10.
The PostgreSQL installers created by EnterpriseDB are designed to make it quick and simple to install PostgreSQL on your computer. The installer provides:
ο· a distribution-independent PostgreSQL ins...
see this video
oh ok
βΊ SPONSOR β
Linode Web Hosting ($20.00 CREDIT)
http://bit.ly/2HsnivM
π If you're learning web development, check out my latest courses on my website @ https://codehawke.com/ π
Patreon - http://bit.ly/ChrisHawkesPatreon
Twitter - http://bit.ly/ChrisHawkesTwitter
LinkedIn...
thx
welcome
ye i did not see that thx
wwhen I run postgresql
why does visual c++ opent up?????????????????????????
hey it asks this what do I do
that is the port your db will be running on, I believe the one you have in the box is the default for postgres
ohh do i have to change that
you can, but you should probably leave it default
where what?
oh it's the location i just set that to defaukt
now gotta learn that
and send a json dict to
sqlite3
kk, I have only used terminal, so this is fairly new to me as well xD
xd
well, you are using postgreSQL, not sqlite
well just learning the basics for sqlite
if you have a postgres db, you will need to use postgres. No need to use sqlite if you are trying to learn
no but i need to send python stuff to db
and for that i need sqlite
i am learnign postgress basics for sqlite
???
what
they are very similar, I am not sure experienced in DB honestly. But postgress basics for sqlite seems contradicting to me, hopefully someone else can elaborate though
ohh ok
hey guys why do I get this error:
import sqlite3
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("""Create Table Employees (
first text,
last text,
pay integer,
) """)
conn.commit()
conn.close()
ERROR
Traceback (most recent call last):
File "c:/Users/user/Desktop/database/db.py", line 6, in <module>
c.execute("""Create Table Employees (
sqlite3.OperationalError: near ")": syntax error
@grim lotus i dont understand your question. the schema you described is fine, but it sounds like your application needs a rate limit or some other way to prevent someone from spamming the database. you might also need to periodically re-index the
usernamefield
@harsh pulsar but as i mentioned 30 million rows a day
In a week its like 200 million rows that means my database slow af
and what do you mean by username re- indexing
Answering from this question
@velvet coyote you have an extra comma at the last field
i got it thx though
SO my json looks like this
[{"User": {"Abhigya": "Pokharel", "Sujan": "Shrestha", "Nishan": "Ghimire"}, "Anshu": "6ixer"}]
is there a way to make it look like this ?
[{
"User":
{
"Abhigya": "Pokharel",
"Sujan": "Shrestha",
"Nishan": "Ghimire"},
"Anshu": "6ixer"
}]
if there is pls ping me
or find a json formatter lib
idk
import json
_json = '[{"User": {"Abhigya": "Pokharel", "Sujan": "Shrestha", "Nishan": "Ghimire"}, "Anshu": "6ixer"}]'
parsed_json = json.loads(_json)
print(json.dumps(parsed_json,indent =3,sort_keys=False))
@velvet coyote
Also i am getting error in this code:
statement = """INSERT INTO products (id, name, description, image,
qty, value, weight) VALUES (%s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE id=%s, name=%s, description=%s, image=%s,
qty=%s, value=%s, weight=%s"""
cursor.executemany(statement, data_y)
db_connection.commit()
An error occured whilst uploading to the database: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s, name=%s, description=%s, image=%s, qty=%s, v...' at line 3
You may use the values function instead
So in the ON DUPLICATE part you would do ON DUPLICATE KEY UPDATE id=VALUES(id), name=VALUES(name), description=VALUES(description),....
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
@torn sphinx
What you have there would work, for a single row/execute but since you have bulk update query, then you can only have a single UPDATE clause in the bulk update statement.
What database to use? Atm i use mongodb for my discord bot it holds the token and everything.
Depends on the size of bot. Sqlite is good for small bots. If you want something that will be scalable or good for larger bots, then postgres is good. Whichever you use be sure to use the async version of the db driver. (aiosqlite for sqlite, and asyncpg for postgres).
I'm currently using heroku for my discord.py bot, is there any good tutorial I could learn postgresql from?
I need to store some ids and additional 2 parameters for each.
how do i autoincrement in sql python ;-; aint working
@torn sphinx You can take a look at this short playlist. https://www.youtube.com/watch?v=xaWlS9HtWYw&list=PL-osiE80TeTsKOdPrKeSOp4rN3mza8VHN
Or more in depth at: https://www.youtube.com/watch?v=qw--VYLpxG4
@vernal sun You can declare the field type as INTEGER PRIMARY KEY for sqlite. Answered in their FAQ, https://www.sqlite.org/faq.html#q1
Yes so if you provide the type as INTEGER PRIMARY KEY it will auto increment for you
Making a Python game and wanna save some data in a file.. any clue how I could do that?
Get the variabels for the data and send the values to database?
use pickle?
@grim lotus for that kind of volume, you probably need separate tables (and possibly separate databases) for storing data vs doing analytics queries
this is getting into professional grade database admin and data engineering, that is a very big amount of data
and its probably outside the scope of what you will get substantive help for on a python chat room
is channel suitable for json question
@harsh pulsar is there any advice you can give/ provide ?
you should look into indexes @grim lotus , since you seemed confused when i mentioned it
it's an important tool in database admin
its not going to help your write throughput though
but it could help make certain queries faster

Then I don't think so table splitting is what i can do right now
#databases message
As i mentioned table splitting is not a solution for me , then i have to lookup into multiple tables for a single row fetch
anyone know how to instantiate an async init ? i just want to make a connection during init inside a class..any clues?
@commands.Cog.listener()
async def on_member_join(self,member):
async with aiosqlite.connect('data/Coins.sql') as db:
await db.execute("UPDATE Gem SET Member_Id = ?, Member_Cash = ?, Member_Hours = ?, Member_Messages = ?, Member_Roles = ?, Member_Play = ? WHERE Guild_ID = ? VALUES (?,?,?,?,?,?,?)", (member.id,0,0,0,0,True,member.guild.id))
``````cmd
sqlite3.OperationalError: near "VALUES": syntax error
dont u need to add "\" before values?
o
OwO
nwm
π
@hasty juniper your syntax is incorrect
ye i fix it ```py
await db.execute("UPDATE Gem SET Member_Id = ?, Member_Cash = ?, Member_Hours = ?, Member_Messages = ?, Member_Roles = ?, Member_Play = ? WHERE Guild_ID = ?", (member.id,0,0,0,0,True,member.guild.id))
yes, this is correct
for future reference, use lower case for column names as far as possible
why?
@commands.Cog.listener()
async def on_member_remove(self, member):
async with aiosqlite.connect('data/Coins.sql') as db:
await db.execute("DELETE FROM Gem WHERE Member_Id = ? and Guild_ID = ? ", (member.id,member.guild.id,))
await db.commit()
``` and by the way, this removes all server members, although only member should
@tepid cradle
I hope you understand
I kind of want to level the fields
why?
@hasty juniper Just a generally followed convention in sql databases. Although, column names are case insensitive, so it doesn't really matter. It just makes typing queries easier, you don't have to keep track of the capitalisation of column names
@grim lotus writing to one table will always be than writing to several tables. its not clear to me whether your "lag" is related to query performance or write performance. they are different problems with different solutions
and by the way, this removes all server members, although only member should
@hasty juniper I don't see why this would remove all rows when you've specified member_id and guild_id. Try doing a select query with the same parameters first, see what it returns. If it's as per expectation, then change it to a delete query
lemme try
@commands.Cog.listener()
async def on_guild_join(self, guild):
async with aiosqlite.connect('data/Coins.sql') as db:
for member in guild.members:
await db.execute("INSERT INTO Gem (Guild_ID,Member_Id,Member_Cash,Member_Hours,Member_Messages,Member_Roles,Member_Play)", (guild.id,member.id,0,0,0,0,True))
await db.commit()
returning exception incomplete input
Traceback (most recent call last):
File "C:\Users\NotToxic89\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiosqlite\core.py", line 95, in run
result = function()
sqlite3.OperationalError: incomplete input
Ignoring exception in on_guild_join
Traceback (most recent call last):
File "C:\Users\NotToxic89\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
, line 13, in on_guild_join
await db.execute("INSERT INTO Gem (Guild_ID,Member_Id,Member_Cash,Member_Hours,Member_Messages,Member_Roles,Member_Play)", (guild.id,member.id,0,0,0,0,True))
File "C:\Users\NotToxic89\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiosqlite\core.py", line 160, in execute
cursor = await self._execute(self._conn.execute, sql, parameters)
File "C:\Users\NotToxic89\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiosqlite\core.py", line 109, in _execute
return await future
File "C:\Users\NotToxic89\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiosqlite\core.py", line 95, in run
result = function()
sqlite3.OperationalError: incomplete input
why do you need this class
just use ```py
async with aiosqlite.connect('...') as db:
i need to use class, cause i need it inside my cogs (cogs = classes ) in discord py bot :/
My query is related to slow down of db / performance
answering to salt rock
I have problem for read from db
as database is big it will have a slow db issue
For reading
I started using PostgreSQL and created new user "dominik" but if I start pgadmin I'm logged as "postgres"
I want be logged as dominik
do u consider json as a database
json is just a file format
it's def not an relational database management system (RDBMS) like postgreSQL, MySQL, or SQLite
how do i pick everything of a specific row except one column,
like i know how to
"SELECT * FROM RPG WHERE id =?;",(id,)
but i dont want one column from the list..how do i remove?
What's the order of return values for a select statement in SQLite?py self.cur.execute("SELECT FormID FROM forms")It always returns the same thing but the order seems weird (FormID is numbers 1-9 and it always returns in the order 2, 3, 4, 5, 7, 6, 1, 9, 8)
Was wondering what the logic behind that specific sequence was
@vernal sun you can't remove a column from a select query, you can only list the columns that you want
@hazy mango in general i think of database tables as unordered. but usually i think its just returning in insertion order
That's weird because I did insert in 1-9 order
But yea I guess it's just being weird lol
!e ```python
import sqlite3
db = sqlite3.connect(":memory:")
with db:
db.execute("create table y (i integer primary key, k text)")
db.execute("insert into y values (1, 'a')")
db.execute("insert into y values (2, 'b')")
db.execute("insert into y values (3, 'c')")
db.execute("insert into y values (4, 'd')")
curs = db.execute("select * from y")
rows = curs.fetchall()
print(rows)
@harsh pulsar :white_check_mark: Your eval job has completed with return code 0.
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
π€·ββοΈ
π€·ββοΈ
Did you delete any rows and then insert after?
Because the ROWID values can be reused.
salt rock lamp, it's however database engine returns
generally optimizing speed
if you want order, then issue "Sort by"
right, thats what i do
Anybody with heroku postgres experience can help me set up 1 table to store and update some data after the command is executed?
What exactly do you need help with? What have you done so far?
So i created a slot machine system yesterday with accounts and balance. I used csv files to store the data but heroku resets all those everyday and I decided to use a sql, which apparently I dont know how to, I just finished watching basics tutorial 30 minutes ago. I need to get data from the table and put it into these temporary .csv files and then when csv gets updated (balance mainly) i want to update the table aswell
You can just update data from your program to the postgres database, and then read it when you need. Is that right? or Do you need the csv?
Well I need a table with 3 columns: member id, balance and max win. I need 1st column so if the member doenst have an account it automatically creates one row with member's id, balance of 500 and win of 0. Then when he spins the slot machine, the balance should change according to how much he won/lost.
If it is possible to get and update table info directly, it would be nice, I just dont know how to access those or create any.
Yes but first, have you managed to make a connection to the database through your code?
No and thats what I need, I have never worked with databases before.
So you would need a python library that will allow you to connect/interact with the database first. Are you using this for a discord bot?
Yes, as u said, I installed asyncpg
Now as I'm trying to connect I get the InvalidAuthorizationSpecificationError
How are you trying to connect?
You would need to import the asyncpg module, and then make the connection. To do so you can do something like:
import asyncpg
bot = commands.Bot(....) #assuming you already have something like this.
async def setup_db(bot):
# This function makes the db connection, and assigns it to the bot variable.
bot.my_db = await asyncpg.create_pool(user="username", password="pass", database="database name", host="host")
bot.loop.create_task(setup_db(bot))
In the example, above i assign the database connection to the bot instance. This is so its easier for you to get a reference to the connection in other parts of the code. Also i made a connection pool here. You can see other examples or read more of making the connection at: https://magicstack.github.io/asyncpg/current/api/index.html#connection
I am just testing it aside from the bot
the basic code provided here
getting some error, telling that SSL is off
Whats the full error? Im guessing it may be a heroku thing
Ive never used heroku before
Try adding ssl=True to the connection details
now it failed ssl certificate
But its ok I think once I upload it to the bot it will work, I'll stick with the local database now
Ok I managed to fetch some data.
The ssl may be a heroku thing, im not sure you may want to wait for someone who has knowledge of heroku.
I think if i get it right to get info and update the database it will be fine
The next step would be to create your tables inside the db. You can do this through the code or directly through the DB
await connection.execute(f"INSERT INTO data VALUES({ctx.author.id}, 500, 0)")```
should I use like this to insert a new row?
Did you setup using the pool as showed you previously?
I havent run it yet honestly, just getting rid of csv files first.
I can create it with a gui
much simplier, isnt it?
Umm, maybe to visualise it yes but you will need to run it through the code later on
I can create it directly on the heroku website, is it still neesarry?
I tested it on other table and it seems to be working
test
Also dont use f strings for sql queries as it can be used for sql injection.
Instead you can use a parameterised query like the following:
async with connection.transaction():
await connection.execute("INSERT INTO data VALUES($1, $2, $3)", ctx.author.id, 500, 0)
Here the $1 references ctx.author.id, $2 references 500 and so on
If its a static value like 500, or 0 then you can directly insert into the values function for the query like you did before, but above is just an example, of how you can do with multiple params.
Oh thank you, I used fstrings everywere actually π
Why doesn't this work python column "asd" does not exist LINE 1: UPDATE CONFIGS SET PREFIX = ASD WHERE GUILDID = 680152209450...
Because column ASD does not exist
If you are trying to set it to the string ASD, you need to enclose it in quotes.
UPDATE CONFIGS SET PREFIX="ASD" WHERE GUILDID=...
@echo urchin
im getting a ```ModuleNotFoundError: No module named 'MySQLdb'
when running my script
but i installed mysql onto my mac
i think i need to install it onto my ide version of python
I'm completely new to the part with Databases. I want to set up a database for my Discord bot. How do I do this & what do i need.
I was having the same issue as this stack overflow question, and I implemented the first solution using pymysql
but now I'm getting a ```sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1130, "ool-457bf9ba.dyn.optonline.net' is not allowed to connect to this MySQL server")
(Background on this error at: http://sqlalche.me/e/13/e3q8)
I changed my URI to something similar in the stackoverflow post
Could anyone help me with this ? For each row in table 1 need to use certain column values from table 1 to pass to a query for table 2. How would such an sql statement look like ?
Could anyone help me with this ? For each row in table 1 need to use certain column values from table 1 to pass to a query for table 2. How would such an sql statement look like ?
@eternal umbra ...what do you mean?
give an example
@shell ocean
Consider for customerid 1, need to query all matching rows in product table for product type between min and max budget
Are you familiar with data analysis?
SELECT * FROM Forms WHERE FormID in (SELECT FormID FROM (SELECT FormID, COUNT(*) c FROM Students GROUP BY FormID HAVING c > 24)) ORDER BY FormID
```This statement will output all forms with > 24 students in it. It outputs the information in the image below.
How can I make it so that it also outputs the student count (c)? I've tried a few things but always get an error along the lines of `No such column: c` or `sub-select returns 2 columns β expected 1`.
how would i prevent a sql attack
so users cant do something extremely stupid like list tables lMAO
@torn sphinx You can use parameterised query, which uses a placeholder for the data you want to provide the query. Each placeholder will have a value that is provided as a parameter to that query.
For example,
("SELECT * FROM table WHERE column = %s AND other_column = %s", (param1, param2))
This would prevent sql injection, however you should still consider sanitizing/verifying user input.
Hey there, I use a postgres database for a discord bot using discord.py. As some of you might know, discord.py is async. I currently use psycopg2 as I found that was the most popular postgres library for python. But psycopg2 is not async and thus results in some slowdowns when searching for much data. This happens often enough for me that i'm willing to transition over to an async library, but i am unsure which one to use as there are multiple around
the 2 most popular i found were asyncpg and aoipg, would you recommend one of these or something completely different?
please @ me when you have an answer
hello
i am working on a dashboard project
using mysql
currently im getting a sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1046, 'No database selected') [SQL: DESCRIBE `users`]
my run.py file contains
db.create_all()
user1 = User(first_name='test')
db.session.add(user1)
db.session.commit()
app.run()```
the db URI connects to a remote server
shouldn't my script automatically create the db?
db.create_all()
@wintry stream asyncpg is hands down the best postgre lib for python overall not just the best async liv
It's the most perfomant, maintained, developed and consistent lib
okay thank you. I'll look into migrating over
@brazen charm do you know off the top of your head; why do i need to specify a db name in my URI if I am creating it in my run file?
should I jsut not create it in my run file I suppose?
and have it built already
@glass gorge probably because it's coded to support multiple dbs at once ig
that makes sense
whats confusing is that I need to specify one, otherwise it can't create it through mysqlalchemy. Maybe it can and I just don't know how.
Can someone help with this table entry, I have a table with a foreign key reference but I keep getting an identifier error for the first foreign key SQL create table Photos ( photo_id varchar2(255), photo_title varchar2(255), photo_desc varchar2(255), upload_date date, view_num number, team_id varchar2(255), primary key(photo_id), foreign key(Mem_ID) references Mem(Mem_ID), foreign key(Album_ID) references Album(Album_ID), foreign key(Comment_ID) references Comments(Comment_ID), foreign key(Location_ID) references Location(Location_ID) );
create table Mem
(
Mem_ID varchar2(255),
Mem_Name varchar2(255),
Mem_Phone number,
Mem_Email varchar2(255),
primary key (Mem_ID)
);```
You need to reference the foreign key columns inside the photos table
For example, you do foreign key(Mem_ID) references Mem(Mem_ID), and so that would mean you need a column inside the Photos table named Mem_ID. This is the column you want the Foreign Key to be in. @fair crescent
Okay, and does the column need to named the same as the foreign key? Can I use the reference to get other columns from another table?
Well when referencing you need to reference by the name of that column, but it doesnt need to be the same name as the PK of the other table you are referencing.
I'm kind of confused, so if I want to use the Mem_ID column in "Photos" I need to make a Mem_ID column in it and reference the Mem_ID in the table "Mem"?
You would reference it in the table where you want the Foreign Key. So in your Photos table.
What you have above is almost correct, except that you are telling it to create a FK reference on a column that doesnt exist. So you would need to create those columns in your Photos table so the FK can be stored in it.
CREATE TABLE Photos(
photo_id int NOT NULL,
PRIMARY KEY (photo_id)
);
CREATE TABLE Users(
user_id int NOT NULL,
photo_id int NOT NULL,
PRIMARY KEY (user_id),
FOREIGN KEY (photo_id) REFERENCES Photos(photo_id)
);
Consider that example. The FK is inside the Users table.
Could I also use a different column apart from the primary key in a table? For example ```SQL
CREATE TABLE Photos(
photo_id int NOT NULL,
test varchar2(255),
PRIMARY KEY (photo_id)
);
CREATE TABLE Users(
user_id int NOT NULL,
photo_id int NOT NULL,
PRIMARY KEY (user_id),
FOREIGN KEY (photo_id) REFERENCES Photos(photo_id)
);``` Could I get "test" to be used in the Users table?
would I just replace it with FOREIGN KEY (photo_id) REFERENCES Photos(test)
Test would need to have a unique constraint
I have a question. I need tens of thousands of user profiles. Each profile will probably have a name, email, and a profile picture. Which database solution (MySQL, PostreSQL, NoSQL) will be best for me? And what is the best way to store these profile images after compressing them
@fair crescent You can use the UNIQUE constraint. However remember this would mean that each value in this column in your table would have to be unique.
Is there anything like postresql sharding to make the db fast
i accidentaly selected the same db twice
so it's example.example
how do I undo that
sorry i used the same db twice
Can someone help me with a one small issue
select Mem.Mem_Name photos.photo_id, photos.photo_title, photos.photo_desc,photos.privacy,photos.upload_date, photos.view_num,photos.img_path,photos.Mem_ID,photos.Album_ID,photos.Location_ID
FROM Mem
JOIN photos
ON Mem.Mem_ID = photos.Mem_ID
WHERE Mem.Mem_Name LIKE '%Name Here%';```
I get a error saying FROM is not where expected
I can't see the issue
@fair crescent you're missing a comma after Mem.Mem_Name
hah
Thank you both
good formatting is important for readability which is important for catching errors like this
is there any way to check if a table is partitioned in athena or awswrangler help xd
with pythjon
if I have a JSON with entries that go from 1 to 8 (and 8 is inside a value), how do I make my script get all the values from 1 to 8 without making a huge mountain of if else blocks?
any amazon athena expert would like to help me :'v}
if I have a JSON with entries that go from 1 to 8 (and 8 is inside a value), how do I make my script get all the values from 1 to 8 without making a huge mountain of if else blocks?
@gentle plaza example?
I figured out the answer by rephrasing what I needed to know inside #python-discussion, sorry
Does big discord bots like mee6, dankmemer use a SQL or nosql db? To store uswr data
oh right..so nosql π ty..
Anyone here well versed with elasticsearch?
How do you implement a database/postgres in a larger project? Where do you place it in the project structure?
I was thinking about writing some kind of class with a connect/commit/close functionality and the different methods to insert/update/delete. Then call those methods somewhere in the application
SQLAlchemy is likely the way to go @charred zephyr
I usually have an SQLAlchemy-based ORM package in my projects that require a database
PostreSQL question, could use an outside opinion.
I have two tables account and player which both have a uuid linked through a foreign key. For multiple reasons, I would like to use a surrogate key.
We agree that once itβs created as SERIAL in account, the right way to propagate it is:
UPDATE player
SET account_id = account.id
FROM account
WHERE player.puuid = account.puuid;
This looks right to me, but the query has been running for ten minutes despite operating on an index (puuid has a unique constraint in account). Also, for some reason, EXPLAIN tells me this is not using the index...
how do i reset table id after deleting from it
like
content | id
---------+----
cn | 1
fn | 2
after deleting the first row the rowid still 2
content | id
---------+----
fn | 2
Isnt ID your primary key?
it is
Also what is the purpose of such a thing? That would seem like a very expensive operation especially if you have many rows, and then that would mean you would have to shift the ids of each row down.
c.execute("BEGIN TRANSACTION")
for nsfw_word in args:
if not check_for_duplicate(nsfw_word, 0):
interfered_word_list.append(f"||{nsfw_word}||")
try:
c.execute("""INSERT INTO nsfw_word_lookup (guildid, word, type)
VALUES (?,?,?)""", (ctx.guild.id, nsfw_word, 0))
except Exception:
c.execute("""UPDATE nsfw_word_lookup
SET type = ?
WHERE guildid= ? AND word = ?""",
(0, ctx.guild.id, nsfw_word))
c.execute("COMMIT")``` is this good practice I am doing this because I want to add a list of words given by the user from the table. Even if one word isnt added due to constraints, the whole process will stop. Moreover, I also need to find out which words failed to be added to the table. So, can I loop through deletion after beginning transaction?
I'm using django models/ORM to process some data. I have two models that have similar, but different information. I'm trying to join them on certain columns matching... [i.e. For ClassAObj.date == ClassBObj.date and ClassAObj.age == ClassBObj.age]... i've done this sort of thing with pandas where i merge/join them on certain columns matching, but im wondering if there was a way to do this easily via shell_plus/django ORM
c.execute("BEGIN TRANSACTION")
for nsfw_word in args:
try:
c.execute("""DELETE FROM nsfw_word_lookup
WHERE word = ? AND guildid=?""", (nsfw_word,ctx.guild.id))
except Exception as err:
print(err)
else:
interfered_word_list.append(f"||{nsfw_word}||")
c.execute("COMMIT")```sqlite3 this doesnt delete a thing from the table and no error occurs
why? pls help
Why don't you use something like
Delete from table where word in (...)
This way you can delete all words in a single query
Hope itβs not a problem for a newbie to just come in here and just post a small question... π§.
Iβm trying to do a team vs team win/loss stat logging and canβt think of how to utilize a database for it. I need to be able to get Team A, B, Cβs win/loss when theyβre playing A, B, or C. Surely making a grid-like database structure is wrong, and I canβt have a thousand rows and a thousand columns? How would you do it?
I already have career stats, but I need the team vs team breakdown
Are INDEXES in-memory or stored in disk ?
do i have to re - create indexes after each boot ?
@torn sphinx normalise the data and store into seperate tables. This is simple example
team > stores team info
matches > team wise match info
match_vs > has info about who played with whom
@grim lotus that depends on the database provider, it is usually stored in disk and you have an option to have it in memory in most popular databases.
It doesn't needs to be recreated on boot, but you might have to re index based on how much of the data has changed in the table to improve performance. Db experts say, you have to re index after modifying 10% of the data volume on the indexed column.
@jovial yew thanks for the response! I had not thought of anything to do with multiple tables, but for match_vs I still donβt know what the column structure should look. Like
Team A | Team B?
Iβd still need to plug in wins/losses
so, im currently helping a friend out with his server, but when we trying to use mysql this error occurs
File "/usr/local/lib/python3.8/dist-packages/mysql/connector/connection_cext.py", line 216, in _open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Host 'vmd61223.contaboserver.net' is not allowed to connect to this MySQL server
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 36, in <module>
sql.sqlsetup()
File "/home/ruby/sql.py", line 15, in sqlsetup
temp=c()
File "/home/ruby/sql.py", line 3, in c
myuser=mysql.connector.connect(
File "/usr/local/lib/python3.8/dist-packages/mysql/connector/__init__.py", line 264, in connect
return CMySQLConnection(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/mysql/connector/connection_cext.py", line 80, in __init__
self.connect(**kwargs)
File "/usr/local/lib/python3.8/dist-packages/mysql/connector/abstracts.py", line 966, in connect
self._open_connection()
File "/usr/local/lib/python3.8/dist-packages/mysql/connector/connection_cext.py", line 218, in _open_connection
raise errors.get_mysql_exception(msg=exc.msg, errno=exc.errno,
mysql.connector.errors.DatabaseError: 1130 (HY000): Host 'vmd61223.contaboserver.net' is not allowed to connect to this MySQL server```
he wanna use this server for his discord bot
what could cause this?
when I setted up my server i didnt got this error
@jovial yew i use psql , is there a way to index on every new row/every insert ?
Or should i re - index on each and every row ?
@jovial yew thanks for the response! I had not thought of anything to do with multiple tables, but for match_vs I still donβt know what the column structure should look. Like
Team A | Team B?
@torn sphinx try something like this, i know the naming is butchered
joining all the tables will give you win/loss vs teams
Hmm, thank you for the effort. Iβll have to see if I can put this together, I think I get it tho
@jovial yew i use psql , is there a way to index on every new row/every insert ?
Or should i re - index on each and every row ?
@grim lotus by psql you mean postgres right?
infact it does kinda gets indexed everytime you insert a new row, think of an index as another table which only has indexed column data along with some metadata, every record in the main table will have a corresponding record in this index table and it keeps updating everytime you modify the original table, so don't think about it too much
reindexing is done to remove fragmentaions, which is a whole another topic which i'm not very competent in π
@client.command(name='welcomesetup', brief='sets the current channel as welcome, provide the message after')
@commands.has_permissions(administrator=True)
async def welcomesetup(ctx, *, welcome_msg: str):
async with aiosqlite.connect(database=(r"C:\Users\\Videos\bot\database.db")) as database:
await database.execute("INSERT INTO Welcomes VALUES (?, ?, ?);", (ctx.guild.id, ctx.channel.id, welcome_msg))
database.commit
await ctx.send( f"Your welcome channel has been set to `{ctx.channel.mention}` with the message of: {welcome_msg}")
this works but im trying to make it so if the guild id is already present in my database, it edits the channelid and message in that row matching the guildid rather than making a new entry
@torn sphinx You can use INSERT OR REPLACE
But for this you would need a unique constraint. I guess you can make the guild ID unique if each guild can only have one entry.
INSERT OR REPLACE would insert if the row does not exist, or replace the values if it does.
ive basically made it so it ignores the guildid and channel id if they match
cur.execute('CREATE TABLE IF NOT EXISTS welcum(guildid INTEGER UNIQUE ON CONFLICT IGNORE, channelid TEXT UNIQUE ON CONFLICT REPLACE, messagecont TEXT UNIQUE ON CONFLICT IGNORE')
sqlite3.OperationalError: near "IGNORE": syntax error
``` ?
def create_table() :
cur.execute('CREATE TABLE IF NOT EXISTS welcum(guildid INTEGER UNIQUE ON CONFLICT IGNORE,'
'channelid TEXT UNIQUE ON CONFLICT REPLACE,'
'messagecont TEXT UNIQUE ON CONFLICT REPLACE');
database.commit()
database.close()
Anyone good with flask sqlalchemy please i need help so
I've used it before, ask your question @upbeat cypress
Hey @upbeat cypress!
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:
sqlalchemy.exc.OperationalError
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: students.addr
[SQL: SELECT students.student_id AS students_student_id, students.name AS students_name, students.city AS students_city, students.addr AS students_addr, students.pin AS students_pin
FROM students]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
Getting this error bro @naive pasture
I don't know where i am doing it wrong
Hey @worn flint!
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:
Am I using this correctly?
user = user_name_ent.get()
password = user_pass_ent.get()
ok_button = tk.Button(master=confirm_buttons_frm, text="OK",
command=lambda: create_user(user, password))
The create user function work independently, but the parameters are passed to the DB.
I have a bunch of objects in a database, under 2 separate classes.
The classes have similar information, but one of their attributes differs from one another (both classes contain important information)
I'm trying to create a third class of objects that will have ALL attributes and match the objects of the two classes together.... (think merge/join objects on a category)...
what is the best way to do this in POSTGRES/django orm?
example:
Class 1:
{'_state': <django.db.models.base.ModelState at 0x7efce8b6f6a0>,
'id': 1,
'away_team': 'Philadelphia',
'home_team': 'Boston',
'away_game_spread': '211',
'home_game_spread': '211',
'away_game_odds': '1.95',
'home_game_odds': '1.95',
'periods': {'1': {'away': 21, 'home': 21},
'2': {'away': 21, 'home': 26},
'3': {'away': 24, 'home': 30},
'4': {'away': 21, 'home': 28}},
'date': 'Oct-16-2018'}
Class 2:
{'_state': <django.db.models.base.ModelState at 0x7efce8b6d8b0>,
'id': 1,
'away_team': 'Philadelphia',
'home_team': 'Boston',
'away_game_spread': '53.5',
'home_game_spread': '53.5',
'away_game_odds': '1.86',
'home_game_odds': '2.03',
'periods': {'1': {'away': 21, 'home': 21},
'2': {'away': 21, 'home': 26},
'3': {'away': 24, 'home': 30},
'4': {'away': 21, 'home': 28}},
'date': 'Oct-16-2018'}
These objects are similar, but different, and i want to create a third object/class that will consolidate the info of both objects
should i be hashing the passwords for my seed first and then putting it on the db
or can i put it on the db first and then hash
for your seed?
my initial load up onto the db
MongoDB is good over JSONs?
We can't tell you
It depends on the scope of the project
Well, the project is big and it'll be applied on a Verified Bot.
hmm.... well, if you need to access the data from multiple places (processes/servers) or if the data is too large to fit into your ram, than yeah, you'd probably have to switch
but it will add complexity to the code and slow down the bot
Trying to learn sqlite3, will this work like I'm expecting?```py
self.cursor.execute("""CREATE TABLE IF NOT EXISTS
message_map(original INTEGER PRIMARY KEY, message INTEGER, log INTEGER)""")
message, log = self.cursor.execute("""SELECT * FROM message_map WHERE original=?""", (payload.message_id,))```
if I don't feel like running conn.commit() every time after running a command with sqlite, could I instead just run it before the program exited?
in sqlite3, why is
c.execute("SELECT ? FROM table WHERE value=?", (var1, var2))
different than
c.execute(f"SELECT {var1} FROM table WHERE value=?", (var2,))
the first statement returns me var1, the second does the proper select
Because the ? substitution doesn't work for column names
Among other things
String formatting is the only way to use dynamic column names in your query
ah, someone mentioned earlier string formatting leads to injection attacks or whatever though
i assume there isn't any way around that though?
Yeah, it certainly can. But that doesn't always apply.
If your column names are coming from an internal source, like a constant list of names, then you don't have to worry since you can assume the list that you made yourself is safe.
But if you're letting a use input an arbitrary name then that's problematic. You have to sanitise the input somehow e.g. check if it's a valid column name.
okay, ty
In this specific case, the other way to avoid is to select * and then pick the index for the column you want
Why don't you use something like
Delete from table where word in (...)This way you can delete all words in a single query
@tepid cradle what if some words in the list cannot be deleted due to contraints? will the other words be deleted if some error like that occurs too?
and also bro I want to add to the list which words are deleted
any ways to do that? I cant do that within the execution statement right
pls ping me when help
Question:
I'm making a small game where i store various info about many players in a database. Is there any reason to create a Player class for every player? Or does the db make the class obsolete? And rather just run various function that manipulate the entries in db
I'm looking for help regarding SQLAlchemy
How do I create this relationship with SQLAlchemy?
I want to save some programme data in a database. Do somebody know how I can do this?
Or does the db make the class obsolete?
@rich fulcrum It most certainly does not. The role of a class is to decouple your data from your logic. The class shouldn't care where your data comes from (e.g. which DB), and would allow you to refactor / modify your project easier.
In fact, if you look at ORMs, they wrap databases into classes.
But isn't a instance variable of something that's already in the database an unneccesary duplicate?
Your logic should still live inside class methods.
Then you get cool things like polymorphism, etc.
Assuming you have different classes.
Anyway, you don't have to use OOP, but I would.
Because what i'm doing atm is whenever something specific triggers i run the function "database_player_update_health(id, amount):"
This is a discord bot, so it might as well be an sql game
I guess π€
do i need to have something like db.open() if i use db.close()?
yes or else u cant access db info @vague haven
oh crap
why do you want to close it in the first place?
@grim lotus by psql you mean postgres right?
infact it does kinda gets indexed everytime you insert a new row, think of an index as another table which only has indexed column data along with some metadata, every record in the main table will have a corresponding record in this index table and it keeps updating everytime you modify the original table, so don't think about it too much
reindexing is done to remove fragmentaions, which is a whole another topic which i'm not very competent in π
@jovial yew i m not talking aboutPRIMARY KEYANDUNIQUEKEY indexing ,
I m talking about the indexes that are made my
CREATE INDEX ON table (cloumn)
I know this is a python server but has anyone worked with making their own mysql or mariadb database and know why
ADD CONSTANT chk_date_added CHECK (date_added !< GETDATE());```
isnt working. It throws error number 1064 which just say syntax error
Hey all! Anyone on has spent a lot of time using Psycopg2? I have a question about executemany vs execute_batch
Which one should we use for performance enhancement?
It's for a select statement
executemany is always slower
even specified in their docs that execute many is slower than just using a for loop
What really?!
for stock in symbols:
stock_info[stock] = get_dict_resultset("SELECT
date, close
FROM security_price
WHERE
security_price.id=%s;", [stock])
So I should just leave this as it?
get_dict_resultset() connects to my dB, pulls data, and stores it into a python dictionary.
I would rather not have to keep connecting to the dB in a loop
Wouldn't it just be better to use executemany ?
@jovial yew i m not talking about
PRIMARY KEYANDUNIQUEKEY indexing ,
I m talking about the indexes that are made my
CREATE INDEX ON table (cloumn)
@grim lotus that shouldn't be a problem
hello there
can i get help downloading mySQL for my pc and connecting it with python
await database.execute("INSERT INTO testwelcome VALUES (?, ?, ?);", (ctx.guild.id, ctx.channel.id, welcome_msg))
doesnt write to my table.
aiosqlite is the libary, and the table is just a ordinary setup with no other parameters other than the basic ones
@torn sphinx do you get some error?
nope
So you don't see the data in the table??
I've not used aiosqlite, but are you committing the transaction?
what's the best way to connect in a database (mariadb)?
what's the best way to connect in a database (mariadb)?
@warm glade I suggest you SQL Alchemy if you want either high level object ORM or just basic queries. The wiki is nice too
Why does this give me an "sqlite3.OperationalError: near ":entry": syntax error" ?
Never tried like this, but replace your string with format option
But it works if i remove the :entry
ex : " UPDATE table SET {} = {}".format(entry, value)
I heard using .format is bad for sql injections?
Well, like every word replacement with content made by users, you can do sql injection
true
If you have a blog and write a small sql injection, you could crash the thing if the backend is not made safe
I will try it your way
I did used that in a function to sanitize the query and I didn't have issues yet
@scarlet schooner I'll see, thank you!
@scarlet schooner I'll see, thank you!
@warm glade https://stackoverflow.com/questions/54834088/python-database-connection-for-mariadb-using-sqlalchemy
do sqlite3 SELECT statements need to be conn.commit()'ed before doing other things? i'm thinking no because it's just a read
SELECT statements dont modify data so it does not need to be saved before hand
only if you modify data
e.g
UPDATE, INSTERT etc...
ok ty
HINT: Check the query against the passed list of arguments. Note that parameters are supported only in SELECT, INSERT, UPDATE, DELETE, and VALUES statements, and will *not* work in statements like CREATE VIEW or DECLARE CURSOR. what should I use then... an fstring?
if i have a remote server with my db on it
and a local fiile that i want to upload as the seed
do i need to scp the content onto the server? or how would I upload it to the remote db?
Is there a way to speed up upserts to PSQL? I am getting an estimated 7 hours to load a 400MB parquet file (20 million rows)
So I decided to use sqlite3 for a password authentication thing, so how do I insert hashed passwords into the database?
Hi everyone. Does anyone use turbodbc? I can't for the life of me get it installed on a windows 10 machine.
So I decided to use sqlite3 for a password authentication thing, so how do I insert hashed passwords into the database?
@gray surge im working on this same issue right now, i would look at flask-bcrypt
@gray surge im working on this same issue right now, i would look at flask-bcrypt
@glass gorge a text field would work no or am I missing something?
You can use a varchar field. You just have to decode it using utf-8 before storing
password_for_db = hashed_password.decode('utf-8')
Something like this
hi guys i want to create a new database file (sqlite3) for my project and couldn't find any good answers online could anyone help me with that?
hi guys i want to create a new database file (sqlite3) for my project and couldn't find any good answers online could anyone help me with that?
@misty carbon Ask a more precise question... Or just runsqlite3 foo.db.
is there anyone here familiar with sqlalchemy?
having some issues with association tables
I just made an SO post about this right now, so instead of writing everything out again here ill link my SO article for anyone interested in the issue
TL;Dr im getting an error with association tables
That should contain the necessary parts of the code for this issue
I'm extremely new to SQL and even actively learning it as we speak. For my discord bot, I am trying to update two values. Every guild should have two columns: prefix and GuildID. You should be able to get a guild's prefix by referencing the guildId and getting it from there.
db.execute(
'''
UPDATE guilds
SET Prefix = prefix
WHERE GuildID = ctx.guild.id
''')
print('table updating')
guild = ctx.guild.id
g = db.execute('''
SELECT Prefix
FROM guilds''')
print(g)
Right now, that is my current code in my command. My SQL table looks like this:
CREATE TABLE IF NOT EXISTS guilds (
GuildID bigint PRIMARY KEY,
Prefix text DEFAULT "sb!"
);
Do you know why I end up getting an sqlite3.OperationalError: no such column: ctx.guild.id?
Any help is appreciated.
also when i try referencing g, i get none
If I have a python list of 512 floats, do I need to do anything special to add it as a single column entry in a row?
im assuming since i havent actually made any entries its basically updating nothin
How would I use the database from a Django project, to be analyzed using Pandas/Numpy? Itβs a .py file
what is a Python file? (not the database used by the django project for sure)
I've got a tricky one
How do I check if a variable is in a collection with pymongo
I'm trying to sync the state of a collection of YAML files with a DB
Using a sort of cloudformation inspired syntax
Is this dumb
can someone help me with a bot database that i am using I need someone to help me generate akamai sensor data
i keep getting a ERROR 1366 (HY000): Incorrect integer value: ' ' for column 'id' at row 1
every solution ive tried on google has not really worked
@glass gorge What is your query like?
You are probably entering invalid type when its expecting integer
i keep getting a
ERROR 1366 (HY000): Incorrect integer value: ' ' for column 'id' at row 1
@glass gorge sounds like you're passing an empty string and not an integer
so i am actually trying to load a csv
into the db
load data infile '/var/lib/mysql-files/users.csv' into table users fields terminated by ',' enclosed by '"' lines terminated by '\r' ignore 1 rows;
the id column, is set to autoincrement
most of the solutions online for this error are for scenarios where you add one row at a time to the db
using insert into or something similar (forgot the call)
they usually involve setting the values of the column to NULL, or 0
I've tried to do that, and even removed the column. And I keep getting the same error
I've tried to populate the column as well with integers
but it's the same error
I would greatly appreciate any help, I've hit a wall
Isnt it supposed to be ignore 1 lines ?
And id say you may want to manually specify the columns, and then set the ID field to null
you might be right on the 2nd point
i guess i have to specify the columns names and not just col1,col2 etc
Yeah
ok made progress
but now...
The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
I hate life
lmao
it shouldn't be doing this
because the file is on the actual server
it's a local file
like I had this problem before
load data local infile I think
Which is the directory you have set for loading such files?
I know there is something you can execute to check this directory if it has been set cant quite remember what it is
/var/lib/mysql-files/
Ok so if the csv file is in that folder then it should be good to load from there
one moment
uhhhh
wtf
ERROR 2 (HY000): File 'var/lib/mysql-files/users.csv' not found (Errcode: 2 - No such file or directory)
but it's there
Which is the directory you have set for loading such files?
@proven arrow i didnt set any directory
As an alternative option you could try setting the value for secure priv to null or just removing it from the config file @glass gorge
The server would need restarting for these changes to take effect though
In the config file youll see secure-file-priv = "your_path", which you can remove.
async def create_db_pool():
global pg_con
try:
pg_con = await asyncpg.create_pool(host='192.168.1.156', database="ChatParty", user="postgres", password='f')
except asyncpg.exceptions.InvalidAuthorizationSpecificationError:
pg_con = await asyncpg.create_pool(host='localhost', database="ChatParty", user="postgres", password='f')
print('[DB] Connection Established.')
@app.route('/login', methods=['GET', 'POST'])
@async_action
async def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
get_username = await pg_con.fetch(f"SELECT * FROM Accounts WHERE username='{username}'")
print(f'data: {get_username}')
return render_template('login.html', PageName='Login')
if __name__ == '__main__':
## -- ASYNCIO -- #
loop = asyncio.get_event_loop()
loop.run_until_complete(create_db_pool())
## -- FLASK -- #
app.run(debug=True)
asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress
@proven arrow it's the my.cnf file that i need to edit?
yeah idk whats going on, secure-file-priv is not actually in my my.cnf file
Yeah something like that. If its not there then it wont be set. What happens if you enter SHOW VARIABLES LIKE "secure_file_priv"; into your db console?
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.00 sec)```
I tried to add the line in the cnf file to specify the path to where files should be loaded
and doing so resulted in my server not running
even after restart
maybe im in the wrong user or something
The file may be different for the OS, what platform are you on?
For an issue like this you may get better answers online
ubuntu droplet
can someone help me?
If your on ubuntu you can try editing the file at /etc/mysql/mysql.conf.d/mysqld.cnf If the secure_file_priv doesn't exist then you can specify one or set it to empty string or null @glass gorge
And dont forget to restart the server after
adkjhasdkjash
ERROR 2 (HY000): File 'var/lib/mysql-file/users.csv' not found (Errcode: 2 - No such file or directory)
@glass gorge do you have a leading slash? /var/ and not var/
can someone help me
I have a Apples Numbers Sheet of Stock Tickers ..... can I create a graph that will plot the tickers day by day..... but also the stock price???
you can do whatever your heart desires with computers
how to connect my database to a host or i want to make my pc as a small server to recive data from other users that use my app
@quaint tiger that was the issue -__-
hey everyone, any one deal with connection issues -> [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: An existing connection was forcibly closed by the remote host. ?
can i use sqlite3 for my bot(discord.py) in many servers?
you can, yes
sqlite can handle a lot of traffic you'll be fine
as long as you have a single writer
what is a single writer
g = db.field("SELECT enabled FROM logs WHERE GuildID = ?", ctx.guild.id)
if g is None:
db.execute('''INSERT INTO logs (enabled, GuildId, msgLogs)
VALUES (?, ?, ?)''', True, ctx.guild.id, 1)
await ctx.send("Starting setup for log channels.")
try:
logCategory = await ctx.guild.create_category(name="Logs", reason="Server manager requested setup.")
messageLogs = await ctx.guild.create_text_channel(name="message-logs", category=logCategory)
db.execute("UPDATE logs SET msgLogs = ? WHERE GuildId = ?", messageLogs.id, ctx.guild.id)
when trying to detect if a discord server already has a guild, it checks successfully and returns none
if it is none, i make a entry with the columns enabled, guildId and msgLogs
when i run the code, i get an error saying tabe logs doesnt have column msgLogs though
CREATE TABLE IF NOT EXISTS logs (
GuildID BIGINT PRIMARY KEY,
logCategory BIGINT,
msgLogs BIGINT,
enabled BOOLEAN DEFAULT FALSE
);
tag me if you get a solution pls
pls help me
c.execute("""SELECT keyword,
tag_use_count,
COUNT(*) OVER() AS "Count",
SUM(tag_use_count) OVER () AS "Uses"
FROM tags
WHERE guildid=? AND authorid = ?
ORDER BY
tag_use_count DESC
LIMIT 3""",(guildid,member.id))
results = c.fetchall()
emoji = 129351 # ord(':first_place:')
tag_string = ""
if results:
owned = results[0]['Count']
uses = results[0]['Uses']``` I get this error
owned = results[0]['Count']
TypeError: tuple indices must be integers or slices, not str```
how to fix this?
I am using sqlite3
@naive sandal
no I got it
I was using sqlite3 so things were somewhat different
I used int instead thanks
Hello. So I have 3 tables:
table_1, table_2, table_3
all 3 tables have a column called nameX (but in table 1, the values in this column may contain extra characters)
I need to
select all columns from table1 where
(nameX in table_1) contains (nameX in table_2 where nameX is not in table_3)
how do I achieve this π
trying things like
select * from testi.jobs2 where NameAtCustomer not REGEXP (select NameAtCustomer from testi.pos where NameAtCustomer not in (select NameAtCustomer from testi.Invoices));``` not getting a good result
Some how my heroku mysql database wants to auto increment everytime with 10. How I can fix this thing?
I have try the query ALTER TABLE table_name AUTO_INCREMENT = 1; but this won't help that number automaticlly goes back to 11.
c.execute("""SELECT keyword,
tag_use_count,
COUNT(*) OVER() AS "Count",
SUM(tag_use_count) OVER () AS "Uses"
FROM tags
WHERE guildid=? AND authorid = ?
ORDER BY
tag_use_count DESC
LIMIT 3""",(ctx.guild.id,member.id))``` over here I want to select keyword which doesnt have nsfw words in it
so I am using betterprofanity
I check it like
if not profanity.contains_profanity(keyword)```
how can I query the sqlite db to select only words which dont contain profanity?
pls ping me when help
I tried this
test_list = ["yeah"]
c.execute(f"""SELECT keyword NOT IN ?,
tag_use_count,
COUNT(*) OVER() AS "Count",
SUM(tag_use_count) OVER () AS "Uses"
FROM tags
WHERE guildid=? AND authorid = ?
ORDER BY
tag_use_count DESC
LIMIT 3""",(test_list,ctx.guild.id,member.id))
results = c.fetchall()```
where test_list is a list of profane words
but that didnt work out either
I got profane words which were in the list in my query
pls help me what to do???
pls ping me when help
test_list = ["yeah"]
c.execute(f"""SELECT keyword,
tag_use_count,
COUNT(*) OVER() AS "Count",
SUM(tag_use_count) OVER () AS "Uses"
FROM tags
WHERE guildid=? AND authorid = ? AND keyword not in ?
ORDER BY
tag_use_count DESC
LIMIT 3""",(ctx.guild.id,member.id,test_list))```
this doesnt work either
is there any database that i can install with pip except Sqlite?
every?
Every database?
@grizzled dagger Postgres uses serial type for auto increment
You want this looks like, https://www.postgresql.org/docs/9.1/sql-altersequence.html
@wispy hazel pip is package manager. you can use this to install a package/library that allows you to connect to a database using python code
test_list = ["yeah"]
c.execute(f"""SELECT keyword,
tag_use_count,
COUNT(*) OVER() AS "Count",
SUM(tag_use_count) OVER () AS "Uses"
FROM tags
WHERE guildid=? AND authorid = ? AND keyword NOT IN {test_list}
ORDER BY
tag_use_count DESC
LIMIT 3""", (ctx.guild.id, member.id))
results = c.fetchall()```this doesnt work either
pls help me
I use sqlite3
@high geyser what do you want to do with count(*) over()?
The syntax of both count and sum are incorrect.
oh
I want to count sum of tag_use_count where keyword is with given conditional
also the number of keywords selected like that
@tepid cradle
and also do you have ideas how to go about my problem?
to check if keyword is not in an external list
I want to count sum of tag_use_count where keyword is with given conditional
@high geyser I don't understand this statement, but count and sum are aggregation functions which need to be used with group by clause. The general syntax is like this:
select user_id, count(transactions), sum(transaction_value)
from user_transaction_history
where user_id in (1, 2, 3, 4, 5)
group by user_id
this will give a count of all transactions and the sum of transaction value for the users 1, 2, 3, 4, and 5
If you use count or sum, you have to use group by, otherwise count and sum can be applied only over the entire table.
I have selected the user_id in my sample query, you can select the keyword the same way
the selected columns must be repeated in the group by clause
oh
@tepid cradle I want to check if the user_id is in a list suppose
but that list contains variables I will add
what will I do then?
test_list = ["yeah"]
c.execute(f"""SELECT keyword,
tag_use_count,
COUNT(keyword),
SUM(tag_use_count)
FROM tags
WHERE guildid=? AND authorid = ? AND keyword NOT IN {test_list}
ORDER BY
tag_use_count DESC
LIMIT 3""", (ctx.guild.id, member.id))
results = c.fetchall()```

