문제

Working with cursors in mysql-python I used to call "BEGIN;", "COMMIT;", and "ROLLBACK;" explicitly as follows:

try:
    cursor.execute("BEGIN;")
    # some statements
    cursor.execute("COMMIT;")
except:
    cursor.execute("ROLLBACK;")

then, I found out that the underlying connection object has the corresponding methods:

try:
    cursor.connection.begin()
    # some statements
    cursor.connection.commit()
except:
    cursor.connection.rollback()

Inspecting the DB-API PEP I found out that it does not mention the begin() method for the connection object, even for the extensions.

Mysql-python, by the way, throws the DeprecationWarning, when you use the method. sqlite3.connection, for example, does not have the method at all.

And the question is why there is no such method in the PEP? Is the statement somehow optional, is it enough to invoke commit() instead?

도움이 되었습니까?

해결책 2

사이트 설정> 사이트 모음 기능 (사이트 모음 관리 헤더 아래에서)에 가면 다음 기능을 사용할 수 있는지 확인할 수 있습니다. 워크 플로 및 활성화되지 않은 경우 활성화하십시오.O365 계정을 확인하고 작동했습니다 :)

다른 팁

look a this previously asked question. Generally the "protocol" to use with transactions is:

cursor = conn.cursor()
try:
    cursor.execute(...)
except DatabaseError:
    conn.rollback()
    raise
else:
    conn.commit()
finally:
    cursor.close()

Starting from python 2.6 sqlite Connection objects can be used as context managers that automatically commit or rollback transactions.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top