首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3 + beatbox :无法查询

Python3 + beatbox :无法查询
EN

Stack Overflow用户
提问于 2016-11-08 15:43:35
回答 1查看 516关注 0票数 0

我已经使用这里提供的说明登录了我的SFDC组织,http://tomhayden3.com/2013/08/04/salesforce-python/。但是,我无法实现它的queryMore部分。它只是什么都没做。当我打印(Query_locator)时,它会打印一个后缀为-500的ID。请有人看看这段代码,并强调我做错了什么吗?

代码语言:javascript
复制
#!/usr/bin/env python3

import beatbox

# Connecting to SFDC
sf = beatbox._tPartnerNS
service = beatbox.Client()
service.serverUrl = 'https://test.salesforce.com/services/Soap/u/38.0'
service.login('my-username', 'my-password')
query_result = service.query("SELECT id, Name, Department FROM User")
records = query_result['records']  # dictionary of results!
total_records = query_result['size']  # full size of results
query_locator = query_result['queryLocator']  # get the mystical queryLocator


# loop through, pulling the next 500 and appending it to your records dict
while query_result['done'] is False and len(records) < total_records:
    query_result = self._service.queryMore(query_locator)
    query_locator = query_result['queryLocator']  # get the updated queryLocator
    records = records + query_result['records']  # append to records dictionary
    print(records['id']) #This should print all IDs??? But it is not.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-10 17:29:46

这里的例子为我解决了这个问题。https://github.com/superfell/Beatbox/blob/master/examples/export.py

代码语言:javascript
复制
#!/usr/bin/env python3

import beatbox
import sqlalchemy

engine_str = 'mysql+mysqlconnector://db-username: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()
service.serverUrl = 'https://test.salesforce.com/services/Soap/u/38.0' #I hard quoted it since I was to test against sandbox only.

def export(objectSOQL):
    service.login('sfdc-username', 'sfdc-pass')
    query_result = service.query(objectSOQL)
    while True:
        for row in query_result[sf.records:]:
            SQL_query = 'INSERT INTO user(' \
                        'id, ' \
                        'name, ' \
                        'department ' \
                        'VALUES(' \
                        '\"{}\",\"{}\",\"{}\")'\
                        .format(
                        row[2],
                        row[3],
                        row[4]
                        )
            try:
                connection.execute(SQL_query)
            except Exception as e:
                print(e)

    # This is key part which actually pulls records beyond 500 until sf.done becomes true which means the query has been completed. 
        if str(query_result[sf.done]) == 'true':
            break
        query_result = service.queryMore(str(query_result[sf.queryLocator]))

SOQL = 'SELECT id, Name, Department FROM User'
export(SOQL)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40490962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档