Skip to content
On this page

範例-Tkinter with SQL

思路:

連接 SQL ( Sqlite / MySQL / ... )-> 設定 SQL 欄位與類別 -> 建立畫面。

安裝設置:

如果是連接 MySQL則可以使用 mysql-connect 或 mysql-connector

cmd
py -m install mysql-connect-python
py -m install mysql-connector

引入Tkinter

py
import tkinter
from tkinter import ttk
from tkinter import messagebox

設定連接 SQL

py
import mysql.connector

link_mysql=mysql.connector()

def login_db(user,psd)
    if psd:
           db=link_mysql.connect(host='localhost', user=user, password=psd, db='db_table')
       cursor=db.cursor()
    else:
        db=link_mysql.connect(host='localhost', user=user, db='db_table')
        cursor=db.cursor()

    savequery = "select * from DB"

    try:
        cursor.execute(savequery)
        myresult=cursor.fetchall()

        for x in myresult:
            print (x)
        print('Welcome')

    except:
        db.rollback()
        print('Error')

設定畫面

py


參考