通过python连接到数据库

时间:2019-01-13 11:09:22

标签: python popen

我正在通过子进程在python中连接Oracle,在这里我试图通过控制台提供输入,并从表all_db_links中获取db的输出,但在此代码中我没有得到结果,甚至没有错误。里面可能有什么问题?已经谢谢你了:)

from subprocess import Popen, PIPE 
value = raw_input()

def runSqlQuery (sqlCommand, connectString): 
    session = Popen(['sqlplus', '-S', connectString], 
    stdin = PIPE, 
    stdout= PIPE, 
    stderr = PIPE) 

    session.stdin.write(sqlCommand)
    return session.communicate()

connectString = 'user/pwd@db' sqlCommand =""" SET HEADING OFF ( select * from all_db_links where OWNER ='?' , (value))"""
outcome = runSqlQuery (sqlCommand connectString) for i in outcome: print(i)

0 个答案:

没有答案