python 连接HANA库,写入城乡划分代码(省市县镇乡村5级)到数据库

 1 import pyhdb,re
 2 def get_connection():
 3     conn_obj = pyhdb.connect(
 4         host = "xxx.172.1.xxx", #HANA地址
 5         port = 30015, #HANA端口号
 6         user = "xxxxx", #用户号
 7         password = "xxxxx" #密码
 8     )
 9     return conn_obj
10 def get_mat(conn,exec_str):
11     cursor = conn.cursor()
12     cursor.execute(exec_str) #连接表和视图都可以
13     return cursor
14 conn = get_connection()
15 file_str = 'D:\city_2017.txt'
16 with open(file_str, 'r', encoding='gb18030') as f:
17     lines = f.readlines()
18     for i in lines:
19         try:
20             upsert_str = 'UPSERT HC_BI_DW.BI_DIM_BASE_PROVINCIAL_CITY VALUES ('+str(re.split("\[|]",i)[1])+') WITH PRIMARY KEY;'
21             print(upsert_str)
22             mats = get_mat(conn,upsert_str)
23             conn.commit()
24         except BaseException as e:
25             print('错误:', e)

 

转载于:https://www.cnblogs.com/Magicn/p/10265928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值