sqlalchemy批量插⼊(存在唯⼀约束)
插⼊oracle时,如果某个列存在唯⼀约束,同时批量插⼊时数据重复,会报error,此时将error的过滤掉,最后commit
def insert_one_by_one():
from sqlalchemy import create_engine
engine = create_engine("oracle://user:xx:xxxx/?service_x", echo=True)
user_list = [{'username': 'jalen1', 'age': '11', 'height': '123'},
{'username': 'jalen2', 'age': '12', 'height': '124'},
{'username': 'jalen3', 'age': '13', 'height': '125'},
{'username': 'jalen4', 'age': '14', 'height': None}]
conn = t()
trans = conn.begin()
try:
for user in user_list:
try:
sql = "INSERT INTO tb_user(username, age, height) VALUES('{0}', '{1}', '{2}')".\
format(user['username'], user['age'], user['height'])
except Exception as e:
logger.ssage)
transmit()
except Exception:
创建唯一约束sql语句
raise
finally:
conn.close()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。