跳过条目小部件 Tkinter None 中的值 Python

Skip None values in entry widgets Tkinter Python

假设我们有一个 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()

来自这些小部件的值用于 UPDATE 数据库。我想要的是能够跳过其中任何一个的值,而不是在 UPDATE 语句中使用它。现在用我的代码检查是否有像这样写在小部件中的东西:

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;""")

问题是,当数据库中的一个值是 None 时,我得到的值 NULL 是正确的,因为 widget 中没有值。我如何才能 'tell' python 当没有值出现时它应该跳过该值并继续更新其他值?

编辑:感谢@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: 经过一番研究,多亏了@TheLizzard、@Arioch 'The、@Mark Rotteveel,我得到了以下工作代码和以下代码之一建议 here 现在安全了:

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)

结果是:

UPDATE table1 SET column_b = '2', column_c = '3' WHERE id = 1;

试试这个:

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?")

我动态创建了一个以 "UPDATE table1 SET " 开头并以 ";" 结尾的字符串,其中我跳过了所有 "".

的值