数据库
首页 > 数据库> > 【Python】连接Oracle数据库并查出某表记录存为csv文件

【Python】连接Oracle数据库并查出某表记录存为csv文件

作者:互联网

如果没有安装cx_Oracle请参考:【Python】连接到Oracle数据库的前奏:安装cx_Oracle - 不朽的飞翔 - 博客园 (cnblogs.com)

代码:

#encoding=utf-8
import cx_Oracle

conn=cx_Oracle.connect('luna','1234','127.0.0.1:1521/orclhy78')
cursor=conn.cursor();

cursor.execute("select * from emp6")
rowset=cursor.fetchall();

with open(r'emp6.csv','w') as outfile:
    for i in range(len(rowset)):
        row=rowset[i]
        
        for j in range(len(row)):
            cell=row[j]
            outfile.write(str(cell)+',')

        outfile.write("\n")

cursor.close();
conn.close();

输出:

1,andy,worker,
2,bill,worker,
3,cindy,worker,
4,douglas,doctor,
5,eliot,doctor,
6,felix,nurse,

END

标签:cursor,rowset,Python,存为,cx,Oracle,conn,row
来源: https://www.cnblogs.com/pyhy/p/15835691.html