Python 将数据帧值作为参数添加到sql查询
我试图遍历一个数据帧,并从单独的列中获取值,以用作sql查询中的参数Python 将数据帧值作为参数添加到sql查询,python,postgresql,pandas,Python,Postgresql,Pandas,我试图遍历一个数据帧,并从单独的列中获取值,以用作sql查询中的参数 for index,frame in df1.iterrows(): sql = "select * from issuers where column_1 = %s;" cur.execute(sql, frame['column_1']) row = cur.fetchone() id = row[0] print id 但是我得到了以下错误 “TypeErr
for index,frame in df1.iterrows():
sql = "select * from issuers where column_1 = %s;"
cur.execute(sql, frame['column_1'])
row = cur.fetchone()
id = row[0]
print id
但是我得到了以下错误
“TypeError:在字符串格式化过程中未转换所有参数”
我怎样才能解决这个问题?如果我需要添加多个参数,我如何才能做到这一点 而不是这个:
cur.execute(sql, frame['column_1'])
试试这个:
cur.execute(sql, [frame['column_1']])
execute
的第二个参数是包含要插入sql
的所有值的列表
要插入多个值,请使用以下内容:
sql = "select * from issuers where column_1 = %s and column_2 = %s;"
cur.execute(sql, ["val1", "val2"])
有关更多信息,请参阅
编辑
下面是SQL中插入到的示例
sql = "INSERT INTO user (firstname, lastname) VALUES (%s, %s)"
cur.execute(sql, ["John", "Doe"])
@我在上面的回答中加了一个例子。我希望这有帮助