#databases
1 messages ยท Page 171 of 1
Primary key is for one column
Composite will take more column and see if both values make a unique row
Depends on use case
What do you wanna achieve
like there are tables like answer, answer_vote (that stores the votes for the answer) and i want that everyone can upvote only once
thank you so much
it works
<3
no homo
Account_id and answe_id composite primary key, dont make them unique individually and it will work
Cool
yes i did PRIMARY KEY (account_id, answer_id)
If I have a dataset my_database(name, amt_spent), what is the most efficient way to write a query to determine how many people have spent under 10 dollars and how many people have spent >= 10 dollars?
I can write a query to do so but it requires two sub-queries which feels messy. Must be a more compact way to do so
I'm not sure how to use UNION to reduce the number of subqueries I have to write
And yes, postgresql to be specific
i'm saying that UNION seems like the right tool for the job, and writing two subqueries doesn't seem that bad
the alternative is SUM(CASE WHEN amt_spent >= 10 THEN 1 ELSE 0 END) which is a lot worse imo, and probably a lot less efficient
That gives me something to think about. Sounds like you might be right, two subqueries isn't too bad. Performance is good so who cares. Thanks
ok
The fastest in my test (sqlite, table with ~165k entries, ssd, no index on column) was:
select
count(*) filter (where amt_spent < 10),
count(*) filter (where amt_spent >= 10)
from my_database
Get familiar with it, use something else, or get someone else to do it for you.
I'm trying to answer this SQL question I'm not too good at SQL though
Would I use a JOIN statement?
@torn sphinx use drop
SELECT Consultant.LastName, AVG(Client.Balance), COUNT(Client.ConsltNum)
RIGHT JOIN Consultant
WHERE Consultant.ConsltNum = Client.ConsltNum
Is this wrong?
Where is from and main table
Select โฆ. From table right join 2ndtable on condition match
Check w3school for syntax
Hi, how can I reorder the columns returned in a SQL query?
Right now the results return 4 columns:
Rentals, City, Country, Amount
I'd like it to return in this order:
City, Country, Rentals, Amount
Is there something I can add to my query to achieve this?
SELECT COUNT(rental.rental_id) AS "Total Rentals",
city.city, country.country, SUM(amount) AS "Total Amount"
FROM rental
INNER JOIN customer ON rental.customer_id = customer.customer_id
INNER JOIN payment ON rental.rental_id = payment.rental_id
INNER JOIN address ON customer.address_id = address.address_id
INNER JOIN city ON address.city_id = city.city_id
INNER JOIN country ON city.country_id = country.country_id
GROUP BY city, country
ORDER BY `Total Rentals` DESC
LIMIT 10;
Oh, I figured it out - just reorder my select statement.
Hey anyone has experience with parquet?
Is it bad practice to use parent id as primary key in one-to-one relationships? I'm using postgres
you should ask your entire question. don't "ask to ask", forcing someone to interview you in order to help.
if it truly is one-to-one, i don't think there's anything wrong with it. are you talking about a situation like this?
user: id, username, email_address
moderator: user_id, level
where every moderator is a user, but not all users are moderators
Kinda. There are nodes that will hold data, and data is a separate entity that should only be present in one node
Just wanted to be sure, thanks
yeah that seems reasonable too, maybe there's some "web scale" reason why it's not a good idea
Why do I keep getting syntax errors - Jet SQL
what is the syntax error?
oh
you wrote TABELE, not TABLE. at the top
but you should always post the actual error
The date in the insert is also not quoted.
it just says Syntax error
in field defintion
Syntax error in field definition - Jet SQL
CREATE TABLE Sept21(
OrderNum INT,
TaskID VARCHAR(5),
Description VARCHAR(50),
ScheduledDate DATE,
QuotedPrice DECIMAL(15, 2)
);
INSERT INTO Sept21(ScheduledDate)
VALUES("9/21/2018");
@arctic granite usually you need single quotes for strings in sql, that might be the problem. VALUES('9/21/2018')
@harsh pulsar I tried both right now Microsoft Access just doesn't like how I am trying to create the table.
unfortunately i don't know the specific of microsoft access, hopefully you figure it out
i made a discord bot and the data is stored in json but i wanna switch to Postgres
how do i quickly convert my code
Is heroku really great in hosting a Postgres DB?
Depends on your project and budget. It's the best free option I've seen.
For a discord bot, is it good?
Sure, it's fast and easy and free to setup so just give it a try
wasn't a question question, more of like general experience with it and if it is good for data storage of around 1gb
that's still a question! and yes, it's fine for data storage of 1gb, although it depends on what the data is and what you need to do with the data
yeah, it's fine for that. good compression, fast (parallelizable) and "lossless" reads. much much better than csv
attempted to writie on it with pandas, but seems there is no appending mode, is that something normal for parquet? is it a common practice to split the files and then merge them all together?
parquet is column-oriented, so it's not really good for appending workflows. however, pandas supports reading from multiple parquet files at once, so you can just put a new file in the same directory. so parquet is not that good (inefficient) for lots of small appends
ow okay so i guess just create multiple 100mb parquet files (size is just estimating) and then just read all
btw create 100mb parquet is like daily archivation
archiving*
yep that's perfectly fine. it'd be silly to make a bunch of files with like 10 rows each. but a 100 mb daily archive is very reasonable
Anyone know a good database for my program I need licenses that expire
Is there a way to use inserts from other tables to make a new one?
Like using insert select
๐ญ Been at this for 10 hours
is the correct datatype for a guild whitelist bigint[]?
if you're storing the ids? yeah
alr
so i have a whitelist command, adding users works fine but viewing the whitelist doesnt.
whitelist = await self.client.db.fetch('SELECT whitelist FROM guild_data WHERE "guild_id" = $1', ctx.guild.id)
print(whitelist)
embed = discord.Embed(description="\n")
embed.set_author(name='Whitelisted Users', icon_url=self.client.user.avatar_url)
for i in whitelist:
member = self.client.get_user(i)
embed.description += f"{member} | {member.mention} [`{member.id}`]\n"```
whitelist is being printed out as `[<Record whitelist=[885172387002351617]>]` and with this code returns `TypeError: unhashable type: 'list'`
i tried a different code using my events cog
```py
wl = []
whitelist = await self.client.db.fetch('SELECT whitelist FROM guild_data WHERE "guild_id" = $1', ctx.guild.id)
for data in whitelist:
wl.append(data['whitelist'])
print(wl)```
this code returns the same error but printing wl returns `[[885172387002351617]]`
both of theses "solutions" dont work
afaik you don't put quotes around the guild_id part
for me it works without using quotes
also you're supposed to be passing in ? for variables inside the query, or else it tries to find a literal $1
Hello, I'm doing a query to find all the actors who appeared in films together in the Sakila database. I'm getting duplicate/inverse results however.
What should I look at to mitigate this?
JULIA MCQUEEN - HENRY BERRY
HENRY BERRY - JULIA MCQUEEN
SELECT concat(a1.first_name," ", a1.last_name) AS "Actor 1 Name", fa1.actor_id AS "Actor 1 ID", concat(a2.first_name," ", a2.last_name) AS "Actor 2 Name", fa2.actor_id AS "Actor 2 ID", COUNT(film.film_id) AS "Films Together"
FROM film
INNER JOIN film_actor fa1 ON film.film_id = fa1.film_id
INNER JOIN actor a1 ON fa1.actor_id = a1.actor_id
INNER JOIN film_actor fa2 ON film.film_id = fa2.film_id
INNER JOIN actor a2 ON fa2.actor_id = a2.actor_id
WHERE
fa1.actor_id != fa2.actor_id
AND fa1.film_id AND fa2.film_id = film.film_id
GROUP BY
fa1.actor_id, fa2.actor_id
ORDER BY `Films Together` DESC;
Thank you for the help! ๐
I think this should get you there: https://stackoverflow.com/questions/1330692/distinct-pair-of-values-sql
Thank you! I'll give this a read now.
Is there anther way to type this statement?
SELECT *
FROM Tasks
ORDER BY Category, Price;
hi! I am seeking an advice on starting with DBs for my project. is this a right place to ask those kinda questions?
@obsidian lichen yes
cool, I am kinda new with python. I have started a project where i would scrape data from web n convert it to dict. I am wondering what's an ideal db to use to store those dicts?
i can explain my project idea if that helps you understand better
@obsidian lichen im not really an expert either im big time noob using this chat foe help on something i have been stuck on for li 9 hours
What is wrong with my INSERT statement
INSERT INTO Sept21(OrderNum, TaskID, ScheduledDate, QuotedPrice)
FROM OrderLine
WHERE ScheduledDate = '9/212018';
aren't you missing keyword VALUES after the tablename?
Im struggling with this so badly.
INSERT INTO Sept21 (OrderNum, TaskID)
SELECT OrderNum, TaskID
FROM OrderLine
WHERE ScheduledDate = '9/21/2018';
its a Data Type mismatch in criteria expression.
A database might be overkill if dumping to JSON is sufficient.
import json
with open("file.json", "wb") as f: f.write(json.dumps(dict).encode("utf-8"))
That third line needs to be indented
@fading patrol Can a Where statment be used on an Insert Into select SQL statement?
!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.pythondiscord.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.
IDK Google it https://stackoverflow.com/questions/485039/mysql-insert-query-doesnt-work-with-where-clause
Use None or don't include it in the insert list? But what would be the purpose of an empty role_id?
Are there any good abstraction layers for python that abstract away Postgres queries into functions such as db.get("KEY", "COLUMN") instead of db.one("SELECT key FROM column")? I know redis does this well but I'm using Postgres with other stuff and I want a Python program to interface into it.
I just absolutely hate dealing with queries in every shape and form, they make no sense to me whatsoever.
cursor = await self.bot.db2.execute("INSERT OR IGNORE INTO mute (guild_id) VALUES (?)", (guild_id,))```
will use the default for role_id which is NULL if not otherwise defined at the create table.
But, again, what would the purpose of such an entry be? Mute the whole guild?
A insert won't overwrite the current entry.
DELETE or UPDATE?
This would delete all rows with that guild_id.
If there are more column and you just want the role_id set you need an update.
await self.bot.db.execute("UPDATE guildData SET role_id = NULL WHERE guild_id = ?", (guild_id,))```
Btw, you changed tablenames.
Be sure to use the table you want.
UPDATE only works on existing rows. If there is no entry nothing gets updated.
What database (and db module) are you using?
you can try:
...
cursor = await self.bot.db.execute("UPDATE guildData SET channel_id = ? WHERE guild_id = ?", (channel_id, guild_id))
if cursor.rowcount == 0:
cursor = await self.bot.db.execute("INSERT INTO guildData (channel_id, guild_id) VALUES(?, ?)", (channel_id, guild_id))```
or:
sql_stmt = (
"INSERT INTO guildData(channel_id, guild_id) VALUES(?, ?)"
" ON CONFLICT(guild_id)"
" DO UPDATE SET channel_id = excluded.channel_id"
)
cursor = await self.bot.db.execute(sql_stmt, (channel_id, guild_id))
That's the UPSERT clause if the guild_id is the primary key or unique.
The default is only used if you don't specify the column on insert.
so
... insert into level_channel(guild_id) values(?) ...
will use the default for channel_id, but
... insert into level_channel(guild_id, channel_id) values(?,?) ...
will use the supplied value.
You define the default value at table creation and it is used only on insert like I described above.
What I'd another way that I can get the same results as:
Select *
FROM Tasks
ORDER BY Category, Price;
Using SQL
hello y'all! I just started in the python's world. do you guys usually use ORMs or rather write down your SQL queries?
What do you mean, another way? That's as simple as it gets.
@grim vault I was asked to do this statement another way but in the end I guess the alternative way to do it was to enter each element in the select statement individually ๐คทโโ๏ธ
@grim vault Could I ask you another question which just a yeas or no answer
SELECT WorkOrders.OrderNum, WorkOrders.OrderDate, WorkOrders.ClientNum, Client.ClientName
FROM WorkOrders
INNER JOIN Client
ON WorkOrders.ClientNum = Client.ClientNum
WHERE WorkOrders.OrderDate = '9-10-2018โ;
In theory this should work right?
Yes, depends on how the db handles dates I guess.
@grim vault MS Access won't accept it, I guess it just on tagt side being annoying
I'll delete it from here, and ask in #algos-and-data-structs.
The ending apostrophe looks wrong โ instead of '
@grim vault OH yea
You are not allowed to use that command here. Please use the #bot-commands channel instead.
hey anyone did some personal testing on pyarrow vs fastparquet engine for parquet? (maybe @harsh pulsar )
This is more of a general question, but how would one take a large collection of photos and automatically rename them based on a list of plaintext values?
i recall a few years ago i switched to fastparquet because there was some bug or unsupported feature in pyarrow, but i don't remember what it was. i don't remember which one was faster, but personally i'd go with pyarrow since it actually uses bindings to the apache arrow library
ola! I am having trouble with flask-sqlalchemy, Here are the errors:
error
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column "profile_image" of relation "profile" does not exist
LINE 1: INSERT INTO profile (profile_image, display_name, descriptio...
^
[SQL: INSERT INTO profile (profile_image, display_name, description, user_id) VALUES (%(profile_image)s, %(display_name)s, %(description)s, %(user_id)s) RETURNING profile.id]
[parameters: {'profile_image': 'https://res.cloudinary.com/', 'display_name': 'texo', 'description': None, 'user_id': UUID('79ee0e7f-da94-47b5-b1f9-dcbc08725ca4')}]
(Background on this error at: https://sqlalche.me/e/14/f405)
class Users(db.Model):
id = db.Column('id', UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
username = db.Column('username', db.String(20), nullable=False, unique=True)
email = db.Column('email', db.Text, nullable=False, unique=True)
password = db.Column('password', db.Text, nullable=False)
profile = db.relationship('Profile', backref='user')
def __repr__(self):
return '<User %r>' % self.username
class Profile(db.Model):
id = db.Column('id', db.Integer, primary_key=True)
image = db.Column('profile_image', db.Text, default='')
display_name = db.Column('display_name', db.String(50))
description = db.Column(db.String(200))
user_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'))
def __repr__(self):
return '<User %r>' % self.id
user = Users(
username=args['0'],
email=args['1'],
password=bcrypt.hashpw(str.encode(args['2']), bcrypt.gensalt(12))
)
profile = Profile(display_name=args['0'], image='https://res.cloudinary.com/', user=user)
db.session.add(user)
db.session.add(profile)
db.session.commit()
args is essentialy 0 for username, 1 for email, and 3 for password
what's the best way to handle user roles in mysql?
should I create a table users with name, password and admin columns?
cursor = await db.execute('SELECT * FROM bans')``` How could I only select a certain column from a table? Let's say that column name is `x`.
This is a SQLite database.
select x, y, z from bans
So SELECT x FROM bans would just select the x column?
yes
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
@tasks.loop(seconds = 60)
async def bans():
await client.wait_until_ready()
guild = client.get_guild(764574130863079424)
db = await aiosqlite.connect('database.db')
cursor = await db.execute('SELECT time_expired FROM bans')
rows = await cursor.fetchall()
stored_timestamp = cursor
stored_date = DT.datetime.fromtimestamp(stored_timestamp)
if stored_date <= DT.datetime.now():
oof
await db.close()
bans.start()``` One more thing- This is kinda database, kinda not... but how could I loop through all the `x` columns of the database so it checks all the stored_date's if it's past now?
Or select only the expired bans- which I'm not sure how.
@ me if you have a response, preferably on the second thing since that'll be the most efficient.
How do i check if theres a document or not in mongodb
Im using motor
How would I insert data into a colum with spaces on sqlite3???
What is wrong with this SQL statement?
CREATE TABLE Horse(
ID SMALLINT UNSIGNED AUTO_INCREMENT,
RegisteredName VARCHAR(15) NOT NULL,
Breed VARCHAR(20) CHECK (Egyptian Arab, Holsteiner, Quarter Horse, Paint, Saddlebred),
Height decimal(3,1) CHECK (Height>=10.0) AND (Height<=20.0),
BirthDate DATE CHECK (DATE>='1-1-2015')
);
Its having issues with my sql CHECK (Height>=10.0) AND (Height<=20.0) statement am I doing it wrong or do I have to do it another way?
db = await aiosqlite.connect('database.db')
cursor = await db.execute('SELECT time_expired FROM bans')
rows = await cursor.fetchall()
stored_timestamp = idk
stored_date = DT.datetime.fromtimestamp(stored_timestamp)
if stored_date <= DT.datetime.now():
oof
await db.close()``` How can I make the database to go through all the data and see if the datetime passes the current time?
Nested for loop: for i in row: for j in i:
Nested?
Then you would set your if condition in the nested for loop fetchall returns a list of tuples you have to iterate through that to fine the stored_date then execute your if
Does that help?
Uhhh- can you explain a bit further? I've never really looped a database before so I'm not sure how I can go abouts doing it :P
That explains it pretty well, fetchall is returning a list with tuples [(column1),(column2),(column3)]. You have to iterate through that list to get to stored_data which is one of the tuples in the list. Hope this helps to clear it up
Could I ask what j is a variable for in my instance?
j would be the data in the tue that it's currently iterating through so in [(column1),(column2),(column3)] it would be column1,column2, or coulmn3 data
Hhmmm, my cursor is currently the row I want cursor = await db.execute('SELECT time_expired FROM bans') Should I use cursor?
Do you know what index your stored_date is at in fetchall?
If you went to that link I sent earlier scroll down to the fetchall it has an example of how to loop through it and grab the appropriate columns
@tasks.loop(seconds = 5)
async def bans():
await client.wait_until_ready()
guild = client.get_guild(764574130863079424)
db = await aiosqlite.connect('database.db')
cursor = await db.execute('SELECT time_expired FROM bans')
rows = await cursor.fetchall()
for row in rows:
print(row[0])
#stored_timestamp = cursor
#stored_date = DT.datetime.fromtimestamp(stored_timestamp)
# if stored_date <= DT.datetime.now():
await asyncio.sleep(1)
await db.close()
bans.start()``` I might be back for a little bit more help, but I think we're good so far ๐
Anyone?
find_one returns None if the documents isn't found
Around 2-3x faster pyarrow
Thanks
hi i need help
so i have this code in sql and i cant really get it to work
this is the code, it dont save the data in it its just blank, this is the code :
myconn = sql.connect('school_program.db')
myc = myconn.cursor()
myc.execute("INSERT INTO students VALUES (:username, :password, :iidentity, :grade)",
{
'username': username.get(),
'password': password.get(),
'iidentity': iidentity.get(),
'grade': grade.get()
})
myconn.commit()
myconn.close()
this is what it saves :
[('', '', '', '')]
mention me if there is any answers
oh wait
nvm
i found the problem
sorry
were those .get() methods returning empty strings?
no it was another code on top of this code that was doing that, i didnt think of it even :
username.delete(0, END)
password.delete(0, END)
iidentity.delete(0, END)
grade.delete(0, END)
In Django, can I create a model with another model?
So if I have a model like books, can I do something such that the author model is automatically populated with the author I gave in the books field?
Haven't done with Django myself, but I think the answer is yes. https://stackoverflow.com/questions/48262544/how-to-specify-parent-child-relationship-within-one-model
Thank you. This would really help.
value = f"""{cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[0]}"""```
writes an error, I can't understand why, help
Not sure if this is the only problem, but that initial { doesn't look like it should be there
Before cursor
Also I don't think .format works with f-strings like that
What does the error message say?
yes, you shouldn't mix f"" and .format. also, don't use f-strings for putting data into sql queries anyway
@torn sphinx โ๏ธ
And if the user is not found you'll get TypeError: 'NoneType' object is not subscriptable
so better do it like:
row = cursor.execute("SELECT cash FROM users WHERE id = ?", (ctx.author.id,)).fetchone()
value = 0 if row is None else row[0]
What is wrong with this SQL statement?
CREATE TABLE Horse(
ID SMALLINT UNSIGNED AUTO_INCREMENT,
RegisteredName VARCHAR(15) NOT NULL,
Breed VARCHAR(20) CHECK (Egyptian Arab, Holsteiner, Quarter Horse, Paint, Saddlebred),
Height decimal(3,1) CHECK (Height>=10.0) AND (Height<=20.0),
BirthDate DATE CHECK (DATE>='1-1-2015')
);
Its having issues with my
CHECK (Height>=10.0) AND (Height<=20.0)
statement am I doing it wrong or do I have to do it another way?
how do i insert a value into a biginit[] array, using asyncpg
Try sql CHECK (Height BETWEEN 10.0 AND 20.0)
@lunar reef ok will do
dou, fix it, is there a way to remove the ( and "?
how can I add a field for each invitation?
thx
how do i insert a value into a biginit[] array, using asyncpg
you get an error because that's not valid syntax ๐ https://sqlite.org/lang_delete.html
well the default row factory for sqlite is kinda garbage, you should switch to sqlite3.Row
then printing will be much easier
but if you have to do it this way you'll have to either do a list comp or append each element in each tuple to a list
ok, I'll try this, thx
How do I set the default value for State to TX?
CREATE TABLE Student (
ID SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(20) NOT NULL,
LastName VARCHAR(30) NOT NULL,
Street VARCHAR(50) NOT NULL,
City VARCHAR(20) NOT NULL,
State CHAR(2) NOT NULL,
Zip MEDIUMINT UNSIGNED NOT NULL,
Phone CHAR(10) NOT NULL,
Email CHAR(30) UNIQUE
);
so im using aiosqlite, and i get this error:
AttributeError: 'Result' object has no attribute 'execute'```
The code:
```py
an = aiosqlite.connect(db_path)
a = an.cursor()
a.execute("""CREATE TABLE IF NOT EXISTS afk (
user_id INTEGER,
message TEXT)""")```
@tasks.loop(seconds = 5)
async def bans():
await client.wait_until_ready()
guild = client.get_guild(764574130863079424)
db = await aiosqlite.connect('database.db')
cursor = await db.execute('SELECT time_expired FROM bans')
a = await cursor.fetchall()
for row in a:
await asyncio.sleep(1)
#print(row[0])
stored_timestamp = row[0]
if row[0] <= DT.datetime.now().timestamp():
muted = discord.utils.get(guild.guild.roles, name="Muted")
await db.close()``` This one is confusing my brain. So in the `for row in a` how can I make it determine what user_id it is currently on?
It needs to be an.execute rather than a.execute
anyone can help me with ER Diagram?
@ancient granite What's the issue?
heyo i need help with SQLAlchemy
i get this error when i use the Session class from sqlalchemy.orm module
with Session(engine) as session:
session: Session = session
data = session.query(model).filter_by(id=primary_key).scalar()
``` this is my code
its only coming in my linter tho my code runs fine
it must actually implement them, then, right?
sounds like their type hints are incomplete
maybe you just need to annotate with a more derived subtype of Session
i checked out the module source code and i can see an __enter__ and __exit__ function implemented
here
maybe there's another Session class imported?
can you hover over Session in your code to see?
it seems it's seeing the sqlalchemy session class
there arent any other Session definitions either
how odd
pylance wack lmao
seems like it... ignore, I guess ๐
yea ig
#type: ignore I mean
I've had similar experience with overzealous linters in VSCode
i've always been curious about the #type: flag in vscode, what other options does it have
that's a good question..
# type: ignore[attr-defined] - it can take an error ID looks like
i see
what errors the error code for the error im getting it doesnt show when i hover over
check the problems view?
omg here comes discord's slow ass image upload
How would I ignore the error
TypeError: 'NoneType' object is not subscriptable
mongodb ^^
@commands.Cog.listener()
async def on_message(self,message):
if message.author.bot:
return
for x in message.mentions:
print(f'{x.id} has been mentioned')
uid = await self.coll.find_one({"user_id": str(x.id)}) #getting the user ID if in db then getting reaction
reaction1 = uid["reaction"]
await message.add_reaction(reaction1)
This is my code
and I get that error if the user mentioned is not in the db
Change your code to check if the response is None, before trying to use it
yeah I managed to fix it^^ Thanks though
how do i insert a value into a biginit[] array, using asyncpg
anyone here??
No
what is mysql
It's one of the popular forms of SQL database. Postgres is a fairly similar alternative. If you've never worked with SQL databases at all, SQLite is yet another alternative which is a little more limited but great for small projects
Could someone tell me the difference between using bigquery.Client() vs. apache_beam.io.ReadFromBigQuery within the data pipeline, in terms of connectivity/performance/usage?
https://docs.microsoft.com/en-us/academic-services/graph/resources-faq#how-to-get-the-latest-dataset
I don't have any first-hand experience, but I would guess that the Beam interface is more limited. Performance is probably not going to be different
Beam is good because you don't have to think about the specific details of the backend
It's the same interface for everything
So you can reuse your knowledge of the library to read from other sources
But ultimately it's the same backend doing the same data processing
The python library you use is not going to be a bottleneck
Ah, I see. Were you able to find any helpful docs?
Has anyone gotten psycopg2 to work with 3.10? Can't seem to get it working. Tried some recommendations on stack overflow and some google searching but none have worked and most seem to be from a few years ago. Wondering if its a version problem
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: DLL load failed while importing _psycopg: The specified module could not be found.
You should really use psycopg3
how do i insert a value into a biginit[] array, using asyncpg
Litterally the same you
Would any other type
Although generally you dont need an array type as much as you tgink you do
Normally you have subtables for that with relational dbs
well its for a whitelist cmd and an array worked perfect with mongodb, not sure if it carries over with sql
await self.client.db.execute('INSERT INTO guild_data("guild_id", whitelist) VALUES ($1, $2)', ctx.guild.id, [user.id])
this code above makes a new row with a guild id and the user
Thank you, totally didn't realize there was a v3
Hi, I am trying to remove the SQL injection problem in my INSERT query.
I tried this, but it doesn't seem to work:
result2 = cursor.execute("INSERT INTO Loan_History (BookID) VALUES (?)", (book_id_entry))
Normally you would structure this table differently, to have 1 row per whitelist entry. Mongo teaches bad habits...
As for the original question, https://stackoverflow.com/a/29320442
The parameters should be a sequence, a list or tuple. Maybe you meant (book_id_entry,)? (book_id_entry) is just book_id_entry
Is it that there is no comma in the values list so its not a tuple and is just a value surrounded by parentheses.
result2 = cursor.execute("INSERT INTO Loan_History (BookID) VALUES (?)", (book_id_entry,))
def show_table(args):
query = "SELECT * FROM {} ".format(args)
my_cursor.execute(query)
res = my_cursor.fetchall() # storing the output of fetchall() in the res
for row in res: # printing all the records in res
print(row)
can anyone tell me the error in this
i am getting the below error
File "C:\Users\Yash\AppData\Local\Programs\Python\Python38\lib\site-packages\mysql\connector\connection_cext.py", line 518, in cmd_query
raise errors.get_mysql_exception(exc.errno, msg=exc.msg,
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(add_bike=None, show_table=['Rent_hours'], view_tables=None)' at line 1
Please help
@swift flax Your sql syntax is wrong.
Try to understand the query you want to write before trying to do it through code.
I cant work out what is wrong
Ik it is the wrong channel but i cant see any other cahnnel tht would be applicatble
i simply want to print all the records , can you tell the mistake
@uncut widget Card-Score should be card_score
either way thank you for the overflow
how i can delete a value from a table with a condition?
You can delete the whole record like DELETE ย FROM ย table_nameย WHEREย condition; but be careful and test your condition with SELECT first
There is no "delete a value" AFAIK, but you can modify a value to null if you want
You didn't say what db you are using etc but this is basic SQL
:incoming_envelope: :ok_hand: applied mute to @odd lintel until <t:1635032944:f> (9 minutes and 58 seconds) (reason: duplicates rule: sent 4 duplicated messages in 10s).
cursor = await db.execute('DELETE FROM warns WHERE time_expired=?', (row[0], ))``` How can I make it just delete just the `time_expired` data and not the whole row/column?
Isn't there update command + set
I think you want to UPDATE warns SET time_expired = NULL WHERE time_expired ='?' or something probably
i am facing a problem while storing a list in mongo db
like when i give the command
1st time - py ["this"]
2nd time - py ["this", "is"]
but in the third time mongo db return boths as same element and appends "me"- py ["this, is", "me"]
Hey, how do I open my .sql file in Postgres if anyone knows ?
since hadoop is still slowly dies, which local database should i use for data governance datawarehouse? in order to keep it fashionable
i think you shouldnt do it. use datagrip to connect to postgre. i make all interactions with this databases using jdbc connector with sqlalchemy and datagrip
result = await conn.fetch("SELECT material,quantity,price,tier FROM materiallistings ORDER BY price ASC"
How can I make it so I only get distinct materials
and always the lowest price
1 result for each different material
Do a groupby on material and min on price?
Is this what you mean? https://kb.objectrocket.com/postgresql/how-to-run-an-sql-file-in-postgres-846
You can also do it with various GUIs like the VS Code extension, DBeaver, PGadmin, and so on
A tutorial explaining how to run an SQL file in Postgres using the SQL extension.
Yesss
Thank you very much
does anyone know how i fix the syntax ive tried alot of stuff and i cant fix this stupid error 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 ') AND `type`='tool'' at line 1 php "SELECT * FROM `catalog_items` WHERE `id` IN (".implode(',',array_map('intval',$invItems)).") AND `type`='$type'";
What is all that (".implode ... )).") business? I don't know MariaDB too well but that doesn't look anything like valid SQL AFAIK
That's PHP for ",".join(map(intval, invItems))
How do you do loops, for instyance I want to code blackjack adn IA mdoing endless if statements is there a better way of doing this
I'm using SQLalchemy to work with MySQL, and getting this error sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1317, 'foreign key constraints are not allowed)
It's only happening when I add bidirectional relationship, simplified version of my code:
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
links = relationship("Link", back_populates="owner")
class Info(Base):
__tablename__ = "links"
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
owner_id = Column(Integer, ForeignKey('users.id')) # Error only when adding this line
owner = relationship("User", back_populates="links")
I'll try figuring out it myself till than, ping me if someone can help.
How do i update values in a sqlite3 db
i want to search for where username == X
and then update the time to the current time
Wrong channel, check #โ๏ฝhow-to-get-help
I believe INSERT INTO Date (LastModifiedTime) VALUES(CURRENT_TIMESTAMP) WHERE username='X'; should work, assuming LastModifiedTime is a column that exists in the current table.
thanks
Anyone have any good tutorials for a beginner in databases
It's a broad topic so it might help if you know more specifically what you want to learn. This guy has a couple of nice ones first to learn what SQL is (https://datagy.io/sql-beginners-tutorial/) and then to implement a basic database with it using Python/SQLite3 (https://towardsdatascience.com/python-sqlite-tutorial-the-ultimate-guide-fdcb8d7a4f30)
thanks
File "C:/Users/XXXXX/Desktop/testing db.py", line 26, in <module>
cursor.execute("""
sqlite3.OperationalError: near "WHERE": syntax error```
What is the exact code you're running?
import time
from datetime import datetime
with sqlite3.connect("some.db") as db:
cursor = db.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS some(town, username, time);""")
db.commit()
timeNow = datetime.now()
town = ['town1', 'town2', 'town3', 'town4', 'town5']
name = ['name1', 'name2', 'name3', 'name4', 'name5']
town3 = 'what'
for i in range(5):
insertData = """INSERT INTO some(town, username, time) VALUES(?,?,?)"""
cursor.execute(insertData, [town[i], name[i], timeNow])
db.commit()
cursor.execute("""
INSERT INTO some(town) VALUES(town3) WHERE (username = name3);""")
db.commit()
cursor.execute("SELECT * FROM some")
rows = cursor.fetchall()
for row in rows:
print(row)```
this is just a testing thing im doign so i can find out how to do it before i put it in my main thing
Can I have two on_message functions in a single program?
I'm not sure if you can execute a query direclty like that isntead of declaring a string first... if that's not the issue, open a help channel #โ๏ฝhow-to-get-help
how do i declare it a string first
Are you asking about #discord-bots ?
you did it with insertData
yes
Which one is a binding parameter
No idea what that means sorry
oh it just said that in an error message cursor.execute(insertData, [town, town3, name3])
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
I don't think that you can use WHERE in an INSERT INTO statement?
You probably meant UPDATE, but I'm missing a bit on terms of context
import time
from datetime import datetime
with sqlite3.connect("some.db") as db:
cursor = db.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS some(town, username, time);""")
db.commit()
timeNow = datetime.now()
town = ['town1', 'town2', 'town3', 'town4', 'town5']
name = ['name1', 'name2', 'name3', 'name4', 'name5']
town3 = 'what'
name3 = 'help'
for i in range(5):
insertData = """INSERT INTO some(town, username, time) VALUES(?,?,?)"""
cursor.execute(insertData, [town[i], name[i], timeNow])
db.commit()
cursor.execute("SELECT * FROM some")
rows = cursor.fetchall()
for row in rows:
print(row)
insertData = """INSERT INTO some(town) VALUES(town3) WHERE username = name3 VALUES(?,?,?)"""
cursor.execute(insertData, [town, town3, name3])
db.commit()
cursor.execute("SELECT * FROM some")
rows = cursor.fetchall()
for row in rows:
print(row)
context
good point
ive sorta messed around with the area but im not sure what im doing lol
your database probably contains a hella lot of repeated entries now? but that probably doesn't matters much
I think so
hmm... ok, so what exactly did you want to do?
Im just trying to find out how to update values in a db
sql update statement
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
query = """
UPDATE some
SET town = ?, time = ?
WHERE username = ?"""
cur.execute(query, [town, time, username])
# or (I think)
query = """
UPDATE some
SET town = :town, time = :time
WHERE username = :username"""
cur.execute(query, {"town": town, "time": time, "username": username})
let me test to make sure
!e ```py
import sqlite3
con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS foo (name TEXT, age INT)")
cur.execute("INSERT INTO foo VALUES (?, ?)", ("bar", 13))
cur.execute("UPDATE foo SET age = :age WHERE name = :username", {"username": "bar", "age": 18})
cur.execute("SELECT * FROM foo")
for row in cur:
print(row)
@storm mauve :white_check_mark: Your eval job has completed with return code 0.
('bar', 18)
I'm slightly surprised that it worked first try
so yeah, you can use either ? + tuple|list or :key + dictionary
ok thanks
I'm saving this as an example for sqlite3 though lol
it doesnt seem to work
lol
Does the query need to be in order of the order that they were created in @storm mauve
you are trying to update where username = 'help'?
im just testing atm
yes
I mean, there's no entry with username = 'help' in the database it seems?
There is a way to check how much time a query takes to execute using sqlite3?
:incoming_envelope: :ok_hand: applied mute to @torn sphinx until <t:1635120984:f> (9 minutes and 58 seconds) (reason: duplicates rule: sent 4 duplicated messages in 10s).
Try this: https://www.sqlite.org/eqp.html
I know in Postgres, this includes the time, hopefully SQLite does the same
But looks like maybe not
hello i coded my bot with a json data base because i thought it was going to be personal use but it's not anymore so we are going to need a db instead
i followed this tutorial
Welcome to the updated discord.py series - the series where I teach you how to build a discord.py bot for your server! Below are some links to get you started.
Series requirements can be found here:
https://files.carberra.xyz/requirements/discord-bot-2020
You'll also need an IDE; I use Sublime Text in this series:
https://www.sublimetext.com/
...
a but disappointed it only sets it up
and doesn't use it yet
anyway
how do i even use it
this is my build.sql file
CREATE TABLE IF NOT EXISTS exp (
UserID integer PRIMARY KEY,
XP integer DEFAULT 0,
Level integer DEFAULT 0,
XPLock integer DEFAULT CURRENT_TIMESTAMP
):```
but in my json db things were like this
{
"server id": {
"prefix": "_"
"user_id": {
"messages": 0
"level": 0
"exp": 0
"last_message": 0
"url": "https://example.com"
}
}
}```
i dont even know where to startr
nvm im using asql
still would like some help on how to remake my db
How do you add to an array in mongodb
?
Mongodb is a database platform hoe do you add to an array in the database
in sqlite, is there a way to test a column value exists in another table, when that column value is NULL? that is, is x IN y possible where x is NULL, and NULL exists in y?
it seems the only way to do this is to only include non-NULL values in y, and then have a separate OR x IS NULL clause after, but this seems clunky since I think it means I'll have to dynamically change the query depending if there is or is not a NULL value in y.
ah
came up with this, does this seem proper? sql x_col IN y OR ( EXISTS (SELECT * FROM y WHERE y_col IS NULL) AND x_col IS NULL )
not that I know of
you mean like an outer join?
hmm, I guess it would be intersection of nulls?
select * from x
full outer join y on y.id = x.id
where x.id is NULL and y.id is NULL
something like that?
no
oh that's not valid anyways
my specific situation is that I have a table with a parent_id column, which might be null
I want to "filter" a table, so that only rows that match one of my desired parent_ids are selected
ok
which include NULL, items without a parent
I came up with this here
so you just want a left join it sounfs like
select * from parent
left join child on child.parent_id = parent.id
where parent.id in (5,6,7) or parent.id is NULL
?
there, I think I got that last constraint in there
well, the where condition is the main thing
the issue is that (5,6,7) are items in a table
the values are passed in to the query, yeah
right now I populate a temp table
and then just do where item.parent_id in temp.filter_parent
except that doesn't work will nulls so I added that whole extra clause
the values are passed in by a temp table?
right now, yeah
what about
where (item.parent_id in (select temp.id from temp.filter_parent) or item.parent_id is NULL)
nope, because the user may or may not want to include items without a parent
that query always includes root items
here's the select I have right (the relevant part anyway) ```sql
SELECT id, parent_id
FROM timeline, timeline_tag
WHERE id = timeline_id
AND (
NOT EXISTS (SELECT * FROM temp.filter_parent)
OR (
timeline.parent_id IN temp.filter_parent
OR (
EXISTS (SELECT * FROM temp.filter_parent WHERE parent_id IS NULL)
AND timeline.parent_id IS NULL
)
)
)
yep
and yeah it's not pretty but it seems to work fine
I was mostly now wondering if there are any problems with it I'm not seeing
something like that, you could change some of it tp joins but it's going to have the same structure basically
yea, I can't think of anythimg right off.. unless you do COALESCE(col, -1) as like a replacement for NULL
ok, I'll use it for the time being then. if I have issues I'll probably return here later lol
thanks for the input
how would I find a certain value in mongo db? cause I tried but TypeError: 'Collection' object is not iterable raised
show your query
bal = economy.find({"_id":12345})
for a in bal:
print(a)
that?
can you show where you define economy as well?
db = cluster["discord"]
economy = db["economy"]
bal = economy.find({"_id":12345})
for a in bal:
print(a)
hi
๐ค can you show the full traceback as well? that query looks fine
A database is an organized collection of structured information, or data, typically stored electronically in a computer system.
you can google "what is a database" to get more info
Ignoring exception in on_command_error
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/disnake/ext/commands/core.py", line 171, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 33, in balance
if ctx.author.id not in economy["_id"]:
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pymongo/collection.py", line 3478, in __next__
raise TypeError("'Collection' object is not iterable")
TypeError: 'Collection' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/disnake/client.py", line 483, in _run_event
await coro(*args, **kwargs)
File "/home/runner/bonbons/cogs/events.py", line 63, in on_command_error
raise error
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/disnake/ext/commands/bot_base.py", line 547, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/disnake/ext/commands/core.py", line 901, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/disnake/ext/commands/core.py", line 180, in wrapped
raise CommandInvokeError(exc) from exc
disnake.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'Collection' object is not iterable
hm
the error is happening at if ctx.author.id not in economy["_id"]:
File "main.py", line 33, in balance
do you see the issue?
hm
economy is defined as economy = db["economy"]
so it is a collection
you need to run a query first (probably to retrieve all user IDs), and then do if ctx.author.id in that result
hmm
can someone give me some use cases of materialized views?
nesting or isn't necessary, a or (b or c) is the same as a or b or c. you can simplify the query for reading purposes by flattening out one layer of nesting and indentation.
SELECT id, parent_id
FROM timeline_tag, timeline_tag
WHERE
timeline.id = timeline_tag.timeline_id
AND (
NOT EXISTS (SELECT * FROM temp.filter_parent)
OR timeline.parent_id IN temp.filter_parent
OR (
EXISTS (SELECT * FROM temp.filter_parent WHERE parent_id IS NULL)
AND timeline.parent_id IS NULL
)
)
i don't quite understand what the NOT EXISTS (SELECT * FROM temp.filter_parent) is supposed to achieve. isn't this going to be "all or nothing" over the entire query, because it's not correlated at all? that is, the inner query doesn't refer to the outer query, so it's either 1 for every row (if there is data in that table) or 0 for every row (if there is no data in that table).
i also tend to prefer JOIN syntax instead of putting the join condition in WHERE, because it removes yet another layer of grouping in WHERE
SELECT id, parent_id
FROM timeline_tag
INNER JOIN timeline_tag ON timeline.id = timeline_tag.timeline_id
WHERE
NOT EXISTS (SELECT * FROM temp.filter_parent)
OR timeline.parent_id IN temp.filter_parent
OR (
EXISTS (SELECT * FROM temp.filter_parent WHERE parent_id IS NULL)
AND timeline.parent_id IS NULL
)
thanks it works now :))
Code which sets up the database
import sqlite3
import requests
import time
from datetime import datetime
timeNow = datetime.now()
print(datetime.now())
with sqlite3.connect("Towns.db") as db:
cursor = db.cursor()
#create the tables
cursor.execute("""
CREATE TABLE IF NOT EXISTS Towns(town, username, time);""")
db.commit()
#inputting all town data
response = requests.get("http://earthmc-api.herokuapp.com/towns/")
response.raise_for_status()
data = response.json()
towns = list(data)
name = [m["name"] for m in towns]
town = [m["mayor"] for m in towns]
ListLength = len(name)
#inputting data into database
for i in range(ListLength):
insertData = """INSERT INTO Towns(town, username, time) VALUES(?,?,?)"""
cursor.execute(insertData, [town[i], name[i], timeNow])
db.commit()
print("Task Completed")
Code which is meant to search for the username and output the time + town that correspond with the username
import sqlite3
with sqlite3.connect("Towns.db") as db:
cursor = db.cursor()
print("Are you looking for a player or towns last logon time?")
Choice = str(input(""))
if Choice == "player":
print("Your choice is player")
print("What is the players name?")
PlayerInput = str(input(""))
with sqlite3.connect("Towns.db") as db:
cursor = db.cursor()
find_user = ("SELECT * FROM Towns WHERE username = ?", (PlayerInput,))
#cursor.execute(find_user,[(PlayerInput)])
Results = cursor.fetchall()
print(Results)```
Output:
Are you looking for a player or towns last logon time?
player
Your choice is player
What is the players name?
sab2003
**[] **
sab2003 is in the database because if u run this code:
with sqlite3.connect("Towns.db") as db:
cursor = db.cursor()
#Show Database
cursor.execute("SELECT * FROM Towns")
rows = cursor.fetchall()
for row in rows:
print(row)```
It prints all the data in the DB
and on line 289 this exists:
```('sab2003', 'Moria', '2021-10-25 14:35:06.572479')```
(Player name, Town, Time)
Is anyone able to help me with why it doesnt output the data in the most recent black box thing i sent? ^
@serene shoal
-
you don't need to write
= str(input("")),input()already returns a string, so you can just write= input(""). however, you might want to check that the user input contains no extra leading or trailing whitespace,= input("").strip() -
withdoes not automatically close a sqlite connection, it starts a transaction. this is a common misunderstanding but it is documented. you might wantwith closing(sqlite3.connect("Towns.db")), https://docs.python.org/3/library/contextlib.html#contextlib.closing -
even if you were closing the connection with
with, the connection would then be closed when you tried to run the query! you would have to run the query and fetch the data inside thewithblock -
cursor.executeis commented out, and thisfind_user =doesn't actually do anything. also, in sqlite3, you can just callexecutedirectly on the connection, you don't need to explicitly create a cursor -
at the top of your code, you create a cursor and don't use it for anything. don't do this. in general, don't use a cursor for more than one query. cursors should be considered "single use"
-
this doesn't affect the code as such, but in python we normally use
lowercasevariable names.TitleCaseis for classes.
you might want to re-write your code this way, which should eliminate these various possible sources of error:
import sqlite3
from contextlib import closing
print("Are you looking for the last logon time of a 'player' or 'town'?")
choice = input("").strip()
if choice == "player":
print("Your choice is 'player'.")
print("What is the player's name?")
username = input("").strip()
with closing(sqlite3.connect("Towns.db")) as db:
cursor = db.execute("SELECT * FROM Towns WHERE username = ?", (username,))
results = cursor.fetchall()
print(results)
Thank you
What is contextlib?
i linked to it in my message. it's part of the python standard library
Ah
have any of you have worked with an API that connects to an Arangodb database hosted in a external server. Any tips on achieving this?
I have two tables , (one parent table, one child table)
How can i insert a row into both tables at once ?
I'm using postgresql with asyncpg
alright
so this is only pseudo-relative to this topic
but Im working on a database client and im using the __setitem__ method to set keys to values
but If I put a dictionary inside of a value, it wont allow me to set a value inside of that dictionary like it would with typical python behavior
can anyone help me?
You want to insert the same row in both tables? This sounds like a terrible idea which defeats the whole purpose of SQL, but if you really must you could use a trigger I guess
Why not just use a foreign key to link back to the parent or vice versa?
not same row
Different data but for one primary key which is used as foeign key for the other table
anyone know how to use asqlite
Ah, that makes more sense. I'm not familiar with asyncpg but can't you just do the two updates in sequence?
Don't ask to ask, just ask
async def main():
async with asqlite.connect('./data/db/users.db') as conn:
async with conn.cursor() as cursor:
# Create table
await cursor.execute('''CREATE TABLE users
(guild_id text, user_id text, last integer DEFAULT 0, experience integer DEFAULT 0, level integer DEFAULT 0, messages integer DEFAULT 0, url text DEFAULT "https://cdn.glitch.com/dff50ce1-3805-4fdb-a7a5-8cabd5e53756%2Fblankrank.png?v=1628032652421")''')
# make new user
async def new_user(guild_id, user_id):
await cursor.execute("INSERT INTO users VALUES ('%s', '%s'", (guild_id, user_id))
# Save (commit) the changes
async def save():
await conn.commit()
asyncio.run(main())``` ok so this is my code
should i put this in my bot.py
i can do that but i wanted to know if there's a way to do it at once
or a db.py
and import it into bot.py
also can i even use functions inside of main()?
If nobody here knows try #discord-bots
note: you are missing a ) at the end of your insert query, and you are not supposed to put quotes around the %s placeholders
await cursor.execute("INSERT INTO users VALUES (%s, %s)", (guild_id, user_id)) like this?
๐
How do I create a key/database in redis.
:incoming_envelope: :ok_hand: applied mute to @torn sphinx until <t:1635190828:f> (9 minutes and 59 seconds) (reason: duplicates rule: sent 4 duplicated messages in 10s).
You're right about the ors and using JOIN instead. I needed a LEFT JOIN for my query to work properly.
The purpose of NOT EXISTS (SELECT * FROM temp.filter_parent) is to ignore that where clause if the temp.filter_parent table is empty. That is, if the table is empty, do not filter rows by parent_id. But, if it isn't empty, only allow rows that have a matching parent_id. I do this to avoid having to dynamically build a query depending on which filtering I'm doing, my full query has two other filtering clauses with similar short-circuiting.
this is the full query I have now, if you're curious. it includes some tag filtering as well. https://paste.pythondiscord.com/ecupuvoyil.sql
you might want to put a comment about this NOT EXISTS business in the query. it's a good trick, but not obvious unless you already know the idiom
why do i get this error when trying to use the db in an event or command
raise ValueError("no active connection")
ValueError: no active connection
async def new_guild(guild_id):
async with aiosqlite.connect("data.db") as db:
await db.execute("INSERT INTO settings VALUES ("+str(100000001)+", '_')")
await db.commit()
await db.close()```
see if i called this on it's own it would be fine
but this
@client.event
async def on_guild_join(guild):
await new_guild(guild.id)``` gives the error
same thing if i just pasted the code into the event
don't do db.close(), the database will be closed when you exit the with block
basically you code is closing the database connection twice: once with await db.close(), and then again when you exit the with block
thx it worked
how do i fetch only 1 column
for example
i want the coloumn that has guild id xxxxxxx
you mean only 1 row?
you can have a WHERE clause ```sql
SELECT * FROM my_table WHERE guild_id = 1234567
also, I'd recommend using parameterized queries ```py
await db.execute("SELECT * FROM my_table WHERE guild_id = ?", (guild_id,))
so does that return the value or do i gotta do something else
ah
yeah, you get a cursor back
cursor = await db.execute("SELECT col1, col2, col3 FROM my_table WHERE guild_id = ?", (guild_id,))
row = await cursor.fetchone()
if row:
col1, col2, col3 = row
# do things with col1, col2, col3 here
else:
# you didn't find any rows with the right guild_id
cool
do i jus ask a quetion abt DB in here?
so I want to find the region with the highest GDP, but I dont know what to put in the WHERE, and I cant use MAX
Hello, I am starting with SQL and I have a question. If I already have Microsoft SQL Server installed, do I need to install MySQL Server again? Or for learning purposes, just installing MySQL workbench is enough? thank you (sorry for my bad english)
You can just use MySQL Workbench as a client and connect to Microsoft SQL Server, that's fine. You don't need to have MySQL running as a server if you don't want to
Thank you
what happens if you try and input data that already exists
ehh ill just check it first
like this but can i use 2 filters?
You can add identical rows to a database if there are no uniqueness constraints. The key will be different
grab if user_id and guild_id are both in there
Yes, I think you just need AND
ok
can i add a number to the value of a column?
like if i have it written down as level 3
and they are now level 4
instead of having to find the value i could just do UPDATE users SET exp = +1 for example
yes, you can do UPDATE users SET exp = exp + 1 to increment EXP by 1 in every row
How do I create a key to Redis?
How to upgrade pymongo code to motar
Hi my graph is not plotting the x pointspoints correctly, my code:
import pandas as pd#import pandas package to read data more easily
import matplotlib.pyplot as plt#imported pyplot to plot graphs
import datetime as dt#date time to read first column of csv file
import numpy as np
from dateutil.parser import parse
d_parser=lambda x:pd.datetime.strptime(x,'%d/%m/%y%H')#
df = pd.read_csv('LAC.csv', parse_dates=['Date'], date_parser=d_parser)
df2 = pd.read_csv('LIT.csv', parse_dates=['Date'], date_parser=d_parser)
#startdate='20/10/2013
#end = dt.datetime.now() #the end date is the present date
y1=df['Close']#refering to the close column in the csv file
y2=df2['Close']
x1=df['Date']
x2=df2['Date']
plt.xlabel('date')
plt.ylabel('price')
fig,ax1=plt.subplots()
ax2=ax1.twinx()
curve1=ax1.plot(x1,y1,label='close1', color='r')
curve2=ax2.plot(x2,y2,label='close2', color='b')
plt.plot()
plt.show()
You sure the date format is '%d/%m/%y%H'? That would mean 01/01/2021 is Jan 1st, 2020 at 21:00
ohh i see i fixed it thankyou
Hi I'm also not sure about this loop as its not plotting anything although its from the csv database:
import pandas as pd#import pandas package to read data more easily
import matplotlib.pyplot as plt#imported pyplot to plot graphs
import datetime as dt#date time to read first column of csv file
import numpy as np
df = pd.read_csv('LAC.csv')
df2 = pd.read_csv('LIT.csv')
df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%Y')
df2['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%Y')
#startdate1='20/10/2013'
#pd.date_range(start="",end="2020-02-02").to_pydatetime().tolist()
#start = df.loc['2011-10-06']
#start='06/10/2011'#picked the same start date for both of the graphs in order to plot them on the same graph
end = dt.datetime.now() #the end date is the present date
y1=np.array(df['Close'])#refering to the close column in the csv file
y2=np.array(df2['Close'])
x1=np.array(df['Date'])
x2=np.array(df2['Date'])
t0=[]
for i in range(len(x1)):
for j in range(len(x2)):
t=x1[j]-x2[i]
while j>i:
x2[j]+=1
if j==len(x2):
x1[i]+=1
t0.append(t)
dcf=[]
def DCF(x1,x2,t0):
d=((x1-np.mean(x1))*(t0-np.mean(x2)))/(np.std(x1)*np.std(x2))
dcf.append(d)
return d
plt.plot(t0,dcf , ls='-', lw='1', color='red', marker='.')
plt.title('DCF vs Lag')
plt.xlabel('time lag')
plt.ylabel('DCF')
plt.show()
You may want to put this into the help-channel you have. I'm not that experienced with all that plot and array things.
Don't forget to put your code between the ``` (backticks).
i see np will do thanks
I don't see you call the DCF() function but using the dcf list, maybe it's still empty?
@livid crypt you can ask in #data-science-and-ml too
Whats the easiest local python database
SQLite if you need a full blown db. Pickle, JSON, etc. If you don't
Well, not that SQLlite is a full blown db really, but it's close
If you really, really need a db then Maria or Postgres
I am trying to get this song meta data
now from audio media it connects to song meta data
hello anyone
What's the question?
I am trying to get all song metadata
but I am not able to figure erd diagram
@fading patrol
Select * from song; ?
Too blurry to read but there's a lot more than just 2-3 entities there
Yeah
So what relationship are you having trouble with?
!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.pythondiscord.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.
^^^ do that in a help channel and hopefully someone else can help you
how can i sort by experience out of everyone with x guild_id and then find their ranking so the highest exp is 0 then 1 then 2
can this be done without getting everyone from the same guild then looping through counting?
i just want to grab x user in x guild's exp ranking
What application is that image from? Are you asking how to build the query in SQL?
do you want their ranking within the guild, or across all guilds?
An online SQL database playground for testing, debugging and sharing SQL snippets.
within
I think that's only supposed to appear when you hover, so if it's stuck, restart. You can probably disable it all together but ask in #editors-ides if you need help finding that
close but not what i mean
so for example
it sorts to one guild
then sorts by exp
i would then say ask for 867655715585130507's rank
it would return 2
or do i need to loop through everyone in the guild and count till i find 867655715585130507
You can select from an select you know:
select * from (
select *, dense_rank() over (order by experience) as user_rank
from users
where guild_id = 554362814794563586
)
where user_id = 867655715585130507
;
ok but does that tell me what position it is on the list
Try and see
you don't need a nested query for this
An online SQL database playground for testing, debugging and sharing SQL snippets.
@calm cedar @grim vault
Ok, I don't have much experience with window functions.
returns #1 everytime ๐ฌ
i might be wrong ๐คทโโ๏ธ
actually yes you're probably right @grim vault , it seems to be applying the where before computing the window function stuff
An online SQL database playground for testing, debugging and sharing SQL snippets.
you probably have enough to take it from here
the sqlite docs explain everything, but admittedly aren't easy to search
yeah i know what to do now
Hello! I am using sqlalchemy and AsyncEngine to make queries and I am trying to move code from sync to async. The old code has
# query is of type: <class 'sqlalchemy.sql.selectable.Select'>
query = session.query(SomeClass)
# engine is of type <class 'qlalchemy.engine.base.Engine'>
df_graph = pd.read_sql(query.statement, engine)
I usually do something like this when working in async
engine: AsyncEngine
async with engine.connect() as conn:
await conn.execute(select(SomeClass)) # Sometimes I use conn.stream but yes
I looked into the docs for a bit but couldnt find something for getting the actual sql query, any help?
Tag me if you reply :)
I am looking more into the code and am not sure if im looking at the wrong file cause it looks like the async session has a lot of stuff missing including this...
silly Q i'm sure but I'm pulling a list from a DB and for some reason the first item always is {}. For example I get:
{"Sam's Hardware"}, 'True Dirt', "Mike's Basement"
Even when I change the order I grab them the first one still is {} ie {"Mike's Basement"} I use json. Just wondering if someone knew what's up.
It might be happening when I'm appending them to a list.... still not sure why it's doing it though.
may need to check the output from the query before you append and determine where it's happening, what's your append to the list look like?
for team in bookD['teams']: if bookD['teams'].get(team) == True: teams.append(team) print('teams: ',teams)
output:
teams: ["Sam's Hardware", 'True Dirt', "Mike's Basement"]
But when I print teams:
{Sam's Hardware} True Dirt {Mike's Basement}
sorry that code paste does, not look good.
I can str the results but then I'll get "" within [] where really I'd prefer it just prints the 3 names.
what does bookD['teams'].keys() get you?
keys : dict_keys(["Sam's Hardware", 'True Dirt', "Kevin's Pumps", "Mike's Basement"])
Kevin's Pumps is false so it shouldn't grab when I want only true's
got it, so you have a dictionary of teams with a boolean
yes
incase the file looks like this
"teams": { "Sam's Hardware": true, "True Dirt": true, "Kevin's Pumps": false, "Mike's Basement": true }
and you're getting brackets around the first team name in your list when you print?
is teams set to an empty list before you start that loop teams = []
quick test by adding more teams, every other
I'm confused...you say you when you print teams, but you're output there looks good...what do you mean when you print teams
are you reading in from JSON or querying an actual DB? Are you querying with raw SQL, an ORM?
reading from JSON and putting the true's into a local list. Then putting that list into a list box.
on screen visually.
so it's the visual piece that's showing it
Because it seems like at the end of that loop, your local teams list looks good, right?
it shows the same as the print via console yes.
on the console teams pint out well:
teams: ["Sam's Hardware", 'Bruces Bumbells', 'True Dirt', "Mike's Basement"]
(adding and removing teams while testing)
what's the code that displays this on screen?
I mean the code that generates that? Is this in a templating language, javascript, python to the terminal?
player_teams_info['text'] = teams
I use tkinter in python. I haven't added space formatting or anything just wanted the output to pop up first. Might be something I come back to in the morning. This was one of those.... I'll just add this into it before I make dinner type of things.
Okay. Short answer, I don't think it's the JSON. After your loop, your list looks good so it's somewhere between that and displaying it
good to know.
I'll try adding some formatting steps between them in the morning.
BTW freaken love python, without schooling, prob my fav language to pick up and learn.
either a debugger or loads of print statements will help you here. if it's a list all the way down and then displays wrong, it's something with tkinter
๐ it's a great language for sure
#data-science-and-ml message does this help?
Can someone help me how do I delete all rows in my sqlite database without deleting my database?
TRUNCATE that biz https://www.techonthenet.com/sqlite/truncate.php
This SQLite tutorial explains how to use TRUNCATE TABLE in SQLite with syntax and examples. The TRUNCATE TABLE statement is used to remove all records from a table.
thanks for help
I separated the items out into the label and it cleaned up the brackets.
("\n".join(teams))
Thanks for the help. ๐
Hello,
I am using the SQLITE3 library in python. I found a problem that I could never quite solve, I hope you guys can help me :)
So, every time I start/restart the Python program, the database completely clears itself. Does anyone have a similar experience? Maybe even a solution?
If I made something unclear in my problem, you guys are free to ask me.
Thank you,
Luna.
Hi so I have gthis cmd
cur.execute(f'''INSERT INTO banlist (user_id) VALUES ({user}) (reason) VALUES ({reason}) (moderator) VALUES ({ctx.message.author})''')
``` or more line but it **always** gives me a syntax error
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 107, in ban
cur.execute(f'''INSERT INTO banlist (user_id) VALUES ({user}) (reason) VALUES ({reason}) (moderator) VALUES ({ctx.message.author})''')
sqlite3.OperationalError: near "(": syntax error
!paste post your code using our paste site. read below for instructions ๐
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.pythondiscord.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.
don't use f-strings for sql
ok but how do i solfe error
read that, let me know if you have any questions
conn = sqlite3.connect("server.db", check_same_thread=False)
curs = conn.cursor()
try: curs.execute("""CREATE TABLE IF NOT EXISTS users (username text, password text)""")
except sqlite3.OperationalError: pass
@harsh pulsar this is the code that I use to connect to the DB and create a table. The problem is that I do not know how to only create a table if it doesn't exist. Because of that, my table always gets cleared.
The solution I tried above does not work :/.
hmmm that's weird
let me check how i do mine
await db.execute('''CREATE TABLE IF NOT EXISTS users(guild_id integer, user_id integer, experience integer DEFAULT 0, level integer DEFAULT 0, last integer, messages integer DEFAULT 0, url text DEFAULT "https://cdn.glitch.com/dff50ce1-3805-4fdb-a7a5-8cabd5e53756%2Fblankrank.pngv=1628032652421")''')```
btw ik this is dumb but ive only every selected 1 row at a time now im trying to make a lb and i already know how to do that and stuff but what do i do with the top 10 after i do cursor.fetchall()
if i recall, you might need to use an explicit transaction using with db:, or use db.commit() after creating a table, otherwise it will only live in memory and never be written to disk. you can configure this by changing the isolation_level parameter when connecting to the database. see https://docs.python.org/3/library/sqlite3.html#sqlite3-controlling-transactions
like how can i make them into variables
if i do col1, col2, col3... will each row be assigned
fetchall() returns a list of tuples. each tuple represents a row. what do you want to do with those rows?
then i'd do col1[2] to get the exp of #1
well since im actually converting this bot from json to db
the lb embed is already made
so i guess just replace the variables
there's no general answer here
embed.add_field(name=f"4. {p4[0]}", value=f"Total Experience: {p4[1]}\nLevel: {p4[2]}\nTotal Messages: {p4[3]}", inline=False)
a list of tuples is a good sensible "default" for storing rows returned from a database
what is p4?
place4
so for example i could just
assign p1-10 to the tuple
and it's already setup
but yesterday i tried using a tuple as is and not break every part down to it's own variable and it gave me an error
I know nothing about db but i want to learn how do i even begin and i want to use mongodb
my advice: don't start with mongodb. start with sqlite
why do you want to assign each column to a variable?
if you want to do something row 4 of the results, what's wrong with rows = curs.fetchall() ; do_something(rows[3])?
well i tried that with something else and it gave me an error
Ok so how do i even start i know 0 about dbs
https://sqlbolt.com and check the pinned messages
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
well i will be doing it
this video helped me a lot
Today, weโre going to cover how to create and edit tables within a database using SQLite in Python. In this tutorial, weโll create a database to manage data about a retail business with a few different locations. Weโll be storing information about each of our retail stores that are located across three different cities. Weโll also create a way t...
try it. if you get an error, show your code and the full error output
and it's short
Thx
Thx
@harsh pulsar so for examople
this is the output because my test server only has 2 users
how would i get the exp for the 2nd user
can't do
row[1][1]
otherwise it will say
TypeError: list indices must be integer s or slices, not tuple
Thanks
Thanks too
I am going to try it later on, thanks for the help :)
Have a great night guys!
oh i didn't even see you didn't commit
yeah you have to commit or else it wont be saved
Ok.
!e ```python
rows = [(867655715585130507, 1102, 4), (428733263251505152, 177, 1)]
print(rows[1][1])
@harsh pulsar :white_check_mark: Your eval job has completed with return code 0.
177
@earnest atlas @calm cedar you can open and commit a transaction nicely using with
db = sqlite3.connect(...)
with db:
db.execute('create table ...')
ok i know that it closes it
but it also commits?
i could have sworn there are times it didn't save till i saod commit
bruh so why doesn't that work for me
it does not close the db in sqlite3. it might in other libraries. this is a common misconception (including by me at one point) and it's imo kind of confusing
if you want to close the db use contextlib.closing
import sqlite3
from contextlib import closing
with closing(sqlite3.connect('...')) as db:
with db:
...
so to answer your question, it only commits, it does not close
because something is different in your code compared to mine ๐ i noticed you wrote row and not rows - perhaps row is only a single tuple, not a list of tuples?
I got it now, thanks guys!
Hi everyone, excuse me for this ask but iยดm really struggled in this issue
work on a Glue Job ETL with pyspark
I need to loop a column that has xml texts, very veryyy long texts, to struct them un another dataframe
i used collect to encapsule the rows and iterate, but this action took more than 4 min to execute just for one register
other option was to convert to pandas and iterate but, the dataset has 500k registers and it causes problems. Can someone has any advice for this problem?
obs. the library to struct xml texts is xmltree
has anyone created their own server? im looking to build my own i am a music producer and would like to access my project files across devices while on the go. And would like to gain experience in this new field , thanks!
you might not want to create your own file server from scratch. have you looked into syncthing or seafile?
interesting, why is that? and I havent! Thanks for the suggestion. I havent heard of those either hahah soo womp
it might just be more work. you can set up a webdav server with nginx pretty easily (no database required).
but in general you wouldn't want or need a database for a file server
interesting so to access my files across devices i can create a file server for my specific needs?
so out of of seafile, nginx, and syncthing which would you recommend? I liked the sound of nginx after I gave all three a google
nginx is the most minimal - it's a general purpose http server that happens to support webdav
in all 3 cases, you will need a domain name and an ssl cert for that domain name (e.g. via letsencrypt), which will require some setup
i haven't tried self-hosting seafile or syncthing, both are reasonably popular and probably have easy-to-deploy docker container versions
however this is no longer about databases
i hear ya, okay thanks ill try the web dev thread then?
sure
no i just forgot the s on discord. the issue was i did for i in rows not i in range of rows
oof
!e ```python
rows = [(867655715585130507, 1102, 4), (428733263251505152, 177, 1)]
for row in rows:
print(row[1])
@harsh pulsar :white_check_mark: Your eval job has completed with return code 0.
001 | 1102
002 | 177
you usually don't need to loop over range(len(thing)))
oh
!e ```python
rows = [(867655715585130507, 1102, 4), (428733263251505152, 177, 1)]
for i, row in enumerate(rows):
print(f'Row number {i}, column 0: {row[0]}, column 1: {row[1]}')
@harsh pulsar :white_check_mark: Your eval job has completed with return code 0.
001 | Row number 0, column 0: 867655715585130507, column 1: 1102
002 | Row number 1, column 0: 428733263251505152, column 1: 177
!e last one: ```python
rows = [(867655715585130507, 1102, 4), (428733263251505152, 177, 1)]
for row_number, (user_id, x, y) in enumerate(rows):
print(row_number, user_id, x, y)
@harsh pulsar :white_check_mark: Your eval job has completed with return code 0.
001 | 0 867655715585130507 1102 4
002 | 1 428733263251505152 177 1
@torn sphinx, this is just a private thing to to sync your own devices? If so the domain and certificate may not be necessary. You could use no-ip if you need remote access, and if this is just for use on a home network, you don't even need that. I haven't used seafile or syncthing my self but if all you want to do is access files across devices, that's exactly what they are meant for
Totally off topic here though, both for this server and this channel!
true, you could do something as simple as set up a raspberry pi at home w/ a ssd connected + dynamic dns
await db.execute(f"UPDATE roles SET role_id = {role.id} WHERE guild_id = {ctx.guild.id}")```
d: roles.role_id
im not trying to insert a new row
im updating
i dont have this with other updates
wait
also i use them because usign ? randomly stops working for me
i thought i sent that to you this morning or yesterday
or maybe it was someone else... my apologies if so
i copied and pasted the link here. either it wasn't you or you just missed it, well read it now!
some of my commands work
"unique constraint failed" means that roles.role_id has a UNIQUE constraint and you tried to insert a duplicate value for that column
but sometimes it's like "wong object type" or something more technical
when tho it's an integer
i find that hard to believe, check to make sure you didn't accidentally pass something else
and ive even used int(guild_id) JUST TO MAKE SURE
if you can reproduce the error, we'd be happy to help here
but once i use f strings the very same variable WORKS
i guarantee it's not a bug in sqlite3's handling of ?
@harsh pulsar ok so that one worked but not this one
ed type
cursor = await db.execute(f"SELECT user_id, experience, level, messages FROM users WHERE guild_id = ? ORDER BY experience DESC LIMIT 10", (ctx.guild.id))```
- remove the
fjust to be safe (ctx.guild.id)is justctx.guild.id- a length-1 tuple is spelled(ctx.guild.id,)
pretty annoying but that's just the way python decided to do things
you can use a list if you really hate the syntax, [ctx.guild.id]
yes
bruh
() is used for grouping, in a way the , is really what makes it a tuple
thanks it worked now
same reason you can write return x, y instead of return (x, y)
and for my issue of discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IntegrityError: UNIQUE constraint faile d: roles.role_id idk why it was saying that but i found you can do INSERT OR REPLACE so i was able to merge all my code into 1 execute
wait 1 more thing if i have 2 variables do i just add a comma to the last one
you only need the trailing comma if there is only one element
if there is more than one, you don't need the trailing comma
ok
(x, y, z) and (x, y, z,) are both valid
but (x) is a special case, it's not a tuple, it's just grouping, as in (a + b + c)
once in a while you might leave a , at the end of a line and then be very annoyed when you get strange errors later in your code
welcome to python
hey guys, do you recommend any sqlite3 UI?
unresolved attribute "Column" in class "SQLAlchemy"
can someone tell me what i'm doing wrong? i followed the same thing in a tutorial and this happens
Code+traceback would be quite nice
alright theres no traceback, its just code
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URL'] = 'sqlite:///database.db'
app.config['SECRET_KEY'] = 'QWERTYUIOPASDFGHJKLZXCVBNM'
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), nullable=False)
password = db.Column(db.String(80), nullable=False)
@app.route('/')
@app.route('/homepage')
def home_page():
return render_template('home.html')
pycharm highlighted .Column and .Integer as unresolved attribute "Column" in class "SQLAlchemy"
when i look inside my db. i added stuff inside it does not show anything @velvet ridge
There is nothing called Column in the module I guess
Maybe try column
didnt work, should i try this instead?
add method
i never encountered these errors before
Did you try lower case?
It's still upper case?
i tried with column still has that error thingy
typed the wrong thing there but same error
if i press add method, what does it add? how does it automatically know what method to add
Ermm dunno, I never used that
It's just the IDE, the code should work.
alright hold up, im gonna add something to it
But you need to do the db = ... after you set the config.
Is anyone experienced with bulk inserts on peewee?
yep i added something and nothing shows up to my db
you use peewee?
uh no srry
oh okok
take a look at C:\database.db
what do you mean? like with db browser?
I mean your database could be in the root directory of your drive.
oh its in my directory, im making a flask app
when sorting a db can i make it stop when an int is too big?
for example
i have 20 rows with an id
their ids are unique but not 1-20
i sort ASC
but i want it to stop when an id is = or > than 20
so maybe it returns
1,3,6,7,8,12,16,19
oh wait
nvm
im dumb
def next_id(self) -> int:
# INSERT
query = "INSERT OR IGNORE INTO cur_id (id) VALUES (?)"
values = tuple([FIRST_ID])
self.cursor.execute(query, values)
# UPDATE
query = "UPDATE cur_id SET id = id + 1 RETURNING id"
result = self.cursor.execute(query).fetchone()
if result[0] > LAST_ID:
query = "UPDATE cur_id SET id = ? RETURNING id"
values = tuple([FIRST_ID])
result = self.cursor.execute(query, values).fetchone()
self.conn.commit() # <----- ERROR
return result[0]
I'm getting an error on the commit, no idea why
sqlite3.OperationalError: cannot commit transaction - SQL statements in progress
My code uses db.command function (MongoDB and PyMongo) but mongomock raises an exception when use (Not Implemented Error). How to mock db.command?
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it
anyone encountered this error? ive searched the internet and docs and nothing helps me. most people are connecting to their db running off their pc using ports. im using it from mongo mongodb+srv://user:password@cluster0.c28qv.mongodb.net/
Hey, guys could anyone please suggest good github related with pandas to practice for beginner?
Is it normal for mysql workbench to freeze whenever I try to stop an instance?
Hello everyone, Im storing an excel file in a filstream(varbinary(MAX)) column in database, how do i now read from the varbinary field and convert it back into an excel document? Any help appreciated
Thank you
When SQL outputs data it says it like this [(100.0,)]. How can I make it only say the number without the [()]?
Unresolved attribute reference 'Column' for class 'SQLAlchemy'
can someone help me?
Hi guys. Here is the Glue ETL job code that is causing headaches to me
That line takes almost 4 minutes to execute for just one register
this job tries to struct XML texts
these texts are stored un pyspark dataframe column. Very long texts
and i need to loop that column to pass texts to extract functions. Tried with collect() and ToLocalIterator() and now with ToPandas()
have the same result. Really need your help guys :c
Can't you loop through it without converting it?
This may be more about #data-science-and-ml
guys, i made code with cooldown, but bot doesnt change anything in db
here code:
@commands.command(
name="daily",
aliases=["Daily"]
)
async def user_daily(self, ctx):
place_datetime = datetime.datetime.now()
local_collection = collection['coins']
data = local_collection.find_one({'id': ctx.author.id})
if not data:
data = {}
data['datetime'] = data.get('datetime', place_datetime - datetime.timedelta(days=1, seconds=60))
if (place_datetime - data['datetime']).days < 1:
await ctx.send(
f"You can only collect the reward once every 1 day! Come via `{int((place_datetime - data['datetime']).seconds / 3600)}` ัะฐัะพะฒ")
else:
local_collection.update_one({'id': ctx.author.id},
{'$inc': {'balance': 50}, '$set': {'datetime': place_datetime}})
await ctx.send(f"You took 50 coins! Come back in a day and pick up more!")
I need to find a set of possible superkeys when given a relation and a set of attributes, does anyone have any direction? everything I look up only tells me how to find the relation given the keys
hey all, I'm working on a Flask/Sqlalchemy guide. I have 2 classes defined in my models.py, that I can query like this: User.query.all() and Post.query.all() and that works fine, but then I have a self-referential association table that also lives in models.py but is not actually a class, it was created using followers = db.Table("followers", ...) , but if I try to query followers I get NameError: name 'followers' is not defined. How could I inspect/query the followers association table?
@fading patrol thanks for the link, I tried this, but still got errors: ```>>> from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine(app.config["SQLALCHEMY_DATABASE_URI"])
Session = sessionmaker(bind=engine)
Session = Session()
Session.query(followers)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'followers' is not defined
Session.query(Base.metadata.tables['followers']).all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'Base' is not defined ```
not sure what I'm missing
Base is from the example so you don't want to use that. And it looks like you didn't declare or import followers into that interpreter session.
Otherwise I think Session.query(followers) is probably on the right track
@fading patrol ok I see, I thought Base was generic to sqlalchemy. In theory followers is already imported/declared since I'm working directly with flask shell, but I also tried re-importing with from app import models (followers is declared in models.py) and I'm still getting the NameError on followers
by contrast, Session.query(User).all() and Session.query(Post).all() work perfectly
@fading patrol adding from app.models import followers made it work! ๐
thanks man!
small question, what's better?
A single but huge database
or small databases with 3/4 tables each?
One database, unless the tables are completely unrelated or you have some other very good reason to handle separately
table tags:
| ID | NAME | CONTENT |
+----+-----------+----------------+
| 1 | email | bruh email |
| 2 | gmail | bruh gmail |
| 3 | morth | bruh morth |
| 4 | gmail.com | bruh gmail.com |
| 5 | nomail | bruh nomail |
SELECT tags.name
FROM tags
WHERE tags.name SMTH 'fmail'
LIMIT 3;```
I want this SMTH to be as LIKE operator but to it return:
```py
[ ['email'], ['gmail'], ['gmail.com'] ]```
Is there any way to do that?
**i am trying to make a afk cog ** with very limited knowledge of python
the screenshot shows what i have in mongodb
what i want to do is:
on_message
if someone mention you
bot check if mention.id in member (on database)
check AFK (on database)
send message: user is afk with {reason}
if member texts
bot check if author.id in member (on database)
set AFK to false (on database)
can anyone help please?
how do i read database, any docs would help too.
Say, I have three models - thread, configuration and server. Now the configuration can be targeted either to a particular thread or all threads of a server. The configuration.id can either be a server ID or a thread ID, now I want to make this id a foreign key relation, so the foreign key relationships can either be a relation with server.id or thread.id. How will I go about doing this? A "union" foreign key relationship
Moved to: #help-kiwi message
Don't think so... Can you state directly what you are trying to accomplish (instead of how you want to accomplish it)?
bot's command:
user executes it like ?tag search <tag_name>
And it shows the list with tags with similar names
getting it from db
LIKE '*mail*'? Or use a chain of LIKE with OR if you want to be more precise
but name can be different
Maybe try in #discord-bots
def get_tag(self, guild_id: int, name):
def disambiguate(rows):
if rows is None or len(rows) == 0:
return 'Did not found anything.'
return '\n'.join(ro[0] for ro in rows)
t = self.bot.database.fetchall('SELECT user_id, name, descr FROM tags WHERE guild_id = %s AND name = %s',
(guild_id, name))
if len(t) == 0:
query = """SELECT tags.name
FROM tags
WHERE tags.guild_id=%s AND tags.name LIKE %s;
"""
r = self.bot.database.fetchall(query, (guild_id, name))
return disambiguate(r)
return t[0]```
this is my current code @fading patrol
that always returns 'Did not found anything.' when name not exists
I'm not familiar with bot, maybe try #discord-bots . But clearly your query isn't returning results.
it is not related to bots but ok bruh...
Tried before coming here
Chat go brrrrr no one replied T_T
no one helps bruv
pls?
I am having a hard time updating a document in mongodb with pymongo.
my collection looks sometihing like so:
{"_id": objectid,
"thing": "sdad",
"otherThing": [{"name": "john", "amount": 15}, {"name": "bill", "amount": 37} ]
}
I am trying to set a new value for the amount field, but only for the one that name is bill.
Hey, anyone can help in dumping data to a mssql DB using pyodbc, actually file is in xlsx format and has approx 250MB file size, I tried to dump it but it is taking 45 or more minutes., Same file when dumped with SSIS package it takes around 5 min. So, is there any way I can optimize my dumping as well
how do I connect to localhost mariadb on windows? (my code doesn't work)
conn = mariadb.connect(
user="root",
password="password",
host="127.0.0.1", #also tried localhost here
port=3306) #also tried nothing here
I'm not sure. But I think the only thing you need to set, except for user and password, is host="localhost".
does windows have a socket file?
when i do that, i get 10061
weird question but I have a table that carries the username and id of users. Is it more traditional to call the table user or users
users cause its plural so its good grammar
thanks
Assuming an RDMBS schema (django ORM specifically), what would be your approach to saving multiple resolutions of some asset - for example - a video that in 4 different resolutions (4 different files), plus 4 accompanying images in the same size for each video?
These video+image pairs are logically the content of one of my models in the system. One option is to just have a model like VideoImagePair (i.e. a single resolution) and have a foreign key to my main model. Another is to have a model like ImageVideoBundle that actually has all the resolutions together.
why do i keep getting this?>
I've been getting contradictory answers about this, but the one thing everyone seems to agree on is to be consistent across tables
yea thanks
How do you add database to your discord bot?
hello
anyone knows python pandas?
from urllib.parse import urlencode
url_data_gov_hk_get = "https://api.data.gov.hk/v1/historical-archive/get-file"
url_covid_csv = "http://www.chp.gov.hk/files/misc/enhanced_sur_covid_19_eng.csv"
time = "20200801-1204"
url_covid = url_data_gov_hk_get + "?" + urlencode({"url": url_covid_csv, "time": time})
print(url_covid)
import pandas as pd
df_covid = pd.read_csv(url_covid)
print(type(df_covid))
df_covid
url_building_csv = "http://www.chp.gov.hk/files/misc/building_list_eng.csv"
time = "20200801-1203" ```
```py
url_building = (
url_data_gov_hk_get + "?" + urlencode({"url": url_building_csv, "time": time})
)
df_building = pd.read_csv(url_building)
df_building```
I know this isn't specifically Python related but is there any good guides/books on creating better databases? Covered the basics in one of my Comp Sci module but obviously making them safe/efficient for actual large scale projects was never really covered and I'd like to get better
for discord.py storage when you're just storing keys and values, would SQL or NoSQL databases be better
ok scratch that question
new question: what databases would be best for discord.py? like specific examples
postgresql with asyncpg generally is the go to db + driver combo
I need some help getting started with asyncpg
for a discord bot
I'm using discord.ext, and I already know how to do the SELECT * FROM table stuff
but idk really how to incorporate it into my code
Hiya, can we query if a point is contained in a given polygon using geointersect operator in MongoDB?
as in to return the documents which are contained in a given polygon, the location for each document is represented as a single point.
I'm trying to write a program that fetches and stores crypto price changes in the last 24 hours in my local machine and deletes them after 24h. which DB management system/library/package is best for this?
it's gonna be storing over 600 coin pair data and the fetching interval will be 15 seconds
I'm not certain but SQLite is probably sufficient, and if so, that's going to be simplest to configure.
thank you
i got a posgresql DB on my pc, how can I transfer it to my raspberry pi?
If this is timeseries data, then you could try postgres with a timescale extension:
https://github.com/timescale/timescaledb
although regular SQL databases are sufficient for your use case!
im having problem at mongodb, anyone can help in #help-cherries ๐
If you're new to databases and stuff, try mongodb for your bot!
Thank you. I looked at SQLite and it looks like it's sufficient.
going to learn the entire thing soon
yep, sqlite might be sufficient, but since you're ingesting data rather fast, it might be worth looking into timescale + postgres later.
or you could try timeseries specific databases, too!
right! if SQLite proves to be slow at the task I'll probably have to go for postgress.
hey, i have 2 tables with some of the following fields:
Results table:
id home_team_results away_team_results results_date
History table:
id h_results_id (FK for results id) home_team_history away_team_history match_date
I want to find out the results id of the enteries that have the same home team, away team, and date in both tables.
Iv tried doing the following, however the issue with this is that results_id variable only gives the last value assigned to it after the for loop is done, therefore i cant use the results_id in my insert query because it just gives the last id.
c.execute("SELECT h.home_team_history, h.away_team_history, h.match_date, r.id, r.home_team_results, r.away_team_results, r.results_date FROM history h LEFT OUTER JOIN results r ON h.h_results_id = r.id WHERE r.id IS NULL")
history_data = c.fetchall()
c.execute("SELECT id, home_team_results, away_team_results, results_date FROM results")
history_results = c.fetchall()
results_id = 0
for j in history_data:
for k in history_results:
history_results_team = (k[1], k[2], k[3])
history_matches_team = (j[0], j[1], j[2])
print(history_results_team, history_matches_team)
if history_results_team == history_matches_team:
results_id = k[0]
print("RESULT FOUNDDD")
print(results_id)
Dump on the PC, transfer the file, import on the Pi, should be pretty simple. https://www.postgresql.org/docs/9.1/backup-dump.html
Hey I am trying to connect to SQL Server Express database, and I am flummoxed by the fact that my script seems to ignore the UID/PWD being specified and keeps trying to connect to the database as my current logged in system user.
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};'
'SERVER=(local)\SQLEXPRESS;'
'DATABASE=tmp_db;'
'UID=tmp_admin;'
'PWD=tmp_pwd;'
'Trusted_Connection=yes;')
oops, well it looks like Trusted_Connection forces the database connection to be through my logged in user account. Removing that at least lets me try to login with another account, even though it is unsuccesful.
