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
I am trying to prevent a record being written to the database if the 'f_name' field is blank.
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.
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. 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.
Can anyone please give me an idea of how to prevent a blank f_name being accepted in this scenario?
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
...
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; the most straigthforward way is to check when performing the 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 .
In the case you missed that, the use of get()
is supposed for dictionaries. For example if you try this in Python REPL:
>> d = {
"key1": 1,
"key2": 2
}
>> print(d.get("key1"))
will return 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.