0

I have a function that I want to use when mass creating tables.
This is the function:

def make_tables(tablenames: list, cursor: sql.Cursor, parameters, connection: sql.Connection):
    for i in tablenames:
        cursor.execute(f"CREATE TABLE IF NOT EXISTS '{scrub(i)}' ('{parameters}')")
    connection.commit()

My parameters variable is parameters = 'name text, id integer' what it does, is create a table with one column, called 'name text, id integer' and type unknown.
The solutions from here: Python sqlite3, create table columns from a list with help of .format don't work. I will have different names and types as input, and the number of columns will vary.
What ways are there to solving this problem?

4
  • 1
    Don't put quotes around {parameters}. That's making it a single string, not parsing it as separate parameters. Commented Nov 3, 2020 at 0:44
  • put that as an answer please so I can accept it Commented Nov 3, 2020 at 0:49
  • I voted to close this question as a simple typo, it's not worth answering. Commented Nov 3, 2020 at 0:51
  • but i meant to type the single quotes Commented Nov 3, 2020 at 0:55

1 Answer 1

1

Don't put quotes around {parameters}, that turns it into a single parameter definition.

        cursor.execute(f"CREATE TABLE IF NOT EXISTS '{scrub(i)}' ({parameters})")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.