【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

上一篇:java获取变量的类型


下一篇:Access denied for user ‘root‘@‘localhost‘ (using password: YES)