我有一个名为 project1 的数据库,其中包含以下表格:
_systbl1
_systbl2
_systbl3
dataset1
dataset2
dataset3
每当添加新表时,都需要授予 MySQL 用户 odbc 对 dataset% 表的 SELECT 权限。
为此,我使用了一个简单的 python 脚本,如下所示:
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(
host="localhost",
user="user",
passwd="pass",
db="project1"
)
# Create Cursor Object
cur = db.cursor()
# get list of tables beginning with dataset
cur.execute("SHOW TABLES FROM project1 LIKE 'dataset%';")
# run GRANT statement for each table
for row in cur.fetchall() :
cur.execute("GRANT SELECT ON `project1`.`%s` TO `odbc`@`localhost`;", row)
cur.execute("GRANT SELECT ON `project1`.`%s` TO `odbc`@`%`;", row)
不幸的是,它给了我以下错误:
Traceback (most recent call last):
File "mysql_query.py", line 20, in <module>
cur.execute("GRANT SELECT ON `project1`.`%s` TO `odbc`@`localhost`;", row)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1146, "Table 'project1.'dataset1'' doesn't exist")
正如您在错误的最后一行中看到的,问题在于 python 在生成查询时在表名周围加上了一个单引号。
我在这里想念什么?