#how would i check if theres nothing in a sqlite row

1 messages · Page 1 of 1 (latest)

quiet quartz
#
        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

#

how would i check if theres nothing in a sqlite row

#

@echo lava

icy wagon
#

hm

#

why not iterate it

#

i think async for item in cursor should work, given you are using aiosqlite

echo lava
#

do postgresql

quiet quartz
#

idk postgres

#

not gonna learn it

echo lava
quiet quartz
#

nothing wrong with asqlite though

#

No

#

too much work

echo lava
#

sqlite is 💩 for bots

quiet quartz
late matrix
#

Sqlite still works

quiet quartz
#

ill test hold on

#

Nvm

#

not even gonna waste my time

#

I know that wouldnt work

late matrix
#

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

quiet quartz
#

whats the difference betweeen if None in check: and if check is None

echo lava
quiet quartz
#

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 '

late matrix
#

Ok so its returning None not a tuple so you were right at the start

quiet quartz
#

what u mean i was right at the start

#

ur saying the code i already had was right?

echo lava
late matrix
#
        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

quiet quartz
#

huh

echo lava
quiet quartz
#

send an example

#

@late matrix what are you saying though

late matrix
#

Are you using aiosqlite or sqlite3

quiet quartz
#

asqlite

#

Rapptz’s asqlite

echo lava
#

use aiosqlite.

quiet quartz
#

No

echo lava
#

not rapptz asqlite

quiet quartz
#

asqlite

#

theres no difference

#

lol

echo lava
quiet quartz
#

theres still no difference bro

echo lava
#

yes there a difference

quiet quartz
#

No

#

same outcome

#

same db

#

same usages

quiet quartz
#

samething

#

No different

#

same usages

#

not my initial question

quiet quartz
#

here ill try it again

echo lava
#

just use aiosqlite

#

stop trying asqlite

quiet quartz
#

Im gonna block u ngl

#

anywho

spice owl
#

Your original code should work.
There are docs you can read, btw.

#

It either returns a Row or None if there is no data.

icy wagon
# echo lava sqlite is 💩 for bots

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

icy wagon
spice owl
#

Oh that actually exists? I thought they were just mistyping aiosqlite

quiet quartz
#

i have like 30

icy wagon
#

ooo interesting

#

what are you storing

quiet quartz
#

configuration stuff for the bot

#

antinuke config antiraid config etc etc etc & more

icy wagon
#

very interesting, is ur bot big?

quiet quartz
#

No lol im recoding a old bot of mine

icy wagon
#

hmm also dont u think, the anti nuke / raid features are a bit obsolete, now since discord themselves have implemented that?

quiet quartz
#

1 server rn and its just a test server

spice owl
#

Even still, sqlite is fine and can handle multiple tables with 10s of thousands of rows.

quiet quartz
#

discord has no antiNuke features just antiraid

icy wagon
#

I'm assuming this is for like ur skill and improvement then?

spice owl
#

It just doesn't scale.

icy wagon
quiet quartz
#

what do you mean lol

icy wagon
#

by scaling or skill improvement

quiet quartz
#

lol

#

so is aiosqlite better than asqlite?

#

I could switch but bro was being annoying about it so i blocked him

icy wagon
#

yes yes yes

quiet quartz
#

how is it better

icy wagon
#

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

spice owl
#

It's just more commonly used, really. asqlite is something Rapptz (the creator of d.py) rolled out. But, aiosqlite already existed.

icy wagon
#

exactly the point ^

quiet quartz
#

right

#

Ill stay with asqlite then i guess

icy wagon
#

yeah sure

spice owl
#

Personally, I use aiosqlite (when needed) in conjuction with SQLAlchemy (or some other ORM ifyou wanted)

quiet quartz
icy wagon
#

remember the most important programmer gif

quiet quartz
#

LOL

spice owl
quiet quartz
#

😭

quiet quartz
#

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

icy wagon
#

oh also aiosqlite supports async iterators over cursors

quiet quartz
#

why doesnt asqlite

#

lame

icy wagon
#

idk if it does

spice owl
#

Yeah, it's not, it should still return None

quiet quartz
#

it does not

icy wagon
#

but rapptz made dpy, he couldn't pay much attention to asqlite i guess?

quiet quartz
#

