简体   繁体   English

如何在Python中的SQL查询的值列表中删除字符串的引号

[英]How to remove quotations of string in list of values for sql query in python

I want to insert values into the table open.roads using SQL in python, the table has the following columns: 我想在python中使用SQL将值插入表open.roads中,该表具有以下列:

id integer NOT NULL DEFAULT nextval('open.res_la_roads_id_seq'::regclass),
run_id integer,
step integer,
agent integer,
road_id integer,
CONSTRAINT res_la_roads_pkey PRIMARY KEY (id)

I'm trying to write various rows of values into sql within one query, therefore I created a list with the values to be inserted in the query, following this example: 我试图在一个查询中将各种值的行写入sql,因此,按照以下示例,我创建了一个列表,其中包含要在查询中插入的值:

INSERT INTO open.roads(id, run_id, step, agent, road_id)
    VALUES (DEFAULT, 1000, 2, 2, 5286), (DEFAULT, 1000, 1, 1, 5234);

The list in Python should contain: Python中的列表应包含:

list1=(DEFAULT, 1000, 2, 2, 5286), (DEFAULT, 1000, 1, 1, 5234), (.....

I have problems with the value "DEFAULT" as it is a string which should be introduced in sql without the quotations . 我的值“ DEFAULT”有问题,因为它是一个字符串,应该在sql中引入而不使用引号 But I don't manage to remove the quotations, I have tried to save "DEFAULT" in a variable as a string and used str.remove(), str.replace() etc. 但是我无法删除引号,我试图将“ DEFAULT”保存为字符串形式的变量,并使用了str.remove(),str.replace()等。

The code I'm trying to use: 我尝试使用的代码:

for road in roads:
    a="DEFAULT", self.run_id, self.modelStepCount, self.unique_id, road
    list1=append.(a)
val=','.join(map(str, list1))
sql = """insert into open.test ("id","run_id","step","agent","road_id")
values {0}""".format(val)
self.model.mycurs.execute(sql)

I get an error because of the quotiations: 由于出现引号,我得到一个错误:

psycopg2.DataError: invalid input syntax for integer: "DEFAULT"
LINE 2:('DEFAULT', 582, 0, 2, 13391),('DEFAULT'

How can I remove them? 如何删除它们? Or is there another way to do this? 还是有其他方法可以做到这一点?

I think this might help you. 我认为这可能对您有帮助。

sql = "INSERT INTO open.roads(id, run_id, step, agent, road_id) VALUES "
values = ['(DEFAULT, 1000, 2, 2, 5286), ', '(DEFAULT, 1000, 1, 1, 5234)']

for value in values:
    sql += value

print(sql)

After running this code, it will print: INSERT INTO open.roads(id, run_id, step, agent, road_id) VALUES (DEFAULT, 1000, 2, 2, 5286), (DEFAULT, 1000, 1, 1, 5234) 运行此代码后,它将打印: INSERT INTO open.roads(id, run_id, step, agent, road_id) VALUES (DEFAULT, 1000, 2, 2, 5286), (DEFAULT, 1000, 1, 1, 5234)

Other solution is to format your string using % . 其他解决方案是使用%格式化字符串。

print("INSERT INTO open.roads(id, run_id, step, agent, road_id) VALUES (%s, %s, %s, %s, %s)" % ("DEFAULT", 1000, 2, 2, 5286))

This will print INSERT INTO open.roads(id, run_id, step, agent, road_id) VALUES (DEFAULT, 1000, 2, 2, 5286) 这将打印INSERT INTO open.roads(id, run_id, step, agent, road_id) VALUES (DEFAULT, 1000, 2, 2, 5286)

Simply omit the column in append query as it will be supplied with DEFAULT value per your table constraint. 只需在追加查询中省略该列,因为根据您的表约束,该列将提供DEFAULT值。 Also, use parameterization of values by passing a vars argument in execute (safer/more maintainable than string interpolation): execute(query, vars) . 另外,通过在execute传递vars参数来使用值的参数化(比字符串插值更安全/更可维护): execute(query, vars) And since you have a list of tuples, even consider executemany() . 并且由于您有一个元组列表,因此甚至可以考虑executemany()

Below assumes database API (ie, pymysql, pyscopg2) uses the %s as parameter placeholder, otherwise use ? 下面假设数据库API(即pymysql,pyscopg2)使用%s作为参数占位符,否则使用? :

# PREPARED STATEMENT
sql = """INSERT INTO open.test ("run_id", "step", "agent", "road_id")
         VALUES (%s, %s, %s, %s);"""

list1 = []
for road in roads:
    a = (self.run_id, self.modelStepCount, self.unique_id, road)
    list1.append(a)

self.model.mycurs.executemany(sql, list1)

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

相关问题 如何从 Python itertools 输出中删除字符串引号、逗号和括号? - How to remove string quotations, commas and parenthesis from Python itertools output? 如何从 python 中的字典中删除引号 - How to remove quotations from a dictionary in python 如何将字符串的python列表传递给sql查询 - How to pass python list of string to sql query 如何删除从 Python 中的文件导入的列表周围的双引号? - How can I remove double quotations surrounding a list imported from a file in Python? 带有引号的Python 2.7.6字符串文字错误 - Python 2.7.6 String Literal error with Quotations in Quotations 使用Python连接字典值时,如何仅用引号将字符串引起来? - How to surround only string with quotations when joining dictionary values using Python? Python-SQL查询将\\ r添加到字符串? 如何将其删除? - Python - sql query adds \r to string ? How to remove it? 如何从Python中删除SQL查询字符串中的引号? - How to remove the quotes from a string for SQL query in Python? 如何使用从python列表派生的字符串插入sql查询字符串 - How to insert into sql query string with a string derived from python list 如何从列表输出中删除逗号和引号 - How do I remove commas and quotations from my list output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM