2

I'm doing some queries ( inserts ) to a database based on some input. However not all the times I get all the data from the user. I still would like though to insert the data that I received. I have a table with close to 10 columns but in the data I also have some arrays.

When I'm trying to insert something I get an exception that the say input['name'] does not exists and the query is not executed.

Is there some way to quickly manage that? If a variable does isn't defined simply throw a warning like in PHP and not break the whole loop.

New to python and only thing I can think of is to check for every single variable but I'd really hope there's something more simpler than this and quicker.

1
  • You can add default value to the variables. Commented Dec 14, 2015 at 13:45

2 Answers 2

1

Do input.get('name')

From the docs https://docs.python.org/2/library/stdtypes.html#dict.get

Return the value for key if key is in the dictionary, else default.
If default is not given, it defaults to None, so that this method never raises a KeyError.
Sign up to request clarification or add additional context in comments.

Comments

0

You should look into exception handling. It sounds like you need to use an try-except-else where you're making use of input['name']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.