質問編集履歴

5

コードの修正(SQL文の , の消去)

2019/01/16 10:37

投稿

ChaCha_MaRu
ChaCha_MaRu

スコア15

test CHANGED
File without changes
test CHANGED
@@ -106,7 +106,7 @@
106
106
 
107
107
  item_code TEXT NOT NULL,
108
108
 
109
- amount INTEGER,
109
+ amount INTEGER
110
110
 
111
111
  )
112
112
 
@@ -114,7 +114,7 @@
114
114
 
115
115
  except:
116
116
 
117
- pass
117
+ print("error")
118
118
 
119
119
 
120
120
 
@@ -173,3 +173,7 @@
173
173
 
174
174
 
175
175
  ```
176
+
177
+
178
+
179
+ エラー処理も含めて変えたのですが、エラーの表記すらされずどこで躓いているのかがわからないです。

4

2019/01/16 10:37

投稿

ChaCha_MaRu
ChaCha_MaRu

スコア15

test CHANGED
File without changes
test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
 
24
24
 
25
- def create_sql(item_name):
25
+ def create_sql():
26
26
 
27
27
 
28
28
 

3

2019/01/15 23:46

投稿

ChaCha_MaRu
ChaCha_MaRu

スコア15

test CHANGED
File without changes
test CHANGED
File without changes

2

コードを簡略化しました。

2019/01/15 16:00

投稿

ChaCha_MaRu
ChaCha_MaRu

スコア15

test CHANGED
@@ -1 +1 @@
1
- Tkinter チェックボック、画面遷移について
1
+ python データベースについて
test CHANGED
@@ -1,3 +1,9 @@
1
+ pythonのデータベースについてです。
2
+
3
+ 3つのEntryに記述されたものをデータベースへ格納したいです。下記のコードでは登録を押しても「登録しました」と表示されず登録が完了しません。不備がどこにあるでしょうか。アドバイスよろしくお願いします。
4
+
5
+
6
+
1
7
  ```python3
2
8
 
3
9
  # -*- coding: utf-8 -*-
@@ -14,111 +20,9 @@
14
20
 
15
21
 
16
22
 
17
- # ログイン画面関数
18
-
19
- def login_gui():
20
-
21
- #ログインの認証
22
-
23
- def foo():
24
-
25
- #正規のユーザーの時create_gui(メイン画面)へ
26
-
27
- if t.get() == "hello":
28
-
29
- root.destroy()
30
-
31
- create_gui()
32
-
33
- #ユーザ名が違うときpass
34
-
35
- else:
36
-
37
- pass
38
23
 
39
24
 
40
-
41
- # root定義
42
-
43
- root = tk.Tk()
44
-
45
- root.title('login')
46
-
47
- frame1 = tk.Frame(root)
48
-
49
- label1 = tk.Label(frame1, text='Your name:')
50
-
51
-
52
-
53
- #入力された文字の取得
54
-
55
- t = tk.StringVar()
56
-
57
-
58
-
59
- #メニュー定義
60
-
61
- entry1 = tk.Entry(frame1, textvariable=t)
62
-
63
- button1 = tk.Button(frame1, text='OK', command=foo)
64
-
65
-
66
-
67
- frame1.grid(row=0,column=0,sticky=(N,E,S,W))
68
-
69
- label1.grid(row=1,column=1,sticky=E)
70
-
71
- entry1.grid(row=1,column=2,sticky=W)
72
-
73
- button1.grid(row=2,column=2,sticky=W)
74
-
75
-
76
-
77
- for child in frame1.winfo_children():
78
-
79
- child.grid_configure(padx=5, pady=5)
80
-
81
-
82
-
83
- root.mainloop()
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
- # 登録画面のGUI
92
-
93
- def create_gui():
94
-
95
- # ----------------------------------------
96
-
97
- # コールバック関数群
98
-
99
- # ----------------------------------------
100
-
101
- # 表示ボタンが押下されたときのコールバック関数
102
-
103
- def select_button():
104
-
105
- root.destroy()
106
-
107
- select_gui()
108
-
109
- # ----------------------------------------
110
-
111
- # 終了ボタンが押下されたときのコールバック関数
112
-
113
- def quit_button():
114
-
115
- root.destroy()
116
-
117
- # ----------------------------------------
118
-
119
- # 登録ボタンがクリックされた時にデータをDBに登録するコールバック関数
120
-
121
- def create_sql(item_name):
25
+ def create_sql(item_name):
122
26
 
