I want to loop through columns [‘A’, ‘B’, ‘C’] and pass the column names to a select statement in SQL, such as
SELECT * FROM table_name
WHERE table_name.`A` IS NULL
Thus each select statement would yield a result set based on the column name. This would be contained in a sql script. I would then read each result through a python DB connection, such as mysql-connector. How would I go about doing this? Thank you!
Edit – I want to keep the solution strictly within SQL.
2
Answers
Construct the query dynamically in Python in a loop.
Taken sqlite example. But queries will be similar for other sequential database like mysql or mssql.
The following will execute queries for each column being null. And for each query, it’ll create a 2d list and store as the value of the
dict
. The key names of thedict
will be the respective column names. You can process the data from thedict
For mysql, you can use
mysql.connector
module and for MSSQL you can usepyodbc
module for connection.