python插入oracle数据

 

# coding=utf-8   
''''' 
Created on 2015-01-14 
 
@author: 
'''

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后面还带有一个逗号

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值