在python脚本中执行多个mysql查询

问题描述 投票:0回答:1

我试图在我的Python脚本中执行多个MySql查询,并且我正在做我认为我应该做的所有事情,但它仍然没有对我的

table_a
做任何事情。 有什么见解吗?

import MySQLdb
conn = MySQLdb.connect("abc","bcd","cde","def")
x = conn.cursor()

try:
   operation = '''
    insert into table_a 
    (id, ip, hostname, username) 
    select distinct id, ip, hostname, username
    from table_b;

    update table_a 
    set 
    purpose = 'test purpose'
    , department = 'test department'
    where
    hostname = 'sample'
'''



   for result in x.execute(operation,multi = True):
      pass

   conn.commit()
except:
   conn.rollback()

conn.close()
python mysql
1个回答
0
投票

有点晚了,我希望你已经有了你需要的东西。

对于其他人,这是我发现对此有用的东西(添加 multi=True)-

for result in x.execute(operation, multi=True)

我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.