#databases

1 messages · Page 178 of 1

torn sphinx
#

as you can see on the top

#

which has userid and money columns

foggy owl
#

I see

#

Ok I'll give it a shot in a bit

#

Thank you

torn sphinx
#

yw

delicate fieldBOT
#

zettelpy/slip_box.py lines 56 to 63

conn.cursor().execute(
    """ CREATE TABLE notes (
        note_id INTEGER PRIMARY KEY AUTOINCREMENT,
        title   TEXT NOT NULL,
        path    TEXT NOT NULL,
        UNIQUE(title)
        )"""
)```
`zettelpy/slip_box.py` line 70
```py
"INSERT OR IGNORE INTO notes (title, path) VALUES ('{}', '{}')".format(```
primal finch
#

okay so I have a table for my users where I store their username, password hash and all that. One of the features I want to implement in my timer site is the ability to create custom timers and that would require storing a purpose for the custom timer as well as the length of the custom timer in a new table. The only issue I have is how I would link the custom timer created to each user so that I only display their custom timers. Anyone know how I would go about solving this issue?
this is for sqlite

fading patrol
#

That way a simple JOIN query can pull the timer data for any specific user you need

primal finch
#

yep thanks

#

mb ghost ping

blazing dawn
#

I'm getting quite sidetracked on what I want to achieve

I'll try explain to my best in short:-

check = ["1", "2", "3"]

and now I have a postgresql database with an id column and a jsonb datatype column named lets just say cards
id cards
-----+--------
1 {"1": 3, "4": 2}

thats the data in the table named test

Question:
How do I convert the cards of id->1 FROM {"1": 3, "4": 2} TO {"1": 4, "4":2, "2": 1, "3": 1}

How I expect the changes to occur:
from the check variable, increment all present inside check variable that exist in the cards jsonb as a key thus changing {"1": 3} to {"1": 4} and add the values that don't exist as a key in the cards jsonb with a value of 1 thus changing {"1":4, "4":2} to {"1":4, "4":2, "2":1, "3":1}
purely through postgres even though I may have used python to showcase the check variable it was solely for explanation you can simply just use pure postgres...

now it might look like I'm asking to be spoonfed but I really haven't managed to find anyway to do this so any assistance would be highly helpful

#

tldr: update statement that checks whether a range of keys from an array exist or not in the jsonb data and automatically increments or inserts respectively the keys into the jsonb with a value of 1

fading patrol
blazing dawn
#

the same with the second answer, the third was for nested

paper flower
#

@blazing dawn I'm just curious - what are you storing in your json field?

fading patrol
#

Is it not an option to modify the dictionary in Python? If you need to do this in pure Postgres I do suspect it's possible but I'm not advanced enough to help you. Using Python this would be very simple if slightly less efficient maybe

blazing dawn
paper flower
#

pithink You could use a relationship here?

blazing dawn
#

well those card_ids are in a relationship with another table of card_ids, name, description, stats, etc

paper flower
#

Really, use a relationship 😅
What's the table that needs that json field?

blazing dawn
#

which is why I was trying to find how it could be done in a single call through pure postgres

paper flower
#

What database are you using?

blazing dawn
#

database as in postgresql?

paper flower
#

yep, rdbms if you want 😅

fading patrol
blazing dawn
#

I'm using postgresql yes

paper flower
#

sqlite, postgres, so postgres it is

blazing dawn
#

oh they have a discord?

paper flower
#

@blazing dawn I think you could use a many-to-many relationship here?

blazing dawn
#

Idk what that is, I'll look it up however

fading patrol
paper flower
#

@blazing dawn 1, 2, 3.. are card slots?

nova cove
#

its a relationship where multiple record from multiple tables are related to eachother

#

@blazing dawn

blazing dawn
#

yes

paper flower
#

Something like this could work

create table tester(
    id integer primary key
);

create table cards(
    id integer primary key,
    name varchar(255) not null
)

create table test_cards(
    tester_id integer references tester(id),
    card_id integer references cards(id),
    slot integer not null,
    unique(tester_id, slot)
)
fading patrol
nova cove
#

where the foreign keys?

paper flower
nova cove
#

there can't be a relationship without foreign key

paper flower
nova cove
#

oh

#

didnt see

#

LMAO

paper flower
#

Also unique users won't be able to have more than one card in single slot pithink

#

Afaik sqlalchemy can map this into a dictionary

rigid mica
#

can anyone help me with setting up postgresql? (connecting it to my file and maybe a little explanation on how to write in it)

paper flower
#

Or you could use an orm if you need one

rigid mica
#

How do I write to a postgresql column?

nova cove
#

lioke add data to it?

torn sphinx
#

uh... can anyone help me im trying to open a .db file to read its contents but i just get this from sqlite (translated: unable to open database reason: file not a database) even tho im trying to open a .db file am i doing something wrong?

rigid mica
nova cove
#

sql query

rigid mica
#

for example if I make a warn command, how do I make it enter member id, reason and amount of warns

nova cove
#

read the docs

#

you have to execute a query

#

you should learn basic SQL also

rigid mica
#

hm

#

this is all just really really difficult to me

torn sphinx
rigid mica
#

@nova cove can we maybe continue in dms or something?

nova cove
#

k

torn sphinx
# torn sphinx can you share the first few lines of the .db file ?

uh they look like chinese in notepad: first two lines in notepad++%² À÷] ’q5•í ª‹ã Ì)lènR¨W4ÓIÑ1x¼ƒ”ÑV†1&fPÉQ«Ù”O%”LÅø—dnЏIYò?1ÛpÉ­~·y7ݨöQ7ÚýÝA†Ø¨z×&¹i¸« first two lines in notepad: ╿லଢ଼熒锵௭讪෣⧌剮垨팴䤛턖輱뱸茖톔虖☱ས쥐ꭑ铙╏䲔撗詮렟奉㿲쥰纭禷㝑﷚䇝窨⛗ྐྵ롩ફ埒횎ᴿ墄昋ᇃ诋⺐ಚᾹꜨɌᬷ诂菃䴻ᅳꊋ㩧ꢀ㚭羆ꖇ喂映鳢⁁썓롨蒐‽ະ錜ᧂ䙒׫箐싕⸶כ㶟⾅ɩ≟껈峢걂欹绅鈱쪘ⅱ࿦䣜Ɬ踷Ӣ㒲΋笪拕ԣ鮑ნ撿ꢷꥫ赸얟契퍁篭襨卾죰䔶憻ᣢ嚢囖웉恄쫤䘰⎹ꌅ筝繦믡酤ⲏヨ揅㪫峯뙽濲찁歶乻鎞蔏縞斀塮㐣쇔佖䯧詗ꏄዎ東愖궂⛙桀켒ԟ朦䗭캦⿎멙暙꡶鋐렣깚쐎쒛䏺╃ꌪ㲒盢煳㽶歵㝹⛽憰㱶ꬄЀꆛ㡰騎⩼攖櫣铋祦㧩ᙌ歘덜谮㨭⛿觠딣彳ℶ䝥蟽鶴덶ဂ默殇僄≮ﺻ㸇侥䳏ᆓ䄟郫݄锇׌Ῠݢ⽥帯殍ﰱ⨮鰝꽛ಅ㠁鼈쎭컵임뽓蒆ℸ￝啖徉䵃眬쀞励蔞吖ǎ䴸ꭞ罀䑵狺㯐㞡쳞㫭鲌蔂䕚禃芅ἢඦ嬜ᇐ痢レ萲吁ﶻ멚掽堄鍕䫫姭면ᲅ温쿍썛䞖㨗䯓ᩅ鶝瞂㸃ᒒ㋠蘶퀀뿛澹寕ᡇ嵟ᱴ꼱쬸ው蛹㤭锶⅍귎⶞熸犌뭱筡⒢໰䬡篯냬Ɪ伭翈머熐钁덃渞냘쥍䇢殜存籔샻䶺橾纰쏜钇䠐澩欜쿛ᝧ掠⪜킩ꢓ냏냐寯᭙⠪ᒛ⠔뒯箲᷻䑳྇惔䨂㻒䍽륙䶷窛鐥闅뒀ꆎ苠켶死᪫미鶸牾ᜍ偸Ẅ웭ᑏ驳砙ᢷ氲ꑙ쐃量㑳軚⬖锫灨樂戄ﮖ釄ㆨ袁ඝꔐ탐塟䘙鰱絽ᝣ䒈❕煸䝟❞򩛛桛쓢⏶曈缁مⰄ⣁ᘽ㯶稉ﷸ揞훋䖭ﶉ졙쒂З㽑ꝃ髨포↫㙭䥚퍡햠姙닟ㆯ낲妈䎆긍㢂֨ﬥ閑눙푘⚌䇘事橷အ덨ళ嘰옴Θ볣藠娸⽰今릸ঈ宀ﱋ዇퐙킗䗐잝睱墟엵⛃ꆽ㍢赺퉒鷓쌻⳽檟䠩聘桙ჸﱃీ笻ₑ∙ᭈ꧍淫鼬Ṋ었伂쀰圪낻攦⼌㻶ぬ곱Ꝿ쓎巉띭渜锪娧飉奕⪞刺ʋᄃ꫚쿽㈸뙏ᱱ㒹Ⓜࡸꛮ⌲狝诊ᾮ칚끒殥踌䕏㳼箸莧᪰䉼﩯癶웕죱ƽ┦抏羸진䠓롒轨篷摨娩픽봵拘꾼᜞ﻯꘐ￀魙騵軻䵁ኗ젩Ɱ鶵ࠓ멛ᶾ䞆䔐걱쪲쑡똰瘪虑㠜ⷣÞ툡帡逮랩锄ᠦ覝䒁윇ΐ้팣鋁墖Ⲱ蜀哽ꍼᖑႴ䙶詘䔮鸎砣픍膀炼䟏催벙볘푓숿ᕺẵ港ⅶ듵辙횭齇삞ꤘ훅ㇵ✝䏖矯ʛ냜ꛞ偨镃柁荠蓕䩆Ǎ䵶哳ڿ홺쮋椇ᦍ嬇䖿屇㠍䭫舿咦蕵嘤턞꽳눪缷ᐳ餌ሪ⟴尡糖攎뮁ਕᒺඈ헃lᱮ攔凖脌肍ᘝ铀㘰댤奿ꀨ葜⡊揷놦瘁ड़ฅ撜쵢舰ࡠؼ⹱ᡌ䇔ﶫ᪷쯟﬙隊ִ릲㡽Ⱉ瓿⚯莂㹣ᭃᵙ鬂쏔⃣鄑쮂Xᗑꭘ刟膸쐦솬뎝㉇镲⅝ǪᏉ䄎ꮝ汛ꔥ왿ಷ߻ḉ「ᬥ徧뾬ꥍ⋌俍칊竩ヅ줞㼇ꉝ韮᤹᠄昽䋂ᓒƣ珗銯䡲ⓝ쵮뮜棝⡀賔㗝磵⌙쀬挂爋噣ᮟ菿㴼弎鸝釆섗빫껑₶遛凰踕⿻哸䇲ድ䣚난迦殊椼倄鐦⼝꯻⒊蕰叅㽫絢ࣤ兓搅۰ߏ⺇呄ሬꪪ흮潚؍륮쮶鞜퀾詆ㅈ톓ᆬ僓됴뎓ၸᴹ悊ユ㈥昀㢇剏廋ᤴ糯佯锘䪁攺㏂蘋铒䃪Ꮊ藓땗ꁁ௓蓥ꙹ嘄鎖䈜窂釟밎㈣⸧㸰뢊៥뜄ጂ믞퀏ꛈ⅘쭳쎥㕀쉒皌犮鞭븋㞇ꓖ뷣䟉㷌赉੥぀﫜즮愈ꄡ牸◄䯲圆ꂔ鐓䧓鲐꼎ꪠ坩賈敠剡蜮࣬╪凭큽ⶊ⼳萕ẇ陆ಝ烒ᕼ㌠㻹ꊁ镋윝쮬憾폍楗⥙ꎎ눲ར䅖䴴᨝娆?鴷Ⳳ₢/뢁ト⬨蔚㘪惏瘖嗒瑐珐৸ʖ젲붂륏ᙒ䥇팰翂Ӹ阹ᰗ횪믳槍㫞囧织㻆赎詨녒շ쯚瞌䀏쯼嗟醨ꉤ漗쯀涟潱츋핱ẋ썞臫dz⥸笎띃䎫軩˛씂胈趔沬뼅䬢켣ˏ也廸⼳蜡ꔄ恞ⓩ⸪邔텔㸺鋔⇾å歆ᮔ杞큨煥愳⊅䮏䖞䥤舎뙭鰕膴쵷淇䏲穠㸴읨婅붭砜ꮚ᪂蕄愀ꇕꚠ嗎櫽䉝ਜ਼ƣ냠챠擑詥䉓訩죆Ꙟ臶栗鈣ഐ髱龬ʳ㔕칯镳婖突߁✧蜽孕垚͂䩈䌮둝ビ囿ꎑ뀎旋鶉❨龪嵶苳㵠ꮌ䣹酠퉏艘鬟싈Ġ欙洉㋪톝随眼豑잋ȴ졇퇦ꖀ諲砤釳者㩣ﲸݱ盨⚲쌽巃規Ἷ䥷㮄摢柌⁍᭏㐣놲☗燛쵾ᣣὤ澧ܞ予큛⛋抳硴ᑴ謈ቇ좍ꈘ줌욑倈䤫㯴ᴆ鈳蚢팠윆챯臁ꕃ퓠藷

#

'look like' is indeed what it is, this is a Unicode issue, the characters aren't recognized so you get these random Unicode characters instead of your local symbols

#

no wonder the content is not recognized as a database

#

did you write the .db file yourself or did someone else ?

torn sphinx
#

did he encrypt it ?

#

idk

#

its a .exe file

#

I'd ask him, it seems like he did

#

ok

rigid mica
#
    await db.execute('''
        CREATE TABLE IF NOT EXISTS warnings(
            user int,
            warns int,
            reasons text[]
        )''')```
#

It always gives error to 'something close to user'

#

am I doing something wrong here?

fluid glen
#

cur.execute("UPDATE orders SET deliverystat, booldone = (?, ?) WHERE id = (?)", (str(defstat), (str(closestat), (str(s1)))))

is giving me issues, is there something wrong with this? I get this error sqlite3.OperationalError: near ",": syntax error

proven arrow
#

@fluid glen query should be, "UPDATE orders SET deliverystat = ?, booldone = ? WHERE ...."

fluid glen
#

ohh shit

#

thank you mate :)

