0
votes

my question is, as the title says, how I can create a variable column in SQL lite used in Pyhton.

Here's my code, to show you what exactly I mean:

#Table with variables
def dynamic_data_entry(list_name, num, column):
    element_name = list_name[num]
    cur.execute("INSERT INTO myliltable (column) VALUES (?)", element_name)
    con.commit()

I want to insert the column name, when I call the function, so I can use the function for more than one column, but I keep getting an error, that the column 'column' doesn't exist. Is there another way to do this, so I can change set the column name when I call the function, or just change the SQL part.

If there is a thread about this already, I would appreciate it, if you could link it, so I can read it myself (I couldn't find anything similar to this topic yet, which worked).

Thanks in advance, I appreciate your help

1

1 Answers

3
votes

You need to use the string's format() method to insert the column name into the SQL string:

cur.execute("INSERT INTO myliltable ({}) VALUES (?)".format(column),
            element_name)

You can find more information in the Python docs.