如何在pandas dataframe中插入query_result的输出?查询结果首先获得500条记录,并使用QueryMore提取剩余的记录。我无法将df中的所有内容与列名称一起推送。
#!/usr/bin/env python3
import beatbox
import sqlalchemy
engine_str = 'mysql+mysqlconnector://db-user:db-pass@localhost/db-name'
engine = sqlalchemy.create_engine(engine_str, echo=False, encoding='utf-8')
connection = engine.connect()
sf = beatbox._tPartnerNS
service = beatbox.Client()
def sync_user(objectSOQL):
service.login('sfdc-username', 'sfdc-creds')
query_result = service.query(objectSOQL)
while True:
for row in query_result[sf.records:]:
SQL_query = 'INSERT INTO user(' \
'id, ' \
'name, ' \
'department, ' \
'sales_team) ' \
'VALUES(' \
'\"{}\",\"{}\",\"{}\",\"{}\"' \
') ON DUPLICATE KEY UPDATE '\
'name = VALUES(name), '\
'department = VALUES(department), '\
'sales_team = VALUES(sales_team) '\
.format(
row[2],
row[3],
row[4],
row[5]
)
# SQL_query = SQL_query.replace('\"None\"', 'NULL')
try:
connection.execute(SQL_query)
except Exception as e:
print(id, e)
if str(query_result[sf.done]) == 'true':
break
query_result = service.queryMore(str(query_result[sf.queryLocator]))
if __name__ == "__main__":
SOQL = 'SELECT id, Name, Department, Sales_Team__c FROM User'
sync_user(SOQL)
答案 0 :(得分:1)
一种方法是制作一个列表,并在每次循环并获得新结果时向其附加一个DataFrame。然后,最后您可以使用pandas.concat
将所有单个DataFrame合并为一个。
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html#pandas-concat