#how would i check if theres nothing in a sqlite row
1 messages · Page 1 of 1 (latest)
hm
why not iterate it
i think async for item in cursor should work, given you are using aiosqlite
Im using asqlite
learn it
sqlite is 💩 for bots
^
Sqlite still works
would it
ill test hold on
Nvm
not even gonna waste my time
I know that wouldnt work
I'm not 100% sure so take with a grain of salt but I think it's: ```py
await cursor.execute(f"SELECT * FROM antiNuke WHERE guildid = ? AND punishment = ?", (ctx.guild.id, 0))
check = await cursor.fetchone()
if None in check:
print("1")
await cursor.execute("INSERT INTO antiPunishment VALUES (?,?)", (ctx.guild.id, 0))
Cuz afaik that returns a tuple
pretty sure this is literally what i have
whats the difference betweeen if None in check: and if check is None
first means is check contains None?, second means is check points to None?
okay ill test
Traceback (most recent call last):
File "C:\Users\nlotg\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\core.py", line 235, in wrapped
ret = await coro(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nlotg\OneDrive\Desktop\selfbot\cogs\antinuke.py", line 252, in pungishment
if None in check:
^^^^^^^^^^^^^
TypeError: argument of type 'NoneType' is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\nlotg\AppData\Local\Programs\Python\Python311\Lib\site-packages\jishaku\features\invocation.py", line 165, in jsk_debug
await alt_ctx.command.invoke(alt_ctx)
File "C:\Users\nlotg\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\core.py", line 1650, in invoke
await ctx.invoked_subcommand.invoke(ctx)
File "C:\Users\nlotg\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\core.py", line 1029, in invoke
await injected(*ctx.args, **ctx.kwargs) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nlotg\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\core.py", line 244, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: argument of type 'NoneType' is not iterable
if None in check:
^^^^^^^^^^^^^
TypeError: argument of type 'NoneType' is not iterable
@late matrix '
Ok so its returning None not a tuple so you were right at the start
have you tried UPSERT?
await cursor.execute(f"SELECT * FROM antiNuke WHERE guildid = ? AND punishment = ?", (ctx.guild.id, 0))
check = await cursor.fetchone()
if check is None:
print("1")
await cursor.execute("INSERT INTO antiPunishment VALUES (?,?)", (ctx.guild.id, 0))
I know i did this wrong lol
That one
huh
idk how to do that
send an example
@late matrix what are you saying though
learn it. https://www.sqlite.org/lang_upsert.html
This original one should be right it's saying check is an instance of NoneType meaning the if check is None: is right
Are you using aiosqlite or sqlite3
use aiosqlite.
No
not rapptz asqlite
asqlite isn't aiosqlite
theres still no difference bro
yes there a difference
didnt work though
here ill try it again
Your original code should work.
There are docs you can read, btw.
It either returns a Row or None if there is no data.
stop shitting on sqlite, a discord bot which has maximum 5 tables does not need PostgresSQL lmao, sure i love postgres too but it would be overkill for a simple discord bot
to be honest, aiosqlite is a better library than asqlite
Oh that actually exists? I thought they were just mistyping aiosqlite
I have more than 5 tables
i have like 30
very interesting, is ur bot big?
No lol im recoding a old bot of mine
hmm also dont u think, the anti nuke / raid features are a bit obsolete, now since discord themselves have implemented that?
1 server rn and its just a test server
no
Even still, sqlite is fine and can handle multiple tables with 10s of thousands of rows.
discord has no antiNuke features just antiraid
I'm assuming this is for like ur skill and improvement then?
It just doesn't scale.
yes you are right
what do you mean lol
by scaling or skill improvement
lol
so is aiosqlite better than asqlite?
I could switch but bro was being annoying about it so i blocked him
yes yes yes
how is it better
aiosqlite has 10x more stars but they both are pretty similar
so it doesn't really matter, you shouldn't care about changes this small, tbh, its an sqlite wrapper under the hood, sqlite being a SYNChronous database system so
its just a wrapper
It's just more commonly used, really. asqlite is something Rapptz (the creator of d.py) rolled out. But, aiosqlite already existed.
exactly the point ^
yeah sure
Personally, I use aiosqlite (when needed) in conjuction with SQLAlchemy (or some other ORM ifyou wanted)
Back to this though it didn’t work
LOL
Then it's becuase asqlite is different.
😭
no no
await cursor.execute(f"SELECT * FROM antiNuke WHERE guildid = ? AND punishment = ?", (ctx.guild.id, 0))
could it be because of this
im supposed to be checking if theres no row or if its None
oh also aiosqlite supports async iterators over cursors
idk if it does
Yeah, it's not, it should still return None
it does not
but rapptz made dpy, he couldn't pay much attention to asqlite i guess?
hold on ill rewrite the code & try again
if check is not None:
?
You're trying to select from, and if there isn't a result, create a result, right?
Yes
Also
also a tip for writing queries:
# NO F STRINGS or sql injection and sad life :,(
await cursor.execute("""
SELECT *
FROM anti_nuke
WHERE
guild_id = :guild_id
AND punishment = :punishment
""", {
"guild_id": ctx.guild.id,
"punishment": 0
})
result = await cursor.fetchone()
Couldn't you just INSERT OR UPDATE?
^ it’s definitely this
0 = ban
1 = kick
btw
Yeah
but
minor issue with that i noticed but idk how to explain it so im just finding another way
you cant update the row if theres no data
im still new to sqlite but it’s definitely this
await cursor.execute(f"SELECT * FROM antiNuke WHERE guildid = ? AND punishment = ?", (ctx.guild.id, 0))
how about update or ignore
Maybe because im checking if the punishment has 0 in it?
Did you create the column as an int or a bool?
int
Hmm.
@late matrix
sec
^^^^
idk how to rewrite it to where it checks if theres a row at all instead of something specific
SELECT * from antiPunishment where guildid = ?
You're currently trying to fetch where guild_id == something AND punishment==0
await cursor.execute(f"SELECT * FROM antiPunishment WHERE guild_id = ?", (ctx.guild.id))
guild_id should be unique.
would this work
it should be ctx.guild.id
guys. dont do select and insert if not exists. use UPSERT
what is it kindly repeat
that would return the row where the guild_id matches, if it exists
im trying to check if a row doesn’t exist or if its None
there is a method called fetch all ig
so that would work for what im trying to do
^ if it doesn’t exist it creates the row
how does your tables looks like?
send me the sql query
and the row you wanna check
await cursor.execute("CREATE TABLE IF NOT EXISTS antiPunishment (guild_id INTEGER, punishment INTEGER)")
it was set to text for some reason but i just changed it to integer
Yeah check if theres anything in punishment
if theres nothing it returns None correct?
Well you don't have a default value, so the row can't exist without both guild and punishment.
-d aiosqlite.fetch_all
fetchall()```
Return all (remaining) rows of a query result as a [`list`](https://docs.python.org/library/stdtypes.html#list "list"). Return an empty list if no rows are available. Note that the [`arraysize`](https://docs.python.org/library/sqlite3.html#sqlite3.Cursor.arraysize "sqlite3.Cursor.arraysize") attribute can affect the performance of this operation.
it returns a list you could iterate over it
mommy python 🤤🤤🤤
Yeah
guild_id should be unique, so select where guild_id == ctx.gulid.id
If the guild is not there, it will return None
default value?
you can set defaults with sql yes
Could i set punishment to 0 by default upon creating it?
yes yes yes
i tried that earlier didnt work
yesssssssssssss
You could, yeah.
what have you tried
i told thousands times
in dms
and you are still repeating same thing
i gave you syntax too
CREATE TABLE IF NOT EXISTS antiPunishment (guild_id INTEGER, punishment INTEGER default 0)
yeah
i went into dpy server and they told me what i was doing was wrong lol
they are mf i hate dpy
so i just removed it tried upsert instead and it didnt fix my problem
they said something like “ you cant update the row if it isnt created “
told me default doesnt work for what im trying to do
i already had it
but alright thanks
ill try
and close this thread if your query got resolved
^^ /solved
How so?
await cursor.execute("CREATE TABLE IF NOT EXISTS antiPunishment (guild_id INTEGER, punishment INTEGER default 0)")
elif anti.lower() == "ban":
await cursor.execute(f"SELECT * FROM antiPunishment WHERE guild_id = {ctx.guild.id} AND punishment = 0")
check = await cursor.fetchone()
if check is not None:
e = discord.Embed(description=f"")
await ctx.send(embed=e)
return
await cursor.execute("UPDATE antiPunishment SET punishment = ? WHERE guild_id = ?", (0, ctx.guild.id))
await self.bot.db.commit()
e = discord.Embed(description=f"")
await ctx.send(embed=e)
ill remove the strings later yes i know bad way to do it
but
yeah but when i check it ignores the check even though the punishment is set to 0 which is ban
so it sends the 2nd embed instead of the 1st even though the punishment is already set to 0/ban
But you have a check for not None and then are trying to update if it IS None
Your syntax is backwards.
check = ...
if check: # basically what you have:
# do some stuff
return
# from this point on, check is None
update check...
this doesn't work, becasue check is None
check = ...
if check:
# update check
# do more stuff, I guess.
return
# check is None
# insert new guild_id
# do more stuff
Is what it should look like
ahahaha optimal outcome
show the code pls