Insert with Where where I don’t have full value

Asked

Viewed 43 times

0

Example, I already have in the table the following value in the column NUM_IMPRESSAO:

K3244.K1019706.FSJC.08915

The new value I want to insert has only the last 4 digits (08915) and I need to insert it in the same line. Follow the select I tried to do:

def update_table(file_name, id):
        date = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%d/%m/%Y'))

        c.execute(f'UPDATE IMPRESSAO SET BINARY = ?,  DATA_ENVIO_BINARY = ? WHERE NUM_IMPRESSAO LIKE ?;', (file_name, date, id))
        c.commit()
        select_all()

But it’s not working.

1 answer

0

I’m guessing the id is the NUM_IMPRESSAO, what you need to do is a split and put the "." as the value to divide the string in a array.

novoValor = str(id).split('.')

novoValor = novoValor[-1]

or use another class method string which is the rfind which returns the value where the searched character is. and then only pass it inside the string to the end.

novoValor = id[id.rfind('.')+1:]

use this variable novoValor and pass on your update.

c.execute(f'UPDATE IMPRESSAO SET BINARY = ?,  DATA_ENVIO_BINARY = ? ,NovoVaor = ? WHERE NUM_IMPRESSAO LIKE ?;', (file_name, date, novoValor, id))

For reference:

split

rfind

  • The value of novoValor would be [3], because you defined it as a list with the size of the list subtracted from 1. I believe you wanted to fetch the last value of the list, so it should be novoValor[len(novoValor)-1], but better than that, just pass the negative index to fetch the last value, novoValor[-1], do not need to calculate the size of it.

  • 1

    Even you can do novo_valor = id[id.rfind('.')+1:] to get the last part after the point, this would avoid creating a list with all substrings just to fetch the last one, having a saving of memory in the execution of the code. Remembering that id is not a good name for a variable, since it would be overwriting a native Python function.

  • That ai Anderson, there are several ways to return this last element that she, I just showed a way. The idea would be that last shape you showed novo_valor = id[id.rfind('.')+1:], thus avoiding unnecessary memory allocation.

  • 1

    The ideal would be to correct this form you presented, as I commented in the first xD comment

  • Ready old man, thanks to the help xD @Andersoncarloswoss

  • novoValor = [-1] that shouldn’t be novoValor = novoValor[-1]?

  • That’s right @Andersoncarloswoss, Big Shot. Thanks. Fixed!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.