简体   繁体   English

防止将空白记录写入数据库的是 SQL3 和 Python

[英]Preventing blank records being written to database is SQL3 & Python

This is the first time I am posting so hope I do this correctly.这是我第一次发帖,所以希望我做得正确。

Can anyone assist me with a problem that I do not seem to be able to get past.任何人都可以帮助我解决我似乎无法解决的问题。 I have had a good google but not found an answer.我有一个很好的谷歌,但没有找到答案。 I am using Python 3.7 and SQL3我正在使用 Python 3.7 和 SQL3

I am trying to prevent a record being written to the database if the 'f_name' field is blank.如果“f_name”字段为空,我试图阻止将记录写入数据库。

I have tried to put a condition on the conn.commit() from not writing - did not work I have tried to do a condition and indent the c.execute if f-name is blank but that has not worked as I guess it has not got the input yet.我试图在 conn.commit() 上设置一个条件,而不是写 - 没有工作我试图做一个条件并缩进 c.execute 如果 f-name 为空白但我猜它没有工作还没有得到输入。

if I print out f_name after the c.execute I get "..entry2" regardless if there is an input or not if that makes any difference or gives a clue.如果我在 c.execute 之后打印出 f_name,我会得到“..entry2”,无论是否有输入,是否有任何区别或提供线索。 I am guessing it is being written directly to the database and does not exist as a variable.我猜它被直接写入数据库并且不作为变量存在。

The error check I use is as follows (I have used it successfully elsewhere so it should be ok)我使用的错误检查如下(我已经在别处成功使用过所以应该没问题)

if f_name =="":
          print("No valid Data in First name field")
          return
else:
          conn.commit()

and I tried using the error check on the conn.commit() but I am guessing it is auto commiting.我尝试在 conn.commit() 上使用错误检查,但我猜它是自动提交的。

Can anyone please give me an idea of how to prevent a blank f_name being accepted in this scenario?谁能告诉我如何防止在这种情况下接受空白 f_name ?

Here is the code being called -这是被调用的代码 -

def submit():
    # Connect to database
    conn = sqlite3.connect('address_book.db')
    c = conn.cursor()

    six_month_reminder = ("f")
    annual_reminder = ("f")
    six_month_reminder_sent = ("f")
    annual_reminder_sent = ("f")

    # Insert Into Table
    c.execute("INSERT INTO addresses VALUES (:f_name, :l_name, :address, :city, :state, :zipcode, :six_month_reminder, :annual_reminder, :six_month_reminder_sent, :annual_reminder_sent)",
            {
                'f_name': f_name.get(),
                'l_name': l_name.get(),
                'address': address.get(),
                'city': city.get(),
                'state': state.get(),
                'zipcode': zipcode.get(),
                'six_month_reminder': six_month_reminder,
                'annual_reminder': annual_reminder,
                'six_month_reminder_sent': six_month_reminder_sent,
                'annual_reminder_sent':annual_reminder_sent
            })

    conn.commit()
    conn.close()

thanks for contributing ot SO.感谢您的贡献。

In your code chunk it is missing the part in which you declare f_name , l_name , address ...在您的代码块中,它缺少您声明f_namel_nameaddress ... 的部分

Assuming you have declared those in a chunk you didn't write in the question, I assume here that fname is set and it is a dictionary;假设您已在问题中未写的块中声明了这些内容,我在这里假设fname已设置并且它是字典; the most straigthforward way is to check when performing the get() :最直接的方法是在执行get()时进行检查:

    ...
    annual_reminder_sent = ("f")

    if f_name.get() == '' or f_name.get() is None:
        return
    ...

EDIT: the second statement for the or is needed as in case of key is not in the dictionary get will return None , see Python documentation about .编辑: or的第二条语句是必需的,因为如果 key 不在字典中get将返回None ,请参阅Python 文档关于.

In the case you missed that, the use of get() is supposed for dictionaries.如果您错过了这一点,则应该使用get()用于字典。 For example if you try this in Python REPL:例如,如果您在 Python REPL 中尝试此操作:

>> d = {
   "key1": 1,
   "key2": 2
}
>> print(d.get("key1"))

will return 1 .将返回1 Dictionaries are key-value maps.字典是键值映射。

In your case it seems you are doing something wrong as you are using get() on names that are not dictionaries.在您的情况下,您似乎做错了什么,因为您在不是字典的名称上使用get()

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

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