简体   繁体   English

Python 和 SQL 交互:从 GUI 查询多个表和插入数据

[英]Python and SQL Interactions: Query Multiple Tables & Insert Data from GUI

I am working on a make my life easier type of program.我正在开发一种让我的生活更轻松的程序。

What I am wanting to do is make a GUI so a user (me and a few friends) can enter data to my SQL table.我想要做的是制作一个 GUI,以便用户(我和几个朋友)可以将数据输入到我的 SQL 表中。 However I am wanting to connect to 2 different databases, compiling into 1.但是我想连接到 2 个不同的数据库,编译成 1。

It consists of 3 different values and I need one of those values (#1) to be from a drop-down box pulled from db1, value #2 entered by user but cannot be greater than a numerical value assigned to value#1 pulled from db1 and finally just my userid.它由 3 个不同的值组成,我需要其中一个值 (#1) 来自从 db1 中提取的下拉框,用户输入的值 #2 但不能大于分配给从中提取的值#1 的数值db1,最后只是我的用户 ID。

Example db1 has customer and # of products bought, Value#1 has drop down of all customers that have purchases, (I select customer ABC, they have 4 active purchases), in values#2 I can enter 4 or less (for shipment purposes).示例 db1 有客户和购买的产品数量,Value#1 是所有购买过的客户的下拉列表,(我 select 客户 ABC,他们有 4 次活动购买),在 values#2 中我可以输入 4 或更少(用于运输目的)。 Then my userid.然后是我的用户名。

Then I hit submit button and it A)auto inserts into table or B)populates a list and I hit a button and all is inserted into table at once (see image).然后我点击提交按钮,它 A) 自动插入表格或 B) 填充列表,然后我点击一个按钮,所有内容都立即插入表格(见图)。

I have the rough GUI working and I am entering info into my test.db but I am unsure on the drop-down options or the part where I would connect to a different db to pull the customers w/ active orders info.我有粗略的 GUI 工作,我正在向我的 test.db 输入信息,但我不确定下拉选项或我将连接到不同数据库的部分以使用活动订单信息拉客户。 (Bonus can the GUI show the values I have entered as a row/column setting and then submit all at once to SQL or do I need to do one at a time?) (额外的 GUI 可以将我输入的值显示为行/列设置,然后一次性提交给 SQL 还是我需要一次做一个?)

Thanks for any help.谢谢你的帮助。

    import pyodbc 
    import tkinter as tk


    conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=server_name;'
                      'Database=db_name;'
                      'Trusted_Connection=yes;')

    cursor = conn.cursor()
    cursor.execute('SELECT * FROM db_name.TestDB')


    window = tk.Tk()

    label1 = tk.Label(window, text='Value1')
    label2 = tk.Label(window, text='Value2')
    label3 = tk.Label(window, text='Value3')

    entry1 = tk.Entry(window)
    entry2 = tk.Entry(window)
    entry3 = tk.Entry(window)

    def submittal():
    cursor.execute('''
                INSERT INTO TestDB.dbo.Info (Value1, Value2, Value3)
                VALUES
                (?, ?, ?),
                (entry1.get(),entry2.get(),entry3.get())
                ''')
    window.destroy()


    button1 = tk.Button(window, text='Submit', command=submittal)


    conn.commit()

在此处输入图像描述

For making your dropdown you can use Combobox widget inside ttk.要制作下拉列表,您可以在 ttk 中使用Combobox小部件。 (If you don't know what ttk is, it has all the features of tkinter as well as some other cool widgets like progress bars and many more. Refer to ttk python docs here: Python Documentation ) (如果您不知道 ttk 是什么,它具有 tkinter 的所有功能以及其他一些很酷的小部件,例如进度条等等。请参阅此处的 ttk python 文档: ZA7F5F35426B927411FC92381B53Z 文档

Then you could link your entry2 to a textvariable so that it goes to some function which checks whether the number inside it is greater than purchases or not.然后你可以将你的entry2链接到一个文本变量,以便它转到一些 function 来检查它里面的数字是否大于购买。

Example:例子:

variable = StringVar()
#Then set the value of `textvariable` option in tkinter to variable

entry_name = Entry(root, textvariable=variable)
entry_name.trace("w", name_of_function)

Inside the function, you could check conditions after selecting it using SELECT .在 function 内部,您可以使用SELECT选择它后检查条件。

Note: The trace method should be in w mode.注意: trace方法应该是w模式。 It is essential for making changes.这对于进行更改至关重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM