jeudi 5 février 2015

How to run sqlplus query in python 3.4

I' m new to python and I am trying to figure out how I can connect to remote oracle DB and run a select query using a python script. Here is what i am trying to achieve:



1) Connect to oracle db and run select query
2) Write the result to the file.


This is the regular connection string I use:



sqlplus temapp/'password'@temappdb
and then I use a select query, lets say select * from employees where employ_id=12;


Not quite sure how to implement this in python 3.4, in 2.7 MySQLdb module/library is being used.



**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) And I have no idea how to write it in the file, my first guess was to manipulate with stdin and stdout by I believe there is a more efficient way to do it.



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()


Can someone please advise on the better solution ?


Aucun commentaire:

Enregistrer un commentaire