SQL截断表,其中表名在函数参数或变量中定义

问题描述 投票:0回答:1

我正在尝试将变量或函数参数传递给execute方法

import pymysql

tablename = 'test_table'

con = pymysql.connect(**...)
with con.cursor() as cur:
    cur.execute("TRUNCATE TABLE %s", tablename)
con.commit()
con.close()

以下错误:

ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test_table'' at line 1")

期望没有错误,并且test_table中没有行。

python pymysql
1个回答
0
投票

SO最近有所懈怠...

我改为这样做,它起作用了:

import pymysql

tablename = 'test_table'

con = pymysql.connect(**...)
with con.cursor() as cur:
    cur.execute("TRUNCATE TABLE %s" % tablename)
con.commit()
con.close()

print('Done')
© www.soinside.com 2019 - 2024. All rights reserved.