0

I am trying to put the current timestamp into the data frame then save the records to the MySQL database. Here is my table design: Table Design

Here is my code `

df = pd.read_csv(uploaded_file, keep_default_na=False)`
df= df.where(pd.notnull(df), None)
column_name = df.columns
current_time = datetime.utcnow()
date_object = datetime.strftime(current_time, '%Y-%m-%d %H:%M:%S:%f')
df.loc[df['wDate']== '',df['wDate']] = date_object
df.loc[df['mDate']== '',df['wDate']] = date_object
try:
    for i,row in df.iterrows():
      sql = "INSERT INTO Build VALUES \
      (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
      print (tuple(row))
      curs.execute(sql, tuple(row))
      #st.write("Record inserted", i)
      conn.commit()
  except Exception as e:
    st.write ('ERROR! DB.', e)
  else:
    st.write('successfully uploaded..')

However, when I try to print the data in the output just to see how the row would look like, they appear like this:

('b1', 'a101', 'fp1', 202, 1109, 'f1', '', 'p1', 'p2', 'p4', '', 'b1', 'b1', 'b12', '', 's1', '', '', 'c1', '', '', '', 'nita', '2021-12-10 11:56:10:778920', '2021-12-10 11:56:10:778920')

It seems like the current dates that I want to input went to two additional columns which cause the "TypeError: not all arguments converted during string formatting"

I am not sure what went wrong.

  • Where (on which line of code) do you receive the TypeError? Which two "additional" column are you referring to ? (i only see two column with datetime, and that seems correct). – Luuk Dec 10 '21 at 12:22
  • Please debug your code, because clearly this (and the next line) are not correct "df.loc[df['wDate']== '',df['wDate']] = date_object" – Luuk Dec 10 '21 at 12:24
  • @Luuk The values should be like this ('b1', 'a101', 'fp1', 202, 1109, 'f1', '', 'p1', 'p2', 'p4', '', 'b1', 'b1', 'b12', '', 's1', '', '', 'c1', '', '2021-12-10 11:56:10:778920', ' '2021-12-10 11:56:10:778920', 'nita' ) – Saksonita Khoeurn Dec 10 '21 at 12:25
  • Sorry, I am tooo lazy to start counting the items in this list. but clearly the lines I referred to in my previous comment cause new items to be added to that list. Maybe [Finding and replacing elements in a list](https://stackoverflow.com/questions/2582138/finding-and-replacing-elements-in-a-list) will help ? – Luuk Dec 10 '21 at 12:32
  • Thank. You were right about the codes. – Saksonita Khoeurn Dec 10 '21 at 12:35
  • It should be like this df.loc[df['wDate'] == '', 'wDate'] = date_object – Saksonita Khoeurn Dec 10 '21 at 12:35

0 Answers0