插入数值到 SQL 数据库从 wx.TextCtrl,并从 wx.ListCtrl 中获取选择的数值。

huangapple go评论105阅读模式
英文:

Insert values into SQL from wx.TextCtrl and pull selection values from wx.ListCtrl

问题

# 在尝试编写代码,从文本框中获取值并将其插入到我的SQL表中。我相当确定代码已经编写正确,但我认为我需要定义一些东西,但我对格式或如何进行操作不确定。我收到了以下错误:

> 缺少3个必需的位置参数'db''query''parameters'

此外我在尝试返回从wx.ListCtrl中选择的值时遇到了问题我可以使其检测到正确的选择但到目前为止我只能返回选择号码例如0,1,2,3),而不是实际的字符串我如何使其正确读取

以下是代码的最小示例

# 从SQL列中读取以用于下拉菜单
# 其余的代码...

class compwin(wx.Frame):
    # 其余的代码...
    
    def add_data(self,e,db: sqlalchemy.engine.base.Engine, query: str,parameters: dict):
        # 其余的代码...

        stmt = sqalchemy.text(query)
        try:
            print("Testing...")
            with engine.connect() as conn:
                conn.execute(stmt, parameters=parameters)
                conn.commit()
        except Exception as e:
            print("Error!")
英文:

Right now I'm trying to write code that will pull a value from a text box, and insert it into my SQL table. I'm pretty sure I have the code written correctly, but I think I need to define a few things, but I'm unsure of the format or how to go about it. I am getting the error:

> missing 3 required positional arguments: 'db', 'query', and 'parameters'

Additionally, I am having trouble returning the selected value from wx.ListCtrl. I can have it detect the correct selection, but all I've been able to do so far is return the selection number (e.g. 0,1,2,3, etc) and not the actual string. How can I get it to read properly?

Here is a minimal example of code, below.

import sys
import wx
from six.moves import urllib
import wx.adv
import datetime
from datetime import date
import sqlalchemy
from sqlalchemy import *
import sqlalchemy_utils 
import pandas as pd
from datetime import datetime
import _strptime
import os

#Reads the SQL Column to use for the dropdown menu
params = urllib.parse.quote_plus("Driver={ODBC Driver 18 for SQL Server};"
                              "Server=SERVER;"
                              "Database=DATABASE;"
                              "Trusted_Connection=yes;"
                              "TrustServerCertificate=yes"
                                                )
engine = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params) 
engine.connect()
#Gets list of Part Numbers for the menubox as well as matching to SQL DF
sqlread = pd.read_sql_query('''SELECT * FROM TABLE''',engine)
pndf = pd.DataFrame(sqlread, columns = ['PN'])
pndf = pndf[pndf.PN != None]
dflist = pndf.values.tolist()


class compwin(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,'Test',
                          size=(400,600))
        panel = wx.Panel(self,-1)

        #Prompts for Update Field
        wx.TextCtrl(panel,value = "________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________",size=(1300,20),pos=(-1,275),style=wx.TE_READONLY|wx.BORDER_NONE)
        wx.TextCtrl(panel,value = "Update What Part?",size=(100,20),pos=(25,325),style=wx.TE_READONLY|wx.BORDER_NONE)
        #Text Boxes for Update Field
        self.ComboUp = wx.ListCtrl(panel, wx.ID_ANY, size=(150,125),pos=(20,350),style=wx.LC_REPORT)
        self.ComboUp.InsertColumn(0,"Select a Part Number:",format=wx.LIST_FORMAT_RIGHT,width=150)
        for Part in dflist:
            self.ComboUp.Append(Part)
        
        
        #update field bindings
        
        #Prompts for add field
        wx.TextCtrl(panel,value = "Part Number",size=(100,20),pos=(75,100),style=wx.TE_READONLY|wx.BORDER_NONE)
        self.PNAdd = wx.TextCtrl(panel,value = "",size=(125,20),pos=(60,125))
        #define buttons
        self.sysexit = wx.Button(panel,label="Exit", name="1",pos=(100,500))
        self.add = wx.Button(panel,label="Add Record", name="2",pos=(300,125))

        #bindings for panel

        self.Bind(wx.EVT_BUTTON,self.OnClick_exit,self.sysexit)
        self.Bind(wx.EVT_BUTTON,self.add_data,self.add)

        self.Bind(wx.EVT_LIST_ITEM_SELECTED,self.OnSelectAdd,self.ComboUp)
        self.add.SetDefault()
        

######################### COMBO BOX SELECTION ######################################################################
    def OnSelectAdd(self,e):
        item_select = self.ComboUp.GetFocusedItem()
        if item_select != -1:
            textprint = self.ComboUp.GetItem(item_select).GetItemText()
            print(textprint)
############################################# EXIT BUTTON ######################################################################
    def OnClick_exit(self,e):
      self.Close()