123
27
 
124
28
 
@@ -126,33 +30,21 @@
126
30
 
127
31
  c = sqlite3.connect("database.db")
128
32
 
129
- # item_nameをWHERE句に渡してitem_codeを取得する
130
-
131
- item_code = c.execute("""
132
-
133
- SELECT item_code FROM item
33
+ # 日付の読み取り
134
-
135
- WHERE item_name = '{}'
136
-
137
- """.format(item_name))
138
-
139
- item_code = item_code.fetchone()[0]
140
-
141
-
142
34
 
143
35
  acc_data = entry1.get().replace("/","-")
144
36
 
145
-
37
+ # 内容の読み取り
38
+
39
+ item_code = entry2.get()
40
+
41
+ # 金額の読み取り
146
42
 
147
43
  amount = entry3.get()
148
44
 
149
45
 
150
46
 
151
- # SQLを発行してDBへ登録
47
+ #
152
-
153
- # python2の場合は、ユニコード文字列でsqlite3に渡す
154
-
155
- # また、コミットする場合は、commitメソッドを用いる
156
48
 
157
49
  try:
158
50
 
@@ -166,77 +58,39 @@
166
58
 
167
59
  c.execute("COMMIT;")
168
60
 
169
- print("1件登録しました")
61
+ print("登録しました")
170
62
 
171
-
63
+ # ドメインエラーなどにより登録できなかった場合のエラー処理
172
64
 
173
65
  except:
174
66
 
175
- print("エラーにより登録できませんでした")
67
+ print("エラー")
176
68
 
177
69
  # ----------------------------------------
178
70
 
179
- # 内訳テーブル(item)にあるitem_nameのタプルを作成する
180
71
 
181
- def createitemname():
182
72
 
183
- # データベースの接続
184
73
 
185
- c = sqlite3.connect("database.db")
186
-
187
- # 空の「リスト型」を定義
188
-
189
- li = []
190
-
191
- # SELECT文を発行し、item_nameを取得し、for文で回す
192
-
193
- for r in c.execute("SELECT item_name FROM item"):
194
-
195
- # item_nameをリストに追加する
196
-
197
- li.append(r)
198
-
199
- # リスト型のliをタプル型に変換して、ファンクションに戻す
200
-
201
- return tuple(li)
202
74
 
203
75
  # ----------------------------------------
204
76
 
205
77
 
206
78
 
207
-
79
+ # 空のデータベースを作成して接続する
208
80
 
209
- dbname = "database.db"
81
+ dbname = "database.db"
210
82
 
211
- c = sqlite3.connect(dbname)
83
+ c = sqlite3.connect(dbname)
212
-
213
- c.execute("PRAGMA foreign_keys = 1")
214
84
 
215
85
 
216
86
 
217
-
218
87
 
219
- try:
220
88
 
221
- # itemテールの定義
89
+ # 既にデータベースが登録されている場合は、ddlの発行でエラーが出るのでexceptブロックで回避する
222
90
 
223
- ddl = """
91
+ try:
224
92
 
225
- CREATE TABLE item
93
+
226
-
227
- (
228
-
229
- item_code INTEGER PRIMARY KEY AUTOINCREMENT,
230
-
231
- item_name TEXT NOT NULL UNIQUE
232
-
233
- )
234
-
235
- """
236
-
237
- # SQLの発行
238
-
239
- c.execute(ddl)
240
94
 
241
95
  # acc_dataテーブルの定義
242
96
 
@@ -250,307 +104,17 @@
250
104
 
251
105
  acc_date DATE NOT NULL,
252
106
 
253
- item_code INTEGER NOT NULL,
107
+ item_code TEXT NOT NULL,
254
108
 
255
- amount INTEGER,
109
+ amount INTEGER,
256
-
257
- FOREIGN KEY(item_code) REFERENCES item(item_code)
258
110
 
259
111
  )
260
112
 
