#databases
1 messages · Page 141 of 1
I mean relational databases are easy to learn too
you're trying to use indexing on a Cursor
So how do I do this now?
https://gist.github.com/anand2312/840aeb3e98c3d7dbb3db8b757c1a7ace#sidenote-why-arent-finds-awaited here are examples of working with Cursor objects
I want to if bank_authorisation.find({ "_id": receiver }) == 0:...
That'll never be equal to zero
Then what happens when there is no results?
None?
Refer the example I linked, there's a to_list method - when you use that, and there are no records, it should be giving you an empty list
iirc find_one will give you None
Uh-huh...
find isn't find_one
I know.
find always gives you a Cursor object, regardless of whether there were matching documents
If you only need one document that's the way to go 🤷♂️
uh huh...
@burnt turret Will if recipient["_id"] == id: work?
sorry if I'm bothering you...
i want to get "281"
!paste
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.
show the code before it, can't answer without seeing
I don't mind, just ping me to get my attention and I'll reply eventually
Depends on what kind of data you're storing but generally a relational database will work fine for almost anything
I just want to go with the basics
examples of popular relational databases are MySQL, Postgres
just to learn it
SQLite is probably the easiest to set up then
ok thanks
https://sqlbolt.com is a good resource to learn SQL
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
Where do people prefer to keep their “select” sql statements.
If the script is lengthy would you prefer to create a view in the database and call the view or keep the lengthy sql script in your python file or read it from your static folder.
I am concerned about the readability and managing the code base.
It’s a matter of preference for what you say.
But even if you have lengthy sql there are design patterns you can follow to abstract it further and still have the code readable/maintainable
In my case, I keep a data access layer and use a service/repository pattern to separate it
Or when I use ORMs sometimes I might add some parts directly to the model.
I was thinking of creating a single ugly py file with a dictionary to keep all the lengthy SQLs at one place. It would keep my execute functions clean with key value instead of the whole sql. Readability counts.
This is my case actually
Thanks for this. I will explore this more.
It also depends on your app as well or what your doing. For ex. if you have many applications making the same select query then sure a view/stored procedure might make better sense, to handle migrations or changes in the schema. Or using views for security reasons.
Correct. My app is actually dependent upon raw data coming from some other system as materialised views. I am creating complex joins over those MVs to make the business logic and feed it to my application.
That business logic joins and where clause conditions are in those SQLs.
I plan to feed that data to my app with a stage table in the middle. So all I have to do is
truncate table stage
insert into stg select * from view
which is much cleaner than writing the whole query there.
any chance anyone can discord call me to help me fix an issue ive been stuck on for a week , its simple but im really stuck
What’s the question
Hi guys, came across the following problem with sqlite3 - My other statements seem to work except this.
ValueError: parameters are of unsupported type```
You need to put a trailing comma in the second argument to make it a tuple - (serverid,)
without this, python doesn't make it a tuple
all works! Thank you very much! Very new to working with databases in python 🙂
SELECT COUNT(column) FROM tablename
Hi
I'm trying to go start a new DB but when i try to make migrations from scratch on it it keeps saying django.db.utils.ProgrammingError: relation "fighters_fighter" does not exist
Hi, have been struggling with getting test data into Django from csvs, for weeks now 😄 I am trying to import test data into the car inspection django model, I attempted via django admin and csv import, and also via python scripts but since tables are all related, one to many, many to many, I struggle to get the test data that is also been related imported to the django model (in sqllite). Doesn't make sense!
help would be awesome and welcome! I can share details and my screen if someone will have time.
If there is a recommended way how to do such imports please do share. Am out of ideas.
cluster = MongoClient["my private direction"] Why is it giving me an error in MongoClient?
I have imported it
from pymongo import MongoClient
Can you show the error?
i am appending a series to an empty pandas data frame but when i print the result i find its still an empty data frame
anyone know why this might be happening
self.pv_DFS[1].append(de_pv, ignore_index=True)
self.pv_DFS is a python list of empty dataframes but i cant seem to edit any of these data frames
is pymongo blocking?
this to me or something else?
nonon something else
ok good lol cause idk what pymongo is
If it does I/O and it doesn't use async/await, it's blocking 🙂
Non-blocking I/O is only non-blocking because it allows you give control back with await when it's waiting for I/O to respond.
okay thanks
https://gist.github.com/anand2312/840aeb3e98c3d7dbb3db8b757c1a7ace I've written a gist about the non-blocking mongodb driver and how you'll switch to it from pymongo
How can I do a sqlite search where if I input Bartick it will give me data about Bartick Maiti
OR
I input Maiti it will give me data about Bartick Maiti
Use LIKE for pattern matching
thanks
Though I randomly searched like this SELECT * FROM cards WHERE NAME CONTAINS ? sqlite
and got the same link 😛
But SQLite doesn’t support contains
Oh never mind I misread what you said
Nha nha I didn't knew so I was just testing
it is not even matching spelling
Like if name is Bartick if someone puts B
it is saying true name found
How can I also ensure that it matches spelling?
You can’t because it matches the pattern
Although you can set a minimum character limit so it tries to find those with a minimum number of characters
You can also do it so it matches anything between the first and last letter, and I’m sure this is covered in the link I sent
we have a directory full of mostly similarly structured json files that i wan't to setup as a databse, should i go towrads sql or nosql^
the json structure changes semi regularly though
its also less than a gig and doesnt need fast access so performance is not that important
just need to be able to query for specific properties easily
MongoDB would probably be the easiest to set up for this case 🤔
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-00.8k05l.mongodb.net:27017: ,cluster0-shard-00-02.8k05l.mongodb.net:27017: (10054, 'WSAECONNRESET'),cluster0-shard-00-01.8k05l.mongodb.net:27017: , Timeout: 30s, Topology Description: <TopologyDescription id: 6054a812480b5326016a69fb, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('cluster0-shard-00-00.8k05l.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-00.8k05l.mongodb.net:27017: ')>, <ServerDescription ('cluster0-shard-00-01.8k05l.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-01.8k05l.mongodb.net:27017: ')>, <ServerDescription ('cluster0-shard-00-02.8k05l.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect("cluster0-shard-00-02.8k05l.mongodb.net:27017: (10054, 'WSAECONNRESET')")>]>```
y do u get this error working on mongo DB?
? Is a placeholder and only works for values. So you use it wherever a value will go.
It won’t work for things like table names or column names, because those must be hardcoded into the string
No
First write a working Sql statement by following the link I sent, and then replace the values with the ?
You need to make (Update) a tuple (Update,)
What is wallet?
So then read your statement back and tell me if it makes sense
Where wallet = user id is what you have
Yayy 😃👏
Yeah but you should pick better easier to understand names for variables
sqlalchemy.exc.OperationalError
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1045, "Access denied for user '@root'@'localhost' (using password: NO)")
(Background on this error at: http://sqlalche.me/e/13/e3q8)
help plzz
how do i make a boolean within a postgres expression?
do i just use $x then pass True as the arg?
yes
is there a gui for postgres?
pgadmin
thx
np
would a many to many relationship be the best way to do members?
discord users to guilds
would this be an appropriate table?
await db.execute("CREATE TABLE IF NOT EXISTS members(userid BIGINT REFERENCES users(userid), guildid BIGINT REFERENCES guilds(guildid), announcements BOOLEAN, PRIMARY KEY(userid, guildid))")
ive never done many-many before but i presume its done like this
Yes, if that’s your pivot/junction table
Just add to the current column
UPDATE ... SET column = column + new_value WHERE ...
cursor_data.execute("SELECT bass FROM userfishdata WHERE memberid = ?",(author,))
print(cursor_data.fetchone())
cursor_data.execute("SELECT common_carp FROM userfishdata WHERE memberid = ?",(author,))
print(cursor_data.fetchone())```
is there a better way than doing this to get individual parts of my database?
I'm doing the django tutorial and it mentioned how SQLite is serverless - could someone explain what that means?
You can select multiple columns in a single statement
Just separate each column with a comma ,
yeah but it errors
Then show the error
cursor_data.execute("SELECT * FROM userfishdata WHERE memberid = ?",(author,))
bass = cursor_data.fetchall()[0]
salmon = cursor_data.fetchall()[1]```
this errors
with
It means it’s a file based database, so you don’t connect to it over a network but instead the file system
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IndexError: list index out of range
Any way to write this a bit better? ```MYSQL
SELECT customerName, country, creditLimit
FROM customers
WHERE country = 'USA' AND creditLimit > 100000
OR country = 'Australia' AND creditLimit > 100000
OR country = 'Japan' AND creditLimit > 100000
but its not out of range\
Indexing starts at 0
You do fetchall once and then that’s it. There’s nothing else to fetch
bass text,
common_carp text,
common_herring text,
red_herring text,
blue_herring text,
memberid text,
golden_herring text
Whatever you fetch the first time, use that as your result
yeah but sometimes it comes up with different errors
oh wait nvm u fixed it ty
If this is the db then why are you indexing with 1?
wym
i start the index with 0
There’s only 1 element in that list, and clearly you were indexing with 1 hence the error
Maybe lookup how indexing works with lists.
@proven arrow
doesnt indexing with lists work like this
like for that i would do
print(bass[0][1])
idk
That’s fine
yeah i know bro im just tired
ive been up for like 30 hours
i dont know why i indexed with 1
@ebon skiff you can do WHERE country IN (‘USA’, ‘Australia’, ‘Japan’) AND creditlimit = 100000
SELECT customerName, country, creditLimit
FROM `customers`
WHERE country IN (‘USA’, ‘Australia’, ‘Japan’) AND creditlimit = 100000 LIMIT 0, 25
MySQL said: Documentation
#1054 - Unknown column '‘USA’' in 'where clause'
Don’t copy what I sent but use the proper quotations of your keyboard
My phone doesn’t send it properly over discord
Here it is.
I did it wrong mb
more sql syntax
await db.execute("""UPDATE members
SET announcements = $1
WHERE userid = $2 and guildid = $3""", False, ctx.author.id, ctx.guild.id)
what did i do wrong
no changes happen, but no errors
Does db not use %s?
yes
checked it myself
these are my tables
await db.execute("CREATE TABLE IF NOT EXISTS guilds(guildid BIGINT PRIMARY KEY, prefix TEXT)")
await db.execute("CREATE TABLE IF NOT EXISTS users(userid BIGINT PRIMARY KEY, announcements BOOLEAN)")
await db.execute("CREATE TABLE IF NOT EXISTS members(userid BIGINT REFERENCES users(userid), guildid BIGINT REFERENCES guilds(guildid), announcements BOOLEAN, PRIMARY KEY(userid, guildid))")
the third one is the one im editing
Show the output of:
await db.execute("""SELECT COUNT(*) FROM members
WHERE userid = $1 and guildid = $2""", ctx.author.id, ctx.guild.id)
record count = 1
So it does exist
the third one should be false, but it ends up remaining true
oh it is false
it was moved to the bottom
lol
its working now
turns out i had a row[0] instead of row[0][0]
is there a way to select a single row?
rankings = levelling.find().sorts("xp",-1)
Instance of 'Cursor' has no 'sorts' member; maybe 'sort'?
How to write query which does pagination in Sql?
What?
I'm getting to learn MySQL but I cannot seem to understand how to use the CREATE USER statement. Could someone help me with that?
import mysql.connector as mysql
username = "scopes"
password = "hello"
db = mysql.connect(
host="localhost",
user="root",
passwd="dbms"
)
sql = 'CREATE USER "'+username+'"@"%" IDENTIFIED BY "'+password+'"'
mycursor = db.cursor()
mycursor.execute(sql)
print(db)
Why when I run I get :
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because
the target machine actively refused it)
You know pagination?
Like say I have 100 records and so I make pagination buttons so it shows 10 records for each page
Then I click next and show next 10 items
How to do this with query
Hmm
Best case would be to just fetch all the info and split them into pages.
Yeah but is inefficient
Why?
To fetch thousands or even million records into memory
Especially if the user will only browse through couple page
Making a connection 10 times because 100/10 = 10 or only once a bit longer?
Instead of the info you could also fetch the row count and fetch the first 10.
Yeah so how do this with the query
SELECT * FROM customers limit 10
``` Fetch first 10 results
Ok then next 10?
SELECT * FROM customers limit 10,10
What's the fastest way of doing update statements in postgres?
is there a better way than iterating through the values to insert?
query = '''UPDATE users SET eyecount = $1 WHERE user_id = $2 AND server_id = $3'''
for key in self.eyecounts:
print(key)
args = key.split(',')
user_id = int(args[0])
server_id = int(args[1])
eyecount = self.eyecounts[key]
await self.cxn.execute(query, eyecount, user_id, server_id)
I have something like that going ^^ but its dreadfully slow even though I tested it on only 200 keys
So I'm having trouble with sqlite3 while learning django... Every time I run py manage.py dbshell I get "CommandError: you appear not to have the 'sqlite3' program installed or on your path." I both have sqlite3 installed and added to my user variables and system variables! Since it wasn't working with the sqlite3 directory in path I even went as far as adding the actual .exe file paths to my env variables... At this point I'm completely lost, any help would be GREATLY appreciated!
Question when it comes to using Flask and MongoDB. Do I want to have a connection created when someone hits an endpoint, or do I want there to be one connection created when the app starts up, and all calls use that?
Is there any other way to not update the same id in json files instead having two of the same id and have different values
Select s.cname FROM Students as S,Registration as R where s.rollno = r.rollno and r.courseno = 12 and R.percent_ > 90;
Error
ORA-00933: SQL command not properly ended
can anyone tell how?
Hey, I have a question: I'm required to query json data from a website, load it into a json array, and put it into a tinyTB database (tinydb and json modules are imported in python). I did all of this, but I'm supposed to change the values of 2 keys to lists first (for all records). For example, a record from the json array like { "a" : "72", "b" : 11, "c" : 3420} should first become { "a" : "72", "b" : [11], "c" : [3420]} and then insert it into the tiny db. How can I do this? Am I supposed to use the update function & a for loop or something?
I need to store banned words for message filtering. Is it better to store multiple records with (guild_id, banned_word) or to have one record like (guild_id, banned_words_blob)?
(using aiosqlite)
Can someone explain this statement from the fastapi-postgres docs:
*### Migrations
As during local development your app directory is mounted as a volume inside the container, you can also run the migrations with
alembiccommands inside the container and the migration code will be in your app directory (instead of being only inside the container). So you can add it to your git repository.Make sure you create a "revision" of your models and that you "upgrade" your database with that revision every time you change them. As this is what will update the tables in your database. Otherwise, your application will have errors.*
When he says 'also run', whats that in contrast to? And
With MongoDB, is it any better to store the ObjectID of a document you want to reference in the document that you look at and go dereference that ObjectID, or is it better to embed the contents of that document into the document that you look at?
This is my codecss cursor.execute("INSERT INTO dono_logs (guild_id, user_id, amount) VALUES (?, ?, ?) ON CONFLICT DO UPDATE SET amount = amount + ?;", [guild, user, amount, amount])
And i get this error```css
Ignoring exception in command dono_add:
Traceback (most recent call last):
File "C:\Users\Miste\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\Miste\Documents\GitHub\Wiggle-Bot\cogs\dono.py", line 57, in dono_add
cursor.execute("INSERT INTO dono_logs (guild_id, user_id, amount) VALUES (?, ?, ?) ON CONFLICT DO UPDATE SET amount = amount + ?;", [guild, user, amount, amount])
sqlite3.OperationalError: near "UPDATE": syntax error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Miste\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Miste\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Miste\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: near "UPDATE": syntax error```
https://www.sqlite.org/lang_UPSERT.html
Example:
INSERT INTO phonebook(name,phonenumber) VALUES('Alice','704-555-1212') ON CONFLICT(name) DO UPDATE SET phonenumber=excluded.phonenumber;The syntax that occurs in between the "ON CONFLICT" and "DO" keywords is called the "conflict target". The conflict target specifies a specific uniqueness constraint that will trigger the upsert. The conflict target is required for DO UPDATE upserts, but is optional for DO NOTHING.
me?
yep
k
you seem to be missing that "conflict target"
Look at the example
CONFLICT(column)
I'm not sure if this is a good way to design my database
I want users to add things to a guild specific shop. Am I heading in the right direction? I also want account balances to be server specific
db.execute will give you a Cursor object
the Cursor will contain the data that was retrieved from the database
Right, so you can either loop over cursor itself, or do something like fetchone
...
# looping over Cursor
# each iteration will give you one row
async for row in cursor:
# do stuff with the row
# using fetch-x methods
row = await cursor.fetchone() # this retrieves just one row
you don't need to do both
either works
i used fetchone because you'd be getting only one row from your database for that query (ideally)
in cases where you will be expecting more rows to be sent, you'd use something like fetchall
and it'd give you a list of rows
await fetchone
also, remember - fetchone is giving you a row from the database
a row will be in the form of a list (or a tuple, i don't really know aiosqlite)
So if you send row directly, it'll look something like (23,)
you can use indexing to retrieve the actual value (row[0] etc)
Right but the module doesn't know that
it behaves as if you will be getting multiple values
So it will always give you a tuple
yep
indexing would be the more accurate term
to get only 44 from it that's just row[0]
Yes
Hi, i found this website called sqliteonline.com, does someone knows if i can actually store data in there or it deletes after certain time?
Please mention me when responding, thanks.
If not, can someone recommend me a free online sqlite/sql database?
Some data, but when I see people start using it im probably going to buy one
I'll prefer to not keep my pc on 24/7
Can someone explain this commnd to me? :
docker run --rm --network=host -v `pwd`:/data postgres \
psql -h db -p 5432 -U postgres -c \
"\\copy temp FROM '/csv_kondo/mint_residences.csv' WITH csv"
-v `pwd`:/data postgres \ whats this part?
seems alright to me 👍
just make sure that all guild IDs fit into an int
Working with a database but I'm having issues in my script.
https://paste.pythondiscord.com/ominoduyok.py Think its from me referencing the userInfo twice? not sure. Error line starts line 204 here. I also tried passing the cursor object to returnUserInfo method but didn't work? Checked w/ line 125 cursor1.connection == True returns False.
ok
How do I import a db file in a db folder thats in a lib folder
Does anyone here use goormide
TypeError: The first argument to execute must be a string or unicode query.
Why does this error happen? What is the cause and how do I fix it?
The error says that you have passed something that is not a string as the first argument to "execute"
You should be giving it an SQL query in the form of a string
Yeah my init.py has
db.build()```
It says no module named lib
from lib import db
Yes
So I import it on my main file
Yeah I have it in lib folder too
ok
Hey, does someone have a database in infinityfree?
Because for some reason I can't connect to my sql database
Probably it's something with the credentials (password, user and stuff)
How can I optimize postgres inserts. I’m trying batch inserts on interval while storing data meanwhile in the cache. But then I’m running into the issue of lack of memory. How could I streamline this to make it as efficient as possible?
can someone help me with this
import tools
import connector
import pyfiglet
connector = connector.connect()
def login():
tools.clear_screen()
mycursor = connector.cursor()
myresult = mycursor.fetchall()
result = pyfiglet.figlet_format("Nemo")
print(result)
username = input(" Enter Your Username: ")
print(" ")
password = input(" Enter Your Password: ")
print(" ")
correct = input(" Is This Correct Y/N: ")
if correct == "Y":
pass
elif correct == "N":
tools.clear_screen()
login()
mycursor.execute(f"SELECT * FROM nemo_tool WHERE username = '{username}' AND password = '{password}' ")
if len(myresult) == 1:
print("You Are In")
elif len(myresult) == 2:
print("Wrong Info")
login()```
its not working
I had some trouble connecting to a redis database
any idea
dont use f strings in db queries
What trouble?
this is my error
Nvm, I figured it out
File "/Users/namorahimi/Projects/Python-Projects/nemo_tool/functions/login.py", line 35, in <module>
login()
File "/Users/namorahimi/Projects/Python-Projects/nemo_tool/functions/login.py", line 12, in login
myresult = mycursor.fetchall()
File "/opt/homebrew/lib/python3.9/site-packages/mysql/connector/cursor.py", line 914, in fetchall
self._check_executed()
File "/opt/homebrew/lib/python3.9/site-packages/mysql/connector/cursor.py", line 376, in _check_executed
raise errors.InterfaceError(ERR_NO_RESULT_TO_FETCH)
mysql.connector.errors.InterfaceError: No result set to fetch from
namorahimi@Namos-MacBook-Pro functions % `
Don’t use f strings in db queries
this is the problem
Quit using the invalid syntax then come back.
mycursor.execute("SELECT * FROM nemo_tool WHERE username = namo AND password = namo ")
better
this sqlite?
mysql
ok
Does someone understand parametres in MySQL? Could someone tell me how to Find the names and ids of all the subjects I have to meet in order to study the subject with the id in the variable $ 0 (hence all its prerequisites, prerequisites prerequisites, their prerequisites, etc.). Name the column ‘Name’, the column id ID ’. Sort the result in ascending order by name and remove any duplicates. I'm attaching a pic of the database.
Hello, what Database should I use for simple storing of some numbers?
Hey there have to store material numbers of the following format: 1234.1234
so four digits, a point and then another 4 digits
what data type do I best use for this? I was trying to avoid text and wanted to go with nubers but then I learned that float makes your life miserable by being unprecise
you can't do a var1 == var2
and when I read the value out of my database with a prepared statement the results I'm getting are absolutely ridiculous
What is the number representing? And where you will be doing this calculation
It's like an identifier for an object
Is it always 4 and 4 digits each side
yes
You could store as int.
And then add decimal later when you need
It’s what I did for storing currency in my website
I guess that would work but it's a bit dirtier than what pip suggested so I'm gonna look into that
thx for the advice
Honestly I'd be surprised if there was one, considering how easy it is to just have a start and end column
I don't think there is one either. That's a derived relationship between values.
import mysql.connector as sql
db_con = sql.connect(
user="root",
passwd="root",
host="localhost",
use_pure=True
)
db_cur = db_con.cursor()
def create_db(db_name):
db_cur.execute("CREATE DATABASE IF NOT EXISTS %s", (db_name,))
db_con.commit()
db_cur.execute("SHOW DATABASES")
print(*db_cur.fetchall())
if __name__ == "__main__":
create_db("hmm")```
why do i get a SQL syntax error for this?
Traceback (most recent call last):
File "/run/media/deep/GAMER/Programming/Python/Rough work/practical_exam/rec1,2,3,4.py", line 22, in <module>
create_db("hmm")
File "/run/media/deep/GAMER/Programming/Python/Rough work/practical_exam/rec1,2,3,4.py", line 14, in create_db
db_cur.execute("CREATE DATABASE IF NOT EXISTS %s", (db_name,))
File "/home/deep/.local/lib/python3.9/site-packages/mysql/connector/cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/deep/.local/lib/python3.9/site-packages/mysql/connector/connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/home/deep/.local/lib/python3.9/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''hmm'' at line 1
The placeholder %s is only meant for values
so they won't work with stuff like column names/table names
Those are generally hardcodeded into the statement
You could use an f-string to compose that query. Just make sure your parameter is sanitized.
O
What do I do then
O I did not read what cyg... Said
Sorry
db_cur.execute(f"CREATE DATABASE IF NOT EXISTS {db_name}") ... obviously you need to ensure db_name isn't something nefarious.
Ehhh, that just makes me twitchy. I don't think mysqlclient has a way to parameterise literals. The DB API doesn't address it, either.
just make sure it doesn't have semi colons 😎
It's 2021 and we're still worried about SQL injection attacks. Sheesh.
I think there's some method you can use to escape it
Connection.escape_string probably
You could use that on the table name and then put it into the query i think 🤔
damn this is for an exam haha
its fine if i use .format() ig
though i will keep this in mind for future usecases
thanks :D
hey, I'm new to databases and I'm wondering how exactly they work -
I use PGadmin to access a remote database, for lets say adding a column in a table. Does it take a lot longer to add it from PGAdmin than just doing it on the remote server itself?
And also, lets say I wanted to backup a database every day from RS (remote server) 1 to RS 2, by using something like this:
pg_dump -h [host address] -Fc -o -U [database user] <database name> > [dump file]
My question is, does it take resources from RS 1, the database host, while it's backing up, or from RS 2, the idle server only used for containing the DBs? I want to ideally leave RS 1 on and active, was wondering if it would slow down as I would be backing it up.
Thanks for the help!
I have two "beginners" book for PostgreSQL and SQL:
Practical SQL: a more traditional book of learning SQL by nostarch
A Curious Moon: a novel in which you will learn how to create and use a database to import and make sense of the date from the Cassini mission to Saturn and more specifically the data from the moon Enceladus. Data to be downloaded from NASA.
I have them both, I want to study both of them, but with which one should I start?
MMM i'd probably start with SQL first because you can use SQLite in python to get the basic idea
then move onto postgres as it'll help you setup the server and everything
I am having a problem with MySQL connector, I suppose it's a permission issue even though the machine I am trying to connect to is the machine I am using to create the server so that's odd
def create_connection(host_name, user_name, user_password):
connection = None
try:
connection = mysql.connector.connect(host=host_name, user=user_name, passwd=user_password)
print("Connection to MySQL DB successful")
except Error as e:
print(e)
return connection
connection = create_connection("127.0.0.1", "root", "")
2003: Can't connect to MySQL server on '127.0.0.1:3306' (10061 No connection could be made because the target machine actively refused it)
II have worked with SQL before (lots), but it has been a while (more than decade). So I need a refresher, and the book Practical SQL includes installing PostgreSQL.
Mmm probably got with PostgreSQL
SQL hasnt changed much / at all in the last decade or so 
Anyone here familiar with content addressed databases, is like to avoid inventing an own one, but it seems to me beside git there is nothing else
hey how do I do insert and append at the same time? i use asyncpg
this is how I insert:
await db.execute("INSERT INTO my_table (column_1) VALUES($1)","something")
this is how I append to the list after the first query:
await db.execute("UPDATE my_table SET list_1 = array_append(list1,$1) WHERE column1 = $2",1234,"something")
this does fine but I have to make two calls , how do I append the value into list_1 within the first query?
Are you looking for "UPSERT" ?
no i am not
i just want to insert into a column and append to list in the same row with same query instead of two
it looks like those are 2 distinct operations then. The UPDATE requires a WHERE clause that doesn't apply to the INSERT. Even if you manage to cram them into a single db.execute I think it's going to have to be 2 separate statements for the DBMS
you still didn't get it , i don't wanna do insert and update within a query . suppose I have a table named my_table, it has two columns: column1 character varying and cloumn2 [] integer. I want to know how do I insert a value to both these columns. I could do it if the second column wasn't a list.
CREATE TABLE my_table (
some_number integer,
some_text text,
an_array integer[]
);
INSERT INTO my_table
VALUES (123, 'abc', '{10000, 10000, 10000, 10000}');
thnks!
ok i need to make a decision
-What database is better (in your opinion)? MongoDB or PostgreSQL (Im choosing between these two, other options are or outdated or ive no interest lookingin)
-Why do you think so?
-What are the differences?
PostgreSQL in just about every way other than ease of use for beginners
neither is really hard (or easy) to use, but postgres is a far more mature product
in what way?
PostgreSQL vs MongoDB
- PostgreSQL is a SQL database while MongoDB is a unstructured NoSQL database, PostgreSQL follows all the relational concepts as most SQL databases do.
- PostgreSQL offers considerably more performance than MongoDB despite mongo being 'web scale'
- MongoDB gets real slow as document amounts grow.
- Mongo's unstructured format although great for beginners makes it a maintainability nightmare with no ability to control and lock down certain requirements
- Any mutli document dependent data is considerably slower to fetch with Mongo due to the lack of efficient JOINs
- Python specifically has a much more mature PostgreSQL driver ecosystem vs Mongo which although offers both async and sync drivers are far slower and less mature.
Where mongo does offer an advantage is the fact that it offers the very familiar JSON style format which is great for beginners but ultimately costs the database it's performance and robustness of data.
postgres has been around for a long time and the development team has been extremely thoughtful and intentional about which features they add and the overall design of the database. it's very stable an it rarely has "surprising" behavior (the bad kind of surprise, not the "surprise its your birthday" kind)
alright. im gonna start with postgres. that deservers a pin, thank you ❤️
trying to design a database around JSON's is inherently ugly
and, is https://www.youtube.com/watch?v=qw--VYLpxG4&ab_channel=freeCodeCamp.org a good video to start with?
i like video tutorials
Learn how to use PostgreSQL in this full course. PostgreSQL is a general purpose and object-relational database management system. It is the most advanced open source database system widely used to build back-end systems.
⭐️ Contents ⭐️
⌨️ (0:03:16) What is a Database
⌨️ (0:05:17) What is SQL And Relational Database
⌨️ (0:09:10) What is Post...
uff
win
postgres on windows is very simple to install
get the exe via the download
run it, it'll install PgAdmin aswell
i like to do stuff in pieces. try and just get the database running first, then try connecting to it using a simple client and adding some tables and inserting rows. then see i you can get the python connector to work.
Hello,
i've a stupid problem... i'll try to use python to request mysql database...
if i do : select VERSION(), no problem...
but i'm not able to insert, or just do a stupid select from one database...
my script "freeze" to :
mycursor.execute("SELECT * FROM table limit 10")
print("here")
And in mysql processlist, i see my connection in sleeping....
i do lot of search on web of the strange problem, have you an idea?
(and if i do my simple select in php, for example, no problem too...)
in the connection, i put autocommit=True to try, but no issue 😦
he never "print" my "here"
and i must to kill my script, ctrl+c doesn't work
it try mariadb library and mysql-connector-python
it's very strange
maybe post your full script here? it's hard to imagine what you have done...
Why does my text file in Pycharm become read-only and stops having permanent changes to it?
what about sqlite? 🙂
import mariadb
try:
mydb = mariadb.connect(
host=db_host,
user=db_user,
password=db_pass,
database="mydatabase",
autocommit=True
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
mydb.auto_reconnect= True
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM mytable limit 10")
#mycursor.execute("SELECT VERSION()") <=OK
print("here1")
mycursor.close()
mydb.close()
Sqlite is \o/
?
thanks @hoary coyote
SHOW TABLES works too..
but select no 😦
(and it's the same problem with INSERT, after execute print("here1") don't print...)
in mysql.log, i see the request select.
so, how to go after mycursor.execute("SELECT * FROM mytable limit 10") ?
after mycursor.execute, you need to retrieve the data from the cursor by looping it
example:
for (first_name, last_name) in mycursor:
print(f"First Name: {first_name}, Last Name: {last_name}")
yes, but, i can't (i have not my print("here1");
just a question..
if i just do :
mycursor.execute("SELECT * FROM mytable limit 10")
mycursor.close()
mydb.close()
print("Hello")
program print "Hello"?
or it's necessary to retrieve datas from the select??
it will print "Hello"
ok, so it's not necessary to retreive datas...
i don't understand what is the problem, why he do anything after pass the request to the database.
Version, or list TABLES i'ts ok
but Select, he "wait" after sending the request...
Is there a necessary specification on tables to request tables with python?
not really...i modified your code a bit to connect to my local database and it works fine...
try:
mydb = mariadb.connect(
host=db_host,
user=db_user,
password=db_pass,
database=db_database,
autocommit=True
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
mydb.auto_reconnect= True
mycursor = mydb.cursor()
mycursor.execute("SELECT first_name, last_name FROM users limit 10")
for (first_name, last_name) in mycursor:
print(f"First Name: {first_name}, Last Name: {last_name}")
mycursor.close()
mydb.close()
@hoary coyote there is probably a specificity of the tables, because i create a new table (with phpmyadmin) "test", avec select works fine...
now the questionis, why no problem with php, and can't select or insert with python on theses tables
Whats an sql pool?
Also, where is my data hosted when using Postgres, and will I have to sign up for anything
@hoary coyote is there a limitation about numbers of cols?
(36 for table who have the problem)
@torn sphinx you can always create another simple table to test it out...but i don't think that's the problem...
im suspecting your connection, or something is locking the table..
i've one table with 3 cols, no problem
a table with 9 cols, no problem
a table with 36 cols, can't insert and select into...
request select is sending to mysql server, but no print my "here"
insert request is NOT sending to mysql server
mhh..
postgres is free and open source, the data is hosted where ever you run the server
connexion no, i'ts on the same network (2 VM)
And if I run it on my computer, is it stored on my computer?
its just a program, it runs where you want. if you just start it on your computer, thats where it is going to put the data
I see, so if I begin on my computer, then transfer, it will still be on my computer unless if I move it
Its connected by a network
with SHOW FULL PROCESSLIST; i see no lock
unless you configure it to use some kind of network attached storage, it is local. in general, its unlikely that you'd be able to configure it to use anything but basic local storage without doing it on purpose
big companies and providers might use non-local storage connected over SAN or NAS, but if you're in that situation, you will know it.
@hoary coyote thanks for help!!
the problem was an mtu problem, in my vrack... i forget for theses server to put mtu to 1400...
can someone help me with my mongodb installation in my debian 10 ?
I already installed mongodb in my vps but i want to to connect it to my database
hi, so im using mongodb and i have structure like db -> col -> array -> dict -> specific value, how would i increment that value
Field | Type | Null | Key | Default | Extra
My field varchar(70) | YES | MUL | 12. To be defined |
It will work If I select a record (Drop down list), only if I set nothing, the default will not show up
I have set NUll as Yes, because I have record already existing and don't want to overwrite it
Any idea why the default would not work ?
I'm looking for a high quality training course of sql, anyone know of any?
so i have this [{key: value}, {key2: value2}] inside MONGODB document, how would i increment value2 by 1
Anyone here familiar with mongoexport? I am trying to export all documents older than 120 days but I am struggling with how to format that mongoexport --host localhost --db pagesuccess --collection problem -q='{"date": {"$lt" : ' --type=csv --out text2.csv --fields date,problemDetails,problem,tags,url,language,institution,section,theme
"problemDate": "2020-05-11",
My dates in the db are formatted like that^
I'd like to get my script to delete all entries older than 120 days from current date
I'd appreciate any help!
https://sqlbolt.com/ is a good one
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
i'm getting this issue when I try to connect to my postgresql database
https://i.imgur.com/RdF7pkF.png
async def create_db_pool():
try:
bot.pg_con = await asyncpg.create_pool(host="192.168.1.156", database="usafdata", user="postgres", password="asd")
except asyncpg.exceptions.InvalidAuthorizationSpecificationError:
bot.pg_con = await asyncpg.create_pool(host="localhost", database="usafdata", user="postgres", password="asd")
thats the code im using to run it
older120Days=$(gdate --date="120 days ago" +%F)
mongoexport --host localhost --db pagesuccess --collection problem -q='{"date": {"$lt" : {older120Days}}} ' --type=csv --out text2.csv --fields date,problemDetails,problem,tags,url,language,institution,section,theme```
Can someone help me with my mongoexport command
it isnt working because I think the -q query is wrong
but I am not sure why
im totally new with databases and im just curious if downloading postgresql is needed if i use heroku postgres
you dont need to download the postgres server if you're provisioning on heroku, but you might want to download some type of client so you can connect to the instance and manage it
i created a few tuples i need to pull data from
6 6 21
10 8 22
and i want to insert new tuples using bits of info from different rows
for instance i want to do an insert into with (6, 21, 22)
however 21 and 22 are in different rows
is there a way for me to do this?
mongo or postgresql
Ive been looking on the internet as the best way to do a For loop for an sql table. However it seems that one shouldn't do For loops in sql tables, but my script literally requires it. Is the best way to have ID's for each row, and then for row in range(0,len(table)):#do code?
Trying to create a DB with the option for one of the values to either be an integer or Null. When creating the table, can I specify one of the values such as create table table(value INTEGER DEFAULT NULL). Putting NULL for integer should be ok right?
What does the RESTART function for in the mysql database?
Why are you specifying NULL as default?
doing some prizes, storing in DB. wanting to do NULL to specify that prize quantity is basically unlimited. if a integer specified then it has set quantity type stuff.
If there isn't a NOT NULL constraint then it will be null by default
Wait im bit confused. If I don't specify the value "value" when inserting to db, it stores Null / None for that row, right? If I specify that value though w/ an int then it inserts an integer yea?
having not null prevents text from breaking a number column.
try looking at this; https://www.w3schools.com/sql/sql_isnull.asp
Or DEFAULT 0
yea some diff number, think i understand the NOT NULL constraint then. cant be 0 if i default to a number cause quantity 0 is removed from db. Thank you both! 
test?
i'm not getting notifications from here, when all messages are on, and this isn't set to mute.
weird.
oh...
Possibly because I muted category oops
I can't stand seeing unreads so I mute all big servers
[{key: value +1}]
did u try that?
you trying to add only once or?
set specific amount
I just deleted the element from list and added new one
Ok
you can send me dm
why are you passing user=None?
yeah they aiomysql docs still have some old code examples on there
mongoexport --host localhost --db pagesuccess --collection problem -q='{ "problemDate": { "$lte" : "2021-03-22"}} ' --type=csv --out text2.csv --fields date,problemDetails,problem,tags,url,language,institution,section,theme
2021-03-23T08:47:20.340-0400 connected to: mongodb://localhost/
2021-03-23T08:47:20.348-0400 exported 8 records
--------------------------------------------------------------------------------------------------------------------
mongoexport --host localhost --db pagesuccess --collection problem -q='{ "problemDate": { "$lte" : "$older120Days"}} ' --type=csv --out text2.csv --fields date,problemDetails,problem,tags,url,language,institution,section,theme
2021-03-23T08:47:27.884-0400 connected to: mongodb://localhost/
2021-03-23T08:47:27.892-0400 exported 0 records```
Anyone know why the first works fine
but when I use variable in the second it says exported 0
?
Please ping me if you know why
bot.connection_url = DBconnectionURL
bot.mongo = motor.motor_asyncio.AsyncIOMotorClient(
str(bot.connection_url))
bot.db = bot.mongo["BlackHole"]
bot.MutesDB = Document(bot.db, "Mutes")
bot.TogglesDB = Document(bot.db, "Toggles")
bot.LevelsDB = Document(bot.db, "Levels")
bot.ReactionRolesDB = Document(bot.db, "ReactionRoles")
bot.CountingDB = Document(bot.db, "Counting")
bot.DisMatchDB = Document(bot.db, "DisMatch")
bot.RemindsDB = Document(bot.db, "Reminds")
does this look like a smart idea for the way i set up my databases for my Discord bot, or is there a smarter way to do it?
does anyone know why I am getting an invalid syntax on the gt rank1 = collection.find({score: {$gt: userxp}}).count()
does anyone know how to store multiple things in a column
btw im using sqlite
how do i solve the error - TooManyConnectionsError
im using asyncpg
i defined a asyncpg pool as a bot attribute and whenever i use it , this error pops up
@weak tinsel you probably need to reuse your connections, instead of just creating a new one each time.
Hi, everybody. Have someone worked with cx_Oracle? I can't connect to a remote db and I need some help.
o how do i do that
it needs to be in a string
"$gt"
oh ok thanks
why do i get __aenter__ error when i access asyncpg pool
You probably forgot to call acquire()
i get a new error - pool is closed 🤔
No lol
Whenever I do something like SELECT Restart FROM something, Restart turns blue, anyone know why?
pool = await asyncpg.create_pool(...)
...
#later
async with pool.acquire() as connection:
# do stuff
thats what i did :/
i name my connections as pool hehe
but yeah , doing that gives pool closed error
Are you sure you didn't do
async with pool as connection:
...
At all in your code?
no
for the relevant code no
does it affect if its present elsewhere
Yes
Because doing async with pool... automatically closes it
how would I make a python script that searches through a bunch of databases on my drive if i type in a certain keyword lmao
any libraries would i need?
Hi guys, I'm looking a few hours to this problem
On Raspberry Pi4, I installed:
- pip install mysql-connector-python
- co2 sensor mh-z19 (works only with python2)
I need to insert co2-data into mysql
when I import mysql via python terminal it is OK, but not in a script
pi@raspberrypi:~/mh-z19 $ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import mysql.connector
exit()
pi@raspberrypi:~/mh-z19 $
pi@raspberrypi:~/mh-z19 $ sudo python co2-mysqlconnector.py
Traceback (most recent call last):
File "co2-mysqlconnector.py", line 6, in <module>
import mysql.connector
ImportError: No module named mysql.connector
Try python -m pip install mysql-connector-python
Thanks for the quick response!
Problem is still the same: ImportError: No module named mysql.connector
Why are you using python2 anyway?
co2 sensor mh-z19 (works only with python2)
pi@raspberrypi:~/mh-z19 $ python -m pip show mysql-connector-python
Name: mysql-connector-python
Version: 8.0.23
Summary: MySQL driver written in Python
Home-page: http://dev.mysql.com/doc/connector-python/en/index.html
Author: Oracle and/or its affiliates
Author-email: UNKNOWN
License: GNU GPLv2 (with FOSS License Exception)
Location: /home/pi/.local/lib/python2.7/site-packages
Requires: protobuf
Required-by:
hey, uhm, how do I enable dark theme for all of MySQL workbench, not just the code editor
because my eyes are turning into crisp
I am asking because I am on windows of course
lucky mac people got dark theme support in MySQL workbench as it follows system wide dark theme on it
Hello so i started using firebase
and i want to know how i can get all the apikey authdomain
and things can i get help
nvm found it
That’s wrong
You need to do group by if you use max function there
max is aggregate function so to use it you need to say which column to group on
I don’t know you have to decide how you want to group it
cursor = await db.execute("SELECT userid, max(time) FROM messages group by userid")
lastuser = []
row = cursor.fetchone()
lastuser.append(row[0])
Also what you are trying to do?
There might be better way to do it
That will work if time is always increasing for each row
Doesn’t matter
Also you need to add await before cursor.fetchone()
You pass a list to get member
That’s wrong
Just integer
user_id is a list
How long you have been doing python?
So this is simple and you should know this
Maybe stop guessing and little thinking?
Hey guys I am working on a python script to delete entries from my mongodb with 100k entries if they're older than 120 days. This will probably cause close to 50k entries being deleted. Do you know if it would be better to use db.collection.deleteMany() or db.collection.remove on a for loop of collection size
session.begin(subtransactions=True)
session.add(__request)
session.flush()
id = __request.id
session.commit()```Hello, im using sqlalchemy in my docker flask app and without any reason session.commit() stopped updating table. Requesting id works fine but updating doesnt work, no errors.
Help with?
I am working with sqllite. Don't get how to form this query: List the lowest and highest salary per city, the total number of employees per city, the number of employees earning the lowest salary, the number of employees earning the highest salary, the percentage of employees earning the highest salary per city and the percentage of employees earning the lowest salary per city. This information should be shown as 1 row per city containing all the information requested.
I tried this but not works: select max(salary), min(salary), count(), count() from employees e, locations l, departments d where l.location_id=d.location_id and e.department_id=d.DEPARTMENT_ID and salary= (Select max(salary), city from employees group by city)
Try looking at this: https://www.w3schools.com/sql/
Should I expect aiosqlite to be slower than sqlite? @ me when responding please.
I ask because im testing, executing the same select command 2000 times, with both, I have been getting significantly slower results from aiosqlite than sqlite. Also, sqlite is blocking, right?
if i have a table with a column that's named 'foo' how do I get the name foo with a statment?
I need help too
Some the guys that used to help here have left I think, but what is your question maybe we can help,
client.pagesuccess.problem.deleteMany({ "problemDate": { "$lte" : n_days_ago}})
File "/usr/local/lib/python3.8/site-packages/pymongo/collection.py", line 3445, in __call__
raise TypeError("'Collection' object is not callable. If you meant to "
TypeError: 'Collection' object is not callable. If you meant to call the 'deleteMany' method on a 'Collection' object it is failing because no such method exists.```
Does anyone know why this error happens
deleteMany is supposed to be called on collections?
the functions in pymongo are named according to PEP8, so it's snake case
delete_many @scenic gale
Although the function is called deleteMany in the mongo shell
hi sorry
had to take a break
just the syntax error
hi guys! can we update a view in SQL? if yes is that will affect the mother table?
guys i have a question
i wanna create a website so i need a database
should i use mysql or something
when i made my flask site i used sqlite for testing, then mysql for production
Hi all, im wondering if a db is right for my use case. I currently have millions of jsons, each with variable lists of stings. There currently in level 9 compressed zips that take up almost 400GB. Im basically wondering if a db would take up less space and/or read faster.
python redis still dont have LPOS
is blockchain about databases
If you have access you can Update view definition. It will not affect mother table. But it will affect all applications which are using the view.
Does anyone know with sqlite if it is possible to create primary keys with 4 digits as opposed to 1. For example:
0001 instead of 1
0012 instead of 12
well with a sql database, it is possible to make it with 4 digits, instead of 1. since I store clientids in my database, so I don't think there any reason why it wouldn't.
Yeah just don't know how to do it. I have had a look on the internet with no luck
!e
add = 0001 + 0012
print(add)```
You are not allowed to use that command here. Please use the #bot-commands channel instead.
😦
why are you adding them? That is not what I need lol
I may just do them directly in python tbh
And just convert them to 4 digit
if len(number) == 4:
number = number
elif len(number) == 3:
number = f'0{number}'
elif len(number) == 2:
number = f'00{number}'
elif len(number) == 1:
number = f'000{number}'
Great minds think alike! Thanks!
there may be an easier way of doing that with a for loop and assigning the value to the number, if you want more numbers than just 4.
Yeah I am going to have fixed to 4 numbers. When it gets to 9999 (if it ever does) then it goes back to 0001
Forgive me but is this relating to my numbers lol?
well numbers can be infinity, as long as there's no max limit set by anyone.
assuming number is a string, you can use number.zfill(4) to do this (zfill standing for zero fill)
cc @median swift ^
!e
number = "1"
print(number.zfill(4))
@icy stream :white_check_mark: Your eval job has completed with return code 0.
0001
Brilliant! Thank you so much
helper :P, not quite a mod
happy to help :D
why is this not a solution?
you selected the first element from the sql, instead of the highest number.
but why? im doing offset N
solved it
just did n-1
try removing the offset, and see what value that gives you.
How can I create a python database
you can create a database with python, however, creating a python database is near to impossible.
OK
"GitHub - msiemens/tinydb: TinyDB is a lightweight document oriented database optimized for your happiness :)" https://github.com/msiemens/tinydb
Tinydb is a document database written 100% in python 
it stores data in json files iirc
not really meant for production
this talk about a database written in python led me to http://labs.codernity.com/codernitydb/
anyone ever tried this out before?
How do I get the KEY of data in MongoDb
like
@client.command(aliases=['pl'])
@commands.has_permissions(manage_channels=True)
async def plugins(ctx):
plugins = pluginData.find_one({})
plEmbed = discord.Embed(title=f"**__Plugins for {ctx.guild.name}__**", description="A list of all the plugins available")
for v in plugins:
if plugins[v] == "disabled":
plEmbed.add_field(name="`{}`".format(plugins[i]),value=f"Status: 🔴", inline=False)
elif plugins[v] == "enabled":
plEmbed.add_field(name="`{}`".format(plugins[i]),value=f"Status: 🟢", inline=False)
await ctx.send(embed=plEmbed)
Basically setting the name of the embed fields to the name of the key
Error?
hence why I said it was near to impossible.
@burnt turret
yeah, can you send that code here again
https://gist.github.com/anand2312/840aeb3e98c3d7dbb3db8b757c1a7ace on this website, they said find doesn't need to be awaited
find doesn't need to be awaited, but find_one does
oh
so, what you're trying to do is INSERT if user doesn't exist in the db, else UPDATE
this is called an UPSERT (UPdate INsert)
you do this by passing upsert=True as a kwarg to collection.update_one
i have this
and then instead of getting the previous message count and increasing, then $seting the new value, you can use $inc (increment)
so that entire thing can be condensed into one query -
await levelling.update_one(..., {"$inc": {"messages": 1}}, upsert=True)
oh
well a database is the best option for you to store data, so if your discord bot needs to store data, sure that's a good combination
are there any errors?
nop
send your code
I can't say for sure, but change that bare except there
if any errors are happening, they're getting ignored silently right now
at least print the exception so you know if something goes wrong
ok
idk, how but the leaderboard command worked, I changed nothing, just restarted the bot
i have the same error in the rank command
@burnt turret So with motor i can send a lot of requests to MongoDB, right?
btw, everything is now working, thx!
has anyone have experience with mongoexport
2021-03-25T16:38:59.196+0000 WARNING: ignoring unsupported URI parameter 'retrywrites'
2021-03-25T16:38:59.196+0000 WARNING: ignoring unsupported URI parameter 'maxidletimems```
i'M NOT SURE WHY THIS HAPPENS
the difference between motor and pymongo is just that motor is asynchronous
👍
How would I go about deleting duplicate rows?
Like this:
DELETE FROM some_table WHERE some_column = some_value LIMIT COUNT(*)-1
?
I know for sure I have at least 1 row, I want to make sure the table doesn't keep duplicate rows.
Or do I have to put a select there:
DELETE FROM some_table WHERE some_column = some_value LIMIT (SELECT COUNT(*) FROM some_table WHERE some_column = some_value)-1
?
Hello guys. Question on Regex and wild cards. I usually work on sql server dbs. I am working on a SQLite db now. I am trying to retrieve fields that have special chars in any position. When I input where column_name regexp ‘\b!?@.,\b’ . I tried the usual ‘%[?,.?,]%’ but that doesn’t work either. Any help much appreciated
is it normal for SQL Server to randomly open up terminal in front of you from time to time and automatically install something then close itself?
it's happening with me, every 12~24 hours a random SQL Server terminal window opens in front of me and steals windows' focus, executes something like installing a package, then closes itself automatically, all that happens in the matter of half a second so I don't get a chance to take a screenshot
Can anyone help me out with this syntax error I'm getting here?
query = f"""UPDATE useravatars SET avatars = CONCAT_WS(',', avatars, $1) WHERE user_id = $2"""
await cxn.execute(query, str(avatar), user_id)
asyncpg.exceptions.IndeterminateDatatypeError: could not determine data type of parameter $1
Figured it out. I just had to cast param $1 as a text
what database you using?
oh mysql
Sorry idk anything about mysql. only postgres and sqlite
for postgres you could store it as a text, or a varchar
then split the result on selection
in postgres you could also store it as a json or jsonb datatype. might have something similar in mysql.
does sqlite have a version control type thing
we have a bunch of reference files for integration tests that we store as a bunch of json in their own repo, and im considering switching over to a db
you still can store ints in a database just by declaring the defined column as an int, Ex: create table users(clientid bigint primary key not null);
but its kind of important to be able to track changes to the refernece files, preferably tagged with a commit
hello
I made this test table. I have set the uid col to auto increment
but i don't really know what's wrong
it added 7 instead of 1
I might just have misconfigured it
but idk
I have this dataframe with all kinds of columns
I have to index 'trial' and 'user'
which I did with
this outputs a new dataframe
with user and trial in the right place
but all the columns, except 't' 'x' and 'y'
are gone
how do I get these other columns, like 'delay' back?
Why not use git?
does sqlite export to a format that git can easily track?
cuz i know tracking binary files with git is a nightmare
is the csv export funcitonality gonna have more or less the same ordering? or am i gonna have a millioon+ deletions and additions because of a change in order
mrhmmm
a single reference json is 60k lines lol, so it's a problem :/
updates are made in bulk, maybe just take a snap shot everytime it happens
is there any way I can set up a remote mysql server so I can access the database from any computer?
idfk i'm new to databases
Yeah, it probably is possible, however it's not the most recommended, just remember to close the sql window after using the computer, otherwise someone could delete your database data.
alright , so a user will be given option in combobox to either pay with cash or in installments
if the user selects installments then his details will be updated in a separate Table
... but i wonder how do i do it ?
I don't come with a computer science background, but really love coding in python. Could someone show me the best way to learn/understand posgresql? The docs are amazing, but a little overwhelming.
Have you set up a server on your local machine and played around with it?
Reading the docs is good but it remains abstract until you actually practise with the real thing
@sterile pond Absolutely! I currently learn through an ORM, but eventually I am going to try running the queries directly too
Ahh noice
As someone who recently entered the industry, if you have the bandwidth to learn more at once, give some cloud fundamentals certs your consideration
Doing them really opened doors for me
Unhandled exception in internal background task 'levelup'.
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/discord/ext/tasks/__init__.py", line 101, in _loop
await self.coro(*args, **kwargs)
File "/home/palace/ranks.py", line 93, in levelup
await db.execute("""UPDATE ranks SET experience = $1 AND level = $2 WHERE id = $3""", experience, lvl, user_id)
File "/usr/local/lib/python3.6/dist-packages/asyncpg/connection.py", line 304, in execute
return_status=True,
File "/usr/local/lib/python3.6/dist-packages/asyncpg/connection.py", line 1632, in _execute
ignore_custom_codec=ignore_custom_codec,
File "/usr/local/lib/python3.6/dist-packages/asyncpg/connection.py", line 1655, in __execute
ignore_custom_codec=ignore_custom_codec,
File "/usr/local/lib/python3.6/dist-packages/asyncpg/connection.py", line 1681, in _do_execute
ignore_custom_codec=ignore_custom_codec,
File "/usr/local/lib/python3.6/dist-packages/asyncpg/connection.py", line 380, in _get_statement
ignore_custom_codec=ignore_custom_codec,
File "asyncpg/protocol/protocol.pyx", line 166, in prepare
asyncpg.exceptions.DatatypeMismatchError: column "experience" is of type bigint but expression is of type boolean
HINT: You will need to rewrite or cast the expression.
any idea how to fix this?
experience = $1, level = $2
Presumably right now you're getting your variable set as false instead of $1
It's evaluating that expression
$1 AND level...
So yeah experience is trying to be set to the boolean result of that expression
@velvet ridge your missing a parenthesis
cur.execute("INSERT INTO login VALUES(?, ?, ?)", (username, password, current_time)<- missing a parenthesis
that is the line that has the problem
How to do it so if parent key is updated all child keys are updated in sql?
🤔 not very sure, i think postgres has something like CREATE TRIGGER you could use for this
or maybe i'm misunderstanding the question
So basically when the primary key updates of any row, it will then update the foreign keys wherever it is used?
in theory i suppose you could write a trigger like that
actually that might be a pain to set up
i think with one event in a trigger you'd be able to do stuff to only one target table and not multiple (like in the case of foreign keys), which would mean you'd be setting up multiple triggers...
🤔 maybe this isn't the best solution, sorry
Traceback (most recent call last):
File "votes.py", line 104, in list_checking
await db.execute("""UPDATE ranks SET experience = $1, level = $2 WHERE id = $3""", experience, lvl, user.id)
File "/usr/local/lib/python3.6/dist-packages/asyncpg/pool.py", line 58, in call_con_method
meth_name))
asyncpg.exceptions._base.InterfaceError: cannot call Connection.execute(): connection has been released back to the pool
why does this happen
Share more code please
async with self.pool.acquire() as db:
for user_info in self.users.keys():
user_id = int(user_info)
touples = await db.fetchrow("""SELECT * FROM ranks WHERE id = $1""", user_id)
if touples is None:
experience = self.users[str(user_id)]["exp"]
lvl = int(experience ** (1 / 3))
await db.execute("""INSERT INTO ranks(id, experience, level) VALUES($1, $2, $3)""", user_id, experience, lvl)
else:
experience = toup["experience"] + self.users[str(user_id)]["exp"]
lvl = int(experience ** (1 / 3))
if lvl >= 150:
lvl = 150
wait db.execute("""UPDATE ranks SET experience = $1, level = $2 WHERE id = $3""", experience, lvl, user_id)
apparently the problem was in the update line
I have no idea what is wrong, never used asyncpg
i want to add on field NULL values / variables
how to insert with "where"
i am using sqlite
as I had said earlier, INSERT doesn't have a WHERE clause
do you want to add a Null in the JackpotBan column of the second row?
you'd use UPDATE in that case
no i want to add the text "ban" to UserId with that function
can you describe that in terms of your table?
you won't be able to add ban to userid
userid looks like it is a numeric field?
you'd want a user's id there
its text
even then, you'd want the user's ID there
naming a column "UserID" and then not storing user IDs there is gonna be very misleading
here i checked if user is in database with userid and it works
do you mean you want to have the text "ban" in the jackpotban column for specific users?
every userid can have "ban" on column jackpotban
only ban?
yes
then why even store anything there
if the user ID exists in the table, it can be implied that it is banned
ik but i want to know how it works :0
so i can use the database more then only for that
alright
can you explain in terms of your table, what you're trying to do now?
do you want the jackpotban column for the second row to have the text "ban"?
making sure the next users you add to it have "ban" is just as simple as inserting the word "ban" as well the next time
you could also have set that column to have a default value of "ban" while creating the table (so if you dont specify a value, "ban" will be used)
nvm i do it with only userid
but how to get the text from database as string?
this what i get
you need to await the coroutine
can u tell me how to do that
await coroutine()
oh wait found haha
Does anyone on here know if I can create linked lists in a Microsoft Access database using python, or they are importable from a text file? (I do not have the option to migrate the database to something that is not Access fyi)
Is there any way to write the json seperated by lines?
hey i need help to add / insert in database with 4 variables (sqlite)
Error:
Ignoring exception in command addgift:
Traceback (most recent call last):
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "W:\Pybot\QGiveaway\bot.py", line 242, in addgift
await add_gift(number, key, plattform, howto)
File "W:\Pybot\QGiveaway\bot.py", line 109, in add_gift
sql.execute("INSERT INTO tbl_gifts (GiftNumber, GiftCode, Plattform, HowTo) VALUES (" + number + ", " + key + ", " + plattform + ", " + howto + ")")
sqlite3.OperationalError: no such column: STEAMKEY
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: no such column: STEAMKEY
Code:
https://media.discordapp.net/attachments/823228011477860355/825045068808323082/unknown.png
Please mention me.
I've told you multiple times now
You do not simply concatenate (add strings) together like that
You have to use the placeholder provided by the library and pass the arguments in separately
in your code that'd be
sql.execute("INSERT INTO tbl_gift (GiftNumber, GiftCode, Plattform, HowTo) VALUES(%s, %s, %s, %s)", (number, key, plattform, howto))```
https://docs.python.org/3/library/sqlite3.html I'd highly recommend you go over this tutorial
@green raptor
cursor.execute(f"SELECT PlayerID FROM Logs WHERE TempbanTimeRemain < {datetime.datetime.now()}") this give me this error sqlite3.OperationalError: near "16": syntax error
its for a discord bot
This error is caused because youre using an f-string
Error is same as what aand said before
Yep
ive used f strings before and they work
They work but they're not what you're supposed to use
Third pin in this channel explains why
The doc I linked just a few messages above also does, and also tells you how to do it right
ty i will check that tutorial
but why i am getting this error for ur sql
Ignoring exception in command addgift:
Traceback (most recent call last):
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "W:\Pybot\QGiveaway\bot.py", line 242, in addgift
await add_gift(number, key, plattform, howto)
File "W:\Pybot\QGiveaway\bot.py", line 109, in add_gift
sql.execute("INSERT INTO tbl_gift VALUES(GiftNumber, GiftCode, Plattform, HowTo) VALUES (%s, %s, %s, %s)", (number, key, plattform, howto))
sqlite3.OperationalError: near "VALUES": syntax error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: near "VALUES": syntax error
Remove the VALUES from there
I'm on mobile I typed wrong
The point of what I wrote was how I'm passing the arguments in
Instead of adding strings together, I use the %s as a placeholder and then pass the arguments in separately
That is what I wanted you to infer from that
I've edited the message
yes understood that
Traceback (most recent call last):
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "W:\Pybot\QGiveaway\bot.py", line 242, in addgift
await add_gift(number, key, plattform, howto)
File "W:\Pybot\QGiveaway\bot.py", line 109, in add_gift
sql.execute("INSERT INTO tbl_gift (GiftNumber, GiftCode, Plattform, HowTo) VALUES (%s, %s, %s, %s)", (number, key, plattform, howto))
sqlite3.OperationalError: near "%": syntax error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Jakob\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: near "%": syntax error
Uhh try ? Instead of %s as the placeholder, I'm not sure which one sqlite uses
@green raptor yeah that's the issue
now i learned
Never use string concatenation for SQL queries
The third pin in this channel explains why that's a bad idea
ik read docs etc etc, but i like learning by doing xd
what do u mean
adding two strings together using + is string concatenation
String interpolation is using something like f-strings, str.format() to bring variables into your string
BOTH of these are bad ideas to be used with SQL queries
The placeholder thing I just showed you is the right way
@burnt turret is causing crashes on my pc?
Cause since i did this command pycharm and other stuff crashing
where can i find the documentation for pymongo?
I wanted to make a desktop auth system
MongoDB
how would i append to the categoryDisabled
so that becomes
_id: int
disabled: Object
categoryDisabled: int
command: "triggered"
command: "snipe"```
oke thanks!
why does mysql installation take... forever
is it because of all the configurations that need to be done in the background?
Is there any way to write json data by seperated lines?
json.dump(data, fp)
fp.write("\n")
``` Seems not work
@torn sphinx https://docs.python.org/3/library/json.html the indent keyword should be what you're looking for
ok thanks
Hi
I've often heard people recommend using SQLite for development and then switching to something else e.g. PostgresQL for deployment
Because apparently SQLite isn't suitable for a deployed web app
If that is the case then why not start straight away (in development) with your intended db e.g. Postgresql? I'm starting to discover that SQLite has a few limitations when compared to PostgresQL so I don't understand why people even use it in the first place when they are intending to switch.
i need some help with sqlite
its a little too much to explain so if some1 has the time dm me
async with aiosqlite.connect(...) as db: await db.execute("INSERT INTO some_table ...") await db.commit()```
I'm thinking about adding this after my bot's on_ready but it'll just
aync with aiosqlite.connect(...) as db:
return db```
So when I use a command that needs data persistence I'll just do
await db.execute(stuff here)```
I just wanted to tell someone about it. I'm going to start writing it. I honestly don't think it'll work but
Hello! That's using a context manger which will automatically close the connection once the code inside there is done.
I would recommend creating a function which connects to the database and returns that connection before defining it as a bot variable
See: https://github.com/mesub7/pogmas/blob/51a1c4dec78e66327c8b73cebbd4f703186c83c3/Pogmas.py#L33 and https://github.com/mesub7/pogmas/blob/51a1c4dec78e66327c8b73cebbd4f703186c83c3/Pogmas.py#L39
I'm going to read it rn, brb
That little guy reminds me of a pet project in python coding projects where you make something like that for a virtual pet
Hey guys iam not sure if this is the right place to ask....but can anyone give me a good source for me to learn about sqlite or sql in particular since iam kinda lost rn...ty
Tysm :)
Np
how do i delete a single value from the db instead of a entine row
DROP column_name FROM table;
Does that work
oh, hold up, my database is on a different local host... so the password is different?
I thought it was
ALTER table DROP COLUMN column_name;
Depends, did you make the password different
ah, I don't think there's a DELETE function.
no, I didn't change the password, It just sets to the default password for the local host, whatever that is.
hey how do you set up a remote mysql server I wanna use it for an auth system
idk
lol
what is the default password for MySQL?
Not sure 🤔
I don't think there's a default password
It asks you to set a password on installation
If you didn't it's probably just blank
Hmmm, well... apparently that's difficult, if you already created a database, and then moved to a different local host.
What do you mean by move to a different localhost
Well, let's say my database installation was created on TimeTree's Wifi Network, and I set a password there, then I moved to FluffyTree's WifiNetwork, now I use the same password, I used for TimeTree's Wifi Network, and then it says Cannot connect to localhost (10159) on FluffyTree's WifiNetwork.
The WiFi network you used while installing a database doesn't change anything really? I don't understand what's going on here
localhost is your computer
How are you trying to connect?
through the MySQL Command Line Client
I had this issue for a little while, and then it worked, so I don't understand.
Huh
The default port for MySQL is 3306, and that message looks like it is trying to connect on 10159
Did you change the settings or something?
Check if the MySQL server is running
Welp,
Hi, can someone help me with a SQLAlchemy question? I am using a decorator to wrap all my database queries in a context manager to handle database sessions, but when I try to decorate classmethods, i can't get it to work, something about the order of the cls and session variables keeps breaking.
There is something that is using the port
ah, of course, there is.
because I remember I got it working by closing every single one of my apps then re-opening mysql
most probably my apache
huh?
does anyone know a free mysql vendor that gives remote access
?
please ping me with your response
WHY DOES MY SQLITE3 DB KEEP LOCKING
Every time I update it and then try read the new update (from a discord bot) it stops for a bit and then says db is locked
Update: I had 3 loops constantly checking and updating the db, I commented them out and it worked, seems to for now, but I need the loops to be running, how can I solve this
using pyodbc, is there a nolock function I should be aware of when querying a sql server (not altering, appending or creating)
Is there any difference between sql,mysql,postgresql,sql server,and sqlite.... iam to new to sql and iam so confuse mind giving me some guide...ty
SQL is the language (Structured Query Language) that you use to interact with a relational database.
The rest of the names you listed (MySQL, PostgreSQL, ...) Are all databases
So you'd use SQL to interact with any of them (they all have some quirks, but if you know SQL you can basically use any of them)
https://sqlbolt.com is a good tutorial to learn SQL
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
After going through that you, sqlite would probably be the easiest to get started with as it won't need any installation
await cursor.execute("Select CardDatabase.Name, CardDatabase.Rating,CardDatabase.CardType,CardDatabase.Value FROM CardOwnership INNER JOIN CardDatabase ON CardOwnership.CardID = CardDatabase.CardID WHERE CardOwnership.DiscordID = ? ORDER BY CardDatabase.Rating DESC LIMIT ?,?",
(userID,self.startValue,self.stopValue,))
data = await cursor.fetchall()
This works for select.
How do I conver this to Delete
?
I'm using asqlite - an sqlite3 wrapper for async
You can use DELETE
Did you commit?
how do i add +(number) to every integer from a column at the same time in sqlite
