SQL lite 和 Python 中的变量列名 [英] Variable column name in SQL lite and Python

查看:32
本文介绍了SQL lite 和 Python 中的变量列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,正如标题所说,如何在 Pyhton 中使用的 SQL lite 中创建一个变量列.

my question is, as the title says, how I can create a variable column in SQL lite used in Pyhton.

这是我的代码,向您展示我的意思:

Here's my code, to show you what exactly I mean:

#Table with variables
def dynamic_data_entry(list_name, num, column):
    element_name = list_name[num]
    cur.execute("INSERT INTO myliltable (column) VALUES (?)", element_name)
    con.commit()

我想在调用函数时插入列名,这样我就可以将该函数用于多列,但我不断收到错误,即列列"不存在.有没有其他方法可以做到这一点,所以我可以在调用函数时更改列名设置,或者只是更改 SQL 部分.

I want to insert the column name, when I call the function, so I can use the function for more than one column, but I keep getting an error, that the column 'column' doesn't exist. Is there another way to do this, so I can change set the column name when I call the function, or just change the SQL part.

如果已经有关于此的主题,我将不胜感激,如果您能链接它,这样我就可以自己阅读(我还没有找到与此主题类似的任何内容,但有效).

If there is a thread about this already, I would appreciate it, if you could link it, so I can read it myself (I couldn't find anything similar to this topic yet, which worked).

提前致谢,感谢您的帮助

Thanks in advance, I appreciate your help

推荐答案

需要使用字符串的format()方法将列名插入到SQL字符串中:

You need to use the string's format() method to insert the column name into the SQL string:

cur.execute("INSERT INTO myliltable ({}) VALUES (?)".format(column),
            element_name)

您可以在 Python 文档中找到更多信息.

You can find more information in the Python docs.

这篇关于SQL lite 和 Python 中的变量列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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