jeudi 20 août 2015

Writing to SQLite table with variable name (Python)

I'm trying to write five variables to tables in a database using SQLite and Python. Below is my code and the error I'm getting:

CODE:

        cur.execute("CREATE TABLE IF NOT EXISTS " + table_name + " (Date real, Morning_5AM_9AM real, Day_9AM_6PM real, Evening_6PM_10PM real, Night_10PM_5AM real)")   # this works
        export_row= p_transpose.iloc[ii]                 # not used at moment
        date_object= p_transpose.iloc[ii,0]
        date_object= date_object.replace('_','')
        export_date= int(date_object)                    # to insert into database table. 
        export_morning= p_transpose.iloc[ii,1]
        export_day= p_transpose.iloc[ii,2]
        export_eve= p_transpose.iloc[ii,3]
        export_night= p_transpose.iloc[ii,4]
        cur.execute("SELECT name FROM sqlite_master WHERE type='table'")
        available_tables=[item[0] for item in cur.fetchall()]         # assigns a list of all table names in database
        for iii in range (0, row_count): 
            if (re.match('\w*'+df_feeder, available_tables[iii])):
                relevant_table= available_tables[iii]
                cur.execute("INSERT INTO " + relevant_table + "VALUES (?,?,?,?,?)" (export_date, export_morning, export_day, export_eve, export_night))

ERROR:

TypeError: 'str' object is not callable

I've made sure that none of the export_... variables contain strings, so the string must be relevant_table. However, creating a table using a string variable (see code above again) worked fine so I don't understand why it's giving this error now.

Any input would be greatly appreciated. Let me know if any additional information would be useful.

Aucun commentaire:

Enregistrer un commentaire