Solved

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

Posted on 2016-08-07
4
203 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 38

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

717 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