[英]Skip None values in entry widgets Tkinter Python
Let's say we have a Tkinter app with many entry widgets like this:假设我们有一个 Tkinter 应用程序,其中包含许多这样的入口小部件:
root = Tk()
entry1 = Entry(root, width=40)
entry1.grid(row=0, column=0)
entry2 = Entry(root, width=40)
enrtry2.grid(row=1,column=0)
entry3 = Entry(root, width=40)
enrtry3.grid(row=2,column=0)
entry4 = Entry(root, width=40)
enrtry4.grid(row=3,column=0)
root.mainloop()
The values from these widgets are used to UPDATE
a database.这些小部件的值用于
UPDATE
数据库。 The thing I want is to be able to skip a value in anyone of them and not use it in the UPDATE
statement.我想要的是能够跳过其中任何一个值,而不是在
UPDATE
语句中使用它。 For now with my code I check if there is something written in the widgets like this:现在我用我的代码检查小部件中是否有这样的东西:
db = fdb.connector
c = db.cursor
values = [entry1.get(), entry2.get(), entry3.get(), entry4.get()]
for v in values:
if len(v) == 0 or v is None:
pass
elif lev(v) != 0:
c.execute(f"""UPDATE table1 SET column1 = {v[0]}, column2 = {v[1]}, column3 = {v[2]}, column4 = {v[3]} WHERE ID = 1;""")
The problem is that when one of the values is None
in the database I get a value of NULL
which is correct, because there is no value in the widget
.问题是,当数据库中的值之一为
None
时,我得到的值是NULL
,这是正确的,因为widget
中没有值。 How can I 'tell' python that when there is no value presented it should skip that value and continue to update just the others?我如何“告诉” python 当没有提供值时,它应该跳过该值并继续更新其他值?
EDIT: Thanks to @TheLizzard this solution works like a charm:编辑:感谢@TheLizzard,这个解决方案就像一个魅力:
string = "UPDATE table1 SET "
at_least_one_set = False
for i in range(12):
if v[i] != "":
string += columns[i] + " = " + "'" + str(v[i]) + "'" + ", "
at_least_one_set = True
if at_least_one_set:
string = string[:-2]
string += f" WHERE column_a = '{v[0]}' OR column_b = '{v[1]}';"
c.execute(string)
else:
print("No values were set. Raise an error?")
EDIT2: After a bit of research, and thanks to @TheLizzard, @Arioch 'The, @Mark Rotteveel I've come with the following working code and following one of the suggestions here this is safe now: EDIT2:经过一番研究,感谢@TheLizzard,@Arioch'The,@Mark Rotteveel,我提供了以下工作代码并遵循此处的建议之一,现在这是安全的:
v = ["", "2", "3", ""]
column_names = ["column_a", "column_b", "column_c", "column_z"]
rowids = [row[0] for row in c.execute("SELECT id FROM table1 WHERE column_a = ? OR column_b = ?", (v[0], v[1], ))]
string = "UPDATE table1 SET "
at_least_one_set = False
for i in range(12):
if v[i] != "":
string += columns[i] + " = '{}'".format(v[i]) + ", "
at_least_one_set = True
if at_least_one_set:
string = string[:-2]
string += " WHERE id = {}".format(rowids[0]) + ";"
c.execute(string)
And the result is:结果是:
UPDATE table1 SET column_b = '2', column_c = '3' WHERE id = 1;
Try this:尝试这个:
v = ["", "2", "3", ""]
column_names = ["column_a", "column_b", "column_c", "column_z"]
string = "UPDATE table1 SET "
at_least_one_set = False
for i in range(4):
if v[i] != "":
string += column_names[i] + " = " + v[i] + ", "
at_least_one_set = True
if at_least_one_set:
string = string[:-2]
string += ";"
print(string)
else:
print("No values were set. Raise an error?")
I dynamically create a string starting with "UPDATE table1 SET "
and ending in ";"
我动态创建一个以
"UPDATE table1 SET "
开头并以";"
结尾的字符串where I skip all of the values that are ""
.我跳过所有
""
的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.