dimanche 1 mai 2016

sqlite , python c.execute AttributeError: 'str' object has no attribute 'execute'

Getting an error: c.execute('CREATE TABLE IF NOT EXISTS top15' +today +'(symbol TEXT, ending_price REAl, volume REAL, percent REAL)') AttributeError: 'str' object has no attribute 'execute'

Below is original code where no error occurs.

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

Below is the new setup. Nothing else has changed other than turning the two lists into a single list with internal lists. However,now I get the c.execute error. Ive read about c.execute errors and cannot find a solution

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

Is there something about the list inside the list creating the problem? Not too sure why this would now result in this error. When it worked perfectly fine below

Aucun commentaire:

Enregistrer un commentaire