五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動(dòng)態(tài)

Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫深入對(duì)比

發(fā)布日期:2021-12-18 22:47 | 文章來源:CSDN

作為數(shù)據(jù)分析師,掌握一門數(shù)據(jù)庫語言,是很有必要的。

今天黃同學(xué)就帶著大家學(xué)習(xí)兩個(gè)關(guān)系型數(shù)據(jù)庫MySQL、Oracle,了解一個(gè)非關(guān)系數(shù)據(jù)庫MongoDB。

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

這一部分的難點(diǎn)在于:環(huán)境配置有點(diǎ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é)會(huì)中文亂碼;
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é)會(huì)中文亂碼;
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)該是國(guó)內(nèi)應(yīng)用最多的數(shù)據(jù)庫。大多數(shù)公司一般都是使用的該數(shù)據(jù)庫。這也就是很多學(xué)生在畢業(yè)之前都會(huì)選擇學(xué)習(xí)該數(shù)據(jù)庫知識(shí),用于面試。

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')

這里面有六個(gè)參數(shù),需要為大家一一介紹一下:

參數(shù)host:mysql服務(wù)器所在的主機(jī)的ip;

參數(shù)user:用戶名;

參數(shù)password:密碼;

參數(shù)port:連接的mysql主機(jī)的端口,默認(rèn)是3306;

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

參數(shù)charset:當(dāng)讀取數(shù)據(jù)出現(xiàn)中文會(huì)亂碼的時(shí)候,需要我們?cè)O(shè)置一下編碼;我們使用python操作數(shù)據(jù)庫的時(shí)候,那么python就相當(dāng)于是client,我們是用這個(gè)client來操作mysql的server服務(wù)器,python3默認(rèn)采用的utf8字符集,我的mysql服務(wù)器默認(rèn)采用latin1字符集,因此mysql中創(chuàng)建的每張表,都是建表的時(shí)候加了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ù)庫

這一部分主要帶大家對(duì)比學(xué)習(xí):關(guān)系型數(shù)據(jù)和非關(guān)系型數(shù)據(jù)庫的不同之處。咱們了解一下即可,不必過深研究,因?yàn)閿?shù)據(jù)分析師基本不會(huì)使用這種數(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)計(jì)查詢;

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ù)庫深入對(duì)比的詳細(xì)內(nèi)容,更多關(guān)于Python操作MySQL MongoDB Oracle對(duì)比的資料請(qǐng)關(guān)注本站其它相關(guān)文章!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部