質問編集履歴
2
削除された内容の復元を行いました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,1 +1,100 @@
|
|
1
|
+
お見苦しいソースですが、salesforceとmysqlのデータ連携を図っておりまして、
|
2
|
+
salesfoceへの接続 s2hdb.py
|
3
|
+
アップデーター s2h_updater.py
|
4
|
+
下記プログラム samlple.com
|
5
|
+
mysqlの最新更新日とsalesforceの最終更新日を比較しsalesforce側の日付が新しければmysql側をupsertするというプログラムです。
|
6
|
+
実行すると
|
7
|
+
File "/home/dbcon/sample.py", line 96, in lo_update_companies
|
8
|
+
cur.execute(q)
|
9
|
+
UnboundLocalError: local variable 'q' referenced before assignment
|
1
|
-
|
10
|
+
とエラーが出てしまします。
|
11
|
+
色々試したのですが解決できず。。。。
|
12
|
+
解決方法を教えていただきたいです。
|
13
|
+
```python2.7
|
14
|
+
#!/usr/bin/env python
|
15
|
+
# -*- coding: utf-8 -*-
|
16
|
+
import s2hdb
|
17
|
+
from datetime import datetime as dt
|
18
|
+
from operator import add
|
19
|
+
#
|
20
|
+
# Salesforce handle Functions
|
21
|
+
#
|
22
|
+
def sf_get_last_modified_date():
|
23
|
+
sf = s2hdb.sf_get_descriptor()
|
24
|
+
q = "SELECT LastModifiedDate FROM Account " + \
|
25
|
+
"ORDER BY LastModifiedDate DESC LIMIT 1"
|
26
|
+
r = sf.query_all(q)
|
27
|
+
last_modified = dt.strptime(r["records"][0]["LastModifiedDate"],
|
28
|
+
'%Y-%m-%dT%H:%M:%S.000+0000')
|
29
|
+
return last_modified
|
30
|
+
def sf_get_updated_companies(last_modified):
|
31
|
+
sf = s2hdb.sf_get_descriptor()
|
32
|
+
q = "SELECT Name, Website, Description, Description_JP__c, " + \
|
33
|
+
"Core_Technology__c, Core_Technology_JP__c, Development_Status__c, " + \
|
34
|
+
"Development_Status_JP__c, Application__c, Application_JP__c, Tags__c" + \
|
35
|
+
" FROM Account WHERE LastModifiedDate > " + \
|
36
|
+
last_modified.strftime('%Y-%m-%dT%H:%M:%S.000+0000')
|
37
|
+
results = sf.query_all(q)
|
38
|
+
coms = []
|
39
|
+
for r in results["records"]:
|
40
|
+
com = []
|
41
|
+
elements = ['Name', 'Website', 'Description', 'Description_JP__c',
|
42
|
+
'Core_Technology__c', 'Core_Technology_JP__c', 'Development_Status__c',
|
43
|
+
'Development_Status_JP__c', 'Application__c', 'Application_JP__c', 'Tags__c']
|
44
|
+
elements = [str(e) for e in elements]
|
45
|
+
for e in elements:
|
46
|
+
# Convert NoneType Object as ""
|
47
|
+
if (r[e] is None):
|
48
|
+
r[e] = str("")
|
49
|
+
r[e] = r[e].encode('utf-8')
|
50
|
+
r[e] = r[e].replace('\'', '\'\'')
|
51
|
+
if (e == 'Tags__c'):
|
52
|
+
if (r[e] is None):
|
53
|
+
r[e] = str("")
|
54
|
+
com.append('{0}'.format(r[e]).strip().split(";"))
|
55
|
+
else:
|
56
|
+
com.append('\'{0}\''.format(r[e]))
|
57
|
+
# Duplication check.
|
58
|
+
# It seems salesforce returns duplicated records.
|
59
|
+
duplicated = False
|
60
|
+
for c in coms:
|
61
|
+
if coms[0] == '\'{0}\''.format(r["Name"]):
|
62
|
+
duplicated = True
|
63
|
+
if (duplicated is True):
|
64
|
+
continue
|
65
|
+
coms.append(com)
|
66
|
+
return coms
|
67
|
+
#
|
68
|
+
# Local DB handle functions
|
69
|
+
#
|
70
|
+
def lo_get_last_modified_date():
|
71
|
+
con = s2hdb.lo_get_descriptor()
|
72
|
+
cur = con.cursor()
|
73
|
+
q = "SELECT last_modified from company ORDER BY last_modified DESC LIMIT 1"
|
74
|
+
cur.execute(q)
|
75
|
+
if (cur.rowcount == 0):
|
76
|
+
# We want to move all from salesforce, so just set very early date
|
77
|
+
last_modified = dt.strptime("2000/1/1 00:00:00", '%Y/%m/%d %H:%M:%S')
|
78
|
+
else:
|
79
|
+
last_modified = cur.fetchone()[0]
|
80
|
+
cur.close()
|
81
|
+
return last_modified
|
82
|
+
def lo_update_companies(data):
|
83
|
+
if (len(data) == 0):
|
84
|
+
return
|
85
|
+
con = s2hdb.lo_get_descriptor()
|
86
|
+
cur = con.cursor()
|
87
|
+
coms = []
|
88
|
+
for t in data:
|
89
|
+
coms.append(t[0:10])
|
90
|
+
q = "INSERT INTO company(legal_name, website, desc, desc_jp, " + \
|
91
|
+
"core_tech, core_tech_jp, " + \
|
92
|
+
"development_status, development_status_jp, application, application_jp, cat_name) " + \
|
93
|
+
"VALUES " + \
|
94
|
+
", ".join(map(lambda x: "(" + ",".join(x) + ")", coms)) + " " + \
|
95
|
+
"ON DUPLICATE KEY UPDATE legal_name = VALUES (legal_name)" + \
|
96
|
+
cur.execute(q)
|
97
|
+
cur.close()
|
98
|
+
con.commit()
|
99
|
+
コード
|
100
|
+
```
|
1
一旦削除
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,118 +1,1 @@
|
|
1
|
-
お見苦しいソースですが、salesforceとmysqlのデータ連携を図っておりまして、
|
2
|
-
salesfoceへの接続 s2hdb.py
|
3
|
-
アップデーター s2h_updater.py
|
4
|
-
下記プログラム samlple.com
|
5
|
-
mysqlの最新更新日とsalesforceの最終更新日を比較しsalesforce側の日付が新しければmysql側をupsertするというプログラムです。
|
6
|
-
|
7
|
-
|
8
|
-
実行すると
|
9
|
-
File "/home/dbcon/sample.py", line 96, in lo_update_companies
|
10
|
-
cur.execute(q)
|
11
|
-
UnboundLocalError: local variable 'q' referenced before assignment
|
12
|
-
|
13
|
-
|
1
|
+
一旦削除いたします。
|
14
|
-
色々試したのですが解決できず。。。。
|
15
|
-
解決方法を教えていただきたいです。
|
16
|
-
|
17
|
-
```python2.7
|
18
|
-
#!/usr/bin/env python
|
19
|
-
# -*- coding: utf-8 -*-
|
20
|
-
|
21
|
-
import s2hdb
|
22
|
-
from datetime import datetime as dt
|
23
|
-
from operator import add
|
24
|
-
#
|
25
|
-
# Salesforce handle Functions
|
26
|
-
#
|
27
|
-
|
28
|
-
def sf_get_last_modified_date():
|
29
|
-
sf = s2hdb.sf_get_descriptor()
|
30
|
-
q = "SELECT LastModifiedDate FROM Account " + \
|
31
|
-
"ORDER BY LastModifiedDate DESC LIMIT 1"
|
32
|
-
r = sf.query_all(q)
|
33
|
-
last_modified = dt.strptime(r["records"][0]["LastModifiedDate"],
|
34
|
-
'%Y-%m-%dT%H:%M:%S.000+0000')
|
35
|
-
return last_modified
|
36
|
-
|
37
|
-
def sf_get_updated_companies(last_modified):
|
38
|
-
sf = s2hdb.sf_get_descriptor()
|
39
|
-
q = "SELECT Name, Website, Description, Description_JP__c, " + \
|
40
|
-
"Core_Technology__c, Core_Technology_JP__c, Development_Status__c, " + \
|
41
|
-
"Development_Status_JP__c, Application__c, Application_JP__c, Tags__c" + \
|
42
|
-
" FROM Account WHERE LastModifiedDate > " + \
|
43
|
-
last_modified.strftime('%Y-%m-%dT%H:%M:%S.000+0000')
|
44
|
-
results = sf.query_all(q)
|
45
|
-
|
46
|
-
coms = []
|
47
|
-
for r in results["records"]:
|
48
|
-
com = []
|
49
|
-
elements = ['Name', 'Website', 'Description', 'Description_JP__c',
|
50
|
-
'Core_Technology__c', 'Core_Technology_JP__c', 'Development_Status__c',
|
51
|
-
'Development_Status_JP__c', 'Application__c', 'Application_JP__c', 'Tags__c']
|
52
|
-
elements = [str(e) for e in elements]
|
53
|
-
for e in elements:
|
54
|
-
# Convert NoneType Object as ""
|
55
|
-
if (r[e] is None):
|
56
|
-
r[e] = str("")
|
57
|
-
r[e] = r[e].encode('utf-8')
|
58
|
-
r[e] = r[e].replace('\'', '\'\'')
|
59
|
-
|
60
|
-
if (e == 'Tags__c'):
|
61
|
-
if (r[e] is None):
|
62
|
-
r[e] = str("")
|
63
|
-
com.append('{0}'.format(r[e]).strip().split(";"))
|
64
|
-
else:
|
65
|
-
com.append('\'{0}\''.format(r[e]))
|
66
|
-
|
67
|
-
# Duplication check.
|
68
|
-
# It seems salesforce returns duplicated records.
|
69
|
-
duplicated = False
|
70
|
-
for c in coms:
|
71
|
-
if coms[0] == '\'{0}\''.format(r["Name"]):
|
72
|
-
duplicated = True
|
73
|
-
if (duplicated is True):
|
74
|
-
continue
|
75
|
-
|
76
|
-
coms.append(com)
|
77
|
-
|
78
|
-
return coms
|
79
|
-
#
|
80
|
-
# Local DB handle functions
|
81
|
-
#
|
82
|
-
|
83
|
-
def lo_get_last_modified_date():
|
84
|
-
con = s2hdb.lo_get_descriptor()
|
85
|
-
cur = con.cursor()
|
86
|
-
q = "SELECT last_modified from company ORDER BY last_modified DESC LIMIT 1"
|
87
|
-
cur.execute(q)
|
88
|
-
if (cur.rowcount == 0):
|
89
|
-
# We want to move all from salesforce, so just set very early date
|
90
|
-
last_modified = dt.strptime("2000/1/1 00:00:00", '%Y/%m/%d %H:%M:%S')
|
91
|
-
else:
|
92
|
-
last_modified = cur.fetchone()[0]
|
93
|
-
cur.close()
|
94
|
-
|
95
|
-
return last_modified
|
96
|
-
|
97
|
-
def lo_update_companies(data):
|
98
|
-
if (len(data) == 0):
|
99
|
-
return
|
100
|
-
con = s2hdb.lo_get_descriptor()
|
101
|
-
cur = con.cursor()
|
102
|
-
|
103
|
-
coms = []
|
104
|
-
for t in data:
|
105
|
-
coms.append(t[0:10])
|
106
|
-
q = "INSERT INTO company(legal_name, website, desc, desc_jp, " + \
|
107
|
-
"core_tech, core_tech_jp, " + \
|
108
|
-
"development_status, development_status_jp, application, application_jp, cat_name) " + \
|
109
|
-
"VALUES " + \
|
110
|
-
", ".join(map(lambda x: "(" + ",".join(x) + ")", coms)) + " " + \
|
111
|
-
"ON DUPLICATE KEY UPDATE legal_name = VALUES (legal_name)" + \
|
112
|
-
cur.execute(q)
|
113
|
-
cur.close()
|
114
|
-
|
115
|
-
con.commit()
|
116
|
-
|
117
|
-
コード
|
118
|
-
```
|