简体   繁体   中英

Creating a new Access table with pyodbc using SELECT … INTO

I am trying to create a new table called Component_New, based on 3 different Access tables, and joined using common fields.

I currently have the following:

Make_Table = """ SELECT mapunit.mukey, copmgrp.cokey, copmgrp.copmgrpkey, copm.pmkind, copm.pmorigin, component.comppct_r, component.compname, component.majcompflag, copmgrp.pmgroupname INTO Component_Table FROM (mapunit INNER JOIN component ON (mapunit.mukey = component.mukey) INNER JOIN copmgrp ON (component.cokey = copmgrp.cokey) INNER JOIN copm ON (copmgrp.copmgrpkey = copm.copmgrpkey) INNER JOIN chorizon ON (component.cokey = chorizon.cokey)) """
cursor.execute(Make_Table)
con.commit()

Can't seem to find my mistake! Thank you in advance!

For a better understanding of my question: I simply want to know how to take the following add/joined information and create a table with it --> JOINing mdb tables with pyodbc

The following code of mine works in Access, but I need to put it into Python so that I can loop through multiple different counties to do the same Add/Join:

SELECT mapunit.mukey, copmgrp.cokey, copmgrp.copmgrpkey, copm.pmkind, copm.pmorigin, component.comppct_r, component.compname, component.majcompflag, copmgrp.pmgroupname 
INTO Component_Table 
FROM (((mapunit INNER JOIN component ON mapunit.mukey = component.mukey) 
INNER JOIN copmgrp ON component.cokey = copmgrp.cokey) 
INNER JOIN copm ON copmgrp.copmgrpkey = copm.copmgrpkey) 
INNER JOIN chorizon ON component.cokey = chorizon.cokey; 

The parentheses in your Python Make_Table string are not in the right places. In your Python script example you are (incorrectly) only putting the parentheses around the ON conditions

SELECT 
    mapunit.mukey, copmgrp.cokey, copmgrp.copmgrpkey, copm.pmkind, copm.pmorigin, component.comppct_r, component.compname, component.majcompflag, copmgrp.pmgroupname 
INTO Component_Table 
FROM 
    (
        mapunit 
        INNER JOIN 
        component 
            ON (mapunit.mukey = component.mukey) 
        INNER JOIN 
        copmgrp 
            ON (component.cokey = copmgrp.cokey) 
        INNER JOIN 
        copm 
            ON (copmgrp.copmgrpkey = copm.copmgrpkey) 
        INNER JOIN 
        chorizon 
            ON (component.cokey = chorizon.cokey)
    )

whereas in the subsequent example that works in Access you have the parentheses (correctly) around the whole INNER JOIN construct, as required by Access SQL

SELECT 
    mapunit.mukey, copmgrp.cokey, copmgrp.copmgrpkey, copm.pmkind, copm.pmorigin, component.comppct_r, component.compname, component.majcompflag, copmgrp.pmgroupname 
INTO Component_Table 
FROM 
    (
        (
            (
                mapunit 
                INNER JOIN 
                component 
                    ON mapunit.mukey = component.mukey
            ) 
            INNER JOIN 
            copmgrp 
                ON component.cokey = copmgrp.cokey
        ) 
        INNER JOIN 
        copm 
            ON copmgrp.copmgrpkey = copm.copmgrpkey
    ) 
    INNER JOIN 
    chorizon 
        ON component.cokey = chorizon.cokey

You need to adjust the SQL string in your Python code so the parentheses are in the right places.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM