#mysql

1 messages · Page 1 of 1 (latest)

vivid cedar
#

well this is mainly a python code issue

#

just write out the code

#
if user_id in claim_stats:
  count = count + 1
else: 
  await cursor.execute('SQL command')
#

something like that would be it

pastel dock
#

noo

vivid cedar
#

?

pastel dock
#

i want to to this in a mysql string

vivid cedar
#

wdym?

pastel dock
#

like mycursor.execute("if UserID in ClaimStats exists then update Count=Count+1 else insert into Claimstats UserID, Count=1")

vivid cedar
#

yeah no

#

thats not going to work

pastel dock
#

i tried things like:
mycursor.execute("INSERT INTO ClaimStats (UserID, Count) VALUES (%s,%s) ON DUPLICATE KEY UPDATE Count=Count+1", (user.id, 1))

vivid cedar
#

why not do the checks within python and then use SQL when you want to insert

pastel dock
#

then i have to do one more select request

#

i wanted it do in one sql statement

vivid cedar
#

I guess

#

hm

little olive
#

you can use upsert

pastel dock
#

i tested it

little olive
#

what happens?

#

do you have a key?

pastel dock
#

i cleared the claimstats

#

then i executed it and it inserted the user.id and 1

#

then i did it again

#

and it not updated it to Count+1

#

it inserted it again

little olive
#

right

#

do you have a key on ClaimStats.UserID

pastel dock
#

uhm

#

i dont know

little olive
#

can you run show indexes from ClaimStats

pastel dock
#

yes wait

little olive
#

so there's no indexes

pastel dock
#

nothing

#

yes

little olive
#

you need to create an index if you expect ON DUPLICATE KEY to do anything

pastel dock
#

how to?

little olive
#

using the create index command

#

or when you create the table, mark the column as a primary key and it'll create the index for you

pastel dock
#

i dont understand

little olive
#

you should do some research on mysql then

grizzled ledge
#

Then INSERT INTO ClaimStats (UserID, Count) VALUES (%s,%s) ON DUPLICATE KEY UPDATE Count=Count+1", (user.id, 1) will work