Home > Mobile >  sql update a column afther reach same amount 1ns
sql update a column afther reach same amount 1ns

Time:08-06

i have a function that needs to update all rows to 0 after colum 1(selection) and column 2(savecounter) have same amount 1ns

I have made some save systhem it needs to work like this. I got multiple employee, after i use a button 2 mails will be send. After the mail send savecounter will update to 1 and if i close the program then he knows where tho start again.

my question now is, how do i make a query that checks after starting the program if savecounter and selection have same amount of 1ns. and make the savecounter column to 0.

the selection column is a constant column its for employee thats on work (true) and selection is for te save systhem for the program if i close that it knows where to start.

def makesavecounterzero():
    conn= sqlite3.connect('schoonschip.db')
    cursorr = conn.cursor()
    employe = cursorr.execute("SELECT selection, savecounter FROM employee")
    selections = cursorr.fetchall()
    for i in selections:
        print(i)
        if i[0] / i[1] >= 1 :
            cursorr.execute('UPDATE employee SET savecounter =0')
    conn.commit()
    conn.close()
makesavecounterzero()

                    selec  savecounter
 [email protected] 1   1
 [email protected]  1   1
 [email protected] 0   0
 [email protected] 0   0
 [email protected]  1   0

if the same amount of 1ns in 2 clumns is reach then i need to refresh whole savecounter column to 0 again.

CodePudding user response:

Use SUM() to get the total of all selection and savecounter. Then compare them and update all the savecounter if selection is higher.

def makesavecounterzero():
    conn= sqlite3.connect('schoonschip.db')
    cursorr = conn.cursor()
    employe = cursorr.execute("SELECT SUM(selection), SUM(savecounter) FROM employee")
    selections, savecounters = cursorr.fetchone()
    if selections > savecounters:
        cursorr.execute('UPDATE employee SET savecounter = 0')
        conn.commit()
    conn.close()
  • Related