Avatar ·

Why do you need Commit, Connection, Cursor and Close?

📁 итератор, python, пример

Basic example

  import psycopg2 # connect to an existabase  conn = conn = psycopg2.connect ( "dbname = test user = postgres" ")  # open a cursor to performation  cupir = Conn.cursor ()  # execute a commander: this creates a new one  cur.execute ()  # pass data to fill a query PlaceHolders and Let PSYCOPG PERFORM   # the Correct Conversion (no more sql injection!)  cur.execute ( "" Insert Into Testa (Num, Data) Values ​​( %S, %S) ", r ... (100,  "" abc'def "" ))  # query the database and obtain data as pythjects  cur.execute ( "" select * from test; "" ) cur.fetchone ()  # make the changes to the database passistent  conn.commit ()  # Close Communication with the Database  cur.close () conn.close ()     

for which needs is used cur.close () , confn.commit () , conn.close ? What are their differences?

, for example conn.commit () , as indicated in the example, applies changes. But it seems more logical that the cursor will be responsible for this, because it is he who is an iterator according to the data.