利用 Python 插入 Oracle 数据
# coding=utf-8 ''''' Created on 2020-01-05 @author: Mr. Zheng ''' import json; import urllib2 import sys import cx_Oracle import exceptions # page=urllib2.urlopen("http://").read() # jsonVal = json.loads(page) def read_json_file(file_name): file_object = open(file_name) db = cx_Oracle.connect('用户名', '密码', 'ip:1521/oracle的serve_name') cursor = db.cursor() ''' rows = cursor.execute('select * from table') for sz in rows: print sz ''' try: all_the_text = file_object.read() jsonVal = json.loads(all_the_text) param = [] for val in jsonVal: #print val["serverid"] #print val["server_name"] param.append((val["serverid"], "san_s" + val["serverid"], val["server_name"])) # cursor.execute('insert into game_server_name(server_id, server_name, chinese_name) values(:server_id, :2, :3)',(val["serverid"], "san_s" + val["serverid"], val["server_name"])) # cursor.execute("insert into game_server_name(server_id, server_name, chinese_name) values('a','b','c')") print param print len(param) cursor.prepare('insert into game_server_name(server_id, server_name, chinese_name) values(:1, :2, :3)') # 一次插入全部数据 cursor.executemany(None, param) db.commit() except Exception, err: print err finally: cursor.close() db.close() file_object.close() def main(): read_json_file('E:/android.json') read_json_file('E:/apple.json') if __name__ == '__main__': main()
当只有一次参数的时候,也要把它写成元组的形式:
例如:Cursor.execute(‘select name from user where id=:1’,(login_Id,))
千万要注意: login_id 后面还带有一个逗号
posted on 2020-01-05 20:47 morgan363 阅读(2287) 评论(0) 编辑 收藏 举报