sqlite3 - SQL question regarding update of a table based on lookup table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gugyu
    New Member
    • Feb 2013
    • 1

    sqlite3 - SQL question regarding update of a table based on lookup table

    I have a table AVI with the following relevant structure:
    POLY_NUM key
    LKP
    SITE1
    SITE2
    SITE3
    Y2BH

    I have a lookup table SILKP with:
    LKP
    SITE1
    SITE2
    SITE3
    Y2BH

    what is the fastest/most efficient way to update table A SITE1-SITE3 and Y2BH fields by linking via lookup.

    What I have:

    Code:
    with sqlite3.connect(db_filename) as conn:
        cursor = conn.cursor()
    
        print "Updating lookup fields..."
        
        cursor.execute("""
                            UPDATE avi SET site1 = (SELECT site1 FROM silkp WHERE silkp.lkp = avi.lkp)
                       """)
        cursor.execute("""
                            UPDATE avi SET site2 = (SELECT site2 FROM silkp WHERE silkp.lkp = avi.lkp)
                       """)
        cursor.execute("""
                            UPDATE avi SET site3 = (SELECT site3 FROM silkp WHERE silkp.lkp = avi.lkp)
                       """)
                            UPDATE avi SET y2bh = (SELECT y2bh FROM silkp WHERE silkp.lkp = avi.lkp)
                       """)
    Is there a faster and more elegant way?
    Last edited by bvdet; Feb 6 '13, 05:16 PM. Reason: Please use code tags when posting code. [code]....[/code]
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You can try joining the tables and updating it in one statement. This works in SQL Server, you can see if it works in SQLite.
    Code:
    UPDATE t1
    SET field1 = t2.field1,
        field2 = t2.field2
    FROM t1 INNER JOIN 
         t2 ON t1.id = t2.id

    Comment

    Working...