In-depth comparison of Python operations on MySQL, MongoDB, and Oracle databases!

As a data analyst, it is necessary to master a database language.

Today, Huang will take you to learn about two relational databases, MySQL and Oracle, and learn about a non-relational database, MongoDB.

For data analysts, the most important thing to learn about databases is to learn their query capabilities. This article takes this as an entry point and tells you how to use Python to operate these three databases.
insert image description here

1. Python operates Oracle database

The difficulty of this part is that the environment configuration is a bit cumbersome. Don't worry, I wrote an article for you all about Oracle environment configuration .

Python uses the cx_Oracle library to operate Oracle. We need to install it in advance using the following command:

pip insatll cx_Oracle

① 3 ways for Python to link to the Oracle server

# ① 用户名、密码和监听写在一起
import cx_Oracle
db = cx_Oracle.connect('scott/a123456@DESKTOP-V4LKB10:1521/orcl')

# ② 用户名、密码和监听分开写
import cx_Oracle
db = cx_Oracle.connect("scott","a123456","192.168.2.1:1521/orcl")

# ③ 配置监听并连接
import cx_Oracle
moniter = cx_Oracle.makedsn('192.168.2.1',1521,'orcl')
db = cx_Oracle.connect('scott','a123456',moniter)

② How does Python get the data in Oracle?

There are three commonly used methods, which will be introduced to you respectively.

Ⅰ fetchone(): fetch one record at a time;
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()

The result is as follows:
insert image description here

Ⅱ fetchall(): Get all records at once;
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()

The result is as follows:
insert image description here

Ⅲ Use the read_sql() method in pandas to directly convert the extracted data into DataFrame for operation;
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)

The result is as follows:
insert image description here

2. Python to operate MySQL database

MySQL database should be the most widely used database in China. This database is generally used by most companies. This means that many students will choose to learn the database knowledge for interviews before graduation.

Python uses the cx_Oracle library to operate MySQL. We need to install it in advance using the following command:

pip insatll pymysql

For more details, please refer to: Python operation Oracle detailed explanation!

① Python link MySQL server

import pymysql 

db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='spiders',charset=' utf8')

There are six parameters here, which need to be introduced for you one by one:

  • Parameter host: the ip of the host where the mysql server is located;
  • Parameters user: username;
  • Parameters password: password;
  • Parameter port: the port of the connected mysql host, the default is 3306;
  • Parameter db: the database name of the connection;
  • Parameter charset: When reading data appears garbled in Chinese, we need to set the encoding; when we use python to operate the database, then python is equivalent to the client, we use this client to operate the mysql server server, python3 defaults to The utf8 character set, my mysql server uses the latin1 character set by default, so each table created in mysql is utf8 encoded when the table is created, so the code set here should be the connection connector;

② How does Python get the data in MySQL?

Ⅰ fetchone(): fetch one record at a time;
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()

The result is as follows:
insert image description here

Ⅱ fetchall(): Get all records at once;
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()

The result is as follows:
insert image description here

Ⅲ Use the read_sql() method in pandas to directly convert the extracted data into DataFrame for operation;
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)

The result is as follows:
insert image description here

3. Python operates MongoDB database

This part mainly takes you to compare and learn: the difference between relational data and non-relational databases. Let's have a look at it, we don't need to delve into it, because data analysts basically don't use this kind of database.

Python uses the pymongo library to operate MongoDB. We need to install it in advance using the following command:

pip insatll pymongo

For more details, please refer to: Python operation MongoDB detailed explanation!

① Python links MongoDB server

from pymongo import MongoClient

conn = MongoClient("localhost",27017)

② How does Python get the data in MongoDB?

Ⅰ Query some documents;
res = collection.find({
   
   "age": {
   
   "$gte"19}})
for row in res:
	print(row)
Ⅱ Query all documents;
res = collection.find()
for row in res:
	print(row)
Ⅲ Statistical query;
res = collection.find().count()
print(res)
Ⅳ Query by id;

A third-party library needs to be introduced here.

from bson.objectid import ObjectId
res = collection.find({
   
   "_id":ObjectId("5cc506289e1d88c95465488e")})
print(res[0])
Ⅴ Sort in ascending order;
res = collection.find().sort("age")
for row in res:
	print(row)
Ⅵ sort in descending order;

Third-party libraries also need to be introduced here.

import pymongo
res = collection.find().sort("age",pymongo.DESCENDING)
for row in res:
	print(row)
VII Pagination query
res = collection.find().limit(3).skip(5)
for row in res:
	print(row)

Guess you like

Origin blog.csdn.net/weixin_41261833/article/details/120930905