質問編集履歴

3

解決いたしました。本当にありがとうございました。

2020/10/07 19:17

投稿

wakaba_
wakaba_

スコア1

test CHANGED
File without changes
test CHANGED
@@ -16,9 +16,7 @@
16
16
 
17
17
  初歩的な質問で恐縮ですが、ご教授いただけますと幸いです。
18
18
 
19
-
20
-
21
- ```ここに言語を入力
19
+ ```
22
20
 
23
21
  import tkinter as tk
24
22
 
@@ -62,8 +60,6 @@
62
60
 
63
61
 
64
62
 
65
-
66
-
67
63
  order_date = entry.get()
68
64
 
69
65
  order_num = entry2.get()
@@ -106,7 +102,7 @@
106
102
 
107
103
 
108
104
 
109
- #送信
105
+ # 送信
110
106
 
111
107
  button = ttk.Button(root, text = "送信", command = create_sql())
112
108
 
@@ -132,15 +128,11 @@
132
128
 
133
129
  root.mainloop()
134
130
 
135
-
136
-
137
- ```
131
+ ```
132
+
138
-
133
+ ---
139
-
140
-
134
+
141
- 追記
135
+ 追記1
142
-
143
-
144
136
 
145
137
  ```
146
138
 
@@ -254,11 +246,7 @@
254
246
 
255
247
  ```
256
248
 
257
-
258
-
259
- エラー内容
249
+ エラー
260
-
261
-
262
250
 
263
251
  ```
264
252
 
@@ -284,15 +272,13 @@
284
272
 
285
273
  ```
286
274
 
287
-
288
-
289
275
  宜しくお願い致します。
290
276
 
291
277
 
292
278
 
293
-
279
+ ---
294
-
280
+
295
- closeをGUI終了後にした場合
281
+ 追記2 closeをGUI終了後にした場合
296
282
 
297
283
  ```
298
284
 
@@ -428,12 +414,8 @@
428
414
 
429
415
  ```
430
416
 
431
-
432
-
433
417
  入力値を代入した、
434
418
 
435
-
436
-
437
419
  ```
438
420
 
439
421
  order_date = entry.get()
@@ -444,8 +426,6 @@
444
426
 
445
427
  をVALUEに渡せていないように思えるのですが、
446
428
 
447
-
448
-
449
429
  ```
450
430
 
451
431
  def create_sql() :
@@ -460,20 +440,14 @@
460
440
 
461
441
  ```
462
442
 
463
-
464
-
465
443
  に変えるとエラーが変わり、
466
444
 
467
-
468
-
469
445
  ```
470
446
 
471
447
  mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''20201008', '975022') VALUES ('20201008', '975022')' at line 1
472
448
 
