質問編集履歴
2
削除された内容の復元を行いました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1 +1,199 @@
|
|
1
|
+
お見苦しいソースですが、salesforceとmysqlのデータ連携を図っておりまして、
|
2
|
+
|
3
|
+
salesfoceへの接続 s2hdb.py
|
4
|
+
|
5
|
+
アップデーター s2h_updater.py
|
6
|
+
|
7
|
+
下記プログラム samlple.com
|
8
|
+
|
9
|
+
mysqlの最新更新日とsalesforceの最終更新日を比較しsalesforce側の日付が新しければmysql側をupsertするというプログラムです。
|
10
|
+
|
11
|
+
実行すると
|
12
|
+
|
13
|
+
File "/home/dbcon/sample.py", line 96, in lo_update_companies
|
14
|
+
|
15
|
+
cur.execute(q)
|
16
|
+
|
17
|
+
UnboundLocalError: local variable 'q' referenced before assignment
|
18
|
+
|
1
|
-
|
19
|
+
とエラーが出てしまします。
|
20
|
+
|
21
|
+
色々試したのですが解決できず。。。。
|
22
|
+
|
23
|
+
解決方法を教えていただきたいです。
|
24
|
+
|
25
|
+
```python2.7
|
26
|
+
|
27
|
+
#!/usr/bin/env python
|
28
|
+
|
29
|
+
# -*- coding: utf-8 -*-
|
30
|
+
|
31
|
+
import s2hdb
|
32
|
+
|
33
|
+
from datetime import datetime as dt
|
34
|
+
|
35
|
+
from operator import add
|
36
|
+
|
37
|
+
#
|
38
|
+
|
39
|
+
# Salesforce handle Functions
|
40
|
+
|
41
|
+
#
|
42
|
+
|
43
|
+
def sf_get_last_modified_date():
|
44
|
+
|
45
|
+
sf = s2hdb.sf_get_descriptor()
|
46
|
+
|
47
|
+
q = "SELECT LastModifiedDate FROM Account " + \
|
48
|
+
|
49
|
+
"ORDER BY LastModifiedDate DESC LIMIT 1"
|
50
|
+
|
51
|
+
r = sf.query_all(q)
|
52
|
+
|
53
|
+
last_modified = dt.strptime(r["records"][0]["LastModifiedDate"],
|
54
|
+
|
55
|
+
'%Y-%m-%dT%H:%M:%S.000+0000')
|
56
|
+
|
57
|
+
return last_modified
|
58
|
+
|
59
|
+
def sf_get_updated_companies(last_modified):
|
60
|
+
|
61
|
+
sf = s2hdb.sf_get_descriptor()
|
62
|
+
|
63
|
+
q = "SELECT Name, Website, Description, Description_JP__c, " + \
|
64
|
+
|
65
|
+
"Core_Technology__c, Core_Technology_JP__c, Development_Status__c, " + \
|
66
|
+
|
67
|
+
"Development_Status_JP__c, Application__c, Application_JP__c, Tags__c" + \
|
68
|
+
|
69
|
+
" FROM Account WHERE LastModifiedDate > " + \
|
70
|
+
|
71
|
+
last_modified.strftime('%Y-%m-%dT%H:%M:%S.000+0000')
|
72
|
+
|
73
|
+
results = sf.query_all(q)
|
74
|
+
|
75
|
+
coms = []
|
76
|
+
|
77
|
+
for r in results["records"]:
|
78
|
+
|
79
|
+
com = []
|
80
|
+
|
81
|
+
elements = ['Name', 'Website', 'Description', 'Description_JP__c',
|
82
|
+
|
83
|
+
'Core_Technology__c', 'Core_Technology_JP__c', 'Development_Status__c',
|
84
|
+
|
85
|
+
'Development_Status_JP__c', 'Application__c', 'Application_JP__c', 'Tags__c']
|
86
|
+
|
87
|
+
elements = [str(e) for e in elements]
|
88
|
+
|
89
|
+
for e in elements:
|
90
|
+
|
91
|
+
# Convert NoneType Object as ""
|
92
|
+
|
93
|
+
if (r[e] is None):
|
94
|
+
|
95
|
+
r[e] = str("")
|
96
|
+
|
97
|
+
r[e] = r[e].encode('utf-8')
|
98
|
+
|
99
|
+
r[e] = r[e].replace('\'', '\'\'')
|
100
|
+
|
101
|
+
if (e == 'Tags__c'):
|
102
|
+
|
103
|
+
if (r[e] is None):
|
104
|
+
|
105
|
+
r[e] = str("")
|
106
|
+
|
107
|
+
com.append('{0}'.format(r[e]).strip().split(";"))
|
108
|
+
|
109
|
+
else:
|
110
|
+
|
111
|
+
com.append('\'{0}\''.format(r[e]))
|
112
|
+
|
113
|
+
# Duplication check.
|
114
|
+
|
115
|
+
# It seems salesforce returns duplicated records.
|
116
|
+
|
117
|
+
duplicated = False
|
118
|
+
|
119
|
+
for c in coms:
|
120
|
+
|
121
|
+
if coms[0] == '\'{0}\''.format(r["Name"]):
|
122
|
+
|
123
|
+
duplicated = True
|
124
|
+
|
125
|
+
if (duplicated is True):
|
126
|
+
|
127
|
+
continue
|
128
|
+
|
129
|
+
coms.append(com)
|
130
|
+
|
131
|
+
return coms
|
132
|
+
|
133
|
+
#
|
134
|
+
|
135
|
+
# Local DB handle functions
|
136
|
+
|
137
|
+
#
|
138
|
+
|
139
|
+
def lo_get_last_modified_date():
|
140
|
+
|
141
|
+
con = s2hdb.lo_get_descriptor()
|
142
|
+
|
143
|
+
cur = con.cursor()
|
144
|
+
|
145
|
+
q = "SELECT last_modified from company ORDER BY last_modified DESC LIMIT 1"
|
146
|
+
|
147
|
+
cur.execute(q)
|
148
|
+
|
149
|
+
if (cur.rowcount == 0):
|
150
|
+
|
151
|
+
# We want to move all from salesforce, so just set very early date
|
152
|
+
|
153
|
+
last_modified = dt.strptime("2000/1/1 00:00:00", '%Y/%m/%d %H:%M:%S')
|
154
|
+
|
155
|
+
else:
|
156
|
+
|
157
|
+
last_modified = cur.fetchone()[0]
|
158
|
+
|
159
|
+
cur.close()
|
160
|
+
|
161
|
+
return last_modified
|
162
|
+
|
163
|
+
def lo_update_companies(data):
|
164
|
+
|
165
|
+
if (len(data) == 0):
|
166
|
+
|
167
|
+
return
|
168
|
+
|
169
|
+
con = s2hdb.lo_get_descriptor()
|
170
|
+
|
171
|
+
cur = con.cursor()
|
172
|
+
|
173
|
+
coms = []
|
174
|
+
|
175
|
+
for t in data:
|
176
|
+
|
177
|
+
coms.append(t[0:10])
|
178
|
+
|
179
|
+
q = "INSERT INTO company(legal_name, website, desc, desc_jp, " + \
|
180
|
+
|
181
|
+
"core_tech, core_tech_jp, " + \
|
182
|
+
|
183
|
+
"development_status, development_status_jp, application, application_jp, cat_name) " + \
|
184
|
+
|
185
|
+
"VALUES " + \
|
186
|
+
|
187
|
+
", ".join(map(lambda x: "(" + ",".join(x) + ")", coms)) + " " + \
|
188
|
+
|
189
|
+
"ON DUPLICATE KEY UPDATE legal_name = VALUES (legal_name)" + \
|
190
|
+
|
191
|
+
cur.execute(q)
|
192
|
+
|
193
|
+
cur.close()
|
194
|
+
|
195
|
+
con.commit()
|
196
|
+
|
197
|
+
コード
|
198
|
+
|
199
|
+
```
|
1
一旦削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,235 +1 @@
|
|
1
|
-
お見苦しいソースですが、salesforceとmysqlのデータ連携を図っておりまして、
|
2
|
-
|
3
|
-
salesfoceへの接続 s2hdb.py
|
4
|
-
|
5
|
-
アップデーター s2h_updater.py
|
6
|
-
|
7
|
-
下記プログラム samlple.com
|
8
|
-
|
9
|
-
mysqlの最新更新日とsalesforceの最終更新日を比較しsalesforce側の日付が新しければmysql側をupsertするというプログラムです。
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
実行すると
|
16
|
-
|
17
|
-
File "/home/dbcon/sample.py", line 96, in lo_update_companies
|
18
|
-
|
19
|
-
cur.execute(q)
|
20
|
-
|
21
|
-
UnboundLocalError: local variable 'q' referenced before assignment
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
1
|
+
一旦削除いたします。
|
26
|
-
|
27
|
-
色々試したのですが解決できず。。。。
|
28
|
-
|
29
|
-
解決方法を教えていただきたいです。
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
```python2.7
|
34
|
-
|
35
|
-
#!/usr/bin/env python
|
36
|
-
|
37
|
-
# -*- coding: utf-8 -*-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
import s2hdb
|
42
|
-
|
43
|
-
from datetime import datetime as dt
|
44
|
-
|
45
|
-
from operator import add
|
46
|
-
|
47
|
-
#
|
48
|
-
|
49
|
-
# Salesforce handle Functions
|
50
|
-
|
51
|
-
#
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
def sf_get_last_modified_date():
|
56
|
-
|
57
|
-
sf = s2hdb.sf_get_descriptor()
|
58
|
-
|
59
|
-
q = "SELECT LastModifiedDate FROM Account " + \
|
60
|
-
|
61
|
-
"ORDER BY LastModifiedDate DESC LIMIT 1"
|
62
|
-
|
63
|
-
r = sf.query_all(q)
|
64
|
-
|
65
|
-
last_modified = dt.strptime(r["records"][0]["LastModifiedDate"],
|
66
|
-
|
67
|
-
'%Y-%m-%dT%H:%M:%S.000+0000')
|
68
|
-
|
69
|
-
return last_modified
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
def sf_get_updated_companies(last_modified):
|
74
|
-
|
75
|
-
sf = s2hdb.sf_get_descriptor()
|
76
|
-
|
77
|
-
q = "SELECT Name, Website, Description, Description_JP__c, " + \
|
78
|
-
|
79
|
-
"Core_Technology__c, Core_Technology_JP__c, Development_Status__c, " + \
|
80
|
-
|
81
|
-
"Development_Status_JP__c, Application__c, Application_JP__c, Tags__c" + \
|
82
|
-
|
83
|
-
" FROM Account WHERE LastModifiedDate > " + \
|
84
|
-
|
85
|
-
last_modified.strftime('%Y-%m-%dT%H:%M:%S.000+0000')
|
86
|
-
|
87
|
-
results = sf.query_all(q)
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
coms = []
|
92
|
-
|
93
|
-
for r in results["records"]:
|
94
|
-
|
95
|
-
com = []
|
96
|
-
|
97
|
-
elements = ['Name', 'Website', 'Description', 'Description_JP__c',
|
98
|
-
|
99
|
-
'Core_Technology__c', 'Core_Technology_JP__c', 'Development_Status__c',
|
100
|
-
|
101
|
-
'Development_Status_JP__c', 'Application__c', 'Application_JP__c', 'Tags__c']
|
102
|
-
|
103
|
-
elements = [str(e) for e in elements]
|
104
|
-
|
105
|
-
for e in elements:
|
106
|
-
|
107
|
-
# Convert NoneType Object as ""
|
108
|
-
|
109
|
-
if (r[e] is None):
|
110
|
-
|
111
|
-
r[e] = str("")
|
112
|
-
|
113
|
-
r[e] = r[e].encode('utf-8')
|
114
|
-
|
115
|
-
r[e] = r[e].replace('\'', '\'\'')
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
if (e == 'Tags__c'):
|
120
|
-
|
121
|
-
if (r[e] is None):
|
122
|
-
|
123
|
-
r[e] = str("")
|
124
|
-
|
125
|
-
com.append('{0}'.format(r[e]).strip().split(";"))
|
126
|
-
|
127
|
-
else:
|
128
|
-
|
129
|
-
com.append('\'{0}\''.format(r[e]))
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
# Duplication check.
|
134
|
-
|
135
|
-
# It seems salesforce returns duplicated records.
|
136
|
-
|
137
|
-
duplicated = False
|
138
|
-
|
139
|
-
for c in coms:
|
140
|
-
|
141
|
-
if coms[0] == '\'{0}\''.format(r["Name"]):
|
142
|
-
|
143
|
-
duplicated = True
|
144
|
-
|
145
|
-
if (duplicated is True):
|
146
|
-
|
147
|
-
continue
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
coms.append(com)
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
return coms
|
156
|
-
|
157
|
-
#
|
158
|
-
|
159
|
-
# Local DB handle functions
|
160
|
-
|
161
|
-
#
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
def lo_get_last_modified_date():
|
166
|
-
|
167
|
-
con = s2hdb.lo_get_descriptor()
|
168
|
-
|
169
|
-
cur = con.cursor()
|
170
|
-
|
171
|
-
q = "SELECT last_modified from company ORDER BY last_modified DESC LIMIT 1"
|
172
|
-
|
173
|
-
cur.execute(q)
|
174
|
-
|
175
|
-
if (cur.rowcount == 0):
|
176
|
-
|
177
|
-
# We want to move all from salesforce, so just set very early date
|
178
|
-
|
179
|
-
last_modified = dt.strptime("2000/1/1 00:00:00", '%Y/%m/%d %H:%M:%S')
|
180
|
-
|
181
|
-
else:
|
182
|
-
|
183
|
-
last_modified = cur.fetchone()[0]
|
184
|
-
|
185
|
-
cur.close()
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
return last_modified
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
def lo_update_companies(data):
|
194
|
-
|
195
|
-
if (len(data) == 0):
|
196
|
-
|
197
|
-
return
|
198
|
-
|
199
|
-
con = s2hdb.lo_get_descriptor()
|
200
|
-
|
201
|
-
cur = con.cursor()
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
coms = []
|
206
|
-
|
207
|
-
for t in data:
|
208
|
-
|
209
|
-
coms.append(t[0:10])
|
210
|
-
|
211
|
-
q = "INSERT INTO company(legal_name, website, desc, desc_jp, " + \
|
212
|
-
|
213
|
-
"core_tech, core_tech_jp, " + \
|
214
|
-
|
215
|
-
"development_status, development_status_jp, application, application_jp, cat_name) " + \
|
216
|
-
|
217
|
-
"VALUES " + \
|
218
|
-
|
219
|
-
", ".join(map(lambda x: "(" + ",".join(x) + ")", coms)) + " " + \
|
220
|
-
|
221
|
-
"ON DUPLICATE KEY UPDATE legal_name = VALUES (legal_name)" + \
|
222
|
-
|
223
|
-
cur.execute(q)
|
224
|
-
|
225
|
-
cur.close()
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
con.commit()
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
コード
|
234
|
-
|
235
|
-
```
|