python 格式化向sql語句輸出元組


想要往執行的sql語句里傳入元組怎么辦?尤其像insert 數據等操作,其實
利用"{}".format(tuple)固定格式傳值即可
 1 results = ((1,2,3),(4,5,6))
 2 #因為hive insert 要求values 格式為   insert overwrite table term_dept_top_backup111 values(),()()
 3 #所以我增加的其他代碼是為了去除外面的“()”
 4 n = 0
 5 sql = "insert overwrite table term_dept_top_backup111 values "
 6 for row in results:
 7     n = n + 1
 8     if n != len(results)+1:
 9         sql = sql + "{}"
10     sql = sql.format(row)
11     if n != len(results):
12         sql = sql +','

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM