[英]Update the timestamp to current timestamp in DB2 using Python ibm_db package
I am trying to set the lastupdated timestamp in db2 to current timestamp everytime the section of my script runs每次我的脚本部分运行时,我都试图将 db2 中的 lastupdated 时间戳设置为当前时间戳
datetimes = datetime.now()
datetime1 = datetimes.strftime("%Y-%m-%d %H:%M:%S")
datetime2 = datetime.strptime(datetime1,'%Y-%m-%d %H:%M:%S')
print(type(datetime2))
print(datetime2)
sql = "update salesorder set LASTUPDATEUSER = 'Testing' and LASTUPDATEDATETIME = ? where code ='0888' "
prepared = ibm_db.prepare(conn, sql)
returnCode = ibm_db.bind_param(prepared,1,datetime2, ibm_db.SQL_PARAM_INPUT,ibm_db.SQL_CHAR)
if returnCode == False:
print("unable to bind")
#ibm_db.execute(upstmt)
#param = param1
returnCode = ibm_db.execute(prepared)
if returnCode == False:
print("unable to execut")
on running this script I get the error在运行此脚本时出现错误
Traceback (most recent call last):
File "new.py", line 27, in <module>
returnCode = ibm_db.execute(prepared)
SQLCODE=-420atement Execute Failed: [IBM][CLI Driver][DB2/LINUXX8664] SQL0420N Invalid character found in a character string argument of the function "BOOLEAN". SQLSTATE=22018
I have tried multiple resolutions like passing the datetime as a string as well but no luck with solving the issue this is the timestamp format used in db2 '2020-02-21 13:37:37'我尝试了多种解决方案,例如将日期时间作为字符串传递,但没有解决问题,这是 db2 '2020-02-21 13:37:37' 中使用的时间戳格式
If someone can please provide a way to resolve this that would be really helpful如果有人可以请提供一种解决此问题的方法,这将非常有帮助
Your update stmt is wrong.您的更新 stmt 错误。 Change:
改变:
update salesorder
set LASTUPDATEUSER = 'Testing' and LASTUPDATEDATETIME = ?
where code ='0888'
to:至:
update salesorder
set LASTUPDATEUSER = 'Testing'
, LASTUPDATEDATETIME = ?
where code ='0888'
or:或者:
update salesorder
set (LASTUPDATEUSER, LASTUPDATEDATETIME) = ('Testing', ?)
where code ='0888'
The error message you get is puzzling, but is due to the fact that you are trying to evaluate a Boolean from a string:您收到的错误消息令人费解,但这是因为您尝试从字符串中评估 Boolean:
db2 "values 'testing' and current_timestamp = current_timestamp"
1
--
SQL0420N Invalid character found in a character string argument of the
function "BOOLEAN". SQLSTATE=22018
compare with与之比较
db2 "values true and current_timestamp = current_timestamp"
1
--
1
even if that succeded you would get an error trying to assign a timestamp column a boolean value.即使成功,您在尝试为时间戳列分配 boolean 值时也会出错。
I guess older versions of Db2 would have objected during prepare, but nowadays type checking is much more relaxed, which leads to very counter intuitive error messages from time to time我猜旧版本的 Db2 在准备期间会反对,但现在类型检查要轻松得多,这会不时导致非常反直觉的错误消息
That said, you may want to look into doing:也就是说,您可能想考虑这样做:
update salesorder
set (LASTUPDATEUSER, LASTUPDATEDATETIME) = ('Testing', current_timestamp)
where code ='0888'
Your code would then be reduced to:然后,您的代码将简化为:
sql = """update salesorder
set (LASTUPDATEUSER, LASTUPDATEDATETIME) = ('Testing', current_timestamp)
where code ='0888'"""
prepared = ibm_db.prepare(conn, sql)
returnCode = ibm_db.execute(prepared)
if returnCode == False:
print("unable to execut")
As a side node, I find it a bit cumbersome to explicitly bind parameters and usually just do:作为一个侧节点,我发现显式绑定参数有点麻烦,通常只是这样做:
ibm_db.execute(prepared, (param1, ...))
If the purpose is just to update the timestamp whenever the row is changed, concider a trigger:如果目的只是在行更改时更新时间戳,请考虑触发器:
create trigger trg1
no cascade before update on salesorder
referencing new as n
for each row
set n.LASTUPDATEDATETIME = current_timestamp
If you change your update statement to:如果您将更新语句更改为:
update salesorder
set LASTUPDATEUSER = 'Testing'
where code ='0888'
the trigger will do that for you:触发器将为您执行此操作:
db2 "select * from salesorder"
LASTUPDATEUSER LASTUPDATEDATETIME CODE
-------------------- -------------------------- -----
Testing 2020-04-11-11.51.22.055602 0888
1 record(s) selected.
./aa.py
db2 "select * from salesorder"
LASTUPDATEUSER LASTUPDATEDATETIME CODE
-------------------- -------------------------- -----
Testing 2020-04-11-11.58.50.222753 0888
1 record(s) selected.
Dependent on your version of Db2, you may also look into declaring the column as:根据您的 Db2 版本,您还可以考虑将列声明为:
FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP
You probably want GENERATED ALWAYS, but I don't think you can alter the column to that, so you will have to do a bit more work to get that in place.您可能希望始终生成,但我认为您不能将列更改为该列,因此您必须做更多的工作才能将其落实到位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.