Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫深入對比
作為數(shù)據(jù)分析師,掌握一門數(shù)據(jù)庫語言,是很有必要的。
今天黃同學就帶著大家學習兩個關系型數(shù)據(jù)庫MySQL、Oracle,了解一個非關系數(shù)據(jù)庫MongoDB。

1. Python操作Oracle數(shù)據(jù)庫
這一部分的難點在于:環(huán)境配置有點繁瑣。不用擔心,我為大家寫了一篇關于Oracle環(huán)境配置的文章。
Python操作Oracle使用的是cx_Oracle庫。需要我們使用如下命令提前安裝:
pip insatll cx_Oracle
① Python鏈接Oracle服務器的3種方式
# ① 用戶名、密碼和監(jiān)聽寫在一起
import cx_Oracle
db = cx_Oracle.connect('scott/a123456@DESKTOP-V4LKB10:1521/orcl')
# ② 用戶名、密碼和監(jiān)聽分開寫
import cx_Oracle
db = cx_Oracle.connect("scott","a123456","192.168.2.1:1521/orcl")
# ③ 配置監(jiān)聽并連接
import cx_Oracle
moniter = cx_Oracle.makedsn('192.168.2.1',1521,'orcl')
db = cx_Oracle.connect('scott','a123456',moniter)
② Python怎么獲取Oracle中的數(shù)據(jù)?
這里有三種常用的方法,分別為大家進行介紹。
Ⅰ fetchone():一次獲取一條記錄;
import cx_Oracle
# 注意:一定要加下面這兩行代碼,負責會中文亂碼;
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
db = cx_Oracle.connect("scott","a123456","192.168.2.1:1521/orcl")
cursor = db.cursor()
cursor.execute('select count(*) from emp1')
aa = cursor.fetchone()
print(aa)
cursor.execute('select ename,deptno,sal from emp1')
for i in range(aa[0]):
a,b,c = cursor.fetchone()
d = "我的名字叫{},所在部門是{},工資是{}美元".format(a,b,c)
display(d)
db.close()
結果如下:

Ⅱ fetchall():一次獲取所有記錄;
import cx_Oracle
# 注意:一定要加下面這兩行代碼,負責會中文亂碼;
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
db = cx_Oracle.connect("scott","a123456","192.168.2.1:1521/orcl")
cursor = db.cursor()
cursor.execute('select ename,deptno,sal from emp1')
aa = cursor.fetchall()
# print(aa)
for a,b,c in aa:
d = "我的名字叫{},所在部門是{},工資是{}美元".format(a,b,c)
display(d)
db.close()
結果如下:

Ⅲ 使用pandas中的read_sql()方法,將提取到的數(shù)據(jù)直接轉化為DataFrame進行操作;
import cx_Oracle
import pandas as pd
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
db = cx_Oracle.connect("scott","a123456","192.168.2.1:1521/orcl")
cursor = db.cursor()
df1 = pd.read_sql("select * from emp where deptno=20",db)
display(df1)
df2 = pd.read_sql("select * from emp where deptno=30",db)
display(df2)
結果如下:

2. Python操作MySQL數(shù)據(jù)庫
MySQL數(shù)據(jù)庫應該是國內應用最多的數(shù)據(jù)庫。大多數(shù)公司一般都是使用的該數(shù)據(jù)庫。這也就是很多學生在畢業(yè)之前都會選擇學習該數(shù)據(jù)庫知識,用于面試。
Python操作MySQL使用的是cx_Oracle庫。需要我們使用如下命令提前安裝:
pip insatll pymysql
更多細節(jié)參考:Python操作Oracle詳解!
① Python鏈接MySQL服務器
import pymysql db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='spiders',charset=' utf8')
這里面有六個參數(shù),需要為大家一一介紹一下:
參數(shù)host:mysql服務器所在的主機的ip;
參數(shù)user:用戶名;
參數(shù)password:密碼;
參數(shù)port:連接的mysql主機的端口,默認是3306;
參數(shù)db:連接的數(shù)據(jù)庫名;
參數(shù)charset:當讀取數(shù)據(jù)出現(xiàn)中文會亂碼的時候,需要我們設置一下編碼;我們使用python操作數(shù)據(jù)庫的時候,那么python就相當于是client,我們是用這個client來操作mysql的server服務器,python3默認采用的utf8字符集,我的mysql服務器默認采用latin1字符集,因此mysql中創(chuàng)建的每張表,都是建表的時候加了utf8編碼的,因此這里設置的應該就是connection連接器的編碼;
② Python怎么獲取MySQL中的數(shù)據(jù)?
Ⅰ fetchone():一次獲取一條記錄;
import pymysql
db = pymysql.connect(host='localhost',user='root',db='huangwei',password='123456',port=3306,charset='utf8')
cursor = db.cursor()
cursor.execute('select count(*) from person')
aa = cursor.fetchone()
print(aa)
cursor.execute('select name,age from person')
for i in range(aa[0]):
a,b = cursor.fetchone()
c = "我的名字叫{},今年{}歲".format(a,b)
display(c)
db.close()
結果如下:

