1、Oracle数据库
Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小微机环境。它是一种高效率的、可靠性好的、适应高吞吐量的数据库方案。
2、安装Pony和cx_Oracle
cx_Oracle是一个Python扩展模块,允许访问Oracle数据库。它符合Python database API 2.0规范,增加了相当多的功能,也有一些例外。在Python 3.6到3.10版本中对cx_Oracle进行了测试。旧版本的cx_Oracle可以与以前的Python版本一起使用。可以将cx_Oracle与Oracle 11.2、12、18、19和21客户端库一起使用。Oracle的标准客户端-服务器版本的互操作性允许连接到新旧数据库。
1)使用pip安装Pony
pip install pony
2)安装数据库的驱动
pip install cx_Oracle
3、Pony Oracle常用操作
使用Pony ORM操作Oracle进行数据增加、删除、修改和查询,示例代码如下,
from __future__ import absolute_import, print_function
from decimal import Decimal
from pony.orm import *
db = Database()
db.bind(provider='oracle', user='admin', password='admin', dsn='docker_oracle11')
class Customer(db.Entity):
id = PrimaryKey(int, auto=True)
name = Required(str)
email = Required(str, unique=True)
orders = Set("OrderInfo")
class OrderInfo(db.Entity):
id = PrimaryKey(int, auto=True)
total_price = Required(Decimal)
customer = Required(Customer)
items = Set("OrderItem")
class Product(db.Entity):
id = PrimaryKey(int, auto=True)
name = Required(str)
price = Required(Decimal)
items = Set("OrderItem")
class OrderItem(db.Entity):
quantity = Required(int, default=1)
order = Required(OrderInfo)
product = Required(Product)
PrimaryKey(order, product)
sql_debug(True)
db.generate_mapping(create_tables=True)
@db_session
def populate_database():
c1 = Customer(name='John Smith', email='john@example.com')
c2 = Customer(name='Matthew Reed', email='matthew@example.com')
c3 = Customer(name='Chuan Qin', email='chuanqin@example.com')
c4 = Customer(name='Rebecca Lawson', email='rebecca@example.com')
c5 = Customer(name='Oliver Blakey', email='oliver@example.com')
p1 = Product(name='Kindle Fire HD', price=Decimal('284.00'))
p2 = Product(name='Apple iPad with Retina Display', price=Decimal('478.50'))
p3 = Product(name='SanDisk Cruzer 16 GB USB Flash Drive', price=Decimal('9.99'))
p4 = Product(name='Kingston DataTraveler 16GB USB 2.0', price=Decimal('9.98'))
p5 = Product(name='Samsung 840 Series 120GB SATA III SSD', price=Decimal('98.95'))
p6 = Product(name='Crucial m4 256GB SSD SATA 6Gb/s', price=Decimal('188.67'))
o1 = OrderInfo(customer=c1, total_price=Decimal('292.00'))
OrderItem(order=o1, product=p1)
OrderItem(order=o1, product=p4, quantity=2)
o2 = OrderInfo(customer=c1, total_price=Decimal('478.50'))
OrderItem(order=o2, product=p2)
o3 = OrderInfo(customer=c2, total_price=Decimal('680.50'))
OrderItem(order=o3, product=p2)
OrderItem(order=o3, product=p4, quantity=2)
OrderItem(order=o3, product=p6)
o4 = OrderInfo(customer=c3, total_price=Decimal('99.80'))
OrderItem(order=o4, product=p4, quantity=10)
o5 = OrderInfo(customer=c4, total_price=Decimal('722.00'))
OrderItem(order=o5, product=p1)
OrderItem(order=o5, product=p2)
commit()
populate_database()
#查询
@db_session
def query():
print(select(p for p in Product if p.id==3)[:])
#多条数据会报错:pony.orm.core.MultipleObjectsFoundError: Multiple objects were found. Use Customer.select(...) to retrieve them
#通过pk获取
print(Product[1].name)
print(Customer.get(id=1).name)
print(Customer.select(lambda c: c.id==1)[:][0].name)
y = 1
print(Product.select_by_sql("SELECT * FROM Product WHERE id=$(y*2)")[0].name)
@db_session
def update():
c1 = Customer.get(id=1)
c1.name="john"
p1 = select(p for p in Product if p.id==3)[:][0]
p1.name="cjavapy"
c2 = Customer.get(id=2)
#更新多个字段
#c.name="cjavapy"
#c.email="cjavapy@gmail.com"
c2.set(name="cjavapy",email="cjavapy@gmail.com")
@db_session
def delete():
p = Product[1]
p.delete()
# 或者:
#delete(p for p in Product if p.id >=5)
# 或:
#使用bulk=True参数是一条delete语句批量删除,否则是先select满足条件数据,然后每次删除一个
Product.select(lambda p: p.id >=5).delete(bulk=True)
query()
update()
delete()
注意:Pony连接Oracle数据库时,dsn
填写为tnsnames.ora
文件中数据库连接的配置名称。