0% found this document useful (0 votes)
20 views

20.python Database Programming (PostgreSQL)

Uploaded by

Arun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

20.python Database Programming (PostgreSQL)

Uploaded by

Arun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In [15]:

# import database related driver psycopg2 is a driver for postgres db in python


import psycopg2

# create connection object


conn = psycopg2.connect(
host="localhost",
database="carzone",
user="postgres",
password="postgres")

# create a cursor
cur = conn.cursor()

# create table

# sql ='''CREATE TABLE EMPLOYEE(


# FIRST_NAME CHAR(20) NOT NULL,
# LAST_NAME CHAR(20),
# AGE INT,
# SEX CHAR(1),
# INCOME FLOAT
# )'''

# if we want to insert multiple data

# data_list=[('user2','user2',19,"F",10000),('user3','user3',19,"F",10000),('user4','use

# for i in data_list:
# print(i)
# sql=f"""insert into employee values('{i[0]}','{i[1]}',{i[2]},'{i[3]}',{i[4]})"""
# print(sql)
# cur.execute(sql)

# insert data

# sql="""insert into employee values('user1','user1',20,'M',20000)


# """

sql="select * from cars_car"

cur.execute(sql)

# if we want to see selected data we can use fetchall() or fetchone()


result = cur.fetchone();
print(result)

conn.commit()

conn.close()

(1, '2017 Ferrari 488 GTB', 'CA', 'Norwood', 'White', '488 GTB', 2017,
'used', 40000, '<p>The&nbsp;<strong>Ferrari 488</strong>&nbsp;(Type F14
2M) is a&nbsp;<a href="https://en.wikipedia.org/wiki/Rear_mid-engine,_r
ear-wheel_drive_layout" title="Rear mid-engine, rear-wheel drive layou
t">mid-engine</a>&nbsp;<a href="https://en.wikipedia.org/wiki/Sports_ca
r" title="Sports car">sports car</a>&nbsp;produced by the Italian autom
obile manufacturer&nbsp;<a href="https://en.wikipedia.org/wiki/Ferrari"
title="Ferrari">Ferrari</a>. The car replaced the&nbsp;<a href="http
s://en.wikipedia.org/wiki/Ferrari_458" title="Ferrari 458">458</a>, bei
ng the first mid-engine Ferrari to use a turbocharged V8 since the&nbs
p;<a href="https://en.wikipedia.org/wiki/Ferrari_F40" title="Ferrari F4
0">F40</a>.</p>\r\n\r\n<p>The car is powered by a 3.9-litre&nbsp;<a hre
f="https://en.wikipedia.org/wiki/Twin-turbocharged" title="Twin-turboch
arged">twin-turbocharged</a>&nbsp;<a href="https://en.wikipedia.org/wik
i/V8_engine" title="V8 engine">V8 engine</a>, smaller in displacement b
ut generating a higher power output than the 458&#39;s&nbsp;<a href="ht
tps://en.wikipedia.org/wiki/Naturally_aspirated_engine" title="Naturall
y aspirated engine">naturally aspirated engine</a>. The 488 GTB was nam
ed &quot;The Supercar of the Year 2015&quot; by car magazine&nbsp;<a hr
ef="https://en.wikipedia.org/wiki/Top_Gear_(magazine)" title="Top Gear
(magazine)"><em>Top Gear</em></a>, as well as becoming&nbsp;<em><a href
="https://en.wikipedia.org/wiki/Motor_Trend" title="Motor Trend">Motor
Trend</a></em>&#39;s 2017 &quot;Best Driver&#39;s Car&quot;.<a href="ht
tps://en.wikipedia.org/wiki/Ferrari_488#cite_note-7">[7]</a>&nbsp;<a hr
ef="https://en.wikipedia.org/wiki/Jeremy_Clarkson" title="Jeremy Clarks
on">Jeremy Clarkson</a>&nbsp;announced the 488 Pista as his 2019 Superc
ar of the Year.<a href="https://en.wikipedia.org/wiki/Ferrari_488#cite_
note-8">[8]</a>&nbsp;The 488 was succeeded by the&nbsp;<a href="http
s://en.wikipedia.org/wiki/Ferrari_F8_Tributo" title="Ferrari F8 Tribut
o">F8 Tributo</a>&nbsp;in February 2019</p>', 'photos/2022/01/19/190253
6891.jpg', 'photos/2022/01/19/1902537043.jpg', 'photos/2022/01/19/19025
37077.jpg', 'photos/2022/01/19/1902537082.jpg', 'photos/2022/01/19/1902
536917.jpg', 'Airbags,Air Conditioning,Alarm System,ParkAssist,Auto Sta
rt/Stop', 'Sport', '3.9-litre twin-turbocharged V8 engine', 'Automati
c', 'Grey', 10000, '3', 2, 'WET75', 5, 'Petrol', '1', True, datetime.da
tetime(2022, 1, 19, 19, 36, 1, 366174, tzinfo=datetime.timezone(datetim
e.timedelta(seconds=19800))))

In [ ]:

You might also like