[英]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.我的参数变量是parameters = 'name text, id integer'
它的作用是创建一个包含一列的表,名为“name text, id integer”,类型未知。
The solutions from here: Python sqlite3, create table columns from a list with help of .format don't work.来自这里的解决方案: Python sqlite3,在 .format 的帮助下从列表创建表列不起作用。 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})")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.