正在将数据从webscraper上载到SQL server



我正在尝试将网络抓取的交易上传到mysql服务器:

def sendToSQL(deals, n):
length = len(deals)
if length < n:
print("Not enough deals",length,n)
return
server = 'zains.database.windows.net'
database = 'COSC'
username = 'zainy'
password = 'pw'
driver= '{ODBC Driver 17 for SQL Server}'
mydb = pyodbc.connect('DRIVER='+driver+';PORT=1433;SERVER='+server+';PORT=1443;DATABASE='+database+';UID='+username+';PWD='+ password)
mycursor = mydb.cursor()
for i in deals[0:n]:
mycursor.execute("CREATE TABLE Dealsea(title text, link text, content text, vendor text)")
mycursor.execute ("""
INSERT INTO DealSea
VALUES
(i.getTitle(),i.getLink(),i.getContent(),i.getVendor())""")
mydb.commit()

给我这个错误:

pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find either column "i" or the user-defined function or aggregate "i.getTitle", or the name is ambiguous. (4121) (SQLExecDirectW)')

您将python变量i作为字符串传递,这导致了问题。请尝试以下操作:

mycursor.execute ("""
INSERT INTO DealSea
VALUES
('"""+i.getTitle()+"""','"""+i.getLink()+"""','"""+i.getContent()+"""','"""+i.getVendor()+"""')""")

当您使用多行注释时,python会将变量i视为导致问题的字符串。

最新更新