How can I use the values returned from a Python SQLite query as the parameters in a new Python SQLite query?

Viewed 38

I have a small SQLite database with only three tables:

  • user
  • hardware
  • software

When I run this script:

import sqlite3
connection = sqlite3.connect("MyAwesomeDB.db")
cursor = connection.cursor()
for row in cursor.execute("SELECT name FROM sqlite_master WHERE type='table' and name NOT LIKE 'sqlite_%'"):
    print(row)

I get this output:

('user',)
('hardware',)
('software',)

What I would like to do is this:

import sqlite3
connection = sqlite3.connect("MyAwesomeDB.db")
cursor = connection.cursor()
for row in cursor.execute("SELECT name FROM sqlite_master WHERE type='table' and name NOT LIKE 'sqlite_%'"):
    new_cursor = connection.cursor()
    for new_row in new_cursor.execute(f"SELECT * FROM {row}"):
        print(new_row)

So I can print the contents of every table and every row within each table, but what I am doing is not working.

I believe the extra characters (parenthesis, tick, and comma) in the row are throwing the new_row query, but I am not sure how to get around this.

I might be able use a RegEx to strip them out, but I assume there is a python function that can do this easier.

0 Answers
Related