【问题标题】:sqlite value wont updatesqlite 值不会更新
【发布时间】: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


【解决方案1】:

我认为您在为列 fechaDeuda 执行 sqUpdateOne() 时遇到异常,因为查询字符串不太正确。由于它是一个文本列,因此您需要将值括在引号中。

所以更改设置变量fecha 的方式,将其用引号括起来:

fecha = f'"{date_time.split(' - ')[0]}"'

旁注:您还可以删除sqUpdateOne() 中显式的return 语句,因为您在这些行之后以任何方式从函数返回。

【讨论】:

  • 我从 link(24:55 时间标记)了解到,如果我的 execute 语句位于 with 语句中(这使得连接,提交并自行关闭连接)。但我还是要试试。非常感谢!
  • 哦,感谢有关退货声明的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 2012-04-30
相关资源
最近更新 更多