Python sqlite3参数化删除表 [英] Python sqlite3 parameterized drop table

查看:443
本文介绍了Python sqlite3参数化删除表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中删除sqlite3表时遇到问题.我正在使用标准的sqlite3模块.

I have a problem with dropping sqlite3 table in python. I am using standard sqlite3 module.

self.conn = sqlite3.connect(...)

sql = """ drop table ? """
self.conn.execute( sql, (u'table_name',) )

给我OperationalError: near "?": syntax error

当我将sql更改为:

sql = """ drop table table_name """

它工作正常.

推荐答案

您不能将参数用于表名或列名.

You cannot use parameters for table names nor column names.

或者,您可以将其分为两步,例如:

Alternatively you could make it a two-step process, e.g.:

sql = """ drop table %s """ % a_table_name
self.conn.execute( sql )

如果这样做,则可能要明确指定可以删除哪些表...

And if you're doing that you may want to explicitly specify which tables can be deleted...

TABLES_THAT_CAN_BE_DROPPED = ('table_a','table_b',)
if a_table_name in TABLES_THAT_CAN_BE_DROPPED:
    sql = """ drop table %s """ % a_table_name
    self.conn.execute( sql )
else:
    pass # handle creatively

这篇关于Python sqlite3参数化删除表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