Pro Girl
Pro Girl

Reputation: 952

SQLITE error while trying to parse every table data with python

I am trying to print the entire table from all the tables inside of a db that I have created.

When I try to print all columns for each table I get an error and it seems that the script tries to parse trough one of the inside elements (first column and first row data) and therefore bringing back an error by saying that there is no such a table.

Here is my code:

import sqlite3
conn = sqlite3.connect('amazon_pages.db')
c = conn.cursor()
all_tables_list = c.execute("select name from sqlite_master where type = 'table'")
for table in all_tables_list:
    argument_execute = 'SELECT * FROM ' + table[0]
    print(argument_execute)
    c.execute(argument_execute)

This is the error that I get:

SELECT * FROM Apple_charger
Traceback (most recent call last):
SELECT * FROM B07JGMC714
  File "/Users/Amato/PycharmProjects/Refine/Amazon_pages_sql_database_creator.py", line 36, in <module>
    c.execute(argument_execute)
sqlite3.OperationalError: no such table: B07JGMC714

Process finished with exit code 1

How do I print all the entire tables from the db?

Upvotes: 0

Views: 145

Answers (1)

LinPy
LinPy

Reputation: 18578

fetch the result first:

all_tables_list = c.execute("select name from sqlite_master where type = 'table'")
rows = all_tables_list.fetchall()
for row in rows:
    print(row)

Upvotes: 1

Related Questions