I know this is a very basic question but for some reason I can't get past this one error. I'm trying to show/put all the names of the tables in a database (named 'GData.db') into a variable available_tables
in Python. Currently I have the following:
con = sql.connect(r'/Users/linnk/Desktop/Results/GData.db')
cur = con.cursor()
cur.execute("SHOW TABLES IN GData")
available_table=(cursor.fetchall())
This gives me the following error for the second-last line:
OperationalError: near "SHOW": syntax error
I've looked at the SHOW TABLES documentation as well as around the web but not found information that helps me.
If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.
The query to list tables in a Sqlite database:
SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;
So your snippet becomes:
con = sql.connect(r'/Users/linnk/Desktop/Results/GData.db')
mycur = con.cursor()
mycur.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;")
available_table=(mycur.fetchall())
See the Sqlite FAQ for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With