खोज…


शुरू करना

PostgreSQL एक सक्रिय रूप से विकसित और परिपक्व खुला स्रोत डेटाबेस है। psycopg2 मॉड्यूल का उपयोग करके, हम डेटाबेस पर प्रश्नों को निष्पादित कर सकते हैं।

पाइप का उपयोग करके स्थापना

pip install psycopg2

मूल उपयोग

मान लेते हैं कि हमारे पास डेटाबेस my_database में एक तालिका my_table है जो निम्नानुसार परिभाषित है।

आईडी पहला नाम उपनाम
1 जॉन हरिणी

हम निम्नलिखित फैशन में डेटाबेस पर प्रश्नों को चलाने के लिए psycopg2 मॉड्यूल का उपयोग कर सकते हैं।

import psycopg2

# Establish a connection to the existing database 'my_database' using
# the user 'my_user' with password 'my_password'
con = psycopg2.connect("host=localhost dbname=my_database user=my_user password=my_password")

# Create a cursor
cur = con.cursor()

# Insert a record into 'my_table'
cur.execute("INSERT INTO my_table(id, first_name, last_name) VALUES (2, 'Jane', 'Doe');")

# Commit the current transaction
con.commit()

# Retrieve all records from 'my_table'
cur.execute("SELECT * FROM my_table;")
results = cur.fetchall()

# Close the database connection
con.close()

# Print the results
print(results)

# OUTPUT: [(1, 'John', 'Doe'), (2, 'Jane', 'Doe')]


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow