我试图在列#34; GameID"下创建从90001到90150(增量为1)的随机数。并在游戏桌中生成它。我该怎么办?我听说序列可能有效,但我不确定这是怎么回事。有什么想法吗?
#!/usr/bin/python
# -*- coding: utf-8 -*-
import psycopg2
import sys
import csv
from itertools import count, cycle
from _tkinter import create
from setuptools.dist import sequence
path = r'C:\Users\sammy\Downloads\E0.csv'
with open(path, "r") as csvfile:
readCSV = csv.reader(csvfile, delimiter=",")
firstline = 1
con = None
con = psycopg2.connect("host='localhost' dbname='football' user='postgres' password='XXX'")
cur = con.cursor()
cur.execute("DROP TABLE games")
cur.execute("DROP TABLE teams")
cur.execute("DROP TABLE referees")
cur.execute("CREATE TABLE games (HomeTeamID VARCHAR, HomeTeam VARCHAR, AwayTeamID VARCHAR, AwayTeam VARCHAR, FTHG INTEGER, ATHG INTEGER, FTR VARCHAR, RefereeID VARCHAR, RefereeName VARCHAR, HY INTEGER, AY INTEGER)")
cur.execute("CREATE TABLE teams (HomeTeamID VARCHAR, HomeTeam VARCHAR, AwayTeamID VARCHAR, AwayTeam VARCHAR)")
cur.execute("CREATE TABLE Referees (RefereeID VARCHAR, RefereeName VARCHAR)")
create sequence sq_demo
start with 10001
increment by 1
maxvalue 15000
minvalue 10001
cache 3
cycle
order;
try:
for row in readCSV:
if firstline:
firstline=0
continue
HomeTeamID = row[2]
HomeTeam = row[2]
AwayTeamID = row[3]
AwayTeam = row[3]
FTHG = row[4]
ATHG = row[5]
FTR = row[6]
RefereeID = row[10]
RefereeName = row[10]
HY = row[19]
AY = row[20]
data1 = (HomeTeamID, HomeTeam, AwayTeamID, AwayTeam, FTHG, ATHG, FTR, RefereeID, RefereeName, HY, AY)
data2 = (HomeTeamID, HomeTeam, AwayTeamID, AwayTeam)
data3 = (RefereeID, RefereeName)
query1 = "INSERT INTO games (HomeTeamID , HomeTeam, AwayTeamID, AwayTeam, FTHG, ATHG, FTR, RefereeID, RefereeName, HY, AY) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
query2 = "INSERT INTO teams (HomeTeamID , HomeTeam, AwayTeamID, AwayTeam) VALUES (%s, %s, %s, %s);"
query3 = "INSERT INTO Referees (RefereeID, RefereeName) VALUES (%s, %s);"
print(HomeTeam)
print(AwayTeam)
print (FTHG)
print (ATHG)
print (FTR)
print (RefereeID)
print (RefereeName)
print (HY)
print (AY)
cursor = con.cursor()
cursor.execute(query1, data1)
cursor.execute(query2, data2)
cursor.execute(query3, data3)
except psycopg2.DatabaseError as e:
if con:
con.rollback()
print ("Error %s % e", e)
sys.exit(1)
finally:
if con:
con.commit()
con.close()
print(" ".join(row))
out=open("new_data.csv", "w")
output = csv.writer(out)
for row in data1:
output.writerow(row)
out.close()
答案 0 :(得分:1)
我正在尝试从90001到90150生成随机数(增量 一个)在一列" GameID"并在游戏中生成它 表
这里看起来不需要序列。 您可以运行此查询:
select i from generate_series(90001, 90150) as x(i) order by random()
并在循环中插入的每个游戏中从中选择一个结果。