質問編集履歴

10

修正

2020/10/08 06:32

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -566,11 +566,7 @@
566
566
 
567
567
  print(msg, args)
568
568
 
569
- if msg == "DB-CNCT":
570
-
571
- cnct()
572
-
573
- elif msg == "DB-INSERT":
569
+ if msg == "DB-INSERT":
574
570
 
575
571
  # データベースとの接続がタイムアウトしていたときのための再接続。
576
572
 

9

修正

2020/10/08 06:32

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -412,7 +412,7 @@
412
412
 
413
413
 
414
414
 
415
- # 質問後追記 変更(2020/10/08 14:37
415
+ # 質問後追記 変更(2020/10/08 14:39
416
416
 
417
417
  ```Python:
418
418
 
@@ -598,7 +598,7 @@
598
598
 
599
599
  elif msg == "DB-DISCNCT":
600
600
 
601
- discnct(con, cur)
601
+ break
602
602
 
603
603
  else:
604
604
 

8

修正

2020/10/08 05:39

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -412,7 +412,7 @@
412
412
 
413
413
 
414
414
 
415
- # 質問後追記 変更(2020/10/08 14:34
415
+ # 質問後追記 変更(2020/10/08 14:37
416
416
 
417
417
  ```Python:
418
418
 
@@ -602,9 +602,9 @@
602
602
 
603
603
  else:
604
604
 
605
+ break
606
+
605
- discnct(con, cur)
607
+ discnct(con, cur)
606
-
607
- break
608
608
 
609
609
 
610
610
 

7

修正

2020/10/08 05:37

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -602,7 +602,7 @@
602
602
 
603
603
  else:
604
604
 
605
- discnct(con, cur)
605
+ discnct(con, cur)
606
606
 
607
607
  break
608
608
 

6

修正

2020/10/08 05:34

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -448,7 +448,7 @@
448
448
 
449
449
  user = "root",
450
450
 
451
- password = "sh1n@n0pisql",
451
+ password = "root",
452
452
 
453
453
  db = "testdb",
454
454
 

5

修正

2020/10/08 05:34

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -412,7 +412,7 @@
412
412
 
413
413
 
414
414
 
415
- # 質問後追記 変更(2020/10/08 12:58
415
+ # 質問後追記 変更(2020/10/08 14:34
416
416
 
417
417
  ```Python:
418
418
 
@@ -448,7 +448,7 @@
448
448
 
449
449
  user = "root",
450
450
 
451
- password = "root",
451
+ password = "sh1n@n0pisql",
452
452
 
453
453
  db = "testdb",
454
454
 
@@ -564,6 +564,8 @@
564
564
 
565
565
  msg, args = data.get()
566
566
 
567
+ print(msg, args)
568
+
567
569
  if msg == "DB-CNCT":
568
570
 
569
571
  cnct()
@@ -600,10 +602,10 @@
600
602
 
601
603
  else:
602
604
 
605
+ discnct(con, cur)
606
+
603
607
  break
604
608
 
605
- time.sleep(0.1)
606
-
607
609
 
608
610
 
609
611
 

4

修正

2020/10/08 05:33

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -412,7 +412,7 @@
412
412
 
413
413
 
414
414
 
415
- # 質問後追記
415
+ # 質問後追記 変更(2020/10/08 12:58)
416
416
 
417
417
  ```Python:
418
418
 
@@ -426,6 +426,8 @@
426
426
 
427
427
  import threading
428
428
 
429
+ import time
430
+
429
431
  import tkinter as tk
430
432
 
431
433
 
@@ -438,8 +440,6 @@
438
440
 
439
441
  def cnct():
440
442
 
441
- global con, cur
442
-
443
443
  # データベース接続
444
444
 
445
445
  con = pymysql.connect(
@@ -460,11 +460,13 @@
460
460
 
461
461
 
462
462
 
463
-
464
-
465
- def discnct():
466
-
467
- global con, cur
463
+ return con, cur
464
+
465
+
466
+
467
+
468
+
469
+ def discnct(con, cur):
468
470
 
469
471
  # データベース切断
470
472
 
@@ -476,9 +478,9 @@
476
478
 
477
479
 
478
480
 
479
- def read_and_csvout():
481
+ def read_and_csvout(cur):
480
-
482
+
481
- global cur, tbl
483
+ global tbl
482
484
 
483
485
  cur.execute("SELECT * FROM " + tbl + ";")
484
486
 
@@ -504,9 +506,9 @@
504
506
 
505
507
 
506
508
 
507
- def clear():
509
+ def clear(cur):
508
-
510
+
509
- global cur, tbl
511
+ global tbl
510
512
 
511
513
  cur.execute("DELETE FROM " + tbl + ";")
512
514
 
@@ -552,61 +554,55 @@
552
554
 
553
555
  def loop(data):
554
556
 
557
+ con, cur = cnct()
558
+
555
559
  # キュー監視ループ
556
560
 
557
561
  while True:
558
562
 
559
- # キューにデータが存在したら、順番通りにデータベースへ保存する
560
-
561
- while not data.empty():
562
-
563
- # キューにある先頭データを取り出す
563
+ # キューにある先頭データを取り出す
564
-
564
+
565
- msg, args = data.get()
565
+ msg, args = data.get()
566
-
566
+
567
- if msg == "DB-CNCT":
567
+ if msg == "DB-CNCT":
568
-
568
+
569
- cnct()
569
+ cnct()
570
-
570
+
571
- elif msg == "DB-INSERT":
571
+ elif msg == "DB-INSERT":
572
-
573
- global cur, con
572
+
574
-
575
- # データベースとの接続がタイムアウトしていたときのための再接続。
573
+ # データベースとの接続がタイムアウトしていたときのための再接続。
576
-
574
+
577
- con.ping(reconnect=True)
575
+ con.ping(reconnect=True)
578
-
576
+
579
- # tmpにあるデータをデータベースに保存
577
+ # tmpにあるデータをデータベースに保存
580
-
578
+
581
- cur.execute("INSERT INTO " + tbl + " VALUES(%s,%s,%s);", (args[0], args[1], args[2]))
579
+ cur.execute("INSERT INTO " + tbl + " VALUES(%s,%s,%s);", (args[0], args[1], args[2]))
582
-
580
+
583
- elif msg == "CSV-OUTPUT":
581
+ elif msg == "CSV-OUTPUT":
584
-
585
- global cur, con
582
+
586
-
587
- # データベースとの接続がタイムアウトしていたときのための再接続。
583
+ # データベースとの接続がタイムアウトしていたときのための再接続。
588
-
584
+
589
- con.ping(reconnect=True)
585
+ con.ping(reconnect=True)
590
-
586
+
591
- read_and_csvout()
587
+ read_and_csvout(cur)
592
-
588
+
593
- clear()
589
+ clear(cur)
594
-
590
+
595
- con.commit()
591
+ con.commit()
596
-
592
+
597
- elif msg == "DB-COMMIT":
593
+ elif msg == "DB-COMMIT":
598
-
599
- global con
594
+
600
-
601
- con.commit()
595
+ con.commit()
602
-
596
+
603
- elif msg == "DB-DISCNCT":
597
+ elif msg == "DB-DISCNCT":
604
-
598
+
605
- discnct()
599
+ discnct(con, cur)
606
-
600
+
607
- else:
601
+ else:
608
-
602
+
609
- break
603
+ break
604
+
605
+ time.sleep(0.1)
610
606
 
611
607
 
612
608
 
@@ -666,10 +662,6 @@
666
662
 
667
663
 
668
664
 
669
- data.put(("DB-CNCT", None))
670
-
671
-
672
-
673
665
  # ボタンバインド
674
666
 
675
667
  button1.bind("<ButtonRelease>", pushed1)

3

追加

2020/10/08 03:59

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -409,3 +409,311 @@
409
409
  con.commit()
410
410
 
411
411
  ```
412
+
413
+
414
+
415
+ # 質問後追記
416
+
417
+ ```Python:
418
+
419
+ from datetime import datetime
420
+
421
+ import csv
422
+
423
+ import pymysql
424
+
425
+ import queue
426
+
427
+ import threading
428
+
429
+ import tkinter as tk
430
+
431
+
432
+
433
+ tbl = "testtb"
434
+
435
+ tmp = []
436
+
437
+
438
+
439
+ def cnct():
440
+
441
+ global con, cur
442
+
443
+ # データベース接続
444
+
445
+ con = pymysql.connect(
446
+
447
+ host = "localhost",
448
+
449
+ user = "root",
450
+
451
+ password = "root",
452
+
453
+ db = "testdb",
454
+
455
+ charset = "utf8"
456
+
457
+ )
458
+
459
+ cur = con.cursor()
460
+
461
+
462
+
463
+
464
+
465
+ def discnct():
466
+
467
+ global con, cur
468
+
469
+ # データベース切断
470
+
471
+ cur.close()
472
+
473
+ con.close()
474
+
475
+
476
+
477
+
478
+
479
+ def read_and_csvout():
480
+
481
+ global cur, tbl
482
+
483
+ cur.execute("SELECT * FROM " + tbl + ";")
484
+
485
+ results = cur.fetchall()
486
+
487
+ lst = []
488
+
489
+ for i in range(len(results)):
490
+
491
+ tmp = results[i]
492
+
493
+ lst.append([ tmp[0], tmp[1], tmp[2] ])
494
+
495
+ with open("/home/pi/デスクトップ/data.csv", "a", newline="") as f:
496
+
497
+ writer = csv.writer(f, lineterminator="\r\n")
498
+
499
+ for i in range(len(lst)):
500
+
501
+ writer.writerow(lst[i])
502
+
503
+
504
+
505
+
506
+
507
+ def clear():
508
+
509
+ global cur, tbl
510
+
511
+ cur.execute("DELETE FROM " + tbl + ";")
512
+
513
+
514
+
515
+
516
+
517
+ def pushed1(e):
518
+
519
+ global data
520
+
521
+ # データベース保存用データをキューに格納
522
+
523
+ for i in range(50):
524
+
525
+ data.put(("DB-INSERT", [str(i+1), "ButtonPushed1", ""]))
526
+
527
+ data.put(("DB-COMMIT", None))
528
+
529
+
530
+
531
+
532
+
533
+ def pushed2(e):
534
+
535
+ global data
536
+
537
+ # データベース保存用データをキューに格納
538
+
539
+ for i in range(50):
540
+
541
+ data.put(("DB-INSERT", [str(i+1), "ButtonPushed2", ""]))
542
+
543
+ data.put(("DB-COMMIT", None))
544
+
545
+ data.put(("CSV-OUTPUT", None))
546
+
547
+
548
+
549
+
550
+
551
+
552
+
553
+ def loop(data):
554
+
555
+ # キュー監視ループ
556
+
557
+ while True:
558
+
559
+ # キューにデータが存在したら、順番通りにデータベースへ保存する
560
+
561
+ while not data.empty():
562
+
563
+ # キューにある先頭データを取り出す
564
+
565
+ msg, args = data.get()
566
+
567
+ if msg == "DB-CNCT":
568
+
569
+ cnct()
570
+
571
+ elif msg == "DB-INSERT":
572
+
573
+ global cur, con
574
+
575
+ # データベースとの接続がタイムアウトしていたときのための再接続。
576
+
577
+ con.ping(reconnect=True)
578
+
579
+ # tmpにあるデータをデータベースに保存
580
+
581
+ cur.execute("INSERT INTO " + tbl + " VALUES(%s,%s,%s);", (args[0], args[1], args[2]))
582
+
583
+ elif msg == "CSV-OUTPUT":
584
+
585
+ global cur, con
586
+
587
+ # データベースとの接続がタイムアウトしていたときのための再接続。
588
+
589
+ con.ping(reconnect=True)
590
+
591
+ read_and_csvout()
592
+
593
+ clear()
594
+
595
+ con.commit()
596
+
597
+ elif msg == "DB-COMMIT":
598
+
599
+ global con
600
+
601
+ con.commit()
602
+
603
+ elif msg == "DB-DISCNCT":
604
+
605
+ discnct()
606
+
607
+ else:
608
+
609
+ break
610
+
611
+
612
+
613
+
614
+
615
+ def close():
616
+
617
+ global data, win, thread
618
+
619
+ data.put(("DB-DISCNCT", None))
620
+
621
+ thread.join()
622
+
623
+ # Tkinterアプリの終了
624
+
625
+ win.destroy()
626
+
627
+
628
+
629
+
630
+
631
+ if __name__ == "__main__":
632
+
633
+
634
+
635
+ # データベースに保存するデータ格納用
636
+
637
+ data = queue.Queue()
638
+
639
+
640
+
641
+ # Tkinter UI
642
+
643
+ win = tk.Tk()
644
+
645
+
646
+
647
+ # ボタン
648
+
649
+ button1 = tk.Button(win, text="Button1")
650
+
651
+ button1.grid(row=0, column=0, sticky="nsew")
652
+
653
+ button2 = tk.Button(win, text="Button2")
654
+
655
+ button2.grid(row=0, column=1, sticky="nsew")
656
+
657
+
658
+
659
+ # データベース操作スレッド
660
+
661
+ # ※データベース操作はスレッドセーフではないため、1つのスレッドでのみ操作が可能
662
+
663
+ thread = threading.Thread(target=loop, args=(data, ))
664
+
665
+ thread.start()
666
+
667
+
668
+
669
+ data.put(("DB-CNCT", None))
670
+
671
+
672
+
673
+ # ボタンバインド
674
+
675
+ button1.bind("<ButtonRelease>", pushed1)
676
+
677
+ button2.bind("<ButtonRelease>", pushed2)
678
+
679
+
680
+
681
+ # UIクローズ検知
682
+
683
+ win.protocol("WM_DELETE_WINDOW", close)
684
+
685
+
686
+
687
+ # イベントループ
688
+
689
+ win.mainloop()
690
+
691
+
692
+
693
+
694
+
695
+ """
696
+
697
+ MariaDB [testdb]> DESC testtb;
698
+
699
+ +-------+------+------+-----+---------+-------+
700
+
701
+ | Field | Type | Null | Key | Default | Extra |
702
+
703
+ +-------+------+------+-----+---------+-------+
704
+
705
+ | data1 | text | YES | | NULL | |
706
+
707
+ | data2 | text | YES | | NULL | |
708
+
709
+ | data3 | text | YES | | NULL | |
710
+
711
+ +-------+------+------+-----+---------+-------+
712
+
713
+ 3 rows in set (0.00 sec)
714
+
715
+ """
716
+
717
+
718
+
719
+ ```

2

修正

2020/10/08 02:57

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -62,7 +62,7 @@
62
62
 
63
63
  user = "root",
64
64
 
65
- password = "sh1n@n0pisql",
65
+ password = "root",
66
66
 
67
67
  db = "testdb",
68
68
 

1

追加

2020/10/08 01:20

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -356,6 +356,10 @@
356
356
 
357
357
 
358
358
 
359
+ (グローバル変数で適当なフラグを立てることを考えたのですが、Button1のデータ書き込みが終わったのか、Button2のデータ書き込みが終わったのか、そもそも書き込んでいないのかまではloop()内で区別できないような気がします。変数だけじゃなくコールバック関数などを駆使すればできる?)
360
+
361
+
362
+
359
363
  どのようにすれば 50,ButtonPushed2 をデータベースに保存した後でCSV出力をすることが可能でしょうか。
360
364
 
361
365
 
@@ -390,7 +394,7 @@
390
394
 
391
395
  cur.execute("INSERT INTO " + tbl + " VALUES(%s,%s,%s);", (tmp[0], tmp[1], tmp[2]))
392
396
 
393
- # データの内容を指定して、CSV出力
397
+ # データの内容を指定して、CSV出力(これは解決策としては×)
394
398
 
395
399
  if tmp == ["50", "ButtonPushed2", ""]:
396
400