Bky*_*kyn 5 python sql oracle pandas
在 Python 中,使用 Pandas,我有一个数据框,我想将其与 Oracle 数据库中的表连接起来。
我没有数据库的写入权限。因此,我目前正在做:
#select the column you want to use to query
ID_list = data['ID']
#cut the list in chunks of 1000 to overcome Oracle limitation on SQL query
chunks = [ID_list[x:x+1000] for x in range(0, len(ID_list), 1000)]
#initiate connection to Oracle
cnxn = pyodbc.connect('DSN=XXX;UID=YYY;PWD=ZZZ')
#initiate an empty frame
df = pd.DataFrame()
#query each chunk of 1000 ID and append it to df
for chunk in chunks:
sql_query = 'SELECT * FROM TABLE_X WHERE ID IN (' + ','.join(map(str, chunk)) + ')'
df = df.append(pd.read_sql(sql_query, cnxn), ignore_index=True)
Run Code Online (Sandbox Code Playgroud)
然后我会处理 df 并可能将其与数据合并。我确信它比 SQL 连接慢得多,但我无法上传我的数据帧。
这是最有效的解决方案吗?
| 归档时间: |
|
| 查看次数: |
1389 次 |
| 最近记录: |