英文:
How to enclose appended value in single quotes from function into a sql string in python
问题
我试图将来自函数的附加值封装到Python中的SQL字符串中,但不知何故无法做到。
getname 返回 Puru
def getValue(getname):
sql = '''SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '''+getname
我想以单引号形式返回它:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = 'Puru'
英文:
Here i am trying to enclose an appended value coming from function into a sql string in python
but somehow i am not able to it.
getname is returning Puru
def getValue(getname):
sql= '''SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '''+getname
I want to return it in this form in single quotes
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = 'Puru'
答案1
得分: 0
以下是要翻译的内容:
这里有三种实现你想要的方式:
- 字符串拼接
sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '" + getname + "'"
- 字符串格式化
sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '{}'".format(getname)
- 格式化字符串(需要 Python 3.6+)
sql = f"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '{getname}'"
英文:
Here are three ways to achieve what you want:
string concatenation
sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '"+getname+"'"
string.format()
sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '{}'".format(getname)
format string (requires python 3.6+)
sql = f"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '{getname}'"
答案2
得分: 0
除了str.format()
之外,Python还提供了string
模块中的Template
类。
Template
提供了更简单的字符串替换功能。与普通的基于%
的替换不同,模板支持基于$
的替换。
例如:
from string import Template
sql = Template("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES "
"WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '$getname'")
sql.substitute(getname='Puru')
$getname
表示一个替换占位符,与映射键**"getname"**匹配。但我建议您使用str.format()
方法,因为它在Python中更易读且被广泛用于字符串格式化。
英文:
For your understanding other than str.format()
python also provides Template
class from string
module.
Templates
provide simpler string substitutions in python
. Instead of the normal %
-based substitutions, Templates support $
-based substitutions.
<br>for ex:
>>> from string import Template
>>> sql = Template("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES "
"WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '$getname'")
>>> sql.substitute(getname='Puru')
"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = 'Puru'"
The $getname
names a substitution placeholder matching a mapping key of "getname". But I would recommend you to use str.format()
method which is more readable and widely used for string formatting in python
.
答案3
得分: -1
You need to replace the '''
with "
. Otherwise Python will not know how to interpret the '
in the middle. If you don't want the line to get too long, you can wrap it in parentheses:
sql = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES "
"WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = ") + getname
Edit: I have to point out the dangers of SQL injection and creating these queries on the fly. There are numerous libraries that will do it for you in a safe manner. But I have a feeling/hope this won't end up in production code anyway.
英文:
You need to replace the '''
with "
. Otherwise Python will not know how to interpret the '
in the middle. If you don't want the line to get too long, you can wrap in in parentheses:
sql = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES "
"WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = ") + getname
Edit: I have to point out the dangers of SQL injection and creating these queries on the fly. There are numerous libraries that will do it for you in a safe manner. But I have a feeling/hope this won't end up in production code anyway.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论