473
449
  ```
474
450
 
475
-
476
-
477
451
  と表示されます。MySQLで、下記は作成済です。
478
452
 
479
453
 
@@ -483,3 +457,117 @@
483
457
  テーブル orders
484
458
 
485
459
  カラム orderDate, orderNum
460
+
461
+
462
+
463
+ ---
464
+
465
+ MySQL再起動の上、下記コードに変えたところ入力値が格納されていました。
466
+
467
+
468
+
469
+ ```
470
+
471
+ import tkinter as tk
472
+
473
+ import tkinter.ttk as ttk
474
+
475
+ import mysql.connector as mydb
476
+
477
+
478
+
479
+ # コネクション作成
480
+
481
+ conn = mydb.connect(
482
+
483
+ host='localhost',
484
+
485
+ user='root',
486
+
487
+ password='********',
488
+
489
+ database='order_list')
490
+
491
+
492
+
493
+ # カーソル作成
494
+
495
+ cur = conn.cursor()
496
+
497
+
498
+
499
+ # SQL
500
+
501
+ def create_sql() :
502
+
503
+ order_date = entry.get()
504
+
505
+ order_num = entry2.get()
506
+
507
+ cur.execute('INSERT INTO orders (orderDate, orderNum) VALUES (%(order_date)s, %(order_num)s)',
508
+
509
+ {'order_date': order_date, 'order_num': order_num})
510
+
511
+ conn.commit()
512
+
513
+
514
+
515
+ # ウィンドウ
516
+
517
+ root = tk.Tk()
518
+
519
+ root.title("入力")
520
+
521
+ root.geometry("400x400")
522
+
523
+
524
+
525
+ # 日付
526
+
527
+ label = ttk.Label(root, text='日付')
528
+
529
+ label.pack()
530
+
531
+
532
+
533
+ entry = ttk.Entry()
534
+
535
+ entry.pack()
536
+
537
+
538
+
539
+ # オーダーNo.
540
+
541
+ label2 = ttk.Label(root, text='オーダーNo.')
542
+
543
+ label2.pack()
544
+
545
+
546
+
547
+ entry2 = ttk.Entry()
548
+
549
+ entry2.pack()
550
+
551
+
552
+
553
+ # 送信
554
+
555
+ button = ttk.Button(root, text = "送信", command = create_sql)
556
+
557
+ button.pack()
558
+
559
+
560
+
561
+ # ウィンドウを動かす
562
+
563
+ root.mainloop()
564
+
565
+
566
+
567
+ # カーソルとコネクションを閉じる
568
+
569
+ cur.close()
570
+
571
+ conn.close()
572
+
573
+ ```

2

修正しました。

2020/10/07 19:17

投稿

wakaba_
wakaba_

スコア1

test CHANGED
File without changes
test CHANGED
@@ -287,3 +287,199 @@
287
287
 
288
288
 
289
289
  宜しくお願い致します。
290
+
291
+
292
+
293
+
294
+
295
+ closeをGUI終了後にした場合
296
+
297
+ ```
298
+
299
+ import tkinter as tk
300
+
301
+ import tkinter.ttk as ttk
302
+
303
+ import mysql.connector as mydb
304
+
305
+
306
+
307
+ # ウィンドウ
308
+
309
+ root = tk.Tk()
310
+
311
+ root.title("入力")
312
+
313
+ root.geometry("400x400")
314
+
315
+
316
+
317
+ # コネクション作成
318
+
319
+ conn = mydb.connect(
320
+
321
+ host='localhost',
322
+
323
+ user='root',
324
+
325
+ password='*******',
326
+
327
+ database='order_list')
328
+
329
+
330
+
331
+ # カーソル作成
332
+
333
+ cur = conn.cursor()
334
+
335
+
336
+
337
+ # 日付
338
+
339
+ label = ttk.Label(root, text='日付')
340
+
341
+ label.pack()
342
+
343
+
344
+
345
+ entry = ttk.Entry()
346
+
347
+ entry.pack()
348
+
349
+
350
+
351
+ # オーダーNo.
352
+
353
+ label2 = ttk.Label(root, text='オーダーNo.')
354
+
355
+ label2.pack()
356
+
357
+
358
+
359
+ entry2 = ttk.Entry()
360
+
361
+ entry2.pack()
362
+
363
+
364
+
365
+ # クエリ
366
+
367
+ def create_sql() :
368
+
369
+ order_date = entry.get()
370
+
371
+ order_num = entry2.get()
372
+
373
+ cur.execute('INSERT INTO orders (%(orderDate)s, %(orderNum)s) VALUES (%(orderDate)s, %(orderNum)s)',
374
+
375
+ {'orderDate': orderDate, 'orderNum': orderNum})
376
+
377
+
378
+
379
+ # 送信
380
+
381
+ button = ttk.Button(root, text = "送信", command = create_sql)
382
+
383
+ button.pack()
384
+
385
+
386
+
387
+ # 保存
388
+
389
+ conn.commit()
390
+
391
+
392
+
393
+ # ウィンドウを動かす
394
+
395
+ root.mainloop()
396
+
397
+
398
+
399
+ # カーソルとコネクションを閉じる
400
+
401
+ cur.close()
402
+
403
+ conn.close()
404
+
405
+ ```
406
+
407
+
408
+
409
+ エラー
410
+
411
+
412
+
413
+ ```
414
+
415
+ Exception in Tkinter callback
416
+
417
+ Traceback (most recent call last):
418
+
419
+ File "C:\Users*****\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
420
+
421
+ return self.func(*args)
422
+
423
+ File "C:\Users*****\Desktop\Python\test.py", line 39, in create_sql
424
+
425
+ {'orderDate': orderDate, 'orderNum': orderNum})
426
+
427
+ NameError: name 'orderDate' is not defined
428
+
429
+ ```
430
+
431
+
432
+
433
+ 入力値を代入した、
434
+
435
+
436
+
437
+ ```
438
+
439
+ order_date = entry.get()
440
+
441
+ order_num = entry2.get()
442
+
443
+ ```
444
+
445
+ をVALUEに渡せていないように思えるのですが、
446
+
447
+
448
+
449
+ ```
450
+
451
+ def create_sql() :
452
+
453
+ order_date = entry.get()
454
+
455
+ order_num = entry2.get()
456
+
457
+ cur.execute('INSERT INTO orders (%(orderDate)s, %(orderNum)s) VALUES (%(orderDate)s, %(orderNum)s)',
458
+
459
+ {'orderDate': order_date, 'orderNum': order_num})
460
+
461
+ ```
462
+
463
+
464
+
465
+ に変えるとエラーが変わり、
466
+
467
+
468
+
469
+ ```
470
+
471
+ mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''20201008', '975022') VALUES ('20201008', '975022')' at line 1
472
+
473
+ ```
474
+
475
+
476
+
477
+ と表示されます。MySQLで、下記は作成済です。
478
+
479
+
480
+
481
+ データベース order_list
482
+
483
+ テーブル orders
484
+
485
+ カラム orderDate, orderNum

1

コード掲載致しました。

2020/10/07 18:52

投稿

wakaba_
wakaba_

スコア1

test CHANGED
@@ -1 +1 @@
1
- Tkinter 入力値をMySQLに格納する方法
1
+ Tkinter入力値をMySQLに格納する方法
test CHANGED
@@ -135,3 +135,155 @@
135
135
 
136
136
 
137
137
  ```