if check is not None:

#

?

spice owl
#

You're trying to select from, and if there isn't a result, create a result, right?

icy wagon
#

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()
quiet quartz
spice owl
#

Couldn't you just INSERT OR UPDATE?

quiet quartz
#

0 = ban
1 = kick

btw

quiet quartz
#

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

spice owl
#

Correct, that's why it's insert or update

#

If no row to update, it inserts instead.

quiet quartz
#

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))
icy wagon
#

how about update or ignore

quiet quartz
#

Maybe because im checking if the punishment has 0 in it?

spice owl
#

So punishmant=False then ban, true kick?

#

Because 0/1 is False/True

quiet quartz
#

0 being ban and 1 being kick

#

Yea

spice owl
#

Did you create the column as an int or a bool?

quiet quartz
#

int

spice owl
#

Hmm.

quiet quartz
#

@late matrix

spice owl
#

sec

quiet quartz
#

idk how to rewrite it to where it checks if theres a row at all instead of something specific

#

SELECT * from antiPunishment where guildid = ?

spice owl
#

You're currently trying to fetch where guild_id == something AND punishment==0

quiet quartz
#
await cursor.execute(f"SELECT * FROM antiPunishment WHERE guild_id = ?", (ctx.guild.id))
spice owl
#

guild_id should be unique.

quiet quartz
#

would this work

late matrix
#

it should be ctx.guild.id

spice owl
echo lava
#

guys. dont do select and insert if not exists. use UPSERT

quiet quartz
#

yeah i forgot the id when typing that lol

#

same question though

late matrix
#

what is it kindly repeat

spice owl
#

that would return the row where the guild_id matches, if it exists

quiet quartz
#

im trying to check if a row doesn’t exist or if its None

late matrix
#

there is a method called fetch all ig

quiet quartz
quiet quartz
late matrix
#

how does your tables looks like?

#

send me the sql query

#

and the row you wanna check

quiet quartz
#
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

late matrix
#

and you want to check punishment row

#

like the row is empty or not

#

right?

quiet quartz
#

Yeah check if theres anything in punishment

#

if theres nothing it returns None correct?

spice owl
#

Well you don't have a default value, so the row can't exist without both guild and punishment.

late matrix
#

-d aiosqlite.fetch_all

tired tendonBOT
#

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.
late matrix
#

it returns a list you could iterate over it

icy wagon
#

mommy python 🤤🤤🤤

quiet quartz
spice owl
#

guild_id should be unique, so select where guild_id == ctx.gulid.id

If the guild is not there, it will return None

icy wagon
#

you can set defaults with sql yes

quiet quartz
#

Could i set punishment to 0 by default upon creating it?

icy wagon
#

yes yes yes

quiet quartz
#

i tried that earlier didnt work

late matrix
#

yesssssssssssss

spice owl
#

You could, yeah.

quiet quartz
#

@late matrix suggested it

#

actually

icy wagon
#

what have you tried

late matrix
#

i told thousands times

#

in dms

#

and you are still repeating same thing

#

i gave you syntax too

quiet quartz
#

CREATE TABLE IF NOT EXISTS antiPunishment (guild_id INTEGER, punishment INTEGER default 0)

late matrix
#

yeah

quiet quartz
late matrix
#

they are mf i hate dpy

quiet quartz
#

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 “

late matrix
#

you got answer

#

now implement it

quiet quartz
#

told me default doesnt work for what im trying to do

#

i already had it

#

but alright thanks

#

ill try

late matrix
#

and close this thread if your query got resolved

spice owl
#

^^ /solved

quiet quartz
#

wait

#

nah didnt work

spice owl
#

How so?

quiet quartz
#
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

spice owl
#

Works when I do it.

#

Don't use f-strings in sql queries.

quiet quartz
#

so it sends the 2nd embed instead of the 1st even though the punishment is already set to 0/ban

spice owl
#

But you have a check for not None and then are trying to update if it IS None

#

Your syntax is backwards.

quiet quartz
#

explain

#

how is it backwards

spice owl
#
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

quiet quartz
#

oh.. that makes alot more sense

#

oh yeah

#

i feel dumb

quiet quartz
#

nvm

#

didnt work either

#

im deleting discord & this bot this is lame

icy wagon
frail crag