
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to insert a Python tuple in a PostgreSql database?
PostgreSql database is by default installed on port number 5432. Python interface to PostgreSql is provided by installing psycopg2 module. Assuming that test database and employee table with fname, sname, age, gender and salary fields is available.
First establish connection and obtain cursor object by following statements in Python script.
import psycopg2 conn = psycopg2.connect(database = "test", user = "postgres", password = "pass123", host = "localhost", port = "5432") cur = conn.cursor()
Data to be inserted in employee table is stored in the form of tuple object
t1=('Mac', 'Mohan', 20, 'M', 2000)
Next set up the insert query using this tuple
sql="insert into employee values(%s,%s,%d,%s,%d)" %t1
Now this query is executed using the cursor object
cursor.execute(sql)
Result of this statement will be that employee table will show new record added in it.
- Related Articles
- How to insert a Python tuple in a MySQL database?
- How to connect to PostgreSQL database using a JDBC program?
- Database INSERT Operation in Python
- How to insert values from one table into another in PostgreSQL?
- How to insert Timestamp value in a database using JDBC program?
- How to insert data into a MySQL database with Java?
- How to insert a record into a table in a database using JDBC API?
- How to create a table in PostgreSQL?
- How to insert current date and time in a database using JDBC?
- How to insert new documents into a MongoDB collection in your database?
- How to insert/store JSON array into a database using JDBC?
- How to insert images in Database using JDBC?
- How to iterate through a tuple in Python?
- How to insert DECIMAL into MySQL database?
- How to insert a Python object in Mongodb?

Advertisements