質問編集履歴
1
ソースコードを載せました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,13 +14,115 @@
|
|
14
14
|
KeyError: 'time'
|
15
15
|
|
16
16
|
### 該当のソースコード
|
17
|
+
import sqlite3
|
18
|
+
from flask import Flask,render_template,request,g
|
19
|
+
|
20
|
+
app = Flask(__name__)
|
21
|
+
|
22
|
+
def get_db():
|
23
|
+
if 'db' not in g:
|
24
|
+
# データベースをオープンしてFlaskのグローバル変数に保存
|
25
|
+
g.db = sqlite3.connect('LoginDB.db')
|
26
|
+
return g.db
|
27
|
+
|
28
|
+
@app.route('/')
|
29
|
+
def index():
|
30
|
+
|
31
|
+
# データベースを開く
|
32
|
+
con = get_db()
|
33
|
+
|
34
|
+
# テーブル「商品一覧」の有無を確認
|
35
|
+
cur = con.execute("select count(*) from sqlite_master where TYPE='table' AND name='日付送信'")
|
36
|
+
|
17
|
-
for row in cur:
|
37
|
+
for row in cur:
|
18
38
|
if row[0] == 0:
|
19
39
|
# テーブル「商品一覧」がなければ作成する
|
20
|
-
cur.execute("CREATE TABLE 商品一覧(コード INTEGER PRIMARY KEY, 商品名 STRING, 日付
|
40
|
+
cur.execute("CREATE TABLE 商品一覧(コード INTEGER PRIMARY KEY, 商品名 STRING, 日付 int)")
|
21
41
|
# レコードを作る
|
22
42
|
|
43
|
+
|
44
|
+
# 商品一覧を読み込み
|
45
|
+
cur = con.execute("select * from 商品一覧 order by コード")
|
46
|
+
data = cur.fetchall()
|
47
|
+
con.close()
|
48
|
+
|
49
|
+
return render_template('index.html', data = data)
|
50
|
+
|
51
|
+
@app.route('/result', methods=["POST"])
|
52
|
+
def result_post():
|
53
|
+
# テンプレートから新規登録する商品名と値段を取得
|
54
|
+
name = request.form["name"]
|
55
|
+
time = request.form["time"]
|
56
|
+
|
57
|
+
# データベースを開く
|
58
|
+
con = get_db()
|
59
|
+
|
60
|
+
# コードは既に登録されているコードの最大値+1の値で新規登録を行う
|
61
|
+
cur = con.execute("select MAX(コード) AS max_code from 商品一覧")
|
62
|
+
for row in cur:
|
63
|
+
new_code = row[0] + 1
|
64
|
+
cur.close()
|
65
|
+
|
66
|
+
# 登録処理
|
67
|
+
sql = "INSERT INTO 商品一覧(コード, 商品名, 値段)values({},'{}',{})".format(new_code, name, time)
|
68
|
+
con.execute(sql)
|
69
|
+
con.commit()
|
70
|
+
|
71
|
+
# 一覧再読み込み
|
72
|
+
cur = con.execute("select * from 商品一覧 order by コード")
|
73
|
+
data = cur.fetchall()
|
74
|
+
con.close()
|
75
|
+
|
76
|
+
return render_template('index.html', data = data)
|
77
|
+
|
78
|
+
if __name__ == '__main__':
|
79
|
+
app.debug = True
|
80
|
+
app.run(host='localhost')
|
23
81
|
python
|
82
|
+
|
83
|
+
HTML
|
84
|
+
|
85
|
+
<!DOCTYPE html>
|
86
|
+
<html lang="jp">
|
87
|
+
<head>
|
88
|
+
<meta charset="UTF-8">
|
89
|
+
<title>商品一覧ページ</title>
|
90
|
+
</head>
|
91
|
+
<body>
|
92
|
+
<div>
|
93
|
+
<table>
|
94
|
+
<thead>
|
95
|
+
<tr>
|
96
|
+
<th>コード</th>
|
97
|
+
<th>商品名</th>
|
98
|
+
<th>時間</th>
|
99
|
+
</tr>
|
100
|
+
</thead>
|
101
|
+
<tbody>
|
102
|
+
{% for item in data %}
|
103
|
+
<tr>
|
104
|
+
<th>{{item[0]}}</th>
|
105
|
+
<th>{{item[1]}}</th>
|
106
|
+
<th>{{item[2]}}</th>
|
107
|
+
</tr>
|
108
|
+
{% endfor %}
|
109
|
+
</tbody>
|
110
|
+
</table>
|
111
|
+
</div>
|
112
|
+
<form action='/result'>
|
113
|
+
<p><新規登録></p>
|
114
|
+
<label>
|
115
|
+
商品名
|
116
|
+
<input type="text" name="name">
|
117
|
+
</label>
|
118
|
+
<label >
|
119
|
+
時間
|
120
|
+
<input type="date" name="time" max="9999-12-31">
|
121
|
+
</label>
|
122
|
+
<button type="submit" formmethod="POST">登録</button>
|
123
|
+
</form>
|
124
|
+
</body>
|
125
|
+
</html>
|
24
126
|
ソースコード
|
25
127
|
```
|
26
128
|
時間
|