Ⅱ fetchall():一次獲取所有記錄;
import pymysql
db = pymysql.connect(host='localhost',user='root',db='huangwei',password='123456',port=3306,charset='utf8')
cursor = db.cursor()
cursor.execute('select name,age from person')
aa = cursor.fetchall()
# print(aa)
for a,b in aa:
c = "我的名字叫{},今年{}歲".format(a,b)
display(c)
db.close()
結果如下:

Ⅲ 使用pandas中的read_sql()方法,將提取到的數(shù)據(jù)直接轉化為DataFrame進行操作;
import pymysql
import pandas as pd
db = pymysql.connect(host='localhost',user='root',db='huangwei',password='123456',port=3306,charset='utf8')
cursor = db.cursor()
df1 = pd.read_sql("select * from student where ssex='男'",db)
display(df1)
df2 = pd.read_sql("select * from student where ssex='女'",db)
display(df2)
結果如下:

3. Python操作MongoDB數(shù)據(jù)庫
這一部分主要帶大家對比學習:關系型數(shù)據(jù)和非關系型數(shù)據(jù)庫的不同之處。咱們了解一下即可,不必過深研究,因為數(shù)據(jù)分析師基本不會使用這種數(shù)據(jù)庫。
Python操作MongoDB使用的是pymongo庫。需要我們使用如下命令提前安裝:
pip insatll pymongo
更多細節(jié)參考:Python操作MongoDB詳解!
① Python鏈接MongoDB服務器
from pymongo import MongoClient
conn = MongoClient("localhost",27017)
② Python怎么獲取MongoDB中的數(shù)據(jù)?
Ⅰ 查詢部分文檔;
res = collection.find({"age": {"$gte": 19}})
for row in res:
print(row)
Ⅱ 查詢所有文檔;
res = collection.find() for row in res: print(row)
Ⅲ 統(tǒng)計查詢;
res = collection.find().count() print(res)
Ⅳ 根據(jù) id 查詢;
這里需要引入第三方庫。
from bson.objectid import ObjectId
res = collection.find({"_id":ObjectId("5cc506289e1d88c95465488e")})
print(res[0])
Ⅴ 升序排序;
res = collection.find().sort("age")
for row in res:
print(row)
Ⅵ 降序排序;
這里也需要引入第三方庫。
import pymongo
res = collection.find().sort("age",pymongo.DESCENDING)
for row in res:
print(row)
Ⅶ 分頁查詢
res = collection.find().limit(3).skip(5) for row in res: print(row)
以上就是Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫深入對比的詳細內容,更多關于Python操作MySQL MongoDB Oracle對比的資料請關注腳本之家其它相關文章!
相關文章
Python利用PIL實現(xiàn)多張圖片合成gif動畫的案例詳解
這篇文章主要介紹了Python利用PIL實現(xiàn)多張圖片合成gif動畫的案例,文章通過代碼示例介紹的非常詳細,對大家的學習或工作有一定的幫助,感興趣的小伙伴可以自己動手試一下2023-11-11
python中關于CIFAR10數(shù)據(jù)集的使用
這篇文章主要介紹了python中關于CIFAR10數(shù)據(jù)集的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
python pyinstaller打包exe報錯的解決方法
這篇文章主要給大家介紹了關于python pyinstaller打包exe報錯的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-11-11
在Windows中設置Python環(huán)境變量的實例講解
下面小編就為大家分享一篇在Windows中設置Python環(huán)境變量的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04

