首页 > > > Python & PostgreSQL
本篇介绍用Python操作PostgreSQL数据,本文的参考教程是 PostgreSQL Python
根据官方的解释 Python - PostgreSQL wiki,建议使用psycopg2
CREATE DATABASE suppliers;
2. 在Python中连接数据库:
(1)最直接的办法,使用psycopg2中的connect函数
conn = psycopg2.connect("dbname=suppliers user=postgres password=postgres")(2)另一种方法,把连接数据库时用到的参数都列出来 (效果同上)
conn = psycopg2.connect(host="localhost",database="suppliers", user="postgres", password="postgres")这里涉及到的参数有:
(3)注意:还可以使用配置文件(configuration file ),这种好处是把所有的参数放在了一个文件里,查找和修改就比较方便了。
database.ini
[postgresql]
host=localhost
database=suppliers
user=postgres
password=postgres3. 放在一起:connect代码块
首先写 config() 函数,这一函数会读取配置文件 database.ini 来获取文件中用于数据库连接的参数. 我们可以保存以下代码块在config.py文件中,以便之后的重复调用。
#!/usr/bin/python
from configparser import ConfigParser
def config(filename='database.ini', section='postgresql'):
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)
# get section, default to postgresql
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db接下来写 connect() 函数。这一函数用于连接数据库,在以下的代码块中,connect() 函数帮助我们连接到 suppliers 数据库,并且打印出PostgreSQL数据库的版本号.
#!/usr/bin/python
import psycopg2
from config import config # 这是上面的config()代码块,已经保存在config.py文件中
def connect():
""" Connect to the PostgreSQL database server """
conn = None
try:
# read connection parameters
params = config()
# connect to the PostgreSQL server
print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
# create a cursor
cur = conn.cursor()
# execute a statement
print('PostgreSQL database version:')
cur.execute('SELECT version()')
# display the PostgreSQL database server version
db_version = cur.fetchone()
print(db_version)
# close the communication with the PostgreSQL
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')
if __name__ == '__main__':
connect()4. 总结一下
工作流程如下,这实际上也是Python对数据库操作的基本流程。
database.ini 文件中读取连接数据库的参数connect() 函数连接数据库cursor,执行(execute) SQL 语句fetchone() 来读取查询的结果close()。cursor 和connection 都需要关闭哦(1) SQL语句
CREATE TABLE
(2) Python代码块
#!/usr/bin/python
import psycopg2
from config import config
def create_tables():
""" create tables in the PostgreSQL database"""
commands = (
"""
CREATE TABLE vendors (
vendor_id SERIAL PRIMARY KEY,
vendor_name VARCHAR(255) NOT NULL
)
""",
""" CREATE TABLE parts (
part_id SERIAL PRIMARY KEY,
part_name VARCHAR(255) NOT NULL
)
""",
"""
CREATE TABLE part_drawings (
part_id INTEGER PRIMARY KEY,
file_extension VARCHAR(5) NOT NULL,
drawing_data BYTEA NOT NULL,
FOREIGN KEY (part_id)
REFERENCES parts (part_id)
ON UPDATE CASCADE ON DELETE CASCADE
)
""",
"""
CREATE TABLE vendor_parts (
vendor_id INTEGER NOT NULL,
part_id INTEGER NOT NULL,
PRIMARY KEY (vendor_id , part_id),
FOREIGN KEY (vendor_id)
REFERENCES vendors (vendor_id)
ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (part_id)
REFERENCES parts (part_id)
ON UPDATE CASCADE ON DELETE CASCADE
)
""")
conn = None
try:
# read the connection parameters
params = config()
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
# create table one by one
for command in commands:
cur.execute(command)
# close communication with the PostgreSQL database server
cur.close()
# commit the changes
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
if __name__ == '__main__':
create_tables()在数据库中使用 \dt 可以列出数据库中的表
suppliers=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+----------
public | part_drawings | table | postgres
public | parts | table | postgres
public | vendor_parts | table | postgres
public | vendors | table | postgres
(4 rows)2. 将数据导入表格
(1)使用insert的方式将数据导入表格
步骤如下:
a. connect
conn = psycopg2.connect(dsn)b. cursor
cur = conn.cursor()c. execute
cur.execute(sql, (value1,value2))d. fetchone
id = cur.fetchone()[0]c. commit
conn.commit()d. close
cur.close()
conn.close()(2)将一行数据插入表中
Inserting one row into a PostgreSQL table example
#!/usr/bin/python
import psycopg2
from config import config
def insert_vendor(vendor_name):
""" insert a new vendor into the vendors table """
sql = """INSERT INTO vendors(vendor_name)
VALUES(%s) RETURNING vendor_id;"""
conn = None
vendor_id = None
try:
# read database configuration
params = config()
# connect to the PostgreSQL database
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the INSERT statement
cur.execute(sql, (vendor_name,))
# get the generated id back
vendor_id = cur.fetchone()[0]
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return vendor_id(3)将多行数据插入表中
Inserting multiple rows into a PostgreSQL table example
def insert_vendor_list(vendor_list):
""" insert multiple vendors into the vendors table """
sql = "INSERT INTO vendors(vendor_name) VALUES(%s)"
conn = None
try:
# read database configuration
params = config()
# connect to the PostgreSQL database
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the INSERT statement
cur.executemany(sql,vendor_list)
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()(4)使用以上代码块
To test the insert_vendor() and insert_vendor_list() functions, you use the following code snippet:
if __name__ == '__main__':
# insert one vendor
insert_vendor("3M Co.")
# insert multiple vendors
insert_vendor_list([
('AKM Semiconductor Inc.',),
('Asahi Glass Co Ltd.',),
('Daikin Industries Ltd.',),
('Dynacast International Inc.',),
('Foster Electric Co. Ltd.',),
('Murata Manufacturing Co. Ltd.',)
])3. 更新表格中的数据
#!/usr/bin/python
import psycopg2
from config import config
def update_vendor(vendor_id, vendor_name):
""" update vendor name based on the vendor id """
sql = """ UPDATE vendors
SET vendor_name = %s
WHERE vendor_id = %s"""
conn = None
updated_rows = 0
try:
# read database configuration
params = config()
# connect to the PostgreSQL database
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the UPDATE statement
cur.execute(sql, (vendor_name, vendor_id))
# get the number of updated rows
updated_rows = cur.rowcount
# Commit the changes to the database
conn.commit()
# Close communication with the PostgreSQL database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return updated_rowsdef get_vendors():
""" query data from the vendors table """
conn = None
try:
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute("SELECT vendor_id, vendor_name FROM vendors ORDER BY vendor_name")
print("The number of parts: ", cur.rowcount)
row = cur.fetchone()
while row is not None:
print(row)
row = cur.fetchone()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()调用:
if __name__ == '__main__':
get_vendors()结果:
The number of parts: 7
(1, '3M Corp')
(2, 'AKM Semiconductor Inc.')
(3, 'Asahi Glass Co Ltd.')
(4, 'Daikin Industries Ltd.')
(5, 'Dynacast International Inc.')
(6, 'Foster Electric Co. Ltd.')
(7, 'Murata Manufacturing Co. Ltd.')
2. fetchall
def get_parts():
""" query parts from the parts table """
conn = None
try:
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute("SELECT part_id, part_name FROM parts ORDER BY part_name")
rows = cur.fetchall()
print("The number of parts: ", cur.rowcount)
for row in rows:
print(row)
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()调用:
if __name__ == '__main__':
get_parts()结果:
The number of parts: 6
(4, 'Antenna')
(5, 'Home Button')
(6, 'LTE Modem')
(1, 'SIM Tray')
(2, 'Speaker')
(3, 'Vibrator')
3. fetchmany
def iter_row(cursor, size=10):
while True:
rows = cursor.fetchmany(size)
if not rows:
break
for row in rows:
yield row
def get_part_vendors():
""" query part and vendor data from multiple tables"""
conn = None
try:
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute("""
SELECT part_name, vendor_name
FROM parts
INNER JOIN vendor_parts ON vendor_parts.part_id = parts.part_id
INNER JOIN vendors ON vendors.vendor_id = vendor_parts.vendor_id
ORDER BY part_name;
""")
for row in iter_row(cur, 10):
print(row)
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()调用:
if __name__ == '__main__':
get_part_vendors()结果:
('Antenna', 'Foster Electric Co. Ltd.')
('Antenna', 'Murata Manufacturing Co. Ltd.')
('Home Button', 'Dynacast International Inc.')
('Home Button', '3M Corp')
('LTE Modem', 'Dynacast International Inc.')
('LTE Modem', '3M Corp')
('SIM Tray', 'AKM Semiconductor Inc.')
('SIM Tray', '3M Corp')
('Speaker', 'Daikin Industries Ltd.')
('Speaker', 'Asahi Glass Co Ltd.')
('Vibrator', 'Dynacast International Inc.')
('Vibrator', 'Foster Electric Co. Ltd.')
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3135648/ ,如需转载,请注明出处,否则将追究法律责任。