0

I have just started learning SQLite and was creating a project which has a .sqlite file in which there are multiple tables. I want to ask the user to input the table_name and then the program will fetch the columns present in that particular table.

So far I have done this.

app_database.py

def column_names(table_name):
    conn = sqlite3.connect('northwind_small.sqlite')

    c = conn.cursor()

    c.execute("PRAGMA table_info(table_name)")
    columns = c.fetchall()
    for c in columns :
        print(c[1])

    conn.commit()
    conn.close()

our-app.py

import app_database
table_name  = input("Enter the table name = ")
app_database.column_names(table_name)

when I run our-app.py I don't get anything.

C:\Users\database-project>python our-app.py
Enter the table name = Employee

C:\Users\database-project>

Can anyone tell me how should I proceed?

  • You are asking for a table literally named `"table_name"`. – mkrieger1 Jun 28 '20 at 06:41
  • I did try `c.execute("PRAGMA table_info(?)",table_name)` yet didnt get any results. –  Jun 28 '20 at 06:42
  • Does this answer your question? [How do I put a variable inside a string?](https://stackoverflow.com/questions/2960772/how-do-i-put-a-variable-inside-a-string) – mkrieger1 Jun 28 '20 at 06:43
  • (See also https://stackoverflow.com/questions/3247183/variable-table-name-in-sqlite) – mkrieger1 Jun 28 '20 at 06:43

0 Answers0