How do you change the SQL isolation level from Python using MySQLdb?

前端 未结 2 1228
刺人心
刺人心 2020-12-20 14:18

The documentation I\'ve run across researching this indicates that the way to do it for other databases is to use multiple statements in your query, a la:

&g         


        
相关标签:
2条回答
  • 2020-12-20 14:35

    I don't think this works for the MySQLdb driver; you'll have to issue separate queries:

    cur = conn.cursor()
    cur.execute("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED")
    cur.execute("SELECT @@session.tx_isolation")
    print cur.fetchall()[0]
    cur.execute("SELECT * FROM bar")
    print cur.fetchall()
    cur.execute("SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ")
    cur.execute("SELECT @@session.tx_isolation")
    print cur.fetchall()[0]
    
    # output
    ('READ-UNCOMMITTED',)
    (('foo',), ('bar',))
    ('REPEATABLE-READ',)
    

    The MySQLdb cursor's execute() method only sees the first query up to the semicolon:

    cur.execute("SELECT * FROM bar WHERE thing = 'bar'; SELECT * FROM bar")
    print cur.fetchall()
    
    # output
    (('bar',),)
    
    0 讨论(0)
  • 2020-12-20 14:39
    cur.executemany("SELECT * FROM bar WHERE thing = 'bar'; SELECT * FROM bar")
    print cur.fetchall()
    

    use cur.executemany to run multiple sql statements with ; separated.

    0 讨论(0)
提交回复
热议问题