rigid mica
#

@proven arrow sorry for the ping, could u help me out rq?

#

('''INSERT INTO warnings (reasons) VALUES ($1) WHERE member = $2''',reason +',', member.id)

#

I tried this

#

to make it insert in the right column

#

but WHERE cant be used in this

proven arrow
#

Please give some background. What are you trying to do with the query?

rigid mica
proven arrow
#

Then if the tagged member already exists, you should be updating the row, not inserting.

proven arrow
#
rigid mica
#

how do I check if it already exists?

fluid glen
#

cur.execute("UPDATE orders SET deliverystat = ?, booldone = ? WHERE id = ?", (str(defstat), (str(closestat), (str(s1)))))
now with this ^ though I'm getting this error:

#

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 3, and there are 2 supplied.

#

what could the issue be

proven arrow
#

Pass values as an array or tuple @fluid glen

rigid mica
fluid glen
#

could you give an example how I might do that? :P

#

please 🙏

proven arrow
#

Just wrap the values in square brackets, ```py
cur.execute("UPDATE orders SET deliverystat = ?, booldone = ? WHERE id = ?", [(str(defstat), (str(closestat), (str(s1))))])

fluid glen
#

ay

#

ty <3

rigid mica
#
@bot.command()
async def warn(ctx, member: discord.Member = None, reason= None):
    try:
        await db.execute('''INSERT INTO warnings (member) VALUES ($1)''',member.id)
    except:
        pass
    await db.execute(
    '''INSERT INTO warnings (reasons) VALUES ($1)''',reason +',')
    await db.execute('''UPDATE warnings SET warns = warns + 1 WHERE member = $1''', member.id)
#remove from database
#await db.execute(f'''UPDATE warnings SET warns = 0 WHERE UserID = {member.id}''')

@bot.command()
async def warns(ctx, member: discord.Member):
    warns= db.record(f"SELECT warns FROM warnings WHERE member= {member.id}") 
    await ctx.send(warns)
@bot.event
async def on_ready():
    await create_db_pool()
    print("Database connected")```
#

@proven arrow

#

could u take a look at this

#

sometimes the reasons are null, even though I gave a reason

#

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotNullViolationError: null value in column "member" of relation "warnings" violates not-null constraint
DETAIL: Failing row contains (null, 0, test,).

proven arrow
#

@rigid mica You are not inserting a reason when you insert.

await db.execute('''INSERT INTO warnings (member) VALUES ($1)''',member.id)

This is your code and there is no reason being added, so its null.

rigid mica
#
await db.execute(
    '''INSERT INTO warnings (reasons) VALUES ($1)''',reason +',')```
proven arrow
#

Are you aware of what insert command does?

rigid mica
#

yh

#

I need to do update right?

proven arrow
#

Your database table is made up of rows. So each insert will add a new row.

proven arrow
#

But why not just add the reason whilst inserting in the first place

#
INSERT INTO warnings (member, reason) VALUES ('lufthansa', 'some reason');
rigid mica
#

bc sometimes there already is a user

#

and it gave me an error

proven arrow
fluid glen
#

okay well

#

I got dis cur.execute("UPDATE orders SET deliverystat = ?, booldone = ? WHERE id = ?", [(str(defstat), (str(closestat), (str(s1))))])
and I get this error ```sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 3, and there are 1 supplied.

grim vault
#

You are doing [(a, (b, (c)))] instead of (a, b, c) or [a, b, c]

fluid glen
#

tyyyy

rigid mica
#

Is there a possibility to make it add reasons after the first reason given?

#

AttributeError: 'Pool' object has no attribute 'record'

#

is what I get for this

#

warns= db.record(f'''SELECT warns FROM warnings WHERE member= {member.id}''')

nova cove
#

fetchrow not record

rigid mica
#

yh

rigid mica
#

@grim vault may I ask you something?

#
    await db.execute('''
        CREATE TABLE IF NOT EXISTS warnings(
            member bigint PRIMARY KEY,
            warns int DEFAULT 0,
            reasons text
        )''')```
#

I got this now, but the reasons text can always only contain 1 thing. Can I like make it write behind the thing that is already there?

paper flower
#

You want to know how to add new columns here?

rigid mica
#

no, but for example

#

I warn someone with reason lol

#

and I warn him again with reason lmao

#

only reason lmao will be there

#

since it replaced lol

#

I want it to be displayed like lol,lmao

#

behind the previous reasons

paper flower
#

You could get a warn for a user 🤷 if it doesn't exist create one

rigid mica
#

For example mee6

paper flower
#

Use update statement?

rigid mica
#

thats what I do

#
@bot.command()
async def warn(ctx, member: discord.Member = None, reason= None):
    try:
        await db.execute('''INSERT INTO warnings (member) VALUES ($1)''',member.id)
    except:
        pass
    await db.execute(
    '''UPDATE warnings set reasons= $1 WHERE member = $2''',reason+',', member.id)
    await db.execute('''UPDATE warnings SET warns = warns + 1 WHERE member = $1''', member.id)```
#

its an update

paper flower
#

So what's wrong then?

rigid mica
#

it will only save the last given reason

paper flower
#

mhm, you want to save all of them?

rigid mica
paper flower
#

So just don't replace the warnings? pithink

rigid mica
paper flower
#

Create a new one, you could get amount of warnings for certain user by using count

rigid mica
#

since it always replaces

paper flower
#

It doesn't, it's how you coded it

rigid mica
#

I wanna save all the reasons

rigid mica
paper flower
#

Don't update old warnings but create a new ones using insert

rigid mica
#

it will be under the column of warnings and on the row of user id right?

paper flower
#

I'm not sure what you mean

rigid mica
#

the warns need to be linked to the user

#

and how do i make it write in the row of the member id?

paper flower
#

Just use a foreign key

rigid mica
#

u got an example?

paper flower
#
create table users(
   id integer primary key
);
create table warnings(
    id integer primary key,
    user_id integer references users,
    ...
);
rigid mica
#

yh I got that

#

a primary key

paper flower
#

You don't have a foreign key though pithink

#

If you use user id as primary key you would only be able to store a single warning for each user

rigid mica
#

I see

#

so whats a foreign key then?

paper flower
rigid mica
#

@ebon skiff couldnt I like get the value inside the text column, take it out and overwrite it with the new and old one

ebon skiff
#

He is talking about a inner join

ebon skiff
#

I also highly recommend to have warning id's

rigid mica
rigid mica
#

so I tried fetching what was inside the column first

#

but if the value is null, it gives error: basicly if the user hasnt been warned yet, its error.

ebon skiff
rigid mica
ebon skiff
# rigid mica

Yeah this but it also needs a warning id if you ever want to remove a warning.

paper flower
#

You should create a separate column for your warning id's and make member a foreign key instead of primary key

rigid mica
#

never remove a specific one

ebon skiff
# rigid mica

I don't get why you would want the warn int to also increase that makes no sense.

ebon skiff
rigid mica
ebon skiff
paper flower
rigid mica
#

yh ik

#

pandabweer, can i explain it in dutch rq?

ebon skiff
#

I don't get why you would want to append text inside reasons and have all warnings inside one row that is pretty bad.

rigid mica
#

ik wil dus als ik iemand meerdere keren warn, dat het de eerste warning niet replaced met de nieuwe warning, maar in een formaat komt te staan zoals 'warning1,warning2

rigid mica
paper flower
rigid mica
#

I see

ebon skiff
rigid mica
#

So new lines would be better?

paper flower
#

You could also store info like who issued that warning and when

ebon skiff
ebon skiff
rigid mica
#
@bot.command()
async def warn(ctx, member: discord.Member = None, reason= None):
    await db.execute('''INSERT INTO warnings (member) VALUES ($1)''',member.id)
    await db.execute(
    '''UPDATE warnings set reasons= $1 WHERE member = $2''',reason+',', member.id)
    await db.execute('''UPDATE warnings SET warns = warns + 1 WHERE member = $1''', member.id)```
#

so this would create new lines each time?

paper flower
#

Nope

#

You still have to change your database structure, also you can do everything you need with a single insert

nova cove
#

I am transitioning my SQLite database over to use the aiosqlite async bridge. I have a db util file, where I added async/await to all of my custom functions for things like execute, commit, etc. In my bot's class, I have a async function of the following:

async def init_db(self):
  for guild in self.guilds:
    await db.execute(f"CREATE TABLE IF NOT EXISTS GUILD_{guild.id} (UserID int NOT NULL, XP int DEFAULT 0, Level int DEFAULT 0, warns int DEFAULT 0, kicks int DEFAULT 0, mutes int DEFAULT 0, bans int DEFAULT 0)")

    for x in guild.members:
      if x.bot != True:
        await db.execute(f"INSERT OR IGNORE INTO GUILD_{guild.id} (UserID) VALUES (?)", x.id)

  db.multiexec("INSERT OR IGNORE INTO guilds (GuildID) VALUES (?)", ((guild.id,) for guild in self.guilds))

  await db.commit()
  await db.autosave(self.scheduler)

The autosave is a cron job: (i am using the AsyncIOScheduler which is what self.scheduler represents)

async def autosave(sched):
    await sched.add_job(commit, CronTrigger(second=0))

My on_ready function consists of: (this is not all of it, the rest is not helpful)

async def on_ready(self):
  if not self.ready:
    self.scheduler.start()
    await self.init_db()
    logging.info("Database initialized.")

The build function, including its decorator function is:

def with_commit(func: Callable[P, R]) -> Callable[P, Awaitable[R]]:
    async def inner(*args: P.args, **kwargs: P.kwargs) -> R:
        func(*args, **kwargs)
        await commit()

    return inner  

@with_commit 
async def build():
    if isfile(BUILD_PATH):
        await scriptexec(BUILD_PATH)

When I run the bot, I expect it to build the db, but instead I get an error: AttributeError: 'Result' object has no attribute 'execute'
Execute function:

async def execute(command, *values):
    await cur.execute(command, tuple(values))

I feel like I might be overcomplicating my code. Help would be appreciated

paper flower
#

Guess your cursor somehow got assigned a Result

nova cove
#

yeah

#

this is a new error i have never seen

paper flower
#

You just assigned to cur somewhere

nova cove
#
cxn = connect(DB_PATH, check_same_thread=False)
cur = cxn.cursor()
#

it is global state, yes

paper flower
#

Probably somewhere else

nova cove
#

hmm

#

idk how

paper flower
#

Also i'd recommend you to not reuse cursors, just create new ones

nova cove
#

i'd have to have that allover the place then'

paper flower
#
async with connection.cursor() as cursor:
    await cursor.execute(stuff)
paper flower
nova cove
#

this would be what I do when I want to create a new cursor?

nova cove
#

ok

nova cove
paper flower
#

There's actually an example in aiosqlite github:

async with aiosqlite.connect(...) as db:
    await db.execute("INSERT INTO some_table ...")
    await db.commit()

    async with db.execute("SELECT * FROM some_table") as cursor:
        async for row in cursor:
            ...
nova cove
#

yeah ik

#

i just dont wanna have to have

async with ... as cursor:

all over the place but

#

its better prob

paper flower
#

It's fine imo pithink

nova cove
#

mhmm

paper flower
#

context managers are great, you don't have to close stuff

nova cove
#

mhmm

#

so my db util file would no longer have any function

paper flower
#

Yep, pretty much

nova cove
#

prolly for the better

paper flower
#

I'd also recommend you to try sqlalchemy, python objects would be easier to work with

nova cove
#

ik

#

ive used it before

#

ORM and shit

#

aight bro thanks for the help

#

i was going to start revamping my bot anyways

#

might as well get rid of the util file that isnt rlly needed

nova cove
#

I am thinking about converting my discord bot's db from SQLITE to PostgreSQL. I was wondering if I should just use asyncpg (which I was learning to use recently), try psycopg3, or use sqlalchemy. Any answers would be appreciated!

nova cove
#

Ok thanks, I was prolly gonna use that anyways

frigid glen
nova cove
#

mhmm that’s why I was asking about sql alchemy above

#

Or something like it

#

And I have sql alchemy experience

frigid glen
nova cove
#

I’ll check it out

#

Kinda looks like Django models

frigid glen
true swift
#

Yep

#

@torn sphinx
I haven't got any experience on PostgreSQL, but in MySQL you can connect with a live environment with cmd

#

But, what OS are you using?

vapid mist
#

im using aiosqlite and am confused

#

how am i able to just get data? with asyncpg it was as simple as x.fetch(query) but i cant seem to find anything useful with aiosqlite

nova cove
#
async with db.fetchrow(...) as db:
  ...

something like this i believe

vapid mist
#

hm

random arch
#

is there any SQL to python tutorials?

nova cove
#

wdym sql to python

potent bridge
#

I was learning about flask API

#

in my main directory, why did this pop up

#

Is it safe to delete

#

.vs and database.db

#

This is all that I've imported

rigid mica
#

How do I wipe all lines in my postgresql database where member = member.id

paper flower
paper flower
meager niche
#

has anyone used popsql?

proven arrow
rigid mica
#

@proven arrow I'm having some trouble

#
@bot.command()
async def warn(ctx, member : discord.Member = None, *, reason = None):
    await db.execute('''INSERT INTO warnings (member, reasons, moderator) VALUES ($1, $2, $3)''', member.id, reason, ctx.message.author.id )
    await db.execute('''UPDATE warnings SET warns = warns + 1 WHERE member = $1''', member.id)
    await ctx.send(f'{member} got warned by {ctx.message.author}, reason: {reason}')




@bot.command()
async def warns(ctx, member: discord.Member):
    try:
        r = await db.fetchrow(f'''SELECT warns FROM warnings WHERE member= $1''', member.id)
        d = await db.fetch(f'''SELECT reasons FROM warnings WHERE member= $1''', member.id)
        e=  await db.fetch(f'''SELECT moderator FROM warnings WHERE member= $1''', member.id)
        warns = r['warns']
        moderator = e['moderator']
        #await ctx.send(r['warns'])
        if warns > 1:
            embed = discord.Embed(
                title = 'Warns',
                description = f"{member} has got {warns} warnings:",
                color = (0x3498db)
            )
        else:
            embed = discord.Embed(
                title = 'Warns',
                description = f"{member} has got {warns} warning:",
                color = (0x3498db)
            )
        order = 0
        for y in d:
            order += 1
            embed.add_field(name = f'warning #{order}:', value = y['reasons']+ moderator[order] , inline = False)
        await ctx.send(embed=embed)
    except:
        embed = discord.Embed(
                title = 'Warns',
                description = f"{member} has got 0 warnings.",
                color = (0x3498db))
        await ctx.send(embed=embed)``` this is my code
#

and I input the moderator id in the database, and i try to get it out and enter it in embed when I ask the warns

mossy onyx
#

if i use with statement for connecting to my SQL server like this

with connect(
        host="localhost",
        user=input("Enter username: "),
        password=getpass("Enter password: "),
    ) as connection:
        print(connection)

so does the connection close automatically ?? or i have to do it like this
connection.close()

ping if reply

nova cove
#

connection.close()

mossy onyx
nova cove
#

you don’t have to ever close the connection though

#

but you prolly should when the action is executed

mossy onyx
#

hm okay

nova cove
#

This is MySQL connector correct

mossy onyx
nova cove
#
CREATE TABLE IF NOT EXISTS guilds
(
    guild_id bigint,
    PRIMARY KEY (guild_id)
);

Anybody know why this raises these errors:
Incorrect syntax near 'IF'. Expecting '.', ID, or QUOTED_ID.
An expression of non-boolean type specified where a condition is expected.
Incorrect syntax near 'guilds'.
Incorrect syntax near 'guilds'. Expecting '('.
Incorrect syntax near 'guild_id'. Expecting '(', or SELECT.
Incorrect syntax near 'guild_id'. Expecting '(', or SELECT.

I am using PostgreSQL btw

nova cove
#

no, its just from writing it. raw sql IF NOT EXISTS never works for me and I want to fix it

proven arrow
nova cove
#

i think i fixed it

#

no more errors are popping up

clever steppe
#

Is there a native and standardize way in SQL to add dates? I'm storing notes in this DB, and I want to have a column with the date of creation of the note, I'm using a sqlite3 database, I'm mentioning it since SQLite sometimes is different than other conventional databases. If not I guess I'll need to create a date TEXT column and use datetime.today() to get a string and insert it there.

#

If you have a better way of doing it, please mention it. Also ping on reply.

paper flower
#

Also you should use datetime or date (if applicable) types

clever steppe
paper flower
#

It's a type like integer, varchar and others

paper flower
#

datetime would store date and time, date would store date pithink

clever steppe
paper flower
#

It's helpful if you need just date

#

e.g. person's birthday

clever steppe
torn sphinx
#

Does anybody know how i can change the view of this so i can only see the databases i can access

fading patrol
#

Looks like PGadmin? I don't think that's possible, but once you save a connection you'll be able to see the one you want that way

fading patrol
gritty marlin
#

Is this correct way for searching if input matches column on db

cursor.execute("SELECT * FROM `xplt_cases` WHERE casetype='XPLT' AND `future_id` LIKE '%case%' OR `rid` LIKE '%case%'")
fallen vault
#

