commit, connection, cursor और close किसके लिए आवश्यक हैं?
📁 पुनरावृत्ति करने वाला, पायथन, उदाहरण
बुनियादी उदाहरण
import psycopg2# मौजूदा डेटाबेस से कनेक्ट करेंconn = psycopg2.connect(""dbname=test user=postgres"")# डेटाबेस ऑपरेशन करने के लिए cursor खोलेंcur = conn.cursor()# एक कमांड निष्पादित करें: यह एक नया table बनाता हैcur.execute(""CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);"")# क्वेरी प्लेसहोल्डर भरने के लिए डेटा पास करें और Psycopg को सही रूपांतरण करने दें# (अब और SQL injections नहीं!)cur.execute(""INSERT INTO test (num, data) VALUES (%s, %s)"",... (100, ""abc'def""))# डेटाबेस से क्वेरी करें और डेटा को Python ऑब्जेक्ट के रूप में प्राप्त करेंcur.execute(""SELECT * FROM test;"")cur.fetchone()# डेटाबेस में परिवर्तनों को स्थायी बनाएंconn.commit()