且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将解析的数据从Python导出到SQL Developer中的Oracle表?

更新时间:2023-01-22 10:49:43

使用CX_Oracle& Python 2.7 你可以看到总体规划。
所以如果你已经将数据解析成csv,你可以轻松地做到:

From Import a CSV file into Oracle using CX_Oracle & Python 2.7 you can see overall plan. So if you already parsed data into csv you can easily do it like:

import cx_Oracle
import csv

dsnStr = cx_Oracle.makedsn("sole.wh.whoi.edu", "1526", "sole")

con = cx_Oracle.connect(user="myusername", password="mypassword",     dsn=dsnStr)
print (con.version)

#imp 'Book1.csv' [this didn't work]

cursor = con.cursor()
print (cursor)

text_sql = '''
INSERT INTO tablename (firstfield, secondfield) VALUES(:1,:2)
'''

my_file = 'C:\CSVData\Book1.csv'
cr = csv.reader(open(my_file,"rb"))
for row in cr:    
    print row
    cursor.execute(text_sql, row)
    print 'Imported'
con.close()