溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫的區(qū)別有哪些

發(fā)布時間:2021-10-27 11:09:56 來源:億速云 閱讀:163 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫的區(qū)別有哪些”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫的區(qū)別有哪些”吧!

1. Python操作Oracle數(shù)據(jù)庫

這一部分的難點在于:環(huá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ù)?

這里有三種常用的方法,分別為大家進行介紹。

Ⅰ 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()

結(jié)果如下:

Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫的區(qū)別有哪些

Ⅱ 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()

結(jié)果如下:

Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫的區(qū)別有哪些

Ⅲ 使用pandas中的read_sql()方法,將提取到的數(shù)據(jù)直接轉(zhuǎn)化為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)

結(jié)果如下:

Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫的區(qū)別有哪些

2. Python操作MySQL數(shù)據(jù)庫

MySQL數(shù)據(jù)庫應(yīng)該是國內(nèi)應(yīng)用最多的數(shù)據(jù)庫。大多數(shù)公司一般都是使用的該數(shù)據(jù)庫。這也就是很多學生在畢業(yè)之前都會選擇學習該數(shù)據(jù)庫知識,用于面試。

Python操作MySQL使用的是cx_Oracle庫。需要我們使用如下命令提前安裝:

pip insatll pymysql

更多細節(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主機的端口,默認是3306;

參數(shù)db:連接的數(shù)據(jù)庫名;

參數(shù)charset:當讀取數(shù)據(jù)出現(xiàn)中文會亂碼的時候,需要我們設(shè)置一下編碼;我們使用python操作數(shù)據(jù)庫的時候,那么python就相當于是client,我們是用這個client來操作mysql的server服務(wù)器,python3默認采用的utf8字符集,我的mysql服務(wù)器默認采用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é)果如下:

Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫的區(qū)別有哪些

Ⅱ 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é)果如下:

Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫的區(qū)別有哪些

Ⅲ 使用pandas中的read_sql()方法,將提取到的數(shù)據(jù)直接轉(zhuǎn)化為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)

結(jié)果如下:

Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫的區(qū)別有哪些

3. Python操作MongoDB數(shù)據(jù)庫

這一部分主要帶大家對比學習:關(guān)系型數(shù)據(jù)和非關(guān)系型數(shù)據(jù)庫的不同之處。咱們了解一下即可,不必過深研究,因為數(shù)據(jù)分析師基本不會使用這種數(shù)據(jù)庫。

Python操作MongoDB使用的是pymongo庫。需要我們使用如下命令提前安裝:

pip insatll pymongo

更多細節(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ù)庫的區(qū)別有哪些”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫的區(qū)別有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI