Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

610 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