Avatar ·

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

📁 iterator, Advanced Python Topics, example

Basic example

  import psycopg2 # connect to an existing database  conn = psycopg2.connect ( "dbname = test user = postgres" ")  # open a cursor to perform database operations  cur = conn.cursor ()  # execute a command: this creates a new table  cur.execute ()  # pass data to fill a query placeholders and let Psycopg perform   # the correct conversion (no more SQL injection!)  cur.execute ( "" INSERT INTO test (num, data) VALUES ​​(%s, %s) ", (100,  "" abc'def "" ))