261
113
  """
262
114
 
263
- # itemテーブルへリファレンスデータの登録
264
-
265
- # 初回起動時に書き換えることで変更可能
266
-
267
- c.execute(ddl)
268
-
269
- c.execute("INSERT INTO item VALUES(1,'食費')")
270
-
271
- c.execute("INSERT INTO item VALUES(2,'住宅費')")
272
-
273
- c.execute("INSERT INTO item VALUES(3,'光熱費')")
274
-
275
- c.execute("COMMIT")
276
-
277
- except:
115
+ except:
278
116
 
279
117
  pass
280
-
281
-
282
-
283
- # rootフレームの設定
284
-
285
- root = tk.Tk()
286
-
287
- root.title("家計簿アプリ")
288
-
289
- root.geometry("300x280")
290
-
291
-
292
-
293
- # メニューの設定
294
-
295
- frame = tk.Frame(root,bd=2,relief="ridge")
296
-
297
- frame.pack(fill="x")
298
-
299
- button1 = tk.Button(frame,text="入力")
300
-
301
- button1.pack(side="left")
302
-
303
- button2 = tk.Button(frame,text="表示",command=select_button)
304
-
305
- button2.pack(side="left")
306
-
307
- button3 = tk.Button(frame,text="終了",command=quit_button)
308
-
309
- button3.pack(side="right")
310
-
311
-
312
-
313
- # 入力画面ラベルの設定
314
-
315
- label1 = tk.Label(root,text="【入力画面】",font=("",16),height=2)
316
-
317
- label1.pack(fill="x")
318
-
319
-
320
-
321
- # 日付のラベルとエントリーの設定
322
-
323
- frame1 = tk.Frame(root,pady=10)
324
-
325
- frame1.pack()
326
-
327
- label2 = tk.Label(frame1,font=("",14),text="日付")
328
-
329
- label2.pack(side="left")
330
-
331
- entry1 = tk.Entry(frame1,font=("",14),justify="center",width=15)
332
-
333
- entry1.pack(side="left")
334
-
335
-
336
-
337
- # 内訳のラベルとエントリーの設定
338
-
339
- frame2 = tk.Frame(root,pady=10)
340
-
341
- frame2.pack()
342
-
343
- label3 = tk.Label(frame2,font=("",14),text="内訳")
344
-
345
- label3.pack(side="left")
346
-
347
- # 内訳コンボボックスの作成
348
-
349
- combo = ttk.Combobox(frame2, state='readonly',font=("",14),width=13)
350
-
351
- combo["values"] = createitemname()
352
-
353
- combo.current(0)
354
-
355
- combo.pack()
356
-
357
-
358
-
359
- # 金額のラベルとエントリーの設定
360
-
361
- frame3 = tk.Frame(root,pady=10)
362
-
363
- frame3.pack()
364
-
365
- label4 = tk.Label(frame3,font=("",14),text="金額")
366
-
367
- label4.pack(side="left")
368
-
369
- entry3 = tk.Entry(frame3,font=("",14),justify="center",width=15)
370
-
371
- entry3.pack(side="left")
372
-
373
-
374
-
375
- # 登録ボタンの設定
376
-
377
- button4 = tk.Button(root,text="登録",
378
-
379
- font=("",16),
380
-
381
- width=10,bg="gray",
382
-
383
- command=lambda:create_sql(combo.get()))
384
-
385
- button4.pack()
386
-
387
-
388
-
389
- root.mainloop()
390
-
391
-
392
-
393
- login_gui()
394
-
395
-
396
-
397
- # 表示画面のGUI
398
-
399
- def select_gui():
400
-
401
- # ----------------------------------------
402
-
403
- # コールバック関数群
404
-
405
- # ----------------------------------------
406
-
407
- # 登録ボタンが押下されたときのコールバック関数
408
-
409
- def create_button():
410
-
411
- root.destroy()
412
-
413
- create_gui()
414
-
415
- # ----------------------------------------
416
-
417
- # 終了ボタンが押下されたときのコールバック関数
418
-
419
- def quit_button():
420
-
421
- root.destroy()
422
-
423
- # ----------------------------------------
424
-
425
- # 表示ボタンが押下されたときのコールバック関数
426
-
427
- def select_sql(start,end):
428
-
429
- # treeviewのアイテムをすべて削除
430
-
431
- tree.delete(*tree.get_children())
432
-
433
- # 開始日と終了日が空欄だったらデフォルト値の設定
434
-
435
- if start == "":
436
-
437
- start = "1900-01-01"
438
-
439
- if end == "":
440
-
441
- end = "2100-01-01"
442
-
443
- #SELECT文の作成
444
-
445
- sql = """
446
-
447
- SELECT acc_date,item_name,amount
448
-
449
- FROM acc_data as a,item as i
450
-
451
- WHERE a.item_code = i.item_code AND
452
-
453
- acc_date BETWEEN '{}' AND '{}'
454
-
455
- ORDER BY acc_date
456
-
457
- """.format(start,end)
458
-
459
- # ツリービューにアイテムの追加
460
-
461
- gokei=0
462
-
463
- i=0
464
-
465
- for r in c.execute(sql):
466
-
467
- #期間内の合計の金額を格納 gokei
468
-
469
- gokei+=r[2]
470
-
471
- "print(gokei)"
472
-
473
- # 金額(r[2])を通貨形式に変換
474
-
475
- r = (r[0],r[1],"¥{:,d}".format(r[2]))
476
-
477
- tree.insert("","end",tags=i,values=r)
478
-
479
- if i & 1:
480
-
481
- tree.tag_configure(i,background="#CCFFFF")
482
-
483
- i+=1
484
-
485
-
486
-
487
- label_gokei=tk.Label(root,text="期間内の合計は"+str(gokei)+"円です")
488
-
489
- label_gokei.pack(fill="x")
490
-
491
- # ----------------------------------------
492
-
493
-
494
-
495
- # 空のデータベースを作成して接続する
496
-
497
- dbname = "database.db"
498
-
499
- c = sqlite3.connect(dbname)
500
-
501
- c.execute("PRAGMA foreign_keys = 1")
502
-
503
-
504
-
505
- # rootフレームの設定
506
-
507
- root = tk.Tk()
508
-
509
- root.title("家計簿アプリ")
510
-
511
- root.geometry("700x700")
512
-
513
-
514
-
515
- # メニューの設定
516
-
517
- frame = tk.Frame(root,bd=2,relief="ridge")
518
-
519
- frame.pack(fill="x")
520
-
521
- button1 = tk.Button(frame,text="入力",command=create_button)
522
-
523
- button1.pack(side="left")
524
-
525
- button2 = tk.Button(frame,text="表示")
526
-
527
- button2.pack(side="left")
528
-
529
- button3 = tk.Button(frame,text="終了",command=quit_button)
530
-
531
- button3.pack(side="right")
532
-
533
-
534
-
535
-
536
-
537
- # 入力画面ラベルの設定
538
-
539
- label1 = tk.Label(root,text="【表示画面】",font=("",16),height=2)
540
-
541
- label1.pack(fill="x")
542
-
543
- chk=tk.BooleanVar()
544
-
545
- chk.set(True)
546
-
547
- chk1=tk.Checkbutton(root,text='支出を表示',variable=chk)
548
-
549
- chk1.pack()
550
-
551
- chk2=tk.Checkbutton(root,text='収入を表示')
552
-
553
- chk2.pack()
554
118
 
555
119
 
556
120
 
@@ -558,150 +122,54 @@
558
122
 
559
123
 
560
124
 
561
- # 期間選択のラベルエントリーの設定
125
+ # rootフレの設定
562
126
 
563
- frame1 = tk.Frame(root,pady=15)
127
+ root = tk.Tk()
564
128
 
565
- frame1.pack()
129
+ root.title("家計簿アプリ")
566
130
 
567
- label2 = tk.Label(frame1,font=("",14),text="期間 ")
131
+ #root.configure(bg="#333333")
568
132
 
569
- label2.pack(side="left")
133
+ root.geometry("300x280")
570
134
 
571
- entry1 = tk.Entry(frame1,font=("",14),justify="center",width=12)
572
135
 
573
- entry1.pack(side="left")
574
136
 
575
- label3 = tk.Label(frame1,font=("",14),text=" ~ ")
137
+ entry1 = tk.Entry(root,font=("",14),justify="center",width=15)
576
138
 
577
- label3.pack(side="left")
139
+ entry1.pack(fill="x")
578
140
 
579
- entry2 = tk.Entry(frame1,font=("",14),justify="center",width=12)
580
141
 
142
+
143
+ entry2 = tk.Entry(root,font=("",14),justify="center",width=15)
144
+
581
- entry2.pack(side="left")
145
+ entry2.pack(fill="x")
582
146
 
583
147
 
584
148
 
585
149
 
586
150
 
587
- # 表示ボタンの設定
151
+ entry3 = tk.Entry(root,font=("",14),justify="center",width=15)
588
152
 
589
- button4 = tk.Button(root,text="表示",
590
-
591
- font=("",16),
592
-
593
- width=10,bg="gray",
153
+ entry3.pack(fill="x")
594
-
595
- command=lambda:select_sql(entry1.get(),entry2.get()))
596
-
597
- button4.pack()
598
154
 
599
155
 
600
156
 
157
+ # 登録ボタンの設定
158
+
159
+ button4 = tk.Button(root,text="登録",
160
+
161
+ font=("",16),
162
+
163
+ width=10,bg="blue",
164
+
165
+ command=lambda:create_sql)
166
+
167
+ button4.pack()
601
168
 
602
169
 
603
170
 
604
-
605
-
606
-
607
- # ツリービューの作成
608
-
609
- tree = ttk.Treeview(root,padding=10)
610
-
611
- tree["columns"] = (1,2,3)
612
-
613
- tree["show"] = "headings"
614
-
615
- tree.column(1,width=100)
616
-
617
- tree.column(2,width=75)
618
-
619
- tree.column(3,width=100)
620
-
621
- tree.heading(1,text="日付")
622
-
623
- tree.heading(2,text="内訳")
624
-
625
- tree.heading(3,text="金額")
626
-
627
-
628
-
629
- # ツリービューのスタイル変更
630
-
631
- style = ttk.Style()
632
-
633
- # TreeViewの全部に対して、フォントサイズの変更
634
-
635
- style.configure("Treeview",font=("",12))
636
-
637
- # TreeViewのHeading部分に対して、フォントサイズの変更と太字の設定
638
-
639
- style.configure("Treeview.Heading",font=("",14,"bold"))
640
-
641
-
642
-
643
- # SELECT文の作成
644
-
645
- sql = """
646
-
647
- SELECT acc_date,item_name,amount
648
-
649
- FROM acc_data as a,item as i
650
-
651
- WHERE a.item_code = i.item_code
652
-
653
- ORDER BY acc_date
654
-
655
- """
656
-
657
- # ツリービューにアイテムの追加
658
-
659
- i=0
660
-
661
- def pulus():
662
-
663
- for r in c.execute(sql):
664
-
665
- # 金額(r[2])を通貨形式に変換
666
-
667
- r = (r[0],r[1],"¥{:,d}".format(r[2]))
668
-
669
- tree.insert("","end",tags=i,values=r)
670
-
671
- if i & 1:
672
-
673
- tree.tag_configure(i,background="#CCFFFF")
674
-
675
- i+=1
676
-
677
-
678
-
679
- # ツリービューの配置
680
-
681
- tree.pack(fill="x",padx=20,pady=20)
682
-
683
-
684
-
685
- # メインループ
686
-
687
- root.mainloop()
171
+ root.mainloop()
688
-
689
-
690
-
691
- # GUI画面の表示
692
-
693
- create_gui()
694
172
 
695
173
 
696
174
 
697
175
  ```
698
-
699
-
700
-
701
-
702
-
703
- ログイン画面にて特定の値を入力しないと入力画面へいけるようにしたい。
704
-
705
- 表示画面にてチェックボックスの「支出を表示」がTRUEの時金額がマイナスのみが表示できるようにしたい。逆に「収入を表示」がTRUEの時プラスを表示のみを表示したい。
706
-
707
- よろしくお願いします。

1

文字数が足りず詳しくかけませんでした。申し訳ありません。

2019/01/14 14:44

投稿

ChaCha_MaRu
ChaCha_MaRu

スコア15

test CHANGED
File without changes
test CHANGED
@@ -695,3 +695,13 @@
695
695
 
696
696
 
697
697
  ```
698
+
699
+
700
+
701
+
702
+
703
+ ログイン画面にて特定の値を入力しないと入力画面へいけるようにしたい。
704
+
705
+ 表示画面にてチェックボックスの「支出を表示」がTRUEの時金額がマイナスのみが表示できるようにしたい。逆に「収入を表示」がTRUEの時プラスを表示のみを表示したい。
706
+
707
+ よろしくお願いします。