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.
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 benovoValor[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.– Woss
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 thatid
is not a good name for a variable, since it would be overwriting a native Python function.– Woss
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.– Tmilitino
The ideal would be to correct this form you presented, as I commented in the first xD comment
– Woss
Ready old man, thanks to the help xD @Andersoncarloswoss
– Tmilitino
novoValor = [-1]
that shouldn’t benovoValor = novoValor[-1]
?– Woss
That’s right @Andersoncarloswoss, Big Shot. Thanks. Fixed!
– Tmilitino