日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sql to sqlalchemy 实例教程

發布時間:2024/4/17 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sql to sqlalchemy 实例教程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Python項目中,經常需要操作數據庫,而 sqlalchemy 提供了 SQL 工具包及對象關系映射(ORM)工具,大大提高了編程開發的效率。為了更好的提升自己的 sql 以及使用 sqlachemy 水平,可以使用 MySQL 自帶的示范數據庫 employees 進行練習。

搭建基于 MySQL 實例數據庫 employees 的 sqlalchemy 開發環境

請參閱下面的鏈接內容:

搭建基于 MySQL 實例數據庫 employees 的 sqlalchemy 開發環境

基本實例

以下九個例子全是以代碼加注釋的形式來展示給大家。

# -*- coding:utf-8 -*- __author__ = '東方鶚' __blog__ = 'http://www.os373.cn'from models import session, Employee, Department, DeptEmp, DeptManager, Salary, Title import operator'''----------------------------------------------第一例-----------------------------------------------功能說明:使用主鍵對 employees 表進行查詢,結果是: 返回該主鍵對應的單條數據! ''''''使用 sql 語句方式進行查詢''' sql = "select * from employees where emp_no = 10006" sql_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date) for d in session.execute(sql)]'''使用 sqlalchemy 方式進行查詢''' d = session.query(Employee).get(10006) alchemy_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date)]'''比較兩個結果,應該是True''' for d in zip(sql_data, alchemy_data):print(d) print('第一例結果是:{}'.format(operator.eq(sql_data, alchemy_data)))'''-------------------------------------------------------------------------------------------------''''''-------------------------------------------第二例--------------------------------------------------功能說明:對 employees 表進行查詢,結果是:從第 4 行開始查詢,返回之后的 10 行數據!值為一個列表。 ''''''使用 sql 語句方式進行查詢''' sql = "select * from employees limit 10 offset 4" sql_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date) for d in session.execute(sql)]'''使用 sqlalchemy 方式進行查詢''' alchemy_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date)for d in session.query(Employee).limit(10).offset(4).all()]'''比較兩個結果,應該是True''' for d in zip(sql_data, alchemy_data):print(d) print('第二例結果是:{}'.format(operator.eq(sql_data, alchemy_data)))'''-------------------------------------------------------------------------------------------------''''''-------------------------------------------第三例--------------------------------------------------功能說明:使用一個精確參數對 employees 表進行查詢(搜索字段 last_name 為 'Nooteboom' 的內容),結果是: 返回該參數對應的第一條數據!僅僅是第一條數據! ''''''使用 sql 語句方式進行查詢''' sql = "select * from employees where last_name = 'Nooteboom' limit 1" sql_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date) for d in session.execute(sql)]'''使用 sqlalchemy 方式進行查詢''' d = session.query(Employee).filter_by(last_name='Nooteboom').first() alchemy_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date)]'''比較兩個結果,應該是True''' for d in zip(sql_data, alchemy_data):print(d) print('第三例結果是:{}'.format(operator.eq(sql_data, alchemy_data)))'''-------------------------------------------------------------------------------------------------''''''-------------------------------------------第四例--------------------------------------------------功能說明:使用一個精確參數對 employees 表進行查詢(搜索字段 last_name 為 'Nooteboom' 的內容),結果是: 返回該參數對應的所有數據!所有數據!值為一個列表。 ''''''使用 sql 語句方式進行查詢''' sql = "select * from employees where last_name = 'Nooteboom'" sql_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date) for d in session.execute(sql)]'''使用 sqlalchemy 方式進行查詢''''''方法一 alchemy_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date)for d in session.query(Employee).filter_by(last_name='Nooteboom').all()] ''''''方法二如下''' alchemy_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date)for d in session.query(Employee.emp_no, Employee.birth_date, Employee.first_name,Employee.last_name, Employee.gender, Employee.hire_date).filter_by(last_name='Nooteboom').all()]'''比較兩個結果,應該是True''' for d in zip(sql_data, alchemy_data):print(d) print('第四例結果是:{}'.format(operator.eq(sql_data, alchemy_data)))'''-------------------------------------------------------------------------------------------------''''''-------------------------------------------第五例--------------------------------------------------功能說明:使用兩個及以上的精確參數對 employees 表進行查詢(搜索字段 last_name 為 'Nooteboom' 并且字段 first_name 為 'Pohua' 的內容),結果是: 返回參數對應的所有數據!所有數據!值為一個列表。 ''''''使用 sql 語句方式進行查詢''' sql = "select * from employees where last_name = 'Nooteboom' and first_name = 'Pohua'" sql_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date) for d in session.execute(sql)]'''使用 sqlalchemy 方式進行查詢''''''方法一 alchemy_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date)for d in session.query(Employee).filter_by(last_name='Nooteboom', first_name='Pohua').all()] ''' '''方法二如下''' alchemy_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date)for d in session.query(Employee).filter(Employee.last_name=='Nooteboom').filter(Employee.first_name=='Pohua').all()]'''比較兩個結果,應該是True''' for d in zip(sql_data, alchemy_data):print(d) print('第五例結果是:{}'.format(operator.eq(sql_data, alchemy_data)))'''-------------------------------------------------------------------------------------------------''''''-------------------------------------------第六例--------------------------------------------------功能說明:使用一個模糊參數對 employees 表進行查詢,結果是: 返回該參數對應的所有數據!所有數據!值為一個列表。提示:1、sqlalchemy 提供了 like, endswith, startswith 函數結合通配符來進行模糊查詢。對于 like, endswith, startswith ,見字如面,請按照英文字面意思理解。2、本例的重點是使用且僅一個模糊參數, 主要是為了展示 like 函數。 ''''''使用 sql 語句方式進行查詢''' sql = "select * from employees where last_name like 'N%te_%'" sql_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date) for d in session.execute(sql)]'''使用 sqlalchemy 方式進行查詢'''alchemy_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date)for d in session.query(Employee).filter(Employee.last_name.like('N%te_%')).all()]'''比較兩個結果,應該是True''' for d in zip(sql_data, alchemy_data):print(d) print('第六例結果是:{}'.format(operator.eq(sql_data, alchemy_data)))'''-------------------------------------------------------------------------------------------------''''''-------------------------------------------第七例--------------------------------------------------功能說明:使用兩個及以上模糊參數對 employees 表進行查詢,查詢字段 last_name 近似于 'N%te_%',并且字段 first_name 在 ('Jaewon', 'os373.cn') 里,同時,字段 birth_date 是以 1955 開頭,且字段 hire_date 是以 05-30 結束的員工信息。結果是: 返回參數對應的所有數據!所有數據!值為一個列表。提示:1、sqlalchemy 提供了 like, endswith, startswith 函數結合通配符來進行模糊查詢。對于 like, endswith, startswith ,見字如面,請按照英文字面意思理解。2、本例的重點是展示 like, endswith, startswith 函數以及 and_, or_, in_ 邏輯運算符函數的用法。彩蛋:思考一下 not in, not equal,is NULL,is not NULL 的用法。 ''''''使用 sql 語句方式進行查詢''' sql = """SELECT*FROMemployeesWHERElast_name LIKE 'N%te_%'AND first_name IN ('Jaewon', 'os373.cn')AND birth_date LIKE '1955%'AND hire_date LIKE '%05-30' """ sql_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date) for d in session.execute(sql)]'''使用 sqlalchemy 方式進行查詢''' from sqlalchemy import and_, or_ alchemy_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date)for d in session.query(Employee).filter(and_(Employee.last_name.like('N%te_%'),Employee.first_name.in_(['Jaewon','os373.cn']),Employee.birth_date.startswith('1955'),Employee.hire_date.endswith('05-30'))).all()]'''比較兩個結果,應該是True''' for d in zip(sql_data, alchemy_data):print(d) print('第七例結果是:{}'.format(operator.eq(sql_data, alchemy_data)))'''-------------------------------------------------------------------------------------------------''''''-------------------------------------------第八例--------------------------------------------------功能說明:使用兩個及以上模糊參數對 employees 表進行查詢,查詢字段 last_name 近似于 'N%te_%',并且字段 first_name 在 ('Jaewon', 'os373.cn') 里的員工信息,或者是,字段 birth_date 是以 1955 開頭,且字段 hire_date 是以 05-30 結束的員工信息的個數。結果是: 返回一個數字。提示:1、sqlalchemy 提供了 like, endswith, startswith 函數結合通配符來進行模糊查詢。對于 like, endswith, startswith ,見字如面,請按照英文字面意思理解。2、本例的重點是展示 like, endswith, startswith 函數以及 and_, or_, in_ 邏輯運算符函數的用法。3、func 函數可以執行數據庫所支持的函數,本例中是為了執行 MySQL 的 count 函數。4、scalar() 函數是為了返回單項數據,與 first(), one() 函數類似,但是前者返回的是單項數據,后兩者返回的是 tuple。 ''''''使用 sql 語句方式進行查詢''' sql = """SELECTcount(*)FROMemployeesWHERE(last_name LIKE 'N%te_%'AND first_name IN ('Jaewon', 'os373.cn'))OR (birth_date LIKE '1955%'AND hire_date LIKE '%05-30') """ sql_data = [d for d in session.execute(sql)][0][0]'''使用 sqlalchemy 方式進行查詢''' from sqlalchemy import and_, or_'''方法一 alchemy_data = session.query(Employee).filter(or_(and_(Employee.last_name.like('N%te_%'),Employee.first_name.in_(['Jaewon','os373.cn'])),and_(Employee.birth_date.startswith('1955'),Employee.hire_date.endswith('05-30')))).count()''''''方法二''' from sqlalchemy import func alchemy_data = session.query(func.count("*")).filter(or_(and_(Employee.last_name.like('N%te_%'),Employee.first_name.in_(['Jaewon','os373.cn'])),and_(Employee.birth_date.startswith('1955'),Employee.hire_date.endswith('05-30')))).scalar()'''比較兩個結果,應該是True''' print(sql_data, alchemy_data) print('第八例結果是:{}'.format(operator.eq(sql_data, alchemy_data)))'''-------------------------------------------------------------------------------------------------''''''-------------------------------------------第九例--------------------------------------------------功能說明:使用兩個及以上模糊參數對 employees 表進行查詢,查詢字段 last_name 近似于 'N%te_%',并且字段 first_name 在 ('Jaewon', 'os373.cn') 里的員工信息,或者是,字段 birth_date 是以 1955 開頭,且字段 hire_date 是以 05-30 結束的員工信息,并按照字段 last_name 進行排序。結果是: 返回參數對應的所有數據!所有數據!值為一個列表。提示:1、由于 MySQL 5.7 中的 sql_mode 設置有 only_full_group_by,因此要求 group by 的使用方法像 oracle 一樣,必須得把要查詢出的字段都羅列在 group by 語句之后,聚合函數除外。按照最靠前的字段來進行排序。 ''''''使用 sql 語句方式進行查詢''' sql = """SELECT*FROMemployeesWHERE(last_name LIKE 'N%te_%'AND first_name IN ('Jaewon', 'os373.cn'))OR (birth_date LIKE '1955%'AND hire_date LIKE '%05-30')GROUP BYlast_name,gender,hire_date,emp_no,birth_date,first_name """ sql_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date) for d in session.execute(sql)]'''使用 sqlalchemy 方式進行查詢''' from sqlalchemy import and_, or_ alchemy_data = [(d.emp_no, d.birth_date, d.first_name, d.last_name, d.gender, d.hire_date)for d in session.query(Employee).filter(or_(and_(Employee.last_name.like('N%te_%'),Employee.first_name.in_(['Jaewon','os373.cn'])),and_(Employee.birth_date.startswith('1955'),Employee.hire_date.endswith('05-30')))).\group_by(Employee.last_name, Employee.gender, Employee.hire_date, Employee.emp_no,Employee.birth_date, Employee.first_name).all()]'''比較兩個結果,應該是True''' for d in zip(sql_data, alchemy_data):print(d) print('第九例結果是:{}'.format(operator.eq(sql_data, alchemy_data)))'''-------------------------------------------------------------------------------------------------''' session.commit() session.close()

其實,這是本人維護的一個 github 項目,歡迎大家能夠提供有意思的 SQL 語句,我們一起來將它轉換為 sqlalachemy 語句。
項目地址——https://eastossifrage.github.io/sql_to_sqlalchemy/

希望你能夠喜歡。

總結

以上是生活随笔為你收集整理的sql to sqlalchemy 实例教程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。