Solved

Grabbing data from one access database and importing to another access database using python

Posted on 2016-08-07
4
166 Views
Last Modified: 2016-08-15
Hello Experts -

I am stuck with this script I am writing. I am trying to write a script in python that will grab all tables from sourcedb (access db) and insert into each table in destdb (access db). I compared both databases and they have the same schema, tables, column names, fields, etc.

Please see my code below and error. Let me know if I need to clarify further.

CODE:
import pypyodbc


DBSource = 'C:\\Users\\Database\\Desktop\\GA005_test.mdb'
conn = pypyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\Database\\Desktop\\GA005_test.mdb;")
cursor = conn.cursor()

DBDest = 'C:\\Users\\Database\\Desktop\\ROWBlank.mdb'
conn2 = pypyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\Database\\Desktop\\ROWBlank.mdb;")
cursor2 = conn2.cursor()

SQL = """select * from HRO;"""
##SQL = """select * from tech;"""
for row in cursor.execute(SQL):
    k = row
    print (k)
    cursor2.execute("""insert into HRO""", (k))
    conn2.commit()

cursor.close()
conn.close()
cursor2.close()
conn2.close()  

Open in new window


ERROR:
(True, '0000159', '    9000162', '9000176', '', '', 'C', datetime.datetime(2009, 7, 17, 0, 0), Decimal('16.9400'), Decimal('0.0000'), Decimal('0.0000'), Decimal('0.0100'), Decimal('0.0000'), Decimal('0.0000'), None, Decimal('1.2400'), Decimal('0.7700'), Decimal('0.0000'), Decimal('18.9600'), Decimal('0.0000'), Decimal('0.0000'), Decimal('0.0000'), datetime.datetime(2009, 7, 17, 0, 0), Decimal('18.9600'), Decimal('0.0000'), Decimal('0.0000'), Decimal('0.0000'), 'Y', 'N', 'Y', None, None, None, None, Decimal('6.6000'), Decimal('9.6800'), '0', '148410', None, None, 0.0, 0.0, 'EMR', 'DOWNEY, SHAY', '678', '333-4768', '2002', 'FORD', 'TAURUS', 'BFK4904', '', '', Decimal('0.0000'), None, 'JB', None, '', '', '', '', '', Decimal('0.0000'), Decimal('0.0000'), Decimal('0.0000'), Decimal('0.0000'), '', 'NNNNNN', datetime.datetime(2009, 7, 17, 14, 44), None, datetime.datetime(2009, 7, 17, 14, 47, 55), False, 0.0, '', Decimal('0.0000'), '0000192', False, False, ' ', ' ', ' ', Decimal('0.0000'), Decimal('0.0000'), None, '', '', '', 0, False, 'TE', '', Decimal('0.0000'), None, "b'6AB7B0D9-8183-4EDB-B023-333736132C90'", False, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, False, None, None, None, None, None, None, datetime.datetime(2013, 4, 12, 11, 54, 2))
Traceback (most recent call last):
  File "C:\Users\Database\Desktop\importoldtonew.py", line 17, in <module>
    cursor2.execute("""insert into ROWBlank.mdb values (?)""", (k))
  File "C:\Users\Database\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pypyodbc-1.3.3-py3.5.egg\pypyodbc.py", line 1470, in execute
    self._BindParams(param_types)
  File "C:\Users\Database\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pypyodbc-1.3.3-py3.5.egg\pypyodbc.py", line 1263, in _BindParams
    raise ProgrammingError('HY000',error_desc)
pypyodbc.ProgrammingError: ('HY000', 'The SQL contains 1 parameter markers, but 117 parameters were supplied')

Open in new window

0
Comment
Question by:sj77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 37

Expert Comment

by:PatHartman
ID: 41746317
I don't have time to research the syntax for you but you should be doing this with an append query rather than a cursor.    The method you are using requires running a separate append query for each row you want to append.  I'm pretty sure that ADO (I use DAO) will allow you to select from a table in databaseA and append to a table in databaseB.
0
 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 41746323
So even when the code contains:
cursor2.execute("""insert into HRO""", (k))

The error message says:
cursor2.execute("""insert into ROWBlank.mdb values (?)""", (k))  ?

It looks like you are executing different code than presenting in the question...

Did you try some table having just let say three columns?
Are you sure the column order is also equal in both databases?
Do you have some plan how to solve autoincremented IDs?

OK, if the error says you have 117 parameters but just one ? in your command then you should provide 117 question marks:

cursor2.execute("insert into HRO values (?" + ",?" * (len(k)-1) + ")", (k))    ## I am just not sure whether such number of parameters is supported...

... so we are back in some simple test with the table having just three columns which is much easier to debug...

Also Access driver supports query from another database so you may do all rows at once:
cursor2.execute("""insert into HRO SELECT * FROM  C:\\Users\\Database\\Desktop\\GA005_test.HRO""")

More info: http://web.synametrics.com/crossjoiningaccess.htm

Another way which I am not sure will work in Python: https://support.microsoft.com/en-us/kb/113701

More about database access in Python: http://www.tutorialspoint.com/python/python_database_access.htm
1
 
LVL 45

Expert Comment

by:aikimark
ID: 41746398
You might use the IN clause in your Insert statement.
Example:
Insert into tablename
From remotetablename IN 'path to remote database'

Open in new window

1
 

Author Closing Comment

by:sj77
ID: 41756324
He was able to provide a timely answer.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question