python sqlite - where condition is empty

ben121

I have a table in an sqlite database called 'General'. It has four columns 'One', 'Two' and three

One has a list of names:

One = ['James', 'Ben', 'John', 'Peter']

Two, a list of country's:

Two = ['Uk', 'USA', 'Germany', 'UK']

three, a score:

Three = [3,6,5,9]

In python I can access this easily enough using pd.read_sql_query and adding .format to get the relevant data.

name = 'John'
country = 'Germany'

conn = sqlite3.connect('score_history.db')
df = pd.read_sql_query('''SELECT * FROM General WHERE (name='{}') & (country='{}')'''.format(name, country), conn)
conn.close()

However, sometimes I just want to search the country (for instance UK) and leave name blank. How would I adapt the above so that the same code can be used to either just search by country / name / score, but also be able to search multiple columns as above for country and name, or country and score or name and score?

Thanks

GMB

You could explicitly handle the empty string in the comparisons, like so:

df = pd.read_sql_query('''
    SELECT * 
    FROM General 
    WHERE ('{}' = '' OR name='{}') AND ('{}' = '' OR country='{}')
    '''.format(name, name, country, country), conn)

You can also use IN to avoid repeating the parameter:

df = pd.read_sql_query('''
    SELECT * 
    FROM General 
    WHERE '{}' IN ('', name) AND '{}' IN ('', country)
    '''.format(name, country), conn)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python, Sqlite - Is there any way to neuter a condition in WHERE clause of a query?

Python: using where condition

SQL query with WHERE condition that allows empty fields

Android SQLite additional WHERE condition after MATCH

Android SQLite query where column is not null and not empty

Python Pandas Where Condition Is Not Working

sqlite DELETE WHERE... not working - python sqlite

TypeORM, add condition in `where` if value is presented and not empty string

Python Produce empty list if condition not satisafied

How to fallback to secondary search condition in sqlite WHERE clause?

sqlite.net table where condition on a child table

Why does a WHERE condition discard content obtained by LEFT JOIN? SQLITE

How can I add a WHERE condition on a Text field in a SQLite query?

SQLite. Stops at the first instance the WHERE condition is met

Python Pandas Concat "WHERE" a Condition is met

Python: np.where with multiple condition

What does this where Condition mean in python script

Python: sum values in column where condition is met

Python: Concat dataframes where one of them is empty

SQLite3 WHERE Clause In Python

Python SQLite3 UPDATE WHERE not updating

How do I check for a condition before writing an empty file in Python?

Python - Intersection on multiple sets on the condition that empty sets are to be excluded

Python: condition to check that none of list's element is an empty list

how to add to an empty list each time a condition is met in a for loop python?

If one column is empty then fetch data using another columns where condition in SQL Server

SQL Query to generate a column condition where it doesn't necessarily need to be empty nor filled

Laravel 5.8: Eloquent where clauses returns null but does not execute if..empty condition

Select multiple entries in SQLite where multiple rows need to match the same condition (including AND, OR and NOT)