【发布时间】:2017-03-06 12:30:57
【问题描述】:
我需要一个可以在表中插入值的通用函数。如何使用可变数量的参数来做到这一点。
例如:
def create_table(self, tb_name, *args):
sqlite3.connect(self.db_name).cursor().execute("DROP TABLE IF EXISTS users")
sqlite3.connect(self.db_name).cursor().execute("CREATE TABLE users(*args[0], *args[1], ....*args[n])")
- *args[0] 是“姓名” *args[1] 是“密码”
- *args[0] 是'姓名' *args[1] 是'密码' *args[2] 是'电子邮件'
函数必须双向起作用。
提前谢谢你。
【问题讨论】:
-
除非 SQLite 默认为文本,否则 SQL 中的
CREATEDDL 命令需要具有命名列的数据类型。你如何将该数据类型列表传递给函数? -
为什么不使用现成的解决方案,比如 SQLAlchemy 或 PonyORM?他们已经解决了所有这些问题,并且非常具有声明性。
-
如果您在 Python 中按照标记执行此操作,为什么不使用字符串连接和
%运算符动态创建字符串"CREATE TABLE users(*args[0], *args[1], ....*args[n])"?