Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫深入對比
作為數(shù)據(jù)分析師,掌握一門數(shù)據(jù)庫語言,是很有必要的。
今天黃同學(xué)就帶著大家學(xué)習(xí)兩個關(guān)系型數(shù)據(jù)庫MySQL、Oracle,了解一個非關(guān)系數(shù)據(jù)庫MongoDB。
1. Python操作Oracle數(shù)據(jù)庫
這一部分的難點在于:環(huán)境配置有點繁瑣。不用擔(dān)心,我為大家寫了一篇關(guān)于Oracle環(huán)境配置的文章。
Python操作Oracle使用的是cx_Oracle庫。需要我們使用如下命令提前安裝:
pip insatll cx_Oracle
① Python鏈接Oracle服務(wù)器的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ù)?
這里有三種常用的方法,分別為大家進(jìn)行介紹。
Ⅰ fetchone():一次獲取一條記錄;
import cx_Oracle # 注意:一定要加下面這兩行代碼,負(fù)責(zé)會中文亂碼; 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()
結(jié)果如下:
Ⅱ fetchall():一次獲取所有記錄;
import cx_Oracle # 注意:一定要加下面這兩行代碼,負(fù)責(zé)會中文亂碼; 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()
結(jié)果如下:
Ⅲ 使用pandas中的read_sql()方法,將提取到的數(shù)據(jù)直接轉(zhuǎn)化為DataFrame進(jìn)行操作;
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)
結(jié)果如下:
2. Python操作MySQL數(shù)據(jù)庫
MySQL數(shù)據(jù)庫應(yīng)該是國內(nèi)應(yīng)用最多的數(shù)據(jù)庫。大多數(shù)公司一般都是使用的該數(shù)據(jù)庫。這也就是很多學(xué)生在畢業(yè)之前都會選擇學(xué)習(xí)該數(shù)據(jù)庫知識,用于面試。
Python操作MySQL使用的是cx_Oracle庫。需要我們使用如下命令提前安裝:
pip insatll pymysql
更多細(xì)節(jié)參考:Python操作Oracle詳解!
① Python鏈接MySQL服務(wù)器
import pymysql db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='spiders',charset=' utf8')
這里面有六個參數(shù),需要為大家一一介紹一下:
參數(shù)host:mysql服務(wù)器所在的主機的ip;
參數(shù)user:用戶名;
參數(shù)password:密碼;
參數(shù)port:連接的mysql主機的端口,默認(rèn)是3306;
參數(shù)db:連接的數(shù)據(jù)庫名;
參數(shù)charset:當(dāng)讀取數(shù)據(jù)出現(xiàn)中文會亂碼的時候,需要我們設(shè)置一下編碼;我們使用python操作數(shù)據(jù)庫的時候,那么python就相當(dāng)于是client,我們是用這個client來操作mysql的server服務(wù)器,python3默認(rèn)采用的utf8字符集,我的mysql服務(wù)器默認(rèn)采用latin1字符集,因此mysql中創(chuàng)建的每張表,都是建表的時候加了utf8編碼的,因此這里設(shè)置的應(yīng)該就是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()
結(jié)果如下:
Ⅱ 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()
結(jié)果如下:
Ⅲ 使用pandas中的read_sql()方法,將提取到的數(shù)據(jù)直接轉(zhuǎn)化為DataFrame進(jìn)行操作;
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)
結(jié)果如下:
3. Python操作MongoDB數(shù)據(jù)庫
這一部分主要帶大家對比學(xué)習(xí):關(guān)系型數(shù)據(jù)和非關(guān)系型數(shù)據(jù)庫的不同之處。咱們了解一下即可,不必過深研究,因為數(shù)據(jù)分析師基本不會使用這種數(shù)據(jù)庫。
Python操作MongoDB使用的是pymongo庫。需要我們使用如下命令提前安裝:
pip insatll pymongo
更多細(xì)節(jié)參考:Python操作MongoDB詳解!
① Python鏈接MongoDB服務(wù)器
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ù)庫深入對比的詳細(xì)內(nèi)容,更多關(guān)于Python操作MySQL MongoDB Oracle對比的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python利用PIL實現(xiàn)多張圖片合成gif動畫的案例詳解
這篇文章主要介紹了Python利用PIL實現(xiàn)多張圖片合成gif動畫的案例,文章通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以自己動手試一下2023-11-11python中關(guān)于CIFAR10數(shù)據(jù)集的使用
這篇文章主要介紹了python中關(guān)于CIFAR10數(shù)據(jù)集的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02python pyinstaller打包exe報錯的解決方法
這篇文章主要給大家介紹了關(guān)于python pyinstaller打包exe報錯的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11在Windows中設(shè)置Python環(huán)境變量的實例講解
下面小編就為大家分享一篇在Windows中設(shè)置Python環(huán)境變量的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04