質問編集履歴

4

修正しました。

2021/06/08 23:32

投稿

python_k
python_k

スコア7

test CHANGED
File without changes
test CHANGED
@@ -6,4 +6,86 @@
6
6
 
7
7
  よろしくお願いします。
8
8
 
9
- test="[[{"id":100,"value":250,"unit":"string","date":datetime.datetime(2021,6,4,17,00)}],[{"id":100,"value":250,"unit":"string","date":datetime.datetime(2021,6,4,17,30)}]"
9
+ test="[[{"id":100,"value":250,"unit":"string","date":"2021/6/4 17:00"}],[{"id":101,"value":250,"unit":"string","date":"2021/6/4 17:30"}]]"
10
+
11
+
12
+
13
+ 下記がデータの送信側のコードです。
14
+
15
+ ```python
16
+
17
+ import json
18
+
19
+ import sys
20
+
21
+ import pymysql
22
+
23
+
24
+
25
+ db_host="*****"
26
+
27
+ name="*****"
28
+
29
+ password="*****"
30
+
31
+ db_name="*****"
32
+
33
+ table="*****"
34
+
35
+
36
+
37
+ try:
38
+
39
+ conn = pymysql.connect(host=db_host, user=name, passwd=password, db=db_name, connect_timeout=25,cursorclass=pymysql.cursors.DictCursor)
40
+
41
+ except:
42
+
43
+ sys.exit()
44
+
45
+
46
+
47
+ def lambda_handler(event, context):
48
+
49
+ cur=conn.cursor()
50
+
51
+ cur.execute("SELECT MAX(date),id FROM {} GROUP BY id;".format(table))
52
+
53
+ results=cur.fetchall()
54
+
55
+ AllDatas=[]
56
+
57
+ for i in results:
58
+
59
+ ID=i["id"]
60
+
61
+ Datetime=i["MAX(date)"]
62
+
63
+ cur.execute("""SELECT id,date,value,unit FROM {} WHERE id="{}" AND date="{}";""".format(table,ID,Datetime))
64
+
65
+ result=cur.fetchall()
66
+
67
+ result[0]["date"]=str(result[0]["date"])
68
+
69
+ AllDatas.append(result[0])
70
+
71
+ response={
72
+
73
+ "statusCode":200,
74
+
75
+ "body":json.dumps(AllDatas),
76
+
77
+ "headers":{
78
+
79
+ "Access-Control-Allow-Origin":"*"
80
+
81
+ }
82
+
83
+ }
84
+
85
+ return response
86
+
87
+ cur.close()
88
+
89
+ conn.close()
90
+
91
+ ```

3

修正しました。

2021/06/08 23:32

投稿

python_k
python_k

スコア7

test CHANGED
File without changes
test CHANGED
@@ -7,77 +7,3 @@
7
7
  よろしくお願いします。
8
8
 
9
9
  test="[[{"id":100,"value":250,"unit":"string","date":datetime.datetime(2021,6,4,17,00)}],[{"id":100,"value":250,"unit":"string","date":datetime.datetime(2021,6,4,17,30)}]"
10
-
11
-
12
-
13
- 下記のようなコードでSQLから取得してきたデータになります。
14
-
15
- ```python
16
-
17
- import json
18
-
19
- import sys
20
-
21
- import pymysql
22
-
23
-
24
-
25
- rds_host="****"
26
-
27
- name="****"
28
-
29
- password="****"
30
-
31
- db_name="****"
32
-
33
- table="****"
34
-
35
-
36
-
37
- try:
38
-
39
- conn = pymysql.connect(host=rds_host, user=name, passwd=password, db=db_name, connect_timeout=25,cursorclass=pymysql.cursors.DictCursor)
40
-
41
- except:
42
-
43
- sys.exit()
44
-
45
- def lambda_handler(event, context):
46
-
47
-   cur=conn.cursor()
48
-
49
- cur.execute("SELECT MAX(date),id FROM {} GROUP BY id;".format(table))
50
-
51
- results=cur.fetchall()
52
-
53
- AllDatas=[]
54
-
55
- for i in results:
56
-
57
- ID=i["id"]
58
-
59
- Datetime=i["MAX(date)"]
60
-
61
- cur.execute("""SELECT id,date,value,unit FROM {} WHERE id="{}" AND date="{}";""".format(table,ID,Datetime))
62
-
63
- result=cur.fetchall()
64
-
65
- AllDatas.append(result)
66
-
67
-
68
-
69
- response={
70
-
71
- "statusCode":200,
72
-
73
- "body":json.dumps(str(AllDatas)),
74
-
75
- "headers":{
76
-
77
- "Access-Control-Allow-Origin":"*"
78
-
79
- }
80
-
81
- }
82
-
83
- ```

2

修正しました。

2021/06/07 23:28

投稿

python_k
python_k

スコア7

test CHANGED
File without changes
test CHANGED
@@ -7,3 +7,77 @@
7
7
  よろしくお願いします。
8
8
 
9
9
  test="[[{"id":100,"value":250,"unit":"string","date":datetime.datetime(2021,6,4,17,00)}],[{"id":100,"value":250,"unit":"string","date":datetime.datetime(2021,6,4,17,30)}]"
10
+
11
+
12
+
13
+ 下記のようなコードでSQLから取得してきたデータになります。
14
+
15
+ ```python
16
+
17
+ import json
18
+
19
+ import sys
20
+
21
+ import pymysql
22
+
23
+
24
+
25
+ rds_host="****"
26
+
27
+ name="****"
28
+
29
+ password="****"
30
+
31
+ db_name="****"
32
+
33
+ table="****"
34
+
35
+
36
+
37
+ try:
38
+
39
+ conn = pymysql.connect(host=rds_host, user=name, passwd=password, db=db_name, connect_timeout=25,cursorclass=pymysql.cursors.DictCursor)
40
+
41
+ except:
42
+
43
+ sys.exit()
44
+
45
+ def lambda_handler(event, context):
46
+
47
+   cur=conn.cursor()
48
+
49
+ cur.execute("SELECT MAX(date),id FROM {} GROUP BY id;".format(table))
50
+
51
+ results=cur.fetchall()
52
+
53
+ AllDatas=[]
54
+
55
+ for i in results:
56
+
57
+ ID=i["id"]
58
+
59
+ Datetime=i["MAX(date)"]
60
+
61
+ cur.execute("""SELECT id,date,value,unit FROM {} WHERE id="{}" AND date="{}";""".format(table,ID,Datetime))
62
+
63
+ result=cur.fetchall()
64
+
65
+ AllDatas.append(result)
66
+
67
+
68
+
69
+ response={
70
+
71
+ "statusCode":200,
72
+
73
+ "body":json.dumps(str(AllDatas)),
74
+
75
+ "headers":{
76
+
77
+ "Access-Control-Allow-Origin":"*"
78
+
79
+ }
80
+
81
+ }
82
+
83
+ ```

1

修正しました。

2021/06/07 06:56

投稿

python_k
python_k

スコア7

test CHANGED
File without changes
test CHANGED
@@ -6,4 +6,4 @@
6
6
 
7
7
  よろしくお願いします。
8
8
 
9
- test="[[{"A":100,"B":"250,"date":datetime.datetime(2021,6,4,17,00)}],[{"A":200,"B":"250,"date":datetime.datetime(2021,6,4,17,30)}]"
9
+ test="[[{"id":100,"value":250,"unit":"string","date":datetime.datetime(2021,6,4,17,00)}],[{"id":100,"value":250,"unit":"string","date":datetime.datetime(2021,6,4,17,30)}]"