There’s a database with multiple tables. Is there a way to print out table names that contain columns related to customers, for example: customer_ID?
What I need to do is:
There’re two tables named "payment" and "customer" that have columns "customer_ID", so names of tables "payment" and "customer" have to be printed.
>Solution :
You can use exists with a subquery:
select m.name from sqlite_master m where m.type = 'table' and exists
(select 1 from pragma_table_info(m.name) m1 where m1.name = 'customer_ID')
import sqlite3
conn = sqlite3.connect('test_db.db')
r = list(conn.cursor().execute('''select m.name from sqlite_master m where m.type = 'table' and exists
(select 1 from pragma_table_info(m.name) m1 where m1.name = 'customer_ID')'''))