我的表中有一个插入字符串,但是有些值是空的,这不是问题,因为应该有意地将这些值留空。我正在寻找一种无需重写我的插入字符串即可解决此问题的聪明方法。它的长度比下面的更长,但是下面显示了两个定义为real
数据类型的字段。
cursor.execute("INSERT INTO table_" + country + " (lat, lon) VALUES (%s, %s)" , (loc['LAT'], loc['LON'], ))
所以有时候
loc['LAT']
将''
和其他时间例如'54.44'
loc['LON']
将''
和其他时间例如'53.46'
如果这些为空白,则会出现以下错误:
pg8000.errors.ProgrammingError: ('ERROR', '22P02', 'invalid input syntax for type real: ""')
如果这些列接受 null
cursor.execute("""
insert into table_" + country + " (lat, lon) values (%s, %s)
""" , (loc['LAT'] or None, loc['LON'] or None)
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句