慕田峪7331174
2022-06-28 10:32:49
我有這個(gè) SQL 查詢(xún),但每次插入新數(shù)據(jù)時(shí)都必須更改 id,因?yàn)樗侵麈I。如何讓它在每次插入時(shí)添加一個(gè)新的未使用的主鍵值?我正在使用 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()
1 回答

慕虎7371278
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊
你沒(méi)有。你讓數(shù)據(jù)庫(kù)來(lái)做。因此,主機(jī)表應(yīng)定義為:
create table host (
id int identity(1, 1) primary key,
ip . . .
);
然后,您將值排除在外insert:
INSERT INTO host.dbo.hosts (ip, host, HSTS, HPKP, XContentTypeOptions, XFrameOptions, ContentSecurityPolicy, Xssprotection, Server, Timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
(請(qǐng)注意,id不在列列表中。)
添加回答
舉報(bào)
0/150
提交
取消