python操作数据库

table_name='log'
uname='sc'
upasswd='esg'
str_sql="insert into log values ('%s','%s') "%(uname,upasswd)
cursor.execute(str_sql)
db.commit()

这样写是对的

table_name='log'
uname='sc'
upasswd='esg'
str_sql="insert into '%s' values ('%s','%s') "%(table_name,uname,upasswd)
cursor.execute(str_sql)
db.commit()

这样写就报错了
pymysql.err.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 ''log' values ('sc','esg')' at line 1")
有没有人指点一下

insert into '%s'
的两个' 去掉即可。

或许你可以改成这样
str_sql=f"insert into {table_name} values ('uname','{upasswd}') "

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632