質問編集履歴
2
コードの変数を下記コメントの通りに修正しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -44,7 +44,7 @@
|
|
44
44
|
|
45
45
|
|
46
46
|
|
47
|
-
```
|
47
|
+
```python
|
48
48
|
|
49
49
|
import sqlite3
|
50
50
|
|
@@ -68,7 +68,11 @@
|
|
68
68
|
|
69
69
|
#table削除
|
70
70
|
|
71
|
+
create_tb = 'CREATE TABLE diet_table('+\
|
72
|
+
|
73
|
+
'id INTEGER PRIMARY KEY AUTOINCREMENT, '+\
|
74
|
+
|
71
|
-
|
75
|
+
'time DATE, weight DOUBLE NOT NULL, step BIGINT NOT NULL, contents TEXT NOT NULL)'
|
72
76
|
|
73
77
|
#tableを作成
|
74
78
|
|
@@ -90,318 +94,318 @@
|
|
90
94
|
|
91
95
|
#create_tb関数を呼び出し、executeで実行
|
92
96
|
|
97
|
+
#insert(cur, '', '1', '1', 'テスト')
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
con.commit()
|
102
|
+
|
103
|
+
#blist変数の中にtableの中の情報をすべて取得
|
104
|
+
|
105
|
+
blist =select_all(cur)
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
#DBとの接続を解除
|
110
|
+
|
111
|
+
con.close()
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
#%for~%endまでは登録した情報を表示する
|
116
|
+
|
117
|
+
if len(diet_table) > 0 :
|
118
|
+
|
119
|
+
tplt = """
|
120
|
+
|
121
|
+
<p>テーブルbottlesを作成しました</p>
|
122
|
+
|
123
|
+
<p>
|
124
|
+
|
125
|
+
%for cell in diet_table[-1]:
|
126
|
+
|
127
|
+
{{cell}},
|
128
|
+
|
129
|
+
%end
|
130
|
+
|
131
|
+
</p>
|
132
|
+
|
133
|
+
"""
|
134
|
+
|
135
|
+
return template(tplt, diet_table=diet_table)
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
return ('初期化操作失敗')
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
@route('/ayad')
|
144
|
+
|
145
|
+
def get_list():
|
146
|
+
|
147
|
+
con = sqlite3.connect('diet.db')
|
148
|
+
|
149
|
+
cur = con.cursor()
|
150
|
+
|
151
|
+
blist = select_all(cur)
|
152
|
+
|
153
|
+
con.close()
|
154
|
+
|
155
|
+
tplt = """
|
156
|
+
|
157
|
+
<h1>ダイエット記録</h1>
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
<p><a href='/ayad/new'>ダイエットを記録する</a></p>
|
162
|
+
|
163
|
+
<p><a href='/login'>管理者用</a></p>
|
164
|
+
|
165
|
+
<hr>
|
166
|
+
|
167
|
+
<table>
|
168
|
+
|
169
|
+
%for b in diet_table:
|
170
|
+
|
171
|
+
<tr>
|
172
|
+
|
173
|
+
<td>{{b[0]}}</td>
|
174
|
+
|
175
|
+
<td><a href='/ayad/{{b[0]}}'>{{b[1]}}</a></td>
|
176
|
+
|
177
|
+
</tr>
|
178
|
+
|
179
|
+
%end
|
180
|
+
|
181
|
+
</table>
|
182
|
+
|
183
|
+
"""
|
184
|
+
|
185
|
+
return template(tplt, diet_table=diet_table)
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
#入力した情報の詳細を表示
|
190
|
+
|
191
|
+
#↓
|
192
|
+
|
193
|
+
@route('/ayad/<id>')
|
194
|
+
|
195
|
+
def show_detail(id):
|
196
|
+
|
197
|
+
con = sqlite3.connect('diet.db')
|
198
|
+
|
199
|
+
cur = con.cursor()
|
200
|
+
|
201
|
+
btl = select_by_id(cur, id)
|
202
|
+
|
203
|
+
con.close()
|
204
|
+
|
205
|
+
tplt = """
|
206
|
+
|
207
|
+
<h1>データの詳細</h1>
|
208
|
+
|
209
|
+
<p><b>id: </b>{{btl[0]}}</p>
|
210
|
+
|
211
|
+
%for i in range(len(clms)):
|
212
|
+
|
213
|
+
<p><b>{{clms[i][1]}}: </b>{{btl[i+1]}}</p>
|
214
|
+
|
215
|
+
%end
|
216
|
+
|
217
|
+
<hr>
|
218
|
+
|
219
|
+
<p><a href='/ayad'>一覧に戻る</a></p>
|
220
|
+
|
221
|
+
"""
|
222
|
+
|
223
|
+
return template(tplt, clms = CLMS, btl = btl)
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
@get('/ayad/new')
|
228
|
+
|
229
|
+
def new():
|
230
|
+
|
231
|
+
tplt = """
|
232
|
+
|
233
|
+
<h1>データの新規作成</h1>
|
234
|
+
|
235
|
+
<form action ='/ayad/new' method = 'POST'>
|
236
|
+
|
237
|
+
<p>日程<br><input type="date" name="time"></p>
|
238
|
+
|
239
|
+
<p>体重<br><input type="number" name="weight"></p>
|
240
|
+
|
241
|
+
<p>歩数<br><input type="number" name="step"></p>
|
242
|
+
|
243
|
+
<p>我慢したこと/筋トレ内容<br><textarea name="contents" rows="4" cols="60"></textarea></p>
|
244
|
+
|
245
|
+
<input type='submit' value='GO'>
|
246
|
+
|
247
|
+
</form>
|
248
|
+
|
249
|
+
"""
|
250
|
+
|
251
|
+
return template(tplt)
|
252
|
+
|
253
|
+
#データを入力する/データの渡し先は/blist/new method = POSTに指定されている
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
@post('/ayad/new')
|
258
|
+
|
259
|
+
def added_new():
|
260
|
+
|
261
|
+
time=request.forms.get('time')
|
262
|
+
|
263
|
+
weight=request.forms.get('weight')
|
264
|
+
|
265
|
+
step=request.forms.get('step')
|
266
|
+
|
267
|
+
contents=request.forms.getunicode('contents')
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
con = sqlite3.connect('diet.db')
|
272
|
+
|
273
|
+
cur = con.cursor()
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
insert(cur, time, weight, step, contents)
|
278
|
+
|
279
|
+
con.commit()
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
blist= select_all(cur)
|
284
|
+
|
285
|
+
con.close()
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
tplt = """
|
290
|
+
|
291
|
+
<h1>データを保存しました</h1>
|
292
|
+
|
293
|
+
""" + detail_template()
|
294
|
+
|
295
|
+
return template(tplt, clms=CLMS, btl = diet_table[-1])
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
def detail_template():
|
302
|
+
|
303
|
+
tplt = """
|
304
|
+
|
305
|
+
<p><b>id: </b>{{btl[0]}}</p>
|
306
|
+
|
307
|
+
%for i in range(len(clms)):
|
308
|
+
|
309
|
+
<p><b>{{clms[i][0]}}: </b>{{btl[i+1]}}</p>
|
310
|
+
|
311
|
+
%end
|
312
|
+
|
313
|
+
<hr>
|
314
|
+
|
315
|
+
<p><a href='/ayad'>一覧に戻る</a></p>
|
316
|
+
|
317
|
+
<p><a href='/ayad/update/{{btl[0]}}'>データを更新</a></p>
|
318
|
+
|
319
|
+
<p><a href='/ayad/delete/{{btl[0]}}'>データを削除</a></p>
|
320
|
+
|
321
|
+
"""
|
322
|
+
|
323
|
+
return tplt
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
def insert(cur, time, weight, step, contents):
|
334
|
+
|
335
|
+
insert_str = 'INSERT INTO deit_table(time, weight, step, contents)' +\
|
336
|
+
|
337
|
+
'VALUES (?,?,?,?)'
|
338
|
+
|
339
|
+
cur.execute(insert_str, (time, weight, step, contents))
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
def select_all (cur):
|
344
|
+
|
345
|
+
select_str = 'SELECT * FROM diet_table'
|
346
|
+
|
347
|
+
cur.execute(select_str)
|
348
|
+
|
349
|
+
return cur.fetchall()
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
def select_by_id(cur, id):
|
354
|
+
|
355
|
+
select_str = 'SELECT * FROM diet_table WHERE id= ?'
|
356
|
+
|
357
|
+
cur.execute(select_str, id)
|
358
|
+
|
359
|
+
return cur.fetchone()
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
def update(cur, id, time, weight, step, contents):
|
364
|
+
|
365
|
+
upd_str = 'UPDATE diet_table SET time=?, weight=?, step=?, contents=? WHERE id =?'
|
366
|
+
|
367
|
+
cur.execute(upd_str, (time, weight, step, contents, id) )
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
def delete(cur, id):
|
372
|
+
|
373
|
+
delete_str = 'DELETE FROM diet_table where id=?'
|
374
|
+
|
375
|
+
cur.execute(delete_str, id)
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
CLMS=[('time', '日付'), ('weight', '体重'), ('step', '歩数'), ('contents', '内容')]
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
run(host='localhost', port=8080, reloader=True, debug=True)
|
384
|
+
|
385
|
+
```
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
**備考**
|
390
|
+
|
391
|
+
デバックは以下のようになります。
|
392
|
+
|
393
|
+
「> File "C:\Users\bubbl\AppData\Local\Programs\Python\Python310\lib\site-packages\bottle.py", line 868, in _handle
|
394
|
+
|
395
|
+
return route.call(**args)
|
396
|
+
|
397
|
+
File "C:\Users\bubbl\AppData\Local\Programs\Python\Python310\lib\site-packages\bottle.py", line 1748, in wrapper
|
398
|
+
|
399
|
+
rv = callback(*a, **ka)
|
400
|
+
|
401
|
+
File "C:\Users\bubbl\Desktop\aya_diet\aya_diet.py", line 23, in init
|
402
|
+
|
93
403
|
insert(cur, '', '1', '1', 'テスト')
|
94
404
|
|
95
|
-
|
96
|
-
|
97
|
-
con.commit()
|
98
|
-
|
99
|
-
#blist変数の中にtableの中の情報をすべて取得
|
100
|
-
|
101
|
-
blist =select_all(cur)
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
#DBとの接続を解除
|
106
|
-
|
107
|
-
con.close()
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
#%for~%endまでは登録した情報を表示する
|
112
|
-
|
113
|
-
if len(diet) > 0 :
|
114
|
-
|
115
|
-
tplt = """
|
116
|
-
|
117
|
-
<p>テーブルbottlesを作成しました</p>
|
118
|
-
|
119
|
-
<p>
|
120
|
-
|
121
|
-
%for cell in diet[-1]:
|
122
|
-
|
123
|
-
{{cell}},
|
124
|
-
|
125
|
-
%end
|
126
|
-
|
127
|
-
</p>
|
128
|
-
|
129
|
-
"""
|
130
|
-
|
131
|
-
return template(tplt, diet=diet_table)
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
return ('初期化操作失敗')
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
@route('/ayad')
|
140
|
-
|
141
|
-
def get_list():
|
142
|
-
|
143
|
-
con = sqlite3.connect('ayad.db')
|
144
|
-
|
145
|
-
cur = con.cursor()
|
146
|
-
|
147
|
-
blist = select_all(cur)
|
148
|
-
|
149
|
-
con.close()
|
150
|
-
|
151
|
-
tplt = """
|
152
|
-
|
153
|
-
<h1>ダイエット記録</h1>
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
<p><a href='/ayad/new'>ダイエットを記録する</a></p>
|
158
|
-
|
159
|
-
<p><a href='/login'>管理者用</a></p>
|
160
|
-
|
161
|
-
<hr>
|
162
|
-
|
163
|
-
<table>
|
164
|
-
|
165
|
-
%for b in diet:
|
166
|
-
|
167
|
-
<tr>
|
168
|
-
|
169
|
-
<td>{{b[0]}}</td>
|
170
|
-
|
171
|
-
<td><a href='/ayad/{{b[0]}}'>{{b[1]}}</a></td>
|
172
|
-
|
173
|
-
</tr>
|
174
|
-
|
175
|
-
%end
|
176
|
-
|
177
|
-
</table>
|
178
|
-
|
179
|
-
"""
|
180
|
-
|
181
|
-
return template(tplt, blist=blist)
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
#入力した情報の詳細を表示
|
186
|
-
|
187
|
-
#↓
|
188
|
-
|
189
|
-
@route('/ayad/<id>')
|
190
|
-
|
191
|
-
def show_detail(id):
|
192
|
-
|
193
|
-
con = sqlite3.connect('diet.db')
|
194
|
-
|
195
|
-
cur = con.cursor()
|
196
|
-
|
197
|
-
btl = select_by_id(cur, id)
|
198
|
-
|
199
|
-
con.close()
|
200
|
-
|
201
|
-
tplt = """
|
202
|
-
|
203
|
-
<h1>データの詳細</h1>
|
204
|
-
|
205
|
-
<p><b>id: </b>{{btl[0]}}</p>
|
206
|
-
|
207
|
-
%for i in range(len(clms)):
|
208
|
-
|
209
|
-
<p><b>{{clms[i][1]}}: </b>{{btl[i+1]}}</p>
|
210
|
-
|
211
|
-
%end
|
212
|
-
|
213
|
-
<hr>
|
214
|
-
|
215
|
-
<p><a href='/ayad'>一覧に戻る</a></p>
|
216
|
-
|
217
|
-
"""
|
218
|
-
|
219
|
-
return template(tplt, clms = CLMS, btl = btl)
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
@get('/ayad/new')
|
224
|
-
|
225
|
-
def new():
|
226
|
-
|
227
|
-
tplt = """
|
228
|
-
|
229
|
-
<h1>データの新規作成</h1>
|
230
|
-
|
231
|
-
<form action ='/ayad/new' method = 'POST'>
|
232
|
-
|
233
|
-
|
405
|
+
File "C:\Users\bubbl\Desktop\aya_diet\aya_diet.py", line 144, in insert
|
234
|
-
|
235
|
-
<p>体重<br><input type="number" name="weight"></p>
|
236
|
-
|
237
|
-
<p>歩数<br><input type="number" name="step"></p>
|
238
|
-
|
239
|
-
<p>我慢したこと/筋トレ内容<br><textarea name="contents" rows="4" cols="60"></textarea></p>
|
240
|
-
|
241
|
-
<input type='submit' value='GO'>
|
242
|
-
|
243
|
-
</form>
|
244
|
-
|
245
|
-
"""
|
246
|
-
|
247
|
-
return template(tplt)
|
248
|
-
|
249
|
-
#データを入力する/データの渡し先は/blist/new method = POSTに指定されている
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
@post('/ayad/new')
|
254
|
-
|
255
|
-
def added_new():
|
256
|
-
|
257
|
-
time=request.forms.get('time')
|
258
|
-
|
259
|
-
weight=request.forms.get('weight')
|
260
|
-
|
261
|
-
step=request.forms.get('step')
|
262
|
-
|
263
|
-
contents=request.forms.getunicode('contents')
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
con = sqlite3.connect('diet.db')
|
268
|
-
|
269
|
-
cur = con.cursor()
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
insert(cur, time, weight, step, contents)
|
274
|
-
|
275
|
-
con.commit()
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
blist= select_all(cur)
|
280
|
-
|
281
|
-
con.close()
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
tplt = """
|
286
|
-
|
287
|
-
<h1>データを保存しました</h1>
|
288
|
-
|
289
|
-
""" + detail_template()
|
290
|
-
|
291
|
-
return template(tplt, clms=CLMS, btl = blist[-1])
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
def detail_template():
|
298
|
-
|
299
|
-
tplt = """
|
300
|
-
|
301
|
-
<p><b>id: </b>{{btl[0]}}</p>
|
302
|
-
|
303
|
-
%for i in range(len(clms)):
|
304
|
-
|
305
|
-
<p><b>{{clms[i][0]}}: </b>{{btl[i+1]}}</p>
|
306
|
-
|
307
|
-
%end
|
308
|
-
|
309
|
-
<hr>
|
310
|
-
|
311
|
-
<p><a href='/ayad'>一覧に戻る</a></p>
|
312
|
-
|
313
|
-
<p><a href='/ayad/update/{{btl[0]}}'>データを更新</a></p>
|
314
|
-
|
315
|
-
<p><a href='/ayad/delete/{{btl[0]}}'>データを削除</a></p>
|
316
|
-
|
317
|
-
"""
|
318
|
-
|
319
|
-
return tplt
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
def insert(cur, time, weight, step, contents):
|
330
|
-
|
331
|
-
insert_str = 'INSERT INTO deit(time, weight, step, contents)' +\
|
332
|
-
|
333
|
-
'VALUES (?,?,?,?)'
|
334
406
|
|
335
407
|
cur.execute(insert_str, (time, weight, step, contents))
|
336
408
|
|
337
|
-
|
338
|
-
|
339
|
-
def select_all (cur):
|
340
|
-
|
341
|
-
select_str = 'SELECT * FROM diet'
|
342
|
-
|
343
|
-
cur.execute(select_str)
|
344
|
-
|
345
|
-
return cur.fetchall()
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
def select_by_id(cur, id):
|
350
|
-
|
351
|
-
select_str = 'SELECT * FROM diet WHERE id= ?'
|
352
|
-
|
353
|
-
cur.execute(select_str, id)
|
354
|
-
|
355
|
-
return cur.fetchone()
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
def update(cur, id, time, weight, step, contents):
|
360
|
-
|
361
|
-
upd_str = 'UPDATE diet SET time=?, weight=?, step=?, contents=? WHERE id =?'
|
362
|
-
|
363
|
-
cur.execute(upd_str, (time, weight, step, contents, id) )
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
def delete(cur, id):
|
368
|
-
|
369
|
-
delete_str = 'DELETE FROM diet where id=?'
|
370
|
-
|
371
|
-
cur.execute(delete_str, id)
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
CLMS=[('time', '日付'), ('weight', '体重'), ('step', '歩数'), ('contents', '内容')]
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
run(host='localhost', port=8080, reloader=True, debug=True)
|
380
|
-
|
381
|
-
```
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
**備考**
|
386
|
-
|
387
|
-
デバックは以下のようになります。
|
388
|
-
|
389
|
-
「> File "C:\Users\bubbl\AppData\Local\Programs\Python\Python310\lib\site-packages\bottle.py", line 868, in _handle
|
390
|
-
|
391
|
-
return route.call(**args)
|
392
|
-
|
393
|
-
File "C:\Users\bubbl\AppData\Local\Programs\Python\Python310\lib\site-packages\bottle.py", line 1748, in wrapper
|
394
|
-
|
395
|
-
rv = callback(*a, **ka)
|
396
|
-
|
397
|
-
File "C:\Users\bubbl\Desktop\aya_diet\aya_diet.py", line 23, in init
|
398
|
-
|
399
|
-
insert(cur, '', '1', '1', 'テスト')
|
400
|
-
|
401
|
-
File "C:\Users\bubbl\Desktop\aya_diet\aya_diet.py", line 144, in insert
|
402
|
-
|
403
|
-
cur.execute(insert_str, (time, weight, step, contents))
|
404
|
-
|
405
409
|
sqlite3.OperationalError: no such table: deit
|
406
410
|
|
407
411
|
127.0.0.1 - - [30/Nov/2021 22:27:41] "GET /ayad/init HTTP/1.1" 500 1615」
|
1
エラーの原因の行がayad/initのcreate構文を突き止めましたが、なぜその行がエラーなのかがわかりません...
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,6 +34,14 @@
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
+
**追記**
|
38
|
+
|
39
|
+
ayad/initのinsert文をコメントアウトし、デバックを確認すると、ayad/initのところにのif文のところで「tableがない」となりました。
|
40
|
+
|
41
|
+
上記から、create構文でtableが作られてないことが原因と考えられます。
|
42
|
+
|
43
|
+
ayad/initのcreate構文はどこがエラーなのでしょうか…?
|
44
|
+
|
37
45
|
|
38
46
|
|
39
47
|
```ここに言語を入力
|