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)

猜你喜欢

转载自www.cnblogs.com/Magicn/p/10265928.html
今日推荐