I have a for loop thats inputing multiple rows of data. Should i commit after each row or after the for loop?

torn sphinx
#

loop

#

faster than connecting everytime

fallen vault
#

!paste

delicate fieldBOT
#

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.

fallen vault
paper flower
#

@fallen vault It should be fine to commit after your loop

verbal steeple
#

Hi

fallen vault
paper flower
fallen vault
#

Having troubles passing the variables. For whatever reason it’s not liking the open curly bracket. What’s the “proper” way to do this? I know passing variables directly opens you up to database attacks (was only using it because the script is the only one writing and it’s not taking user entry)

paper flower
fallen vault
#

sqlite3.OperationalError: unrecognized token: “{“

paper flower
#

Can you show your query?

fallen vault
#

Currently not anymore. Kid is sleep and I’m ssh-ing on a chrome book that I can’t log into discord on lmao

#

I can however take a pic of the new error message lmao.

#

Oh wait I have an iPhone.

fallen vault
# paper flower Can you show your query?

cur.execute(" INSERT INTO countries (Country,NewConfirmed, TotalConfirmed, NewDeaths, TotalDea
ths, NewRecovered, TotalRecovered,Date) VALUES (%5,%5,%5 ,%5 ,%5 ,%5 ,%5 ,%s);""'". (Country,NewConfirmed
• TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered, Date) )

#

The 5 are actually s

#

And there’s actually three double quotes at the beginning.

paper flower
#

I'm pretty sure you should use ? to substitute your arguments?

grim vault
#

Depends on the database module, but I don't see any { inside the SQL for the error mentioned.

fallen vault
#

Oh I didn’t put the new error, sorry thought I copied it. New error was a syntax error near %.

#

Alright, been throwing things at the wall. Finally got it figured out. Fortunately my daughter just woke up so i can close DBViewer (that was going to be a problem lol)\

grim vault
#

The error mentioned sqlite3, so you should use ? instead of %s as the doctor ordered :)

fallen vault
#

Same for aiosqlite?

#

Im only using sqlite to populate the database. About to switch to aiosqlite to actually start working on the bot

#

Oh the problem was the date field btw. Tried to wrap it in str() but it did not like that. I guess ill just record the date and save it as a module variable when i pull the json from the api

grim vault
#

You paste code is Date = {str(country['Date'])} which is a set of one string entry and sqlite doesn'tknow what to do with it.

fallen vault
#

Itll be alright. I dont really need it since the api is updated once a day for the free version.

#

Thank you though.

gritty marlin
#

Does this error means I have to update my mysql? or there is something in my code.

                cursor.execute("SELECT * FROM `cases` WHERE casetype='perm' AND 'future_id'=:iarg OR rid LIKE :sarg OR rid=:sarg2 OR did LIKE :sarg OR did=:sarg2 ORDER BY casetype DESC, future_id DESC", {"iarg": int(case), "sarg": "%|" + str(case) + "|%", "sarg2": "|" + str(case)})
vapid mist
#

whats the best way to fetch with aiosqlite?

rigid mica
#

my database is bugging. I warn a user with a discord bot, and it warns twice. I only got it running once so it shouldnt happen

#

it also doesnt happen for some of the commands

#

please ping me on response, it only happens in my warn cog, all my other cogs dont go double

nova cove
vapid mist
random arch
#

Could google sheets be viable as a database?

brave bridge
paper flower
random arch
#

Its not alot of data, someone i know could use a discord bot using the google sheet

vapid mist
nova cove
vapid mist
random arch
#

Does anyone have a good tutorial for MySQL + Python?

sly garden
random arch
#

Do i need to learn every single tutorial

fading patrol
sly garden
#

CRUD operations I'd mainly focus on

hollow pike
#
CREATE TABLE users (
    user_id serial PRIMARY KEY,
    email VARCHAR ( 50 ) UNIQUE NOT NULL,
    provider VARCHAR ( 50 ) NOT NULL,
    created_on TIMESTAMP NOT NULL,
    project_id INTEGER UNIQUE,
    CONSTRAINT fk_project
      FOREIGN KEY(project_id) 
      REFERENCES projects(id)
);

CREATE TABLE projects (
    id serial PRIMARY KEY,
    project_id INTEGER UNIQUE NOT NULL,
    name VARCHAR ( 50 ) NOT NULL,
    description VARCHAR ( 50 ) NOT NULL,
    difficulty VARCHAR ( 50 ) NOT NULL,
    repo_url VARCHAR ( 50 ) NOT NULL,
    created_on TIMESTAMP NOT NULL
);

I'm getting a Schema Error: error: relation "projects" does not exist error. Any Ideas? Thanks

fleet ibex
#

prolly would be projects_id(id) .. but i really cant say im 100 on that

nova cove
#

why do you have another project_id in the second table

hollow pike
#

I had to create the projects table before the users table...thats what the error was

nova cove
#

yeah thats what i thought the issue was

balmy folio
#

I'm having trouble translating specific joins from c# to Django. For example I see this kind of code:

db.someTable1.Join(
  db.someTable2,
  l => new { l.machineNumber, l.clientId },
  r => new { r.machineNumber, r.clientId },
  (l, r) => new 
  {
    historyId = l.rd,
    l.machineNumber,
    l.clientId,
    ...
  }
)

For reference, the tables do not contain any foreign keys and I am not able to modify the database.

How would I make the above in Django?

My attempt is

