收到错误: c.execute('CREATE TABLE IF NOT EXISTS top15'+ today +'(symbol TEXT,ending_price REAl,volume REAL,%REAL)') AttributeError:'str'对象没有属性'execute'
以下是未发生错误的原始代码。
conn = sqlite3.connect('Top15.db')
c = conn.cursor()
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS top15' +today +'(symbol TEXT, ending_price REAl, volume REAL, percent REAL)')
f15= ['hgd', 'bbd.a', 'mri.u', 'iam', 'hnd', 'tth', 'bbd.b', 'bbd.pr.c', 'esp', 'enl', 'rmp', 'amm', 'vrx', 'xtc', 'cxr']
f45=['4,433,389', '2.96', '-13.7', '1,209,421', '2.25', '-13.1', '3,000', '8.60', '-8.5', '1,000', '1.06', '-7.8', '1,180,466', '21.76', '-7.6', '41,777', '0.97', '-7.6', '32,423,597', '1.89', '-7.4', '43,737', '15.20', '-7.3', '87,604', '1.96', '-7.1', '5,239', '34.00', '-6.2', '2,688,261', '1.83', '-5.7', '63,301', '1.39', '-5.4', '1,664,689', '41.83', '-5.4', '63,453', '13.45', '-5.3', '1,642,197', '36.48', '-5.0']
def dynamic_data_entry():
volume = first_45[i]
ending_price = first_45[i+1]
percent = first_45[i+2]
symbol = first_15[z]
c.execute("INSERT INTO top15" +today +"(symbol,ending_price, volume, percent) VALUES (?, ?, ?, ?)",
(symbol,ending_price, volume, percent))
conn.commit()
create_table()
for i, z in zip(range(0,45,3),range(15)):
dynamic_data_entry()
c.close
conn.close
以下是新设置。除了将两个列表(f15,f45)转换为具有内部列表的单个列表(结果)之外,没有其他任何改变。但是,现在我收到了c.execute错误。我读过有关c.execute错误但无法找到解决方案
conn = sqlite3.connect('Top15.db')
c = conn.cursor()
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS top15' +today +'(symbol TEXT, ending_price REAl, volume REAL, percent REAL)')
f15=[15 list items]
f45=[45 list items]
f45i = iter(f45)
result = [[a, c, b, d] for (a, b, c, d) in zip(f15, f45i, f45i, f45i)]
result = filter(lambda l: l[0].count('.') <= 1, result)
print result
result=[['hgd', '2.96', '4,433,389', '-13.7'], ['bbd.a', '2.25', '1,209,421', '-13.1'], ['mri.u', '8.60', '3,000', '-8.5'], ['iam', '1.06', '1,000', '-7.8'], ['hnd', '21.76', '1,180,466', '-7.6'], ['tth', '0.97', '41,777', '-7.6'], ['bbd.b', '1.89', '32,423,597', '-7.4'], ['esp', '1.96', '87,604', '-7.1'], ['enl', '34.00', '5,239', '-6.2'], ['rmp', '1.83', '2,688,261', '-5.7'], ['amm', '1.39', '63,301', '-5.4'], ['vrx', '41.83', '1,664,689', '-5.4'], ['xtc', '13.45', '63,453', '-5.3'], ['cxr', '36.48', '1,642,197', '-5.0']]
def dynamic_data_entry():
symbol = result[i][0]
ending_price = result[i][1]
volume = result[i][2]
percent = result[i][3]
c.execute("INSERT INTO top15" +today +"(symbol, ending_price, volume, percent) VALUES (?, ?, ?, ?)",
(symbol,ending_price, volume, percent))
conn.commit()
create_table()
i=0
while i <len(result):
dynamic_data_entry()
i+=1
c.close
conn.close
列表中的列表是否存在问题?不太确定为什么这会导致此错误。当它在下面完美地工作时