我正在forfor循环中编写查询,但我不知道我在哪里出错。 简而言之,我需要做一个for循环,并在WHERE语句中放入dict的内容。 有人能帮我吗?
import pyodbc
class Dbconnect:
playerslist:list
server:str
database:str
username:str
password:str
cnxn:str
cursor :object
query:str
def __init__(self, playerslist):
self.playerslist = playerslist
return
def insertdb(self,cursor,players):
self.cursor = cursor
for x,y in enumerate(players):
self.query =(f"SET NOCOUNT ON; INSERT INTO [dbo].[players] (player_id,first_name,last_name,surname,rating) " \
f"VALUES({(players[x]['id'])},'{str(players[x]['firstname'])}','{str(players[x]['lastname'])}',"
f"'{str(players[x]['surname'])}',{players[x]['rating']})")
self.cursor.execute(self.query)
准确地说,当我执行上述代码时,得到的结果没有插值>:(
INSERT INTO [dbo].[players] ([player_id],[first_name],[last_name],[surname],[rating]) VALUES({players[x]['id']},{players[x]['firstname']},{players[x]['lastname']},{players[x]['surname']},{players[x]['rating']}))
我做错了什么?