Search code examples
pythonms-accesspyodbc

Adding new data to access database from python with variables


This isn't really a question, because I figured this out and wanted to post the resolution. I was trying to add variables from python to an insert into an access database, and didn't really find anything here that explained it. I created several variables, and used this statement to insert new rows. Something else I noticed was that the variables don't work if the field names are the same. Upper and lower case does make the same spelling different. I hope this may help anyone.

         cursor.execute(''' INSERT INTO file_info_main(Drive, Path, filename_full, filename, extension, size, create_dt_tm, mod_dt_tm, ins_dt_tm) 
            VALUES(?,?,?,?,?,?,?,?,?) ''',
            (drive, path, p_name, filename, ext, p_filesize, cr_dt_tm, m_dt_tm, in_dt_tm)
           )

Solution

  • This statement did work to update the access database table. I thought I'd also mention, for the date/time data, I had to format the data into the variable with

    cr_dt_tm = p_ctime.strftime("%m/%d/%Y %H:%M:%S")
    

    This pattern formatted the date/time to be inserted properly as a date/time field in access.