############################################ ADD RECORD BUTTON ############################################################
    def add_data(self,e,db: sqlalchemy.engine.base.Engine, query: str,parameters: dict):
        log_headline: str = "insert_data() ::"
        """
        :param db:
        :param query: INSERT INTO DATABASE.TABLE(PN) VALUES(:PN)
        :param parameters: {self.PNAdd.GetValue():PN}
        :return:
        """
        stmt = sqalchemy.text(query)
        try:
            print("Testing...")
            with engine.connect() as conn:
                conn.execute(stmt, parameters=parameters)
                conn.commit()
        except Exception as e:
            print("Error!")



if __name__ == '__main__':
    app = wx.App()
    frame = compwin()
    frame.Show()
    app.MainLoop()

答案1

得分: 1

以下是您要翻译的代码部分:

这是获取已在 `wx.ListCtrl` 中选择的项目详细信息的一种方法
您将数据以列的方式放入因此需要查询所需的列
所选项目的索引包含在事件中

import wx

dflist = [
    ["123", "Widget 1", "w101"],["456", "Widget 2", "w2091"],["789", "Widget 3", "w3000"],["132", "Widget 4", "w404"],
    ["465", "Widget X", "x555"]
]

class compwin(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Shortage Sheet Compiler v0.1',
                          size=(600,300))
        panel = wx.Panel(self, -1)

        #文本框输出

        #更新字段的提示
        wx.TextCtrl(panel, value="更新哪个部分?", size=(150,20), pos=(25,20), style=wx.TE_READONLY|wx.BORDER_NONE)

        #更新字段的文本框
        self.Combobox = wx.ListCtrl(panel, wx.ID_ANY, size=(400, -1), pos=(25, 50), style=wx.LC_REPORT)
        self.Combobox.InsertColumn(0, "项目", wx.LIST_FORMAT_RIGHT)
        self.Combobox.InsertColumn(1, "名称", wx.LIST_FORMAT_RIGHT)
        self.Combobox.InsertColumn(2, "零件号", wx.LIST_FORMAT_RIGHT)
        self.Combobox.SetColumnWidth(1, 150)
        self.Combobox.SetColumnWidth(2, 150)

        for item in dflist:
            self.Combobox.Append((item[0], item[1], item[2]))
        self.Combobox.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)

    def OnItemSelected(self, event):
        ind = event.GetIndex()
        item = self.Combobox.GetItem(ind, 0).GetText()
        name = self.Combobox.GetItem(ind, 1).GetText()
        part = self.Combobox.GetItem(ind, 2).GetText()
        print("项目", item, "名称", name, "零件号", part)

if __name__ == '__main__':
    app = wx.App()
    frame = compwin()
    frame.Show()
    app.MainLoop()

关于 Add 函数:
您已将按钮事件绑定到一个程序。因此,它将使用 selfevent 调用。
通过奇妙的思考,您在其参数中包括了数据库定义、查询和输入数据。而这些都没有定义或传递 - 可见错误。
add_data 程序中定义这些内容,并从参数列表中删除它们,参数列表应包含 (self, event)

英文:

Here is one way of getting the details of an item that has been selected in the wx.ListCtrl.
You put the data in, in columns, so you need to quiz the columns that you require.
The index of the selected item, is contained in the event.

import wx
dflist = [
["123","Widget 1", "w101"],["456","Widget 2", "w2091"],["789","Widget 3", "w3000"],["132","Widget 4", "w404"],
["465","Widget X", "x555"]
]
class compwin(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'Shortage Sheet Compiler v0.1',
size=(600,300))
panel = wx.Panel(self,-1)
#textbox printout
#Prompts for Update Field
wx.TextCtrl(panel,value = "Update What Part?",size=(150,20),pos=(25,20),style=wx.TE_READONLY|wx.BORDER_NONE)
#Text Boxes for Update Field
self.Combobox = wx.ListCtrl(panel, wx.ID_ANY, size=(400, -1),pos=(25, 50), style=wx.LC_REPORT)
self.Combobox.InsertColumn(0, "Item", wx.LIST_FORMAT_RIGHT)
self.Combobox.InsertColumn(1, "Name", wx.LIST_FORMAT_RIGHT)
self.Combobox.InsertColumn(2, "Part Number", wx.LIST_FORMAT_RIGHT)
self.Combobox.SetColumnWidth(1, 150)
self.Combobox.SetColumnWidth(2, 150)
for item in dflist:
self.Combobox.Append((item[0],item[1],item[2]))
self.Combobox.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
def OnItemSelected(self, event):
ind = event.GetIndex()
item = self.Combobox.GetItem(ind, 0).GetText()
name = self.Combobox.GetItem(ind, 1).GetText()
part = self.Combobox.GetItem(ind, 2).GetText()
print("item", item, "name", name, "part number", part)
if __name__ == '__main__':
app = wx.App()
frame = compwin()
frame.Show()
app.MainLoop()

Regarding the Add function:
You have bound the button event to a routine. So it will be called with self and the event.
By way of magical thinking, you've included in it's parameters: the database definition, the query and the input data. None of which you have defined or passed - witness the error.
Define those things within the add_data routine and remove them from the parameter list, which should consist of (self, event)

Happy trails!

huangapple
  • 本文由 发表于 2023年8月11日 03:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定