[英]How to "format" column names and types into python sqlite3?
我有一个要在批量创建表时使用的函数。
这是函数:
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()
我的参数变量是parameters = 'name text, id integer'
它的作用是创建一个包含一列的表,名为“name text, id integer”,类型未知。
来自这里的解决方案: Python sqlite3,在 .format 的帮助下从列表创建表列不起作用。 我将使用不同的名称和类型作为输入,并且列数会有所不同。
有什么方法可以解决这个问题?
不要在{parameters}
周围加上引号,这会将其变成单个参数定义。
cursor.execute(f"CREATE TABLE IF NOT EXISTS '{scrub(i)}' ({parameters})")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.