138
+
139
+
140
+
141
+ 追記
142
+
143
+
144
+
145
+ ```
146
+
147
+ import tkinter as tk
148
+
149
+ import tkinter.ttk as ttk
150
+
151
+ import mysql.connector as mydb
152
+
153
+
154
+
155
+ root = tk.Tk()
156
+
157
+ root.title("入力")
158
+
159
+ root.geometry("400x400")
160
+
161
+
162
+
163
+ # コネクションの作成
164
+
165
+ conn = mydb.connect(
166
+
167
+ host='localhost',
168
+
169
+ user='root',
170
+
171
+ password='********',
172
+
173
+ database='order_list'
174
+
175
+ )
176
+
177
+
178
+
179
+ # 接続状況
180
+
181
+ conn.ping(reconnect=True)
182
+
183
+ print(conn.is_connected())
184
+
185
+
186
+
187
+ # DB操作用にカーソルを作成
188
+
189
+ cur = conn.cursor()
190
+
191
+
192
+
193
+ # 日付
194
+
195
+ label = ttk.Label(root, text='日付')
196
+
197
+ label.pack()
198
+
199
+
200
+
201
+ entry = ttk.Entry()
202
+
203
+ entry.pack()
204
+
205
+
206
+
207
+ # オーダーNo.
208
+
209
+ label2 = ttk.Label(root, text='注文番号')
210
+
211
+ label2.pack()
212
+
213
+
214
+
215
+ entry2 = ttk.Entry()
216
+
217
+ entry2.pack()
218
+
219
+
220
+
221
+ # クエリ
222
+
223
+ def create_sql() :
224
+
225
+ order_date = entry.get()
226
+
227
+ order_num = entry2.get()
228
+
229
+ cur.execute('INSERT INTO orders (%(orderDate)s, %(orderNum)s) VALUES (%(orderDate)s, %(orderNum)s)', {'orderDate': orderDate, 'orderNum': orderNum})
230
+
231
+ conn.commit()
232
+
233
+
234
+
235
+ # 送信
236
+
237
+ button = ttk.Button(root, text = "送信", command = create_sql)
238
+
239
+ button.pack()
240
+
241
+
242
+
243
+ # DB操作が終わったら、カーソルとコネクションを閉じる
244
+
245
+ cur.close()
246
+
247
+ conn.close()
248
+
249
+
250
+
251
+ # ウィンドウを動かす
252
+
253
+ root.mainloop()
254
+
255
+ ```
256
+
257
+
258
+
259
+ エラー内容
260
+
261
+
262
+
263
+ ```
264
+
265
+ True
266
+
267
+ Exception in Tkinter callback
268
+
269
+ Traceback (most recent call last):
270
+
271
+ File "C:\Users*****\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
272
+
273
+ return self.func(*args)
274
+
275
+ File "C:\Users*****\Desktop\Python\test.py", line 41, in create_sql
276
+
277
+ cur.execute('INSERT INTO input (%(orderDate)s, %(orderNum)s) VALUES (%(orderDate)s, %(orderNum)s)', {'orderDate': order_date, 'orderNum': order_num})
278
+
279
+ File "C:\Users*****\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 537, in execute
280
+
281
+ raise errors.ProgrammingError("Cursor is not connected")
282
+
283
+ mysql.connector.errors.ProgrammingError: Cursor is not connected
284
+
285
+ ```
286
+
287
+
288
+
289
+ 宜しくお願い致します。