怎么用python 连接数据库 执行select ID1 from table1 然后将ID1 传出来 给第二个SELECT 用:

2025-06-22 20:40:33
推荐回答(4个)
回答1:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn(ip,port,SID)
connection = cx_Oracle.connect(username,password,dsn_tns)
cursor = connection.cursor()
cursor.execute("select ID1 from table1")
id1list = [r[0] for r in cursor]
for id1 in id1list:
print '-'*4, id1, '-‘*16
cursor.execute("select ID2 from table2 where ID = %s", id1))
for r in cursor:
print r

回答2:

search `pymysql` may help you

回答3:

安装模块 pymysql

回答4:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn(ip,port,SID)
connection = cx_Oracle.connect(username,password,dsn_tns)
cursor = connection.cursor()
ID1 = cursor.execute("select ID1 from table1")
ID2 = cursor.execute("select ID2 from table2 where ID = %s"%(ID1))