Cannot insert new records into Access (Python)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scoke
    New Member
    • Nov 2012
    • 1

    Cannot insert new records into Access (Python)

    This program runs and then at the end where i select the entire database and read it out to the console i see the results. The issue i am having is that when i go to the actual access database there is no changes and it is still blank.

    Code:
    import os
    import adodbapi
    
    database = "db1.mdb"
    constr = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s'  % database
    tablename = "address"
    
    # connect to the database
    conn = adodbapi.connect(constr)
    
    # create a cursor
    cur = conn.cursor()
    
    path = 'XXXXXXXX'
    listing = os.listdir(path)
    i = 0
    for infile in listing:
        i + 1
        try:
            if infile.endswith('.xml'):
    
                print('File: ' + path + infile)
    
                from xml.dom import minidom
    
                xmldoc = minidom.parse(path + infile)
            
                xfadata = xmldoc.getElementsByTagName("xfa:data")[0]
                CostDealsForm = xfadata.getElementsByTagName("CostDealsForm")[0]
                P1 = CostDealsForm.getElementsByTagName("P1")[0]
                RefSub = P1.getElementsByTagName("RefSub")[0]
            
                RefNum = RefSub.getElementsByTagName("RefNum")[0].firstChild.data
                Date = RefSub.getElementsByTagName("Date")[0].firstChild.data
            
                #print(RefNum, Date)
    
                # extract all the data
                sql2 = "select * from %s" % tablename
                sql = "INSERT INTO %s (Reference_Number, Date1) Values ('" %tablename + RefNum + "', '"+ Date + "');"
                cur.execute(sql)
                cur.commit(sql)
    
        except AttributeError:
            RefNum = 'Error'
            Date = 'Error'
            sql = "INSERT INTO %s (Reference_Number, Date1) Values ('" %tablename + RefNum + "', '"+ Date + "');"
            cur.execute(sql)
    
    cur.execute(sql2)
    
    # show the result
    result = cur.fetchall()
    for item in result:
        print item
     
    # close the cursor and connection
    cur.close()
    conn.close()
    Any help would be much appreciated.
    Be kind as this is my first day on python :$

    Thanks
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    database = "db1.mdb"
    constr = 'Provider=Micro soft.Jet.OLEDB. 4.0; Data Source=%s' % database
    The database is possibly being created in some directory other than the one you are looking in. Provide a complete path to the location you want to use.

    Comment

    • Layvian
      New Member
      • Dec 2012
      • 4

      #3
      I personally work with mysql, however going based off my knowledge with databases and accessing tables for them I noticed two things:
      1. I see no IP address for the database.
      2. I do not see where a username or password was entered for the table/database.


      Usually you have to list the IP of the database trying to be accessed, and usually you must have a user and its password to access its tables. Again I am going based off mysql database.

      Comment

      Working...