Solved

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

Posted on 2016-08-07
4
81 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
4 Comments
 
LVL 34

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 41

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now