【发布时间】:2015-04-05 22:22:11
【问题描述】:
我是 python 新手,我想弄清楚如何连接到远程 oracle DB 并使用 python 脚本运行选择查询。这是我想要实现的目标:
1) Connect to oracle db and run select query
2) Write the result to the file.
这是我使用的常规连接字符串:
sqlplus temapp/'password'@temappdb
and then I use a select query, lets say select * from employees where employ_id=12;
不太清楚如何在 python 3.4 中实现这一点,在 2.7 中使用 MySQLdb 模块/库。
**Here is my blueprint:**
#!/bin/python
import sqlite3
import sys
import os
config = {
'user': 'user',
'password': '*****',
'host': '127.0.0.1',
'database': 'test',
'raise_on_warnings': True,
}
conn = sqlite3.connect('config') # not sure how to pass user and password
c = conn.cursor()
c.execute('select * from employees where employ_id=12')
2) 我不知道如何在文件中写入,我的第一个猜测是使用 stdin 和 stdout 进行操作,我相信有一种更有效的方法。
orig_stdout = sys.stdout
out = open("/output.txt", 'w')
sys.stdout = out
c.execute('select * from employees where employ_id=12')
sys.stdout = orig_stdout
out.close()
有人可以建议更好的解决方案吗?
【问题讨论】:
标签: python oracle sqlite select python-3.x