简体   繁体   中英

How to insert values into table by using inner query as select statement using Python Flask and PyOdbc? Input of Inner queries fetched from HTML form

Environment Details. Windows 10 - Python3.5 - Flask - SQL SERVER - PyOdbc.

cur.execute("insert into T1 (ID_CUSTOMER, ID_ENVIRONMENT) values (?, ?)",
                    select id from CUSTOMER where name = form['customerId'],
                    select id from ENVIRONMENT where name = form['environmentId'],
                    )

Remove the values , you can directly use INSERT SELECT .

INSERT INTO T1 
            (ID_CUSTOMER, 
             ID_ENVIRONMENT) 
SELECT ID_CUSTOMER =(SELECT Max(id) 
                     FROM   customer 
                     WHERE  NAME = 'abc'), 
       ID_ENVIRONMENT = (SELECT Max(id) 
                         FROM   environment 
                         WHERE  NAME = 'xyz') 

The Max(id) here is to make sure the inner query returns a single value

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