The code is supposed to execute the query base on the input from the user in the python environment (not within the sql query). For example, the variable is defined in the Python environment, and in raw input for table name = customers
, I would expect the query to print the column names of the table customers.
However, the below code report syntax error. If I remove the backward slash and the inner quotation mark, it will report no such column: table_name
. It appears that the value customers
is not being pass through into the query, and the query is reading table_name
as a string.
Please help. Thanks
import sqlite3
def ask_column(db_name, table_name):
conn = sqlite3.connect(db_name)
c = conn.cursor()
c.execute('SELECT sql FROM sqlite_master WHERE type = \'table\' And name = \'table_name\'')
print c.fetchall()
conn.close()
db_name = raw_input("Please type the database name : ")
table_name = raw_input("Please type the table name: ")
ask_column(db_name, table_name)