Python/MySQL“插入"带变量 [英] Python/MySQL "Insert into" with variables

查看:36
本文介绍了Python/MySQL“插入"带变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向 MySQL 表中插入新行时遇到问题.表的名称会改变,所以它必须是一个变量,我最麻烦.如何使用变量更改表second"的名称?有什么想法吗?

I have a problem with inserting new rows to the MySQL table. The name of the table will change, so it must be a variable and with that I have the most trouble. How can I change name of the table "second" with variable? Any idea?

add_word = ("INSERT INTO second "
               "(name, surname) "
               "VALUES (%s, %s)")
    data_word = (name1, surname1)
    cursor.execute(add_word, data_word)

推荐答案

你这样做:

add_word = ("INSERT INTO {table} "
            "(name, surname) "
            "VALUES (%s, %s)")
atable = 'second'
data_word = (name1, surname1)
cursor.execute(add_word.format(table=atable), data_word)

这篇关于Python/MySQL“插入"带变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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