我在下面创建具有唯一索引“ StockName”的以下SQL表:
cursor.execute("CREATE TABLE factorcol (StockName varchar(225))")
cursor.execute("CREATE UNIQUE INDEX symbs ON factorcol (StockName)")
conn.commit()
然后,通过ea因数循环,理想情况下-ea因数作为新列添加到表中,每个因数共享相同的索引(因数的索引始终相同,这就是为什么我尝试创建一个唯一索引)。
for i in ['MAXDD','PE']:
# adds new column for factor
cursor.execute("ALTER TABLE factorcol ADD %s float(4)" % i)
conn.commit()
# this returns a different set of values for ea factor
# but index always the SAME
ser = func(i)
for ix, val in ser.iteritems():
cursor.execute("INSERT into factorcol(StockName, %s) values (?, ?)" % i, [ix, val])
conn.commit()
我正在得到:
StockName MAXDD PE
AAPL -.2 NULL
GOOGL -.3 NULL
AAPL NULL 17
GOOGL NULL 20
所需的输出为:
StockName MAXDD PE
AAPL -.2 17
GOOGL -.3 20