一个父行,另一个表中的多个子行。(One parent row, multiple child rows in another table. How to get them all in one row?)
我是一名当地非营利组织的志愿者,在节日期间赠送玩具和食物。 我帮助他们的一种方式是在网上提出他们的礼物/食物要求。 这样,您可以在15个小时内手动将1000个左右的名称手动输入到Excel中,只需单击链接即可在几秒钟内导出整个内容。
我的问题与如何在一个查询中从数据库中获取数据有关。 我相信这是可能的,但不能完全包围我能做到的。
有一个父表,其中包含将收到礼物的孩子的家庭信息(每个家庭1行)。 有一个子表,其中包含家庭的孩子的信息(每个孩子1行,每个父行有很多行)。 每个父行将有1-3个子行。
PARENT TABLE ----------------------------- id name address city CHILDREN TABLE ----------------------------- parent_id name gift1 gift2 gift3
在从数据库导出数据时,我希望将所有孩子的信息与父母的信息一起获取,以便将其放入CSV文件中。 我知道我可以通过两个单独的查询来执行此操作,但如果可能的话,我不想为每个家庭的孩子访问数据库。
我已经思考了一个小时左右,并且无法想出我将如何实现这一目标。 我确定我需要加入这些表,但我不确定如何区分每个孩子并避免重复它们而不是让下一个孩子(如果有的话)。
I volunteer for a local non-profit that gives out toys and food during the holiday season. One way that I am helping them is by putting their gift/food requests online. This way instead of manually entering 1,000 or so names manually into Excel over the course of 15 hours they can simply click on a link and have the whole thing exported in the matter of seconds.
My issue is related to how I can get the data from the database in one query. I believe it is possible but can't quite wrap my head around I can do it.
There is a parent table which contains the family information for the kids who will receive gifts (1 row per family). There is a child table that contains the family's childrens' information (1 row per child, many rows for each parent row). There will be 1-3 children rows for each parent row.
PARENT TABLE ----------------------------- id name address city CHILDREN TABLE ----------------------------- parent_id name gift1 gift2 gift3
I would like to get all of the childrens' information with the parents' information in one row when exporting the data from the database so it can be put into a CSV file. I know I can do this with two separate queries but I don't want to hit the database for each and every family's children if possible.
I've pondered this for an hour or so and can't quite come up with how I would accomplish this. I'm sure I need to join the tables but I am unsure how to differentiate each child and avoid duplicating them instead of getting the next child (if there is one).
原文:https://stackoverflow.com/questions/3512948
最满意答案
你快到了。 您只需要通过lambda或functools.partial传递名称:
newbuttonname = username newbuttonname = Tkinter.Button(win, text = newbuttonname, command = lambda name=username:click_one(name))
您可以使用相同的技术传递实际的wodget或任何其他数据。
You are almost there. You simply need to pass the name via lambda or functools.partial:
newbuttonname = username newbuttonname = Tkinter.Button(win, text = newbuttonname, command = lambda name=username:click_one(name))
You can use this same technique to pass in the actual wodget, or any other data.
相关问答
更多-
python tkinter 怎么获取按钮本身[2023-08-13]
def b2cmd(event): print(event.widget['text']) b2=Button(root,text = 'b2') b2.pack() b2.bind(" ",b2cmd) -
我想你想要这样的东西: from tkinter import * from tkinter import ttk root = Tk() board_frame = ttk.Frame(root, padding=5) board_frame.grid(column=0,row=0) COORDS_LIST = [] buttons_dict = {} ########################################### def fire_here(x, y): print ...
-
如何获取我刚刚点击的按钮的变量名称,tkinter(How to get the variable name of a button I have just clicked, tkinter)[2021-11-08]
假设您以通常的方式创建按钮,您可以使用lambda传递参数。 lambda允许您使用参数创建一个匿名函数,然后您可以使用它来调用您的函数。 如果要传递实际的按钮引用,则需要分两步执行,因为按钮对象在创建之后才会存在。 for i in range(10): button = tk.Button(...) button.configure(command=lambda b=button: move(b)) 你的move功能需要如下所示: def move(var): mGridv = ... -
感谢Bryan Oakley,最后我注意到用Mac改变按钮高度是不可能的,他建议使用标签作为替代,这可能是解决问题的最佳选择。 再次感谢您的帮助。 Thanks to Bryan Oakley and finally I noticed that it is impossible to change the button height with Mac, and he suggested using label as alternative which is probably the best choice ...
-
tkinter - 获取按钮名称?(tkinter - get button name?)[2023-04-19]
你快到了。 您只需要通过lambda或functools.partial传递名称: newbuttonname = username newbuttonname = Tkinter.Button(win, text = newbuttonname, command = lambda name=username:click_one(name)) 您可以使用相同的技术传递实际的wodget或任何其他数据。 You are almost there. You simply need to pass ... -
在tkinter按钮不显示(In tkinter button is not showing)[2022-04-28]
该按钮不可见,因为它的父( my_gui )不可见。 您需要在my_gui上使用pack , place或grid使其可见。 例如: my_gui = Resizer(root) my_gui.pack(fill="both", expand=True) The button isn't visible because it's parent (my_gui) isn't visible. You need to use pack, place or grid on my_gui to make it v ... -
多行Tkinter按钮?(multi line Tkinter button?)[2023-07-01]
使用wraplength选项是一个很好的方法来实现它的换行,并使用justify选项来执行除center之外的其他操作。 option1 = tkinter.Button(opt1, wraplength=80, justify=LEFT, text='This is the text') wraplength的值以屏幕单位或像素为单位。 您也可以在字符串中间放一个'\n'字符来手动分割线条,但如果您这样做,也可能必须手动调整按钮大小。 Using the wraplength option is a g ... -
我的Tkinter按钮错误(My Tkinter button error)[2023-05-07]
如果你想让按钮有一个处理函数,你需要在使用这个函数之前声明它,如下所示: def activate(): os.system("networksetup -setairportpower airport on") ActivateButton = Button(text="Activate", fg="green", bg="black", command=activate) 所以你需要切换你的部分#Activate/Deactivate command在脚本中#Activate/Deactiv ... -
要字面回答你的问题: 是否有一个函数来获取Tkinter中小部件的主框架的名称? winfo_parent正是您所需要的。 为了有用,您可以将它与_nametowidget结合使用(因为winfo_parent实际上返回了父代的名称)。 parent_name = widget.winfo_parent() parent = widget._nametowidget(parent_name) To literally answer your question: Is there a function to ...
-
winfo_height返回以像素为单位的值。 除非按钮或标签仅包含图像,否则按钮和标签的height选项会将值视为平均大小的字符数。 您可以使用带有适当选项的grid来确保每个窗口小部件的大小完全相同,而不是使用place 。 布局GUI时, place很少是正确的选择。 Tkinter非常擅长计算尺寸和位置。 winfo_height returns values in pixels. The height option for buttons and labels treats the values ...