这个函数用起来很舒服,直接用一个connection就把大批数据添加到数据库中。
但这个函数有巨大的风险,最主要的就是to_sql的if_exists参数:
if_exists{‘fail’, ‘replace’, ‘append’}, default ‘fail’
How to behave if the table already exists.
fail: Raise a ValueError.
replace: Drop the table before inserting new values.
append: Insert new values to the existing table.
一般很少使用fail,而是使用replace。可replace会删除原表直接添加新表,而to_sql的字段类型都使用的默认text等,会严重破坏原表定义的各种约束如Unique,Primary Key等等,带来很多的未知风险。
如果一定要用,要记住将if_exists设置为append,这样原有的表约束都不会破坏,原来的字段定义也会保留,会安全得多。