1. 程式人生 > >8、【Python】Python orator處理資料庫

8、【Python】Python orator處理資料庫

一、orator 簡介

    Orator提供一個簡單和方便的資料庫資料處理庫。它的靈感來源於PHP的Laravel框架,藉助其思想實現了python版的查詢構造器和ORM(物件關係對映)。
    完整的介紹文件地址:http://orator-orm.com/docs

二、orator基本使用

1、安裝

    使用orator的前提是:我們已經安裝了MySQL資料庫管理系統,並建立裡相應的資料庫和表,然後我們可以使用orator方便的對資料進行管理和使用。在安裝使用orator之前,需要先安裝mysql和pymysql。

(1)mysql安裝
    在Mac OS上安裝MySQL參見我的另一篇部落格:

https://mp.csdn.net/mdeditor/84954508#

(2)pymysql安裝
    直接在終端使用命令安裝pymysql。

pip install pymysql
2、配置

    假設我們已經安裝好MySQL和pymysql,並在MySQL中建立了一個名為test的資料庫,test資料庫中有一個名為instructor的表,instructor表中有四個欄位:id/name/dept_name/salary。接下來讓我們看看如何使用orator對資料庫進行操作。

    設定資料庫配置引數,建立一個DatabaseManager例項。在後面的程式中就可以使用這個DatabaseManager例項來操作相應的資料庫。

from orator import DatabaseManager

config = {
    'mysql': {
        'driver': 'mysql',
        'host': 'localhost',
        'database': 'test',#想要操作的資料庫名
        'user': 'root',#mysql資料庫的使用者名稱
        'password': 'password',#root使用者的密碼
        'prefix': ''
    }
}

db = DatabaseManager(config)
3、操作
(1)插入操作insert

往表中插入一條資料:

db.table('instructor').insert(id='0002', name='jim', dept_name='chinese', salary=8789.89)

或者使用如下方式:

db.table('instructor').insert({
	'id' : '0002',
	'name' : 'jim',
	'dept_name' : 'chinese',
	'salary' : 8789.89
})

向表中插入多條資料:

db.table('instructor').insert([
	{'id': '0002', 'name': 'jim', 'dept_name': 'chinese', 'salary': 8789.89},
	{'id': '0003', 'name': 'sam', 'dept_name': 'chinese', 'salary': 8789.89}
])
(2)更新操作update

更新表中某一行中某個欄位:

db.table('instructor').where('id', 1).update(name='hello')
#或者使用另一種方式
db.table('instructor').where('id', 1).update({'name': 'hello'})

增加或者減少某一列的值:

#增加某一列的值
db.table('instructor').increment('salary') #Increment the value by 1
db.table('instructor').increment('salary', 5) #Increment the value by 5

#減少某一列的值
db.table('instructor').decrement('salary') #Decrement the value by 1
db.table('instructor').decrement('salary', 5) #Decrement the value by 5

指定增加某一行記錄的值:

#指定將name=‘john’這一行的salary的值增加5
db.table('instructor').increment('salary', 5, name='john')
(3)刪除資料delete

刪除資料:

#刪除salary小於5000的行
db.table('instructor').where('salary', '<', 5000).delete()

刪除所有的資料:

db.table('instructor').delete()

清空表資料:

db.table('instructor').truncate()

注意:刪除所有資料和清空表資料的區別參見:https://mp.csdn.net/mdeditor/84995444

(4)合併查詢unions

unions(unions_all)用於合併連個查詢:

frist = db.table('instructor').where_null('frist_name')

names = db.table('instructor').where_null('last_name').unions(frist).get()
(5)悲觀鎖
db.table('instructor').where('salary', '>', 7000).shared_lock().get()

db.table('instructor').where('salary', '>', 7000).lock_for_update().get()
(6)查詢操作select

查詢表中所有資料

records = db.table('instructor').get()

for record in records:
    print(record['name'])

分片查詢表中的資料

for records in db.table('instructor').chunk(100):
    for record in records:
        print(record['name'])

查詢表中某一條資料

record = db.table('instructor').where('name': 'john').first()
print(record['name'])

查詢某一行的某一列資料

#查詢name為john的這一行,salary列的值
record = db.table('instructor').where('name', 'john').pluck('salary')

查詢某一列的資料

roles = db.table('instructor').lists('name')

這個方法返回的是一個list,如果在加一個引數將返回一個字典:

roles = db.table('instructor').lists('name', 'dept_name')

指定一個select語句

users = db.table('instructor').select('name', 'email').get()

users = db.table('instructor').distinct().get()

users = db.table('instructor').select('name as user_name').get()

新增一個select語句在額外查詢語句裡

query = db.table('instructor').select('name')

records = query.add_select('salary').get()

使用where操作:

records = db.table('instructor').where('salary', '>', 8000).get()

使用or操作

records = db.table('instructor').where('salary', '>', 8000).or_where('name', 'john').get()

使用where between和where not between操作

records = db.table('instructor').where_between('salary', [5000, 7000]).get()

records = db.table('instructor').where_not_between('salary', [5000, 7000]).get()

使用where in和where not in操作

records = db.table('instructor').where_in('id', [1, 2, 3]).get()

records = db.table('instructor').where_not_in('id', [1, 2, 3]).get()

使用where null查詢null記錄

records = db.table('instructor').where_null('updated_at').get()

使用order by,group by and having操作

#以name為基準排序,desc降序,asc升序
query = db.table('instructor').order_by('name', 'desc')
#group_by語句對query的查詢結果按照salary的值分組,所有具有相同salary的值元組為一組
query = query.group_by('salary')
#having短語作用於組,從中選擇滿足條件的組
query = query.having('salary', '>', 1000)

records = query.get()

使用offset and limit操作

#從表instructor的頭開始跳過10條記錄,然後取5條記錄
records = db.table('instructor').skip(10).take(5).get()
#從表instructor的頭開始跳過10條記錄,然後取5條記錄
records = db.table('instructor').offset(10).limit(5).get()

使用Join操作

使用join可以進行連表查詢,假設資料庫中有三張表分別是:users/contacts/orders。則連表查詢的相關操作如下:

基本join操作

db.table('users')
.join('contacts', 'users.id', '=', 'contacts.user_id')
.join('orders', 'users.id', '=', 'orders.user_id')
.select('users.id', 'contacts.phone','orders.price')
.get()