简体   繁体   English

使用Python连接字典值时,如何仅用引号将字符串引起来?

[英]How to surround only string with quotations when joining dictionary values using Python?

i'm practicing on Python and trying to create a class that helps performing database operations, but when inserting to a database here's the code : 我正在使用Python进行练习,并试图创建一个有助于执行数据库操作的类,但是在插入数据库时​​,代码如下:

def Insert(self, **kwargs):
    self.__query.execute("INSERT INTO {} ({}) VALUES ({})".format(self.table, ", ".join(kwargs.keys()), ", ".join(str(v) for v in kwargs.values())))
    self.__db.commit()

When i ran this code for testing: 当我运行此代码进行测试时:

MyTable.Insert(id=3, name="jack", age=23)

I got this error : 我收到此错误:

sqlite3.OperationalError: no such column: jack sqlite3.OperationalError:没有这样的列:jack

When i replaced the execute command with print i got this : 当我用print替换execute命令时,我得到了:

INSERT INTO testTbl111 (id, name, age) VALUES (3, jack, 23)

I guess jack must be surrounded by quotations. 我猜jack必须用引号引起来。

My question: is how to surround jack with quotation while doing ", ".join(str(v) for v in kwargs.values()) ? 我的问题:就是如何围绕jack与报价,而做", ".join(str(v) for v in kwargs.values())

You don't want to try to escape value parameters yourself, instead you want to build the insert query and put placeholders ( ? works for sqlite3) for values - something like: 您不想自己尝试转义值参数,而是想要构建insert查询并为values放置占位符( ?适用于sqlite3)-类似:

query = 'INSERT INTO {} ({}) VALUES({})'.format(self.table, ', '.join(kwargs), ','.join(['?'] * len(kwargs)))

Then, use the second method of execute (either on the db object or cursor object) to pass in the values to be substituted - these will automatically be correctly escaped for the database. 然后,使用第二种执行方法(在db对象或游标对象上)传递要替换的值-这些值将自动为数据库正确转义。

self.__db.execute(query, list(kwargs.values()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中的SQL查询的值列表中删除字符串的引号 - How to remove quotations of string in list of values for sql query in python 如何从 python 中的字典中删除引号 - How to remove quotations from a dictionary in python Python在字典中搜索键时仅使用字符串中的第一个字母 - Python only using first letter in string when searching keys in a dictionary 如何使用 python 中的键从字典中仅获取特定值 - how to get only specific values from dictionary using key in python 当我使用 function eval(String dictionary) -> dictionary PYTHON 时,如何处理字典中的缺失值? - How can I handle missing values in the dictionary when I use the function eval(String dictionary) -> dictionary PYTHON? 带有引号的Python 2.7.6字符串文字错误 - Python 2.7.6 String Literal error with Quotations in Quotations 如何使用Python确保JSON中的报价匹配? - How to ensure matched quotations in JSON using Python? 如何使用Python在此字典中使用空字符串将值添加为缺少的键 - How to add the missing keys in this dictionary with values as empty string using python 如何使用包含多个键值的字典在python中替换字符串 - How to replace a string using a dictionary containing multiple values for a key in python 当我只能访问该字典的字符串值时,如何从字典中调用.format()? - How can I call .format() from a dictionary when I can only access the string values of that dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM