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] # Note: p_transpose is the transpose of a DataFrame I read in from Excel
date_object= p_transpose.iloc[ii,0] # date_object is a string here
date_object= date_object.replace('_','')
export_date= int(date_object) # to insert into database table as int instead of string
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 on the last line:
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.
EDIT :
Here is my traceback, gotten using traceback.format_exc():
'Traceback (most recent call last):\n File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/idlelib/run.py", line 112, in main\n seq, request = rpc.request_queue.get(block=True, timeout=0.05)\n File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/Queue.py", line 176, in get\n raise Empty\nEmpty\n'
FINAL EDIT, RESOLVED:
For information, thanks to scytale, this now works, using:
cur.execute("INSERT INTO " + relevant_table + " VALUES (?,?,?,?,?)", (export_date, export_morning, export_day, export_eve, export_night))
I thought I had tried changing the punctuation and spacing in all ways possible, but this finally did the trick.
you're missing a comma in your last line after the SQL string - it should be
cur.execute("INSERT INTO " + relevant_table + "VALUES (?,?,?,?,?)",
(export_date, export_morning, export_day, export_eve, export_night))
Really, for clarity and future maintenance you should be specific as to what and where you are inserting. So your statement should read something like this, for example:
try:
result = self.db.execute("insert into Recent (Filename,TimeStamp,Icon) values (?,?,?)",(filename,time_stamp,itype))
except sqlite3.Error as e:
wx.MessageBox('Insert Recent file list error'+str(e), 'Error', wx.OK | wx.ICON_INFORMATION)
This makes it clear, not only what the data items are but where you are putting them.
A good reason for declaring what you are inserting explicitly, is that should you add columns to the table at a later date, then the code will still work as expected.
As a side note, has everyone forgotten the power of the print statement when it comes to debugging?
Got a problem? PRINT the damned stuff out and look at it!! 9 times out of ten it's obvious.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.