In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要讲解了"Python操作MySQL MongoDB Oracle三大数据库的区别有哪些",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Python操作MySQL MongoDB Oracle三大数据库的区别有哪些"吧!
1. Python操作Oracle数据库
这一部分的难点在于:环境配置有点繁琐。不用担心,我为大家写了一篇关于Oracle环境配置的文章。
Python操作Oracle使用的是cx_Oracle库。需要我们使用如下命令提前安装:
pip insatll cx_Oracle
① Python链接Oracle服务器的3种方式
# ① 用户名、密码和监听写在一起import cx_Oracledb = cx_Oracle.connect('scott/a123456@DESKTOP-V4LKB10:1521/orcl')# ② 用户名、密码和监听分开写import cx_Oracledb = cx_Oracle.connect("scott","a123456","192.168.2.1:1521/orcl")# ③ 配置监听并连接import cx_Oraclemoniter = cx_Oracle.makedsn('192.168.2.1',1521,'orcl')db = cx_Oracle.connect('scott','a123456',moniter)
② Python怎么获取Oracle中的数据?
这里有三种常用的方法,分别为大家进行介绍。
Ⅰ fetchone():一次获取一条记录;
import cx_Oracle# 注意:一定要加下面这两行代码,负责会中文乱码;import osos.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 osos.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()方法,将提取到的数据直接转化为DataFrame进行操作;
import cx_Oracleimport pandas as pdimport osos.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数据库
MySQL数据库应该是国内应用最多的数据库。大多数公司一般都是使用的该数据库。这也就是很多学生在毕业之前都会选择学习该数据库知识,用于面试。
Python操作MySQL使用的是cx_Oracle库。需要我们使用如下命令提前安装:
pip insatll pymysql
更多细节参考:Python操作Oracle详解!
① Python链接MySQL服务器
import pymysql db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='spiders',charset=' utf8')
这里面有六个参数,需要为大家一一介绍一下:
参数host:mysql服务器所在的主机的ip;
参数user:用户名;
参数password:密码;
参数port:连接的mysql主机的端口,默认是3306;
参数db:连接的数据库名;
参数charset:当读取数据出现中文会乱码的时候,需要我们设置一下编码;我们使用python操作数据库的时候,那么python就相当于是client,我们是用这个client来操作mysql的server服务器,python3默认采用的utf8字符集,我的mysql服务器默认采用latin1字符集,因此mysql中创建的每张表,都是建表的时候加了utf8编码的,因此这里设置的应该就是connection连接器的编码;
② Python怎么获取MySQL中的数据?
Ⅰ 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()方法,将提取到的数据直接转化为DataFrame进行操作;
import pymysql import pandas as pddb = 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数据库
这一部分主要带大家对比学习:关系型数据和非关系型数据库的不同之处。咱们了解一下即可,不必过深研究,因为数据分析师基本不会使用这种数据库。
Python操作MongoDB使用的是pymongo库。需要我们使用如下命令提前安装:
pip insatll pymongo
更多细节参考:Python操作MongoDB详解!
① Python链接MongoDB服务器
from pymongo import MongoClientconn = MongoClient("localhost",27017)
② Python怎么获取MongoDB中的数据?
Ⅰ 查询部分文档;
res = collection.find({"age": {"$gte": 19}})for row in res: print(row)
Ⅱ 查询所有文档;
res = collection.find()for row in res: print(row)
Ⅲ 统计查询;
res = collection.find().count()print(res)
Ⅳ 根据 id 查询;
这里需要引入第三方库。
from bson.objectid import ObjectIdres = collection.find({"_id":ObjectId("5cc506289e1d88c95465488e")})print(res[0])
Ⅴ 升序排序;
res = collection.find().sort("age")for row in res: print(row)
Ⅵ 降序排序;
这里也需要引入第三方库。
import pymongores = 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三大数据库的区别有哪些"的内容了,经过本文的学习后,相信大家对Python操作MySQL MongoDB Oracle三大数据库的区别有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.