【发布时间】:2020-06-09 21:16:40
【问题描述】:
我正在尝试更新 Sqlite 数据库中的客户(“客户”)信息,但只有一个值不会更新。当我试图更新列“fechaDeuda”(债务日期)时,它只是不会这样做。奇怪的是:我编写了一个函数来处理所有更新,它适用于所有其他情况,即使在“问题行”中调用(见下面的代码)该函数执行数据库更新的每个语句并且不抛出任何异常,但仍然数据库中的值不会改变。
这是我更新同一行上的三个值的代码:
try:
table = 'clientes'
_cliente = sqGenericSelectOne(table, None, 'id', generic.id) # This function retrieves one row based on the arguments passed
clienteID = _cliente[0]
fecha = date_time.split(' - ')[0]
sqUpdateOne(table, 'fechaDeuda', fecha, 'id', clienteID) #### Problematic line (!!!)
nuevoSaldo = float(_cliente[9]) - _pago
if nuevoSaldo < 0:
nuevoSaldo = 0
sqUpdateOne(table, 'saldo', nuevoSaldo, 'id', clienteID) # This line works fine
nuevoTotalPagado = float(_cliente[11]) + _pago
sqUpdateOne(table, 'totalPagado', nuevoTotalPagado, 'id', clienteID) # This one works fine too
except Exception as e:
error(0, e)
**编辑(由于某种原因,当我发布时它被删除了)**这里是“sqUpdateOne”代码:
# Sqlite database value updater
def sqUpdateOne(table, column, newValue, refColumn, refValue):
try:
with conn:
c.execute(f'''UPDATE {table} SET {column} = {newValue} WHERE {refColumn} = {refValue}''')
print()
return
except Exception as e:
error(3, e, table)
return
下面是创建数据库表“clientes”的方法,以备不时之需。该表适用于其他一切。我使用https://sqliteonline.com/ 跟踪它
table = 'clientes'
try:
c.execute(f'''CREATE TABLE {table} (
id integer,
nombre text,
apellido text,
valoracion real,
direccion text,
tel_1 text,
tel_2 text,
correo text,
cantServicios integer,
saldo real,
fechaDeuda text,
totalPagado real,
descripcion text)''')
state(2)
except Exception as e:
error(2, e, table)
编辑:我在这里添加sqUpdateOne 的屏幕截图,当调用更新“fecha”时,你们可以看到它获得的值(同样,它在所有其他情况下都可以正常工作):
提前致谢!
【问题讨论】:
-
您确定
fecha不是空字符串吗?问题可能在这一行:fecha = date_time.split(' - ')[0] -
不,我检查了几次,
sqUpdateOne收到了正确的字符串。我会在一分钟内编辑帖子并上传调试器的屏幕截图
标签: python-3.x sqlite