当前位置:首页 > 数据库 > SQlite

在python中创建sqlite3表的安全方法

对于插入表格,安全的方法是

c.execute("insert into table (?,?,?,...)",my_tuple)

但是,如何安全地创建表呢?我已经尝试过这样的事情:

conn = sqlite3.connect(database)
c = conn.cursor()
cmd = "create table ? (? text,? text)"
my_tuple = ("my_table","first","second")
c.execute(cmd,my_tuple)

但我得到这样的错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: near "?": syntax error

我是否应该在python中组装一个字符串并将其扔到sqlite来创建表?

解决方法:

如果您只是想导入csv文件,并假设some user who might try to mess with your database不会将其发送给您,则可以将其放到Python中:

"create table %s (%s text,%s text)" % ("my_table","first","second")

如果电子表格很可能来自恶意用户,那么您就需要担心如何清理列/文件名.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!