程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Processing idea of possible null fields during SQL splicing in Python

編輯:Python

A problem encountered in the morning , Now there is a need , Will upload excel The file is parsed and the parsed field information is saved to mysql database . With pandas analysis , A string of format Method splicing sql, Like this :

sql = f""" insert into xxx(c1, c2, c3) values ("{
v1}", "{
v2}", "{
v3}"); """

excel Fields in may have values or may not have values , for instance v1 Field , The type is date type , If it's worth it c1 Save date value , If not c1 Just keep it null. One of the problems we are facing now is ,{v1} Handling of outside quotation marks , If not quoted ,v1 If there is a value, an error will be reported when inserting a date , If you use quotation marks ,v1 Insert when there is no value "null" You will also report mistakes. , because c1 Column only accept null Or date .

After thinking for a long time, I didn't think of a good solution in the code , Even thought of using triggers to solve , Later, I consulted my colleagues , I was immediately impressed , It can only be said , What a long long road! ! My colleague just added a code :sql.replace('"null"', 'null'), It's so simple and elegant .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved