?
Solved

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

Posted on 2016-08-07
4
Medium Priority
?
550 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 40

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 43

Accepted Solution

by:
pcelba earned 2000 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 46

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

864 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