0

I have been banging my head trying to get my Sqlite table to update given the code below.. looping through Pandas dataframe cells in specific columns to update specific columns in the sqlite table given a date match.

Everything works up until updating the table but the table won't update. It is always printing Fail. Any help would be greatly appreciated!

            cur.execute('ALTER TABLE Car_parts'+Part_str+' ADD COLUMN Close_'+Column_ID+' number')
            conn.commit()
            cur.execute('ALTER TABLE Car_parts'+Part_str+' ADD COLUMN Volume_'+Column_ID+' number')
            conn.commit()                
            y=0
            while y<len(Car_Data.index):
                print(y)
                Update_date=Car_Data.iloc[y,0]
                #Update_date=datetime.strptime(UpdateDate,date_format)
                Update_close=str(CAR_Data.iloc[y,1])
                Update_volume=str(Car_Data.iloc[y,2])
                print(type(Update_date),type(Update_close), type(Update_volume))
                try:
                    cur.execute('UPDATE Car_parts'+Part_str+' SET Close_'+Column_ID+' = ?, Volume_'+Column_ID+' = ? WHERE Date= ?',(Update_Close, Update_Volume, Update_date,)) 
                    conn.commit()
                    print("Success")
                except:
                    print("fail")
                    pass
                y+=1
ShibbyBoss
  • 55
  • 1
  • 7
  • 1
    If the sqlite3 db is only one table, you can read sql to dataframe and pd.concat() the missing df. After that you can write the whole df back with DataFrame.to_sql(). Drop the old sql table before you create the new one. – Hermann12 Feb 03 '23 at 18:45

0 Answers0