modelForTable1.objects.prefetch_related(machinenumber__machinenumberids_set', 'clientid__machinenumberids_set')

Am I on the right track?

solemn root
#

Hi, I've been working on a new command for my bot, but the code doesn't execute even though it's reachable. here's the structure of the command and the pastebin of my code:

command structure:
~pay [amt] <user mention>

Pastebin:
https://pastebin.com/243TeWtb
The print statements in my code are for debugging, but even they don't print. I don't know where I went wrong. The code complies, but my database doesn't register the changes even though all the database statements are valid & executable.

balmy folio
solemn root
verbal lava
#

MongoDB py for user in collection: if user["username"] == username: return "UsernameAlreadyExistsError"`````` return collection_class(self.database, self.name + '.' + name, TypeError: can only concatenate str (not "int") to str

burnt turret
#

show more of your code

#

the error and the code don't match

paper flower
grim vault
#
>>> 1 + "1"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> "1" + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>>```
so it must be `name`
long summit
#

I don't know where is appropriate to give tips like this but I saw this in a merge request today and thought I'd share. It is pretty simple yet useful to know if you want to avoid nested loops while getting field values from query strings.

Double for loop:

list_to_fill = []
    for item in query_string:
        for item_field_value in item.item_field.all():
            list_to_fill.append((item_field_value.type, item_field_value))

Using .values_list() - Could use .values() too, It gives you a list of dictionaries, if you prefer to reference things by their name/location rather than index:

list_to_fill = query_string.values_list(
    'item_field__type',
    'item_field__item_field_value',
)
frigid berry
#

how do i check if a table exists in sqlite3?

#

I looked and saw in a video a code that looked like this:

"SELECT name FROM sqlite3_master WHERE type='table' AND name='yourtablename'"```
#

problem is that I'm bugged in this, I put in place of "yourtablename" the name of my table and went to check, but it wasn't right

frigid berry
grim vault
#

No, you select the name, so it should be empty or the name of the table (and it should be sqlite_master without the 3).

#

If you want a number use count(*)

#

!e

import sqlite3

conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE test_table(test_id INTEGER PRIMARY KEY)")

sql_name = "SELECT name FROM sqlite_master WHERE type = ? AND name = ?"
sql_count = "SELECT COUNT(*) FROM sqlite_master WHERE type = ? AND name = ?"

for bind in (('table', 'test_table'), ('table', 'unknown')):
    print(bind)
    row = conn.execute(sql_name, bind).fetchone()
    print(f"\tsql_name -> {row = }")
    row = conn.execute(sql_count, bind).fetchone()
    print(f"\tsql_count -> {row = }")
delicate fieldBOT
#

@grim vault :white_check_mark: Your eval job has completed with return code 0.

001 | ('table', 'test_table')
002 | 	sql_name -> row = ('test_table',)
003 | 	sql_count -> row = (1,)
004 | ('table', 'unknown')
005 | 	sql_name -> row = None
006 | 	sql_count -> row = (0,)
fallen vault
#

Im having binding troubles. py @bot.command() async def a(ctx, *, location): db = await aiosqlite.connect('./covid.db') cursor = await db.execute("SELECT * FROM countries WHERE Country='?'",location) row = await cursor.fetchone() print(row) await db.close()
This give the following error: ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 4 supplied

The value being passed is 'Chad'

fallen vault
#

Im just reading from the database, no need to commit. Right?

willow rain
#

What is happening after you run it?

fallen vault
#

i use the command and it gives me the ProgrammingError code. When i had the test message that was sending in my server so its after accessing the database.

willow rain
#

try

cursor = await db.execute(f"SELECT * FROM countries WHERE Country = {location}")
fallen vault
#

Sorry, after the connect command. Should be more specific lol

#

One second

willow rain
#

and

row = await cursor.fetchall()

because anyways only 1 value is being called right

grim vault
#

The second parameter must be a iterable (list or tuple):

cursor = await db.execute("SELECT * FROM countries WHERE Country = ?", [location])```
fallen vault
grim vault
#

and no ' for the ?

willow rain
#

It selected everything from countries table where country name was Chad

#

turns out no Chad

grim vault
willow rain
#

and yea the quotes

fallen vault
willow rain
#

Wait let me give you an example

fallen vault
willow rain
#
cursor.execute(
        f"SELECT wallet_amt FROM account_table WHERE user_id = {ctx.author.id};"
    )
    db.commit()
    result_w = str(cursor.fetchone())
    result_w = str(result_w).strip('[](),')
    result_w = int(result_w)

This works

#

Its just that I commited.

#

and turned the value into a string nothing else

#

You can do the same too

fallen vault
#

This is the database. Chad was just the least amount of character for testing lol

willow rain
#

Ok just tell what you are trying to do

#

it will be better

#

Ok wait hold up

#

cursor is not supposed to be that

#
cursor = db.cursor()
grim vault
willow rain
#
@bot.command()
async def a(ctx, *, location):
    db = await aiosqlite.connect('./covid.db')
    cursor = db.cursor()
    await db.execute(f"SELECT * FROM countries WHERE Country={location};")
    db.commit()
    row = await cursor.fetchall()
    row = str(row).strip("{}[](),")
    print(f"{row}")
    await db.close()

raw may not work but try it

grim vault
#

You don't need to commit a select, you didn't change the data.

fallen vault
willow rain
#

I have a habit of commiting whenever I use SQL xD

grim vault
#

That: row = str(row).strip("{}[](),") is not good. row is a tuple, just use the index to get the column you want!

sterile pelican
#

How can I make it so if the value becomes negative the database raises an exception? For example, I need it to raise the exception if the money gets negative on update

UPDATE some_table SET money = money - 100 WHERE id = 1234```
#

Postgres

proven arrow
#

@sterile pelican You can add a check constraint if you want to handle this on the DB level.

fleet ibex
#

or look up custom error handling

sterile pelican
proven arrow
grim vault
# sterile pelican Could you show an example please

!e

import sqlite3

conn = sqlite3.connect(":memory:")
conn.execute(
    "CREATE TABLE some_table("
       " id INTEGER PRIMARY KEY,"
       " money INTEGER DEFAULT 0 CHECK(money >= 0))"
    )
conn.execute("INSERT INTO some_table VALUES(1234, 50)")
conn.execute("UPDATE some_table SET money = money - 100 WHERE id = 1234")
delicate fieldBOT
#

@grim vault :x: Your eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "<string>", line 10, in <module>
003 | sqlite3.IntegrityError: CHECK constraint failed: some_table
glossy coral
#

I get this error when trying to make an AI id

nova cove
#

Where is the query that creates the table

glossy coral
#

didnt do it via query just did it via the ui

nova cove
#

you created the table in SQLite browser?

glossy coral
#

on sqlite

#

app

nova cove
#

That’s the table with the issue?

proven arrow
#

@glossy coral Which column are you trying to make auto increment for?

glossy coral
glossy coral
proven arrow
nova cove
#

I’m guessing you have an entry into the AI column that isn’t an integer

proven arrow
#

The error message is pretty clear

nova cove
#

^

glossy coral
#

sorted

#

ty

glossy coral
#
    def edit_employee(self):
        while True:
            try:
                verify_code = int(input('Admin code required (To exit type 0): '))
                if verify_code == self.code:
                    user_id = int(input('Enter ID: '))
                    user_decide = input(f"""*To exit type 'done'*
ENTER WHICH INFORMATION YOU WANT TO CHANGE FROM THE FOLLOWING:
Name, Surname, Age, EMail, Number, Country, City, Role
: """)
                    varchar = ['name', 'surname', 'email', 'country', 'city', 'role']
                    integer = ['age', 'number']
                    if user_decide.lower() in varchar:
                        new_value = input(f'Enter new {user_decide}: ')
                        query = f'UPDATE employees SET {0} = ? WHERE id = ?'.format(user_decide)
                        self.mycursor.execute(query, (new_value, user_id))
                        self.connection.commit()
                        print('Information updated.')
                    elif user_decide in integer:
                        new_value = input(f'Enter new {user_decide}: ')
                        query = f"UPDATE employee SET {user_decide}='{new_value}' WHERE id={user_id}"
                        self.mycursor.execute(query)
                        self.connection.commit()
                        print('Information updated.')
                    elif user_decide == 'done': break
                    else: print('Incorrect response, ')
                else:
                    print('Incorrect code.')
            except Error:
                print(f'Error: {Error}')```
#

acc

#

!past

delicate fieldBOT
#

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.

glossy coral
#

I have the problem where if I enter 'name' to update in sql i get the rror

#

Error: <class 'sqlite3.Error'>

upper rampart
#
   def storedata(self):
        decide = input("Data: ").lower()
        if decide == "name":
            psn = input("PSN: ")
            psn = str(psn)
            cursor.execute(f"INSERT INTO logs (PSN) VALUES({psn})")
            connection.commit()
#

Every time I try to add a string to my table in phpmyadmin, I get this error

#

I have the value set as VARCHAR in the table also

#

Can someone help?

grand ice
#

Value of a string?

fading patrol
grand ice
#

Value usually being an evaluation of numeric string

sinful rivet
#

ok, is there any way to add a table name from a variable? ||i bet not understand, so here's code from my understanding||

def add_code(code):
    code = str(code)
    cur.execute("INSERT INTO (codes) VALUES (?)",(code,))
    cur.execute('''CREATE TABLE IF NOT EXISTS ?
             (
             ID             TEXT    NOT NULL
             );''',(code,))
    conn.commit()
#

:l

proven arrow
sinful rivet
#

noooooo

#

ok

proven arrow
#

Generally this kind of thing is not necessary, and the only way to do such a thing would be to build the query yourself after sanitising input.

proven arrow
# sinful rivet noooooo

Why do you need to dynamically create tables? Maybe there is a better way to achieve what you are doing.

sinful rivet
proven arrow
#

Yeah that’s bad design.

sinful rivet
#

any idea what should i do instead :V

proven arrow
#

Codes should be stored in a single table.

#

You can have two columns. code and status. Where status is the value of whether the code has been claimed or not.

sinful rivet
proven arrow
#

This approach allows you to add additional data to the code claim process as well. Such as when it was claimed and by who.

proven arrow
sinful rivet
#

new question, is it possible to add a list into a row

proven arrow
sinful rivet
proven arrow
#

You can look up database normalisation.

sinful rivet
proven arrow
#

Database normalization is the process of structuring a database, usually a relational database, in accordance with a series of so-called normal forms in order to reduce data redundancy and improve data integrity. It was first proposed by Edgar F. Codd as part of his relational model.
Normalization entails organizing the columns (attributes) and ...

sinful rivet
#

k ty

sinful rivet
proven arrow
proven arrow
# sinful rivet need it

So do it the correct way. 🤷‍♂️ You may think it’s silly but this is how databases were designed to be used. Don’t try to fight your database, otherwise maintaining and writing queries later will just be a pain.

sinful rivet
#

so

#

how?

proven arrow
#

You need to create a many to many relationship, between your users, and codes table. For this an extra table is required, which allows you to keep a map of which codes have been claimed by which users.

paper flower
#

It would be one to many relationship if single code can't be claimed by multiple users

#
Users
id | username
1    Doctor
2    Soap
Codes
id | code
1    abc
2    abc2
Claimed Codes
user_id | code_id
1         1
1         2

You would have 3 tables like these, users and codes tables and one to associate them in some way, in this example it's who clamed which code

#

I claimed both codes 😃

#
create table users(
  id serial primary key,
  username varchar(255) not null
);
create table codes(
  id serial primary key,
  code varchar(255) not null
);
create table claimed_codes(
  user_id integer references users(id),
  code_id integer references codes(id)
);

This is a many to many relationship, but if you want code to only be claimed by single user, then you could add user_id column to codes table instead and get rid of claimed_codes table (you don't need a secondary table in one to many relationship)

#

@sinful rivet

runic coyote
#

can someone ELI5 ORM?

paper flower
runic coyote
#

explain like i'm 5

thorn rivet
#

i need some help idk the issue lol this is my line of code

watchdb.search(User.avatarid == avatarid and User.userid == ctx.author.id)

i thought this would give all results that meet both conditions but it doesnt it returns results with same userid but different avatarids and not sure why also im using tinydb

#

finally figured it out was

watchdb.search((User.avatarid == avatarid) & (User.userid == ctx.author.id))

i guess both makes since was pulling my hair out lol

paper flower
#

Also helps you to query database from your language without using sql

runic coyote
#

So it just a layer for easier use?

#

In its core it use raw query?

paper flower
#

Yep, it would translate everything to sql anyway

#

But if you're selecting something from your database it could map it to python objects

#
class User(Base):
    __tablename__ = "users"

    id: Mapped[uuid.UUID] = Column(
        UUID(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
    )
    username: Mapped[str] = Column(String(length=255), nullable=False)

Doing

select(User).filter(User.username == "Doctor")

would select all users with username "Doctor", it would send sql like this to the database:

select users.id, users.username from users where users.username = 'Doctor';

But it would also map results to instances of User class 😃

runic coyote
#

so for more freedom you would use raw query rather than orm?

paper flower
#

Some ORMs limit you on what sql query can you create from python, some don't

#

Django ORM is quite limiting imo, sqlalchemy isn't

runic coyote
#

thanks that definitely clears something up

subtle basin
#

hello, anyone uses TortoiseORM?

#

does any ORM allows to bind multiple models to the same one table?

paper flower
#

SQLAlchemy could do this, not sure about tortoise 🤔 You're looking for single table inheritance

#

Doesn't seem like TortoiseORM supports model inheritance/polymorphism if i'm not missing something

subtle basin
subtle basin
paper flower
#

You're having troubles saving them?

#

Sorry, i don't follow really 😅

#

Could you maybe show your models?

subtle basin
#

one row -> one object, and bound to ORM Model, so, if 5 objects have same model they are aggreagated and passes as values

paper flower
#

Mhm

#

If i simplify (so i can give an example):
You could have multiple Post models and m-m relationshop to Tag model?

#

And posts 1...5 have the same tag, right?

gusty path
#

is it worth compressing data before putting it in the database?

paper flower
#

Usually no

gusty path
#

like a 1080x1080 image

paper flower
#

Don't store images in the database 👍

#

Also they're already compressed

gusty path
#

or a really long json

paper flower
#

Don't store json in databases 😅
If you need to postgres has bson type - binary json

#

Should be pretty efficient, i didn't use it tho

gusty path
#

oh cool thanks

paper flower
#

Maybe, i don't really remember what it's called 🤔

#

Usually you can normalize your data to not store json in your tables, and storing images is just a bad idea

gusty path
paper flower
#

Just saying 😅

gusty path
#

my json goes 2 or 3 levels deep so I cant really normalize it without inefficiently using more rows

paper flower
#

If it's well structured i'd say just normalize it

gusty path
#

i wont ever need to analyze any data with select statements (which is the point of normalizing) so its not worth it

paper flower
#

If you want to just store i'd say you can go with json

sullen sky
#

im trying to find difference between dates in sql

#

but it gives me this error

#

anyone know why?

celest zodiac
#

@sullen sky apparently it's just two parameters
SELECT DATEDIFF("2017-06-25", "2017-06-15");

misty vigil
#

^

misty vigil
#

try removing the Day part at the start

sullen sky
#

every source online has the first parameter for some reason

misty vigil
#

ah it might just be an example

sullen sky
#

hmm

#

anyway, thank you very much for your time! @celest zodiac @misty vigil

misty vigil
#

"funny valentine theme" so youre a jojo fan huh

#

glad to help :D

sullen sky
misty vigil
#

but does the code work though?

sullen sky
misty vigil
#

nice!

sullen sky
#

here it says its a parameter

#

weird

misty vigil
#

great to hear that

#

ah

#

w3schools' resources are usually outdated, that might be why

sullen sky
#

ah ok

#

thanks

grim vault
#

MySQL != SQLServer

rigid mica
#

is there a way to set a variable in a cog available in another cog?

fleet ibex
#

How would a query look if I need to sort a column by max, and get everything after the nth max .. so like,
100
100
90
90
80 everything 80 and after to 0

#

the column will have many repeated values of say like 81-100

#

i think i got it, just have to use a high limit to get offset to besure I catch everything

paper flower
fleet ibex
#

i fought with it, i could not find a reference that said I had to use it with limit.. was just always referenced with limit in everything I read.. so i guess you cant.. :(

paper flower
#

You can use offset and limit independently

#

e.g.
select * from users offset 42;
select * from users limit 5;

fleet ibex
#

weird i tried and always got an error near offset.. I will examine it again when i get a chance

rigid mica
#

@paper flower I'm not sure if you know (also sorry for the ping) but how do i for example make something that just adds a value to a row that already exists?
for example on_guild_join I add prefix and guild id, but I would also like to store 2 channel ids in there. But in the same row

#

i got a command for adding btw

paper flower
rigid mica
#

hold up

paper flower
#

Why do you need to store channel ids?

rigid mica
#
    @commands.Cog.listener()
    async def on_guild_join(self,guild):
        await db.execute('''INSERT INTO setup (guild, prefix) VALUES ($1, $2)''', guild.id, '!')
        print(f'added {guild} to database')

    @commands.command()
    async def welcome(self,ctx,channel : discord.TextChannel = None):
        if channel is not None:
            await db.execute('''UPDATE setup SET welcome = $1 WHERE guild = $2''', channel.id, ctx.guild.id)
            welcome = await db.fetch('''SELECT welcome FROM setup WHERE guild = $1''', ctx.guild.id)
            channel1 = welcome["welcome"]
            await channel1.send('Channel configured as welcome channel')```
rigid mica
paper flower
#

You can get server of the message and look up the prefix if that's what you want

#

What's stopping you from adding welcome_channel column?

rigid mica
#

but I dont feel like its actually adding things to my database

#

maybe an issue of dc atm

paper flower
#

Maybe 🙂

rigid mica
#

it didnt register on guild join

paper flower
#

You didn't commit btw

rigid mica
#

wdym commit?

paper flower
#

What connector are you using?

#

aiosqlite?

rigid mica
#
async def create_db_pool():
    global db
    db = await asyncpg.create_pool(database = "", user = "", password = "")
    await db.execute('''
        CREATE TABLE IF NOT EXISTS setup(
            guild bigint,
            prefix text,
            welcome bigint
        )''')```
#

asyncpg

paper flower
#

I see

rigid mica
#

but doctor

#

would this code work?

#
    @commands.command()
    async def welcome(self,ctx,channel : discord.TextChannel = None):
        if channel is not None:
            await db.execute('''UPDATE setup SET welcome = $1 WHERE guild = $2''', channel.id, ctx.guild.id)
            welcome = await db.fetch('''SELECT welcome FROM setup WHERE guild = $1''', ctx.guild.id)
            channel1 = welcome["welcome"]
            await channel1.send('Channel configured as welcome channel')```
paper flower
#

Seems like you don't have to issue commit in asyncpg 🤔

rigid mica
#

bc I saw someone who said I used a channel argument instead of a channel object

#

so I was kinda confused

paper flower
#

Looks fine to me, maybe channel is None?

rigid mica
#

It shouldnt

#

I mention it

#

but my bot doesnt want to respond atm

paper flower
#

Debug it 😉

rigid mica
#

oh I lost connection to dc

paper flower
#

Go do other things, you would have troubles developing your bot atm

rigid mica
#

true

#
    @commands.Cog.listener()
    async def on_guild_join(self,guild):
        await db.execute('''INSERT INTO setup (guild, prefix) VALUES ($1, $2)''', guild.id, '!')
        print(f'added {guild} to database')

    @commands.command()
    async def welcome(self,ctx,channel : discord.TextChannel = None):
        if channel is not None:
            await db.execute('''UPDATE setup SET welcome = $1 WHERE guild = $2''', channel.id, ctx.guild.id)
            welcome = await db.fetch('''SELECT welcome FROM setup WHERE guild = $1''', ctx.guild.id)
            channel1 = welcome["welcome"]
            await channel.send('Channel configured as welcome channel')``` but like could u take a look at it? u think it would work this way?
#

alright the on_guild_join seems to be working

#

@paper flower error: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: list indices must be integers or slices, not str

#

in here

#
        @commands.command()
        async def welcome(self,ctx,channel : discord.TextChannel = None):
            if channel is not None:
                await db.execute('''UPDATE setup SET welcome = $1 WHERE guild = $2''', channel.id, ctx.guild.id)
                welcome = await db.fetch('''SELECT welcome FROM setup WHERE guild = $1''', ctx.guild.id)
                channel1 = welcome["welcome"]
                await channel.send('Channel configured as welcome channel')```
#

alright, it seems to be working with setting the id in the database

paper flower
#

i guess db.fetch returns list and not a dictionary

rigid mica
#

no more coding for today

#

got my bot running, but is offline

#

bot will have trouble logging in xd_pray

#

and sending messages

rigid mica
#

and here i use a dictionary too

paper flower
#

fetch is not fetchrow

rigid mica
#

truee

#

thats the reason

#

if u fetch a row, u get multiple responses right?

abstract pivot
#

when should you create a transaction from an acquired connection from an asyncpg.Pool/what does it do?

rigid mica
#

I'm not that experienced with databases, could u explain it a little more easier?

abstract pivot
#

I think you misunderstood, I wasn't responding to your question

harsh pulsar
# abstract pivot when should you create a transaction from an acquired connection from an `asyncp...

creating a transaction sets up a reset point. if something goes wrong in the code, you can handle it by "rolling back" the transaction, causing the changes in the transaction to be discarded. when you are satisfied that the changes have proceeded correctly, you can save them to the database by "committing" the transaction.

starting a transaction is like opening a new document; committing the transaction is like saving the document; rolling back the transaction is like closing the document without saving.

#

a transaction also has the property that if anything in the transaction results in an error, then the entire transaction is rolled back. this prevents your database from getting stuck in a bad state with half-finished operations

#

the wikipedia page has a decent explanation https://en.wikipedia.org/wiki/Database_transaction

A database transaction symbolizes a unit of work performed within a database management system (or similar system) against a database, and treated in a coherent and reliable way independent of other transactions. A transaction generally represents any change in a database. Transactions in a database environment have two main purposes:

To provid...

knotty wharf
#

I have read on w3schools about prevent SQL Injection
'he mysql.connector module has methods to escape query values:
Escape query values by using the placholder WHERE first_name = %s method

I am using MariaDB, for the WHERE statement they advise on their website to use WHERE first_name = ?
does the '?' also prevent from SQL injection or should I use '%s' ?

abstract pivot
#

or are there more specific cases where they should be used

harsh pulsar
knotty wharf
#

@harsh pulsar okay thank you!

knotty wharf
#

By the way, I already created my database using SQL
But now I want a general user interface (GUI) to do queries like SELECT/INSERT/UPDATE/DELETE
Where should I start? I don’t need it to be really fancy like a website. But just a GUI where you can input some queries and looks up for things.

nova cove
#

as in pressing a button executes a query

knotty wharf
#

I don't get it @nova cove 😢

nova cove
#

For example, press a random button and it fetches data from the db through a query

knotty wharf
#

oh ok, so it would be fine if I'm using TKinter for that?

nova cove
#

it should be fine, but I have never used tkinter before so idk about how it works with databases

knotty wharf
#

what would you advise me to use ? what are you using ?

nova cove
#

wdym what am I using

knotty wharf
#

you are working with database right?

#

are you using any user interface ?

nova cove
#

I am making a discord bot where I use a database

paper flower
nova cove
#

I have not implemented databases and worked wit them in websites yet, which I plan on doing

knotty wharf
#

oh alright

#

thank you @paper flower I'll check into it, I am currently using MariaDB

paper flower
#

I often use db browser built-in into pycharm

random arch
#

Should I learn MongoDB over MySQL?

paper flower
random arch
paper flower
#

I'd say SQL, it's wide-spread, also learning nosql after sql shouldn't be hard

random arch
#

is there a way to see an SQL database as i would look at a spreadsheet like excel or google sheets?

paper flower
#

Yes, there are tools like sqlitebrowser and pgAdmin, also pycharm professional should have a db browser

#

VSCode should have an extension too

torn sphinx
#

@random arch Check out mysql2excel if you want a gentle intro into what you're used to vs sql.

solemn root
#

Hi, I've been working on a new command for my bot, but the code doesn't execute even though it's reachable. here's the structure of the command and the pastebin of my code:

command structure:
~pay [amt] <user mention>

Pastebin:
https://pastebin.com/S5kDGx1i
The print statements in my code are for debugging, but even they don't print. I don't know where I went wrong. The code complies, but my database doesn't register the changes even though all the database statements are valid & executable.

rigid mica
#

How do I pass a variable of my database to another cog?

proven arrow
rigid mica
#

asyncpg.exceptions.TooManyConnectionsError: sorry, too many clients already

#

can i close a client?

delicate fieldBOT
#

Hey @shut zephyr!

It looks like you tried to attach file type(s) that we do not allow (.pdf). We currently allow the following file types: .gif, .jpg, .jpeg, .mov, .mp4, .mpg, .png, .mp3, .wav, .ogg, .webm, .webp, .flac, .m4a, .csv, .json.

Feel free to ask in #community-meta if you think this is a mistake.

proven arrow
proven arrow
rigid mica
#

but I call it multiple times

#

its not just the loop

#

for example

#

    #pingreact
    @commands.Cog.listener("on_message")
    async def pingreact(self,message):
        prfx = await db.fetchrow('''SELECT prefix FROM setup WHERE guild = $1''', message.guild.id)
        prefix = prfx["prefix"]
        if re.fullmatch(rf"<@!?{self.bot.user.id}>", message.content):
            await message.channel.send(f"My prefix here is `{prefix}`")
            return```
#

in this cmd, I take the prefix out

#

or should I just add this to the loop?

proven arrow
#

Not sure I follow. How is this related to creating a pool?

high geyser
#

I'm planning to move to postgreSQL from mysql because it natively supports uuids

#

I want to use uuids, to expose it to the end user. I dont want to show the autoincrement pk to the end user as it is easy to guess

#

So I'm thinking of having an int pk and uuid to expose to the client.

#

How would I handle foreign keys here? Would foreign keys be uuids or ints?

high geyser
#

So should I be using uuids here?

#

Does that have any performance hits?

rigid mica
# proven arrow Not sure I follow. How is this related to creating a pool?

Traceback (most recent call last):
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 351, in run_event
await coro(*args, **kwargs)
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 1034, in on_message
await self.process_commands(message)
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 1030, in process_commands
ctx = await self.get_context(message)
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 940, in get_context
prefix = await self.get_prefix(message)
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 885, in get_prefix
ret = await discord.utils.maybe_coroutine(prefix, self, message)
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py", line 514, in maybe_coroutine
return await value
File "c:\Users\bert\Downloads\discord bots\Bertie's discord bots\bertie's discord bots.py", line 20, in get_prefix
db = await asyncpg.create_pool(database = "Bertie", user = "postgres", password = "B3rtm0r3@u!!")
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\asyncpg\pool.py", line 413, in async__init

await self._initialize()
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\asyncpg\pool.py", line 441, in _initialize
await first_ch.connect()
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\asyncpg\pool.py", line 133, in connect
self._con = await self._pool._get_new_connection()
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\asyncpg\pool.py", line 511, in _get_new_connection
con = await connection.connect(
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\asyncpg\connection.py", line 2085, in connect
return await connect_utils._connect(
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\asyncpg\connect_utils.py", line 881, in _connect
return await _connect_addr(
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\asyncpg\connect_utils.py", line 781, in _connect_addr
return await __connect_addr(params, timeout, True, *args)
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\asyncpg\connect_utils.py", line 831, in __connect_addr
await compat.wait_for(connected, timeout=timeout)
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\site-packages\asyncpg\compat.py", line 66, in wait_for
return await asyncio.wait_for(fut, timeout)
File "C:\Users\bert\AppData\Local\Programs\Python\Python38\lib\asyncio\tasks.py", line 483, in wait_for
return fut.result()
asyncpg.exceptions.TooManyConnectionsError: sorry, too many clients already

rigid mica
#

I cant run my commands bc of that error

#

I use my database a lot

random bobcat
#

anybody there

rigid mica
#

me

random bobcat
#

THANK YOU

#

OK

#

I

#

need

#

halp

#

with mongo db

rigid mica
#

...

#

I dont know mongo

oak oyster
#

Don't flood

random bobcat
#

):

oak oyster
#

so ask away

random bobcat
#

ok

oak oyster
#

sooo are you gonna ask your question?

random bobcat
#

yes

#

i need halp with my mongo db setup for my bot

random bobcat
#

THANK YOU

oak oyster
#

soo what part do you need help with in the setup?

random bobcat
#

like i have an account

#

and i need to make a cluster

oak oyster
#

mhm?

random bobcat
#

and the part where i have to linkit to thecode

#

is

#

kinda confusing

oak oyster
random bobcat
#

yes

oak oyster
#

oh

proven arrow
oak oyster
#

just copy the link mongo gives you, and paste it where you need to paste it

random bobcat
#

oki gib a min

#

ryt now i need to go to shared ryt?

#

or

nova cove
random bobcat
#

im a peasant basically so i cant use moni FOR NOW

paper flower
oak oyster
paper flower
#

32/64 bits vs 128 bits is not that big of a difference, you're already probably storing string data in your columns too, it's A LOT of bytes

nova cove
#

mhmm

random bobcat
#

OK GOT IT

oak oyster
#

yes

random bobcat
#

okk

#

now i need to add ip address ryt?

oak oyster
#

yes

random bobcat
#

now?

oak oyster
#

click connect

high geyser
random bobcat
#

ok

#

@oak oyster

#

halp

oak oyster
random bobcat
#

okk

#

which version

#

3.12?

oak oyster
#

3.6 or later

random bobcat
#

okk

#

now

oak oyster
# random bobcat

copy the url, replace username/password with your username/password and myFirstDatabase with ur database name

random bobcat
#

okk

#

then

#

okk

#

how do i find the database name

#

i kept literally nothing

#

oh wait

#

nvm

#

its cluster0

oak oyster
#

no thats your cluster name

random bobcat
#

umm

#

thenn....

oak oyster
#
cluster: #mongo format
  database:
    collections
``` you have to make a database
random bobcat
#

hmm

proven arrow
# high geyser How do you do that?

The column type would be bigint. https://en.m.wikipedia.org/wiki/Snowflake_ID

However, another alternative approach is to stick with your current method of doing it but to add a new column for uuid/snowflake id which acts as a slug. This would cover your security concerns, and also not have that much of an impact on performance.

Snowflake IDs, or snowflakes, are a form of unique identifier used in distributed computing. The format was created by Twitter and is used for the IDs of tweets. The format has been adopted by other companies, including Discord, and Instagram, which uses a modified version.

oak oyster
high geyser
random bobcat
#

its showing create cluster not database

proven arrow
high geyser
random bobcat
#

;-;

oak oyster
random bobcat
#

okk

oak oyster
#

then click Add My Own Data

random bobcat
#

theres no database or collections

#

okk

#

which is greater

oak oyster
#

it will prompt you for a database & collection name

random bobcat
#

database or collection

oak oyster
random bobcat
#

like

#

which is inside which

nova cove
#

collection stores db's

random bobcat
#

is collection inside database

#

okk

#

ty

#

DONE

oak oyster
#

after you've done that, replace myFirstDatabase from the uri earlier to the collection/db name

random bobcat
#

yes i did it

#

umm

random bobcat
#

sry for so much qns im new ;-;

oak oyster
#

but i used my collection name

random bobcat
#

hmm

#

okk

#

then ill use it

#

now how to link it

#

i feel like its the other way

#

like

nova cove
#

there should be a place to get the URL with the info

#

i haven't used mongo in a while lmao

random bobcat
#

oof

#

oki lemme see tuto

nova cove
#

lemm ecreate a db rq

random bobcat
#

okii

oak oyster
oak oyster
random bobcat
#

okk

nova cove
random bobcat
#

okk

proven arrow
# high geyser So I should use slugs as foreign keys?

No. Slugs are on the parent table themselves. It’s only used as an alias to the primary key. What is the current issue you have with using the primary key value? What resource do you not want the user to easily guess?

high geyser
#

If I have a todos table, then todos.user_id returns the users int pk

solemn root
proven arrow
# high geyser If I have a todos table, then todos.user_id returns the users int pk

You should still be enforcing some sort of access control policy application side as well to see if only the authenticated user can access their their posts. As for people guessing user ids it shouldn’t really matter then as you are controlling that each user can only access or manipulate their own items, which you should be doing anyways. If you really want to obscure the ids then your options are two use a uuid/snowflake as the primary/foreign key.

Regarding slugs the alternative approach I gave you, it would mean you have that you have to make a lookup on the users table with the slug field. You seem to be getting confused on how they work. This should help you, https://stackoverflow.com/questions/19335215/what-is-a-slug

carmine anvil
#
    def registration_stat(self):
        if (self.registration_exp - date.today()) < 14:
            return "Soon"
        elif (self.registration_exp - date.today()) < 0:
            return "Expired"
        else:
            return "Good"```
Would this be an appropriate use of the hybrid property? or is there a better way to check if a datefield has expired
paper flower
#

@nova cove linked you the documentation 😉

nova cove
#

👍

carmine anvil
paper flower
#

Right

#

Like in any python class

high geyser
#

So I think I'll use uuids as primary keys

carmine anvil
paper flower
abstract pivot
carmine anvil
carmine anvil
#

or rather how do i create a function that can be applied to any field in my model

woeful torrent
#

Is there a more efficient way to getting the code of a query with a limit than having to add the count to every row with func.count().over()?

#

Ideally I'd like the count and the queried columns in separate rows

#

but the only way I can think of to do this is to run the query twice - once for counting and once to get the actual columns that are counted

torn sphinx
#

Does anyone know how I can do a perms check with the database, I have a command to add a person in the premium category and I would like her to use only those commands. I use mongodb

carmine anvil
#

nvm @classmethod was the answer

errant ridge
#

you guys are about to hear the simplest question ever

#

how do i access a .db file using sqlite3 on terminal

#

its just not letting me

#

am i being extra thick or

grim vault
woeful torrent
#

In general in the terminal if you want to use a path with space you need to either 1. add quotation marks around your path or 2. escape spaces and special characters with backspace

#

Otherwise the space will be interpreted as an argument separator

hollow pike
#

Every time a new user is inserted into the "users" table, I want to add a new row to the "profile" table also but doesnt seem to be working

grim vault
hollow pike
#

@grim vault yea that was the issue. thanks

vapid solstice
#

I'm using postgresql, and I have a query: SELECT * FROM banks WHERE id = (SELECT bank_id FROM balances WHERE user_id = $1 LIMIT 1) LIMIT 1
So as you can see in the where clause there is a check to another select clause. Will the bank_id be selected again with every row (which would obviously be inefficient and slow) or will the bank_id be processed once in the beginning (by selecting the value) and then have that value used for each row (without re-selecting) ?

proven arrow
vapid mist
#

await cursor.execute(f'UPDATE invite_cache SET invite_uses = {new_invites} WHERE invite_code = {cached_invite}')

sqlite3.OperationalError: no such column: MYGFVjHUq9

grim vault
vapid mist
#

ah thats how you do it, with asyncpg i did $1 $2, didnt know how to in sqlite

nova cove
#

?

#

Is the placeholder

#

in SQLite

vapid mist
#

👍

#

works now

#

ty

gusty mulch
#

If I'm using serial datatype on my primary key of one of db tables, how would I call that in my code? Normally, as i'm writing a discord bot, I would make the guild ID the primary key and pull the data for that table that way but since i'm using serial for this particular table I'm not quite sure how i call it in my code

fading patrol
gusty mulch
#

this is my sql for the tablesql -- Table: public.suggestions -- DROP TABLE IF EXISTS public.suggestions; CREATE TABLE IF NOT EXISTS public.suggestions( suggestion integer NOT NULL DEFAULT nextval('suggestions_suggestion_seq'::regclass), guild bigint, original bigint, post bigint, member bigint, message text COLLATE pg_catalog."default", CONSTRAINT suggestions_pkey PRIMARY KEY (suggestion) )and this my function of how the bot access's the tablepy async def get_sug(self, suggestion): async with self.pool.acquire() as conn: res = await conn.fetchrow("SELECT * FROM suggestions WHERE suggestion=$1",) if not res: res = await conn.fetchrow("INSERT INTO suggestions VALUES ($1, $2, $3, $4, $5, $6) RETURNING *", suggestion, None, None, None, None, None) return resand this is how i'm writing info to the tablepy async with self.bot.pool.acquire() as conn: await conn.execute("UPDATE suggestions SET ctx.guild = $1, ctx.message.id = $2, post.id=$3, ctx.author.id = $4, message = $5 where suggestion = $3", ctx.guild.id, ctx.message.id, post.id, ctx.author.id, message, serial)and this is the full command: https://sourceb.in/Rt6VRZJZBA

fading patrol
gusty mulch
#

altho I guess i could store the latest serial in my data table and fetch it that way but with that way I can only get the latest suggestion

torn sphinx
#

im trying to use pymongo but it says ModuleNotFoundError: No module named 'pymongo' even when i already installed it with pip install pymongo

fading patrol
torn sphinx
winged sand
#

guys can you help me with sql

#

SELECT * FROM linjat WHERE numero LIKE ("1[0-9][0-9]%");

#

i want to select all with first number 1 and 2 0-9 and third 0-9 and fourth being whatever character

#

but its not working

#

so that it would select this for example : 123A
but not select this 12A

storm mauve
#

you can use . or \w for "anything" or "any word character", respectively - that is, LIKE ("1[0-9][0-9].%");

#

shouldn't you use single quotes though? (not sure)

winged sand
#

i dont know about quotes they should work with single or double

#

thanks bro

torn sphinx
#

Anyone able to help me with a simple dataframe issue or should I post in one of the help channels?

uncut canyon
#

can anyone help me out withgoogle spreadsheets?

nimble lava
#

hey i recently reset my pc so my mysql tables and stuff got deleted but i have the ibd files saved
how can i recover the data ?

nimble lava
#

nvm fixed

true quail
#

lol

fading patrol
# torn sphinx how do i know what virtual environment i installed it in

If you're using one (and you should) 1) make sure it's active and then 2) use pip to confirm what version of the package is installed. If you're not using one, just do #2. I can't remember the exact command to check a package version with pip but you can try installing it again and if it's already installed, pip will tell you that. Or you can run pip freeze and find it on the list

grim vault
# winged sand SELECT * FROM linjat WHERE numero LIKE ("1[0-9][0-9]%");

The LIKE operator only has _ (single char) and % (zero or more chars) as wildcards. No other pattern matching can be done. Most databases have extensions for a more sophisticated patterns matching, eg SQLite has GLOB and REGEXP operator. You'll have to take a look at your database documentaion.

ebon skiff
#

?

#

Woah

#

That's new

brave bridge
#

well, maybe you shouldn't be using a third-party client 🙂

pure raft
#

As well as the website/mobile version

torn sphinx
#

result_set = sqlalchemy.select([table]).where(table.columns.column1 == '1234')
Is there something wrong with this syntax? It does not give me anyhting.

#

got a table named table, column named column with value 1234

#

It should work right? code doesnt crash, but does not give me any output...

torn sphinx
#
WHERE table.column1 = :column1_1```
#

this is what it says in terminal

paper flower
#

You have to execute it first

#

select() just constructs a statement

torn sphinx
#

thats the tutorial im following

#

oh ofc

#

im stupid

paper flower
#

You have to execute it with engine.connect()

#
>>> with engine.connect() as conn:
...     for row in conn.execute(stmt):
...         print(row)
torn sphinx
#

it worked

#

xD

#

Thanks

#

Sorry for my noobyness

paper flower
#

np

tame quest
#

I use sqlite3,

shut zephyr
#

This is my schema. Please check the rightmost parts relating to user and videogame.

#

I have inserted some dummy records in those three tables. Now I want to create a MYSQL function that takes in 2 arguments (which are usernames) and returns the videogames which have been played by both the users. How to do so? I have the entire SQL script ready to share for your reference if anyone can help.

I already have made a query to get the games which are played by ALL the users. But don't know enough to write the function described above. Please DM or ping me here if you can help. I'll share the SQL code asap

grim vault
shut zephyr
mild merlin
#

i have thic command, and get this error ```python
def check(m):
return m.channel == channel
msg = await j1mk0l.wait_for("message", check=check)
test = msg.content
#try:
mycursor.execute("DELETE FROM license WHERE name=test;")
mydb.commit()

Command raised an exception: ProgrammingError: 1054 (42S22): Unknown column 'test' in 'where clause'

fading patrol
#

Like 'test'

gusty mulch
#

Hey so i'm not sure I read data from a table that uses the serial datatype for it's primary key. For my other tables I am using guild ID's so when i call my db function I can just pass the context guild ID into the function for it to give me all of the data from the row related to that guild ID however I am not sure what I need to pass in for the serial datatype since it is postgres itself generating the serial primary key.

fading patrol
dapper eagle
#

heyy all im trying to run sql in pycharm but its not working 😭

#

it comes up with errors

#

when i run it

fading patrol
gusty mulch
# fading patrol If I'm understanding correctly, it sounds like you need to add the guild ID as a...

well I do have a guild ID column in this table but I'm not sure it needs to be a foreign key. This is how the SQL of my table is set up and as you can see the suggestion is a integer that increments itselfsql -- Table: public.suggestions -- DROP TABLE IF EXISTS public.suggestions; CREATE TABLE IF NOT EXISTS public.suggestions( suggestion integer NOT NULL DEFAULT nextval('suggestions_suggestion_seq'::regclass), guild bigint, original bigint, post bigint, member bigint, message text COLLATE pg_catalog."default", CONSTRAINT suggestions_pkey PRIMARY KEY (suggestion) )how would I call that in my function is really what I'm trying to work out. Since for my data table it's as easy as doingpy data = await self.bot.get_data(ctx.guild.id) # Passing the context guilds ID in so that the bot knows where to look in the db.but doing py suggestions = await self.bot.get_sug(Serial) # I'm not sure what the serial needs to be hereI guess I could restructure the entire table to make the original msg ID the primary key but I'm sure that will cause issus for me later down the line. plus ppl have told me before that making it a incrementing number is the best way to do things

fading patrol
dapper eagle
gusty mulch
# fading patrol Yes, generally you want an incrementing primary key. Beyond that I don't know ...

okay this is get_data and get_sug ```py
async def get_data(self, guild):
async with self.pool.acquire() as conn:
res = await conn.fetchrow("SELECT * FROM data WHERE guild=$1", guild)
if not res:
res = await conn.fetchrow(
"INSERT INTO data VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *",
guild, None, None, [], None, None, None, False, [], [], False,)
return res

async def get_sug(self, suggestion):
async with self.pool.acquire() as conn:
res = await conn.fetchrow("SELECT * FROM suggestions WHERE suggestion=$1",)
if not res:
res = await conn.fetchrow(
"INSERT INTO suggestions VALUES ($1, $2, $3, $4, $5, $6) RETURNING *",
suggestion, None, None, None, None, None)
return res```as for ctx; it represents the context in which a command is being invoked under (i.e. the guild, channel, person who used the command etc). Does that answer all of your questions or do i need to provide more info.

I'm not using the ctx.guild.id as a primary key for this table as a guild will likely have more than one suggestion, I could use the ID of the command that was used to make the suggestion but I want the bot to delete the suggestion once it fully completes the command therefore the message associated with the ID will no longer exist, I could use the ID of the new msg that gets sent for the successful post but if that fails to generate for whatever reason then the db call will fail

fading patrol
# dapper eagle

It's telling you that the username and/or password is incorrect

dapper eagle
fading patrol
#

When you are incrementing your primary key, a foreign key is important for this reason

mild merlin
#

i have thos command but i get this error ```python
sql = "INSERT INTO customers (name, code) VALUES (%s,%s)"
val = (user,test)
mycursor.execute(sql, val)
mydb.commit()

in redeem
mycursor.execute(sql, val)
Command raised an exception: MySQLInterfaceError: Python type Member cannot be converted

pallid shard
#

Hi , im using sqlite3 and i used the type DATE it return me unix but i dont know how to convert date to this unix
int((datetime.now() + relativedelta(hours=6)).timestamp()) this not works im not sure here or #data-science-and-ml so im typing on both of this channels

gusty mulch
#

Okay i'm getting this errorpy PostgresSyntaxError: syntax error at or near ","which produces this tracebackpy ERROR:cogs.error_handler:PostgresSyntaxError: syntax error at or near "," (In suggest) Traceback: File "/usr/local/lib/python3.8/dist-packages/discord/ext/commands/core.py", line 85, in wrapped ret = await coro(*args, **kwargs) File "/home/modmail/cogs/general.py", line 329, in suggest await conn.execute("UPDATE suggestions SET member = $1, original = $2, message = $3 where post = $4, guild = $5", ctx.author.id, ctx.message.id, suggestion_mesage, post.id, ctx.guild.id) File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 320, in execute _, status, _ = await self._execute( File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 1659, in _execute result, _ = await self.__execute( File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 1684, in __execute return await self._do_execute( File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 1719, in _do_execute stmt = await self._get_statement( File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 398, in _get_statement statement = await self._protocol.prepare( File "asyncpg/protocol/protocol.pyx", line 168, in preparewhich seems to be caused by thispy await conn.execute("UPDATE suggestions SET member = $1, original = $2, message = $3 where post = $4, guild = $5", ctx.author.id, ctx.message.id, suggestion_mesage, post.id, ctx.guild.id)which is probably because I'm using a serial in one of my columns of the db but i'm not mentioning that in the insert bc i'm not sure how to

zealous rain
#

Hey, any idea what's wrong? py File "/home/kamil/Dokumenty/bert_trainer/migrations/env.py", line 25, in <module> str(current_app.extensions['migrate'].db.get_engine().url).replace( TypeError: get_engine() missing 1 required positional argument: 'app'

#

from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_restx import Api, fields, Resource
from flask_marshmallow import Marshmallow
from flask_migrate import Migrate
from flask_swagger_ui import get_swaggerui_blueprint

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
# Init config
app.config.from_object(Config())
# Init db
db = SQLAlchemy(app)
migrate = Migrate(app, db)
#

flask db init

#

flask db migrate -m "Initial migration."

grim vault
gusty mulch
grim vault
#

You don't give it to an insert, as shown by the example provided. It's done by the database itself. After the insert you can just use it like any other id.

gusty mulch
# grim vault You don't give it to an insert, as shown by the example provided. It's done by t...

would the serial need to be the final column, I have it in the middle of my tables columns but i keep getting sql DataError: invalid input for query argument $3: 'testing testing 1 2 3' (an integer is required (got type str))and the insert is currentlypy await conn.execute("UPDATE suggestions SET member = $1, original = $2, message = $3 where post = $4 and guild = $5", ctx.author.id, ctx.message.id, suggestion_mesage, post.id, ctx.guild.id)

grim vault
#

The order doesn't matter. And that is an update not an insert. Looks like the message is a integer column? Check your suggestions table.

ruby topaz
#

anyone use AWS or ETL tools for data. Was hoping to have a resource for best practices in my new job.

fading patrol