質問編集履歴

2

schema 追加

2019/03/29 14:29

投稿

teityura
teityura

スコア84

test CHANGED
File without changes
test CHANGED
@@ -28,21 +28,35 @@
28
28
 
29
29
  入れたいJSON文字列
30
30
 
31
- [
31
+ sqlite> .schema target_table
32
32
 
33
- {'start': '2019-03-28 04:05:38', 'id': 1, 'end': '2019-03-28 13:34:58', 'status': 'Destroyed'},
33
+ CREATE TABLE `target_table` (
34
34
 
35
- {'start': '2019-03-28 07:10:34', 'id': 2, 'end': None, 'status': 'OK'},
35
+ `id` INTEGER NOT NULL DEFAULT '0',
36
36
 
37
- {'start': '2019-03-29 06:10:33', 'id': 3, 'end': None, 'status': 'Error'},
37
+ `status` TEXT DEFAULT NULL,
38
38
 
39
- {'start': '2019-03-29 06:10:35', 'id': 4, 'end': None, 'status': 'Error'},
39
+ `start` datetime DEFAULT NULL,
40
40
 
41
- {'start': '2019-03-29 06:10:38', 'id': 5, 'end': None, 'status': 'Error'},
41
+ `end` datetime DEFAULT NULL
42
42
 
43
- {'start': '2019-03-29 06:10:39', 'id': 6, 'end': None, 'status': 'Error'}]
43
+ );
44
44
 
45
+ 実行するinsert文?replace文?のイメージ
46
+
47
+ sqlite> replace into target_table select key from json_each([
48
+
49
+ {'start': '2019-03-28 04:05:38', 'id': 1, 'end': '2019-03-28 13:34:58', 'status': 'OK'},
50
+
51
+ {'start': '2019-03-29 06:10:33', 'id': 2, 'end': None, 'status': 'Error'},
52
+
53
+ {'start': '2019-03-29 06:10:35', 'id': 3, 'end': None, 'status': 'Error'},
54
+
55
+ {'start': '2019-03-29 06:10:38', 'id': 4, 'end': None, 'status': 'Error'},
56
+
57
+ {'start': '2019-03-29 06:10:39', 'id': 5, 'end': None, 'status': 'Error'}
58
+
45
- ]
59
+ ])
46
60
 
47
61
  """
48
62
 
@@ -62,6 +76,6 @@
62
76
 
63
77
  cursor = conn.cursor()
64
78
 
65
- cursor.executemany(query, args) # json文字列をインサートしたい
79
+ cursor.executemany(query, args) # json文字列を上のようにインサートしたい
66
80
 
67
81
  ```

1

誤字修正

2019/03/29 14:29

投稿

teityura
teityura

スコア84

test CHANGED
File without changes
test CHANGED
@@ -8,19 +8,45 @@
8
8
 
9
9
  [The JSON1 Extension - SQLite # json_each(json)](https://www.sqlite.org/json1.html#jeach)
10
10
 
11
+
12
+
11
13
  下記サイトの、この辺りでいけそうな気がしているのですが、
12
14
 
13
15
  使い方がいまいちよくわかりません。
14
16
 
15
17
  `replace into json_table select key,value from json_each('{"a":{"c":789},"b":456}')`
16
18
 
17
- <a href="https://qiita.com/SoraKumo/items/091328b5e5d18e357ed3#%E3%83%87%E3%83%BC%E3%82%BF%E3%81%AE%E6%8C%BF%E5%85%A5">SQLite3のJSON機能が便利だった2</a>
19
+ [SQLite3のJSON機能が便利だった2](https://qiita.com/SoraKumo/items/091328b5e5d18e357ed3#%E3%83%87%E3%83%BC%E3%82%BF%E3%81%AE%E6%8C%BF%E5%85%A5")
18
20
 
19
21
 
20
22
 
21
23
 
22
24
 
23
- ```
25
+ ``` python
26
+
27
+ """
28
+
29
+ 入れたいJSON文字列
30
+
31
+ [
32
+
33
+ {'start': '2019-03-28 04:05:38', 'id': 1, 'end': '2019-03-28 13:34:58', 'status': 'Destroyed'},
34
+
35
+ {'start': '2019-03-28 07:10:34', 'id': 2, 'end': None, 'status': 'OK'},
36
+
37
+ {'start': '2019-03-29 06:10:33', 'id': 3, 'end': None, 'status': 'Error'},
38
+
39
+ {'start': '2019-03-29 06:10:35', 'id': 4, 'end': None, 'status': 'Error'},
40
+
41
+ {'start': '2019-03-29 06:10:38', 'id': 5, 'end': None, 'status': 'Error'},
42
+
43
+ {'start': '2019-03-29 06:10:39', 'id': 6, 'end': None, 'status': 'Error'}]
44
+
45
+ ]
46
+
47
+ """
48
+
49
+
24
50
 
25
51
  # MySQLでSELECT文を実行し、
26
52