2

I made a database for a project and I want to be able to search the database for the value but better than just a simple == operation. For example, If someone types in "columbia" for the search, I want the dictionary that has "Columbia University " as the value for the "Affiliation" key for that specific person

1

1 Answer 1

4

You can search using regular expressions:

>>> db.search(User.name.matches('[aZ]*'))
>>> db.search(User.name.search('b+'))

These correspond to Python's re.match and re.search where the latter searches for an occurenc that may start anywhere in the string.

Alternatively you can use a custom test expression like so:

>>> test_contains = lambda value, search: search in value
>>> db.search(User.name.test(test_contains, 'Columbia'))

For more details, refer to the relevant section of the TinyDB docs.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you elaborate on the test_contains example? I've tried this as written and I end up with a NameError: name 's' is not defined
Thanks for the hint, @AaronCiuffo! The second code example was wrong, it should be fixed now 🙂

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.