我有這個 SQL 查詢,但每次插入新數據時都必須更改 id,因為它是主鍵。如何讓它在每次插入時添加一個新的未使用的主鍵值?我正在使用 Microsoft SQL Server Studioimport urllib.request as urllibimport socketimport pyodbcfrom datetime import datetime#Timestamp for unders?gelsetimestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')#Host info og IPhost = "www.rejseplanen.dk"dest = socket.gethostbyname(host)hdata = 'host',host,'IP:',dest#Responseheader requestrequest = urllib.Request('http://rejseplanen.dk')request.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36')response = urllib.urlopen(request)rdata = response.info()#SQL Connection til local databasecon = pyodbc.connect('Driver={SQL Server Native Client 11.0};' 'Server=DESKTOP-THV2IDL;' 'Database=host;' 'Trusted_Connection=yes;')cursor = con.cursor()cursor.execute('SELECT * FROM host.dbo.hosts')for row in cursor: print(row)con.execute('INSERT INTO host.dbo.hosts (Id, ip, host, HSTS, HPKP, XContentTypeOptions, XFrameOptions, ContentSecurityPolicy, Xssprotection, Server, Timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (4123, host, dest, rdata['Strict-Transport-Security'], rdata['Public-Key-Pins'], rdata['X-Content-Type-Options'], rdata['X-Frame-Options'], rdata['Content-Security-Policy'], rdata['X-XSS-Protection'], rdata['Server'], timestamp))con.commit()
Python每次插入新數據時如何自動生成不存在的主鍵
慕田峪7331174
2022-06-28 10:32:49