AttributeError: 'pyodbc.Connection' object has no attribute 'executemany'

AttributeError: 'pyodbc.Connection' object has no attribute 'executemany'

这些是 2 类 dbclient 和 mykiosk 客户端, 我正在尝试 运行 下面的 4 行代码 但我得到了 AttributeError。我只想通过 python 将 api 数据输入 sql table 并牢记这种格式。有人可以帮我吗?

db =DatabaseClient()
mk = MyKiosk()

freq_data = mk.get_frequency_data()
db.write_frequency_data(freq_data)

class DatabaseClient:
    def __init__(self):
        self.connect()
    
    def connect(self):
        server = "sql-test-server-vdzbi.database.windows.net"
        database = "sql-test-database-vdzbi"
        username = "vdzbi-admin"
        password = "VD9ffqjHaugvTf2"
        self.cursor = pyodbc.connect("DRIVER={SQL Server};SERVER="+server+";DATABASE="+database+";UID="+username+";PWD="+password)
        
    
    def write_frequency_data(self, data):
       # self.cursor.fast_executemany = True
        
        stage_query = "INSERT INTO staging.Frequency (FreqId, Title) VALUES (?,?)"
        merge_query = """INSERT INTO mykiosk.Frequency(FreqId, Title) 
            SELECT FreqId, Title
            FROM staging.Frequency stage 
            WHERE stage.FreqId not in (Select FreqId from mykiosk.Frequency)"""
        truncate_query = "TRUNCATE TABLE staging.Frequency"
            
        try:
            self.cursor.executemany(stage_query,data)
            self.cursor.executemany(merge_query,data)

######### api.py 文件 ###########

import requests
import json
import simplejson


class MyKiosk:
    object_endpoint = "/api/Objektgruppe/GetObjektgruppen"
    price_endpoint = "/api/Objekt/getObjekteByHauptgruppeId"
    frequency_endpoint = "/api/erscheinungsweise/GetErscheinungsweisen"

    def __init__(self):
        self.host = "https://www.mykiosk.com"

    def get_frequency_data(self):
        
        # endpoint = "/api/Objektgruppe/GetObjektsgruppen"
        r = requests.get(f"{self.host}/{self.frequency_endpoint}")
        assert r.status_code == 200
        freq = r.json()
        data =[]
        for i in freq:
            val = (i["ErscheinungsweiseId"], i["Bezeichnung"])
            data.append(val)
        return data
            
    

提出的错误是错误的。

您将 pyodbc.Connection 而不是 pyodbc.Cursor 分配给了 self.cursor .

您应该分配 connect().cursor()。

换行:

self.cursor = pyodbc.connect("DRIVER={SQL Server};SERVER="+server+";DATABASE="+database+";UID="+username+";PWD="+password)

收件人:

connection = pyodbc.connect("DRIVER={SQL Server};SERVER="+server+";DATABASE="+database+";UID="+username+";PWD="+password)
self.cursor = connection.cursor()