简体   繁体   中英

How to "format" column names and types into python sqlite3?

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?

不要在{parameters}周围加上引号,这会将其变成单个参数定义。

        cursor.execute(f"CREATE TABLE IF NOT EXISTS '{scrub(i)}' ({parameters})")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM