Tkinterで注文管理のデスクトップアプリを作りたいと考えています。
色んなホームページからコピーペーストしているのですが、
Entryなどで入力したデータをMySQLに格納する方法がどうしても分かりません。
getで取得してから代入して、引数に入れてみたりしましたが実行できません。
送信ボタンを押すと、複数のデータが各指定の列に格納されるのをイメージしています。
初歩的な質問で恐縮ですが、ご教授いただけますと幸いです。
import tkinter as tk import tkinter.ttk as ttk import mysql.connector as mydb # ウィンドウ作成 root = tk.Tk() # タイトル root.title("入力") # サイズ root.geometry("400×400") # コネクションの作成 conn = mydb.connect( user='root', password='********', host='localhost', database='order_list') # DB操作用にカーソルを作成 cur = conn.cursor() order_date = entry.get() order_num = entry2.get() def create_sql() : cur.execute( INSERT INTO orders (orderDate, orderNum) VALUES (order_date, order_num)) # 日付 label = ttk.Label(root, text='日付') label.pack() entry = tk.Entry() entry.pack() # 注文番号 label2 = ttk.Label(root, text='注文番号') label2.pack() entry2 = tk.Entry() entry2.pack() # 送信 button = ttk.Button(root, text = "送信", command = create_sql()) button.pack() # 保存 conn.commit() # DB操作が終わったらカーソルとコネクションを閉じる cur.close() conn.close() # ウィンドウを動かす root.mainloop()
追記1
import tkinter as tk import tkinter.ttk as ttk import mysql.connector as mydb root = tk.Tk() root.title("入力") root.geometry("400x400") # コネクションの作成 conn = mydb.connect( host='localhost', user='root', password='********', database='order_list' ) # 接続状況 conn.ping(reconnect=True) print(conn.is_connected()) # DB操作用にカーソルを作成 cur = conn.cursor() # 日付 label = ttk.Label(root, text='日付') label.pack() entry = ttk.Entry() entry.pack() # オーダーNo. label2 = ttk.Label(root, text='注文番号') label2.pack() entry2 = ttk.Entry() entry2.pack() # クエリ def create_sql() : order_date = entry.get() order_num = entry2.get() cur.execute('INSERT INTO orders (%(orderDate)s, %(orderNum)s) VALUES (%(orderDate)s, %(orderNum)s)', {'orderDate': orderDate, 'orderNum': orderNum}) conn.commit() # 送信 button = ttk.Button(root, text = "送信", command = create_sql) button.pack() # DB操作が終わったら、カーソルとコネクションを閉じる cur.close() conn.close() # ウィンドウを動かす root.mainloop()
エラー
True Exception in Tkinter callback Traceback (most recent call last): File "C:\Users*****\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "C:\Users*****\Desktop\Python\test.py", line 41, in create_sql cur.execute('INSERT INTO input (%(orderDate)s, %(orderNum)s) VALUES (%(orderDate)s, %(orderNum)s)', {'orderDate': order_date, 'orderNum': order_num}) File "C:\Users*****\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 537, in execute raise errors.ProgrammingError("Cursor is not connected") mysql.connector.errors.ProgrammingError: Cursor is not connected
宜しくお願い致します。
追記2 closeをGUI終了後にした場合
import tkinter as tk import tkinter.ttk as ttk import mysql.connector as mydb # ウィンドウ root = tk.Tk() root.title("入力") root.geometry("400x400") # コネクション作成 conn = mydb.connect( host='localhost', user='root', password='*******', database='order_list') # カーソル作成 cur = conn.cursor() # 日付 label = ttk.Label(root, text='日付') label.pack() entry = ttk.Entry() entry.pack() # オーダーNo. label2 = ttk.Label(root, text='オーダーNo.') label2.pack() entry2 = ttk.Entry() entry2.pack() # クエリ def create_sql() : order_date = entry.get() order_num = entry2.get() cur.execute('INSERT INTO orders (%(orderDate)s, %(orderNum)s) VALUES (%(orderDate)s, %(orderNum)s)', {'orderDate': orderDate, 'orderNum': orderNum}) # 送信 button = ttk.Button(root, text = "送信", command = create_sql) button.pack() # 保存 conn.commit() # ウィンドウを動かす root.mainloop() # カーソルとコネクションを閉じる cur.close() conn.close()
エラー
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users*****\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "C:\Users*****\Desktop\Python\test.py", line 39, in create_sql {'orderDate': orderDate, 'orderNum': orderNum}) NameError: name 'orderDate' is not defined
入力値を代入した、
order_date = entry.get() order_num = entry2.get()
をVALUEに渡せていないように思えるのですが、
def create_sql() : order_date = entry.get() order_num = entry2.get() cur.execute('INSERT INTO orders (%(orderDate)s, %(orderNum)s) VALUES (%(orderDate)s, %(orderNum)s)', {'orderDate': order_date, 'orderNum': order_num})
に変えるとエラーが変わり、
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
と表示されます。MySQLで、下記は作成済です。
データベース order_list
テーブル orders
カラム orderDate, orderNum
MySQL再起動の上、下記コードに変えたところ入力値が格納されていました。
import tkinter as tk import tkinter.ttk as ttk import mysql.connector as mydb # コネクション作成 conn = mydb.connect( host='localhost', user='root', password='********', database='order_list') # カーソル作成 cur = conn.cursor() # SQL def create_sql() : order_date = entry.get() order_num = entry2.get() cur.execute('INSERT INTO orders (orderDate, orderNum) VALUES (%(order_date)s, %(order_num)s)', {'order_date': order_date, 'order_num': order_num}) conn.commit() # ウィンドウ root = tk.Tk() root.title("入力") root.geometry("400x400") # 日付 label = ttk.Label(root, text='日付') label.pack() entry = ttk.Entry() entry.pack() # オーダーNo. label2 = ttk.Label(root, text='オーダーNo.') label2.pack() entry2 = ttk.Entry() entry2.pack() # 送信 button = ttk.Button(root, text = "送信", command = create_sql) button.pack() # ウィンドウを動かす root.mainloop() # カーソルとコネクションを閉じる cur.close() conn.close()
回答3件
あなたの回答
tips
プレビュー