python连接数据库oracle,mysql,sql server

本文介绍了Python连接Oracle、MySQL和SQL Server数据库的方法。对于Oracle,使用cx_Oracle库;对于MySQL,使用pymysql库;对于SQL Server,使用pymssql库。此外,还详细阐述了万能的SQLAlchemy库如何与pandas结合,实现不同数据库的连接,最终返回DataFrame以方便数据处理。推荐使用sqlalchemy,因其强大的功能和易用性。
摘要由CSDN通过智能技术生成

1.python连接oracle 使用的库为cx_Oracle

    我们先把cx_Oracle模块安装好,我主要是使用pip install cx_Oracle ,python版本是3.6. 注意如下代码中:connect('user/password@ip:port/database') 其中的user 对应数据库的用户名,password对应密码,ip对应机器地址 ,port对应端口 ,database对应数据库,大家在实际使用的时候不要照搬,注意灵活替换哦。


import cx_Oracle as co
import pandas as pd
#connect oracle database
conn=co.connect('user/password@ip:port/database')
#create sursor,remember to close the cursor when you finished using
c=conn.cursor()
#query 
sql='select * from table a '
r=c.execute(sql)
#get the all rusult,when get one result you can use fetchone
result=c.fetchall()
pd_result=pd.DataFrame(result)
print(pd_result)
#release the connection
c.close()
conn.close()

2.python 连接mysql   

使用的库为pymysql 先安装pymysql ,依旧使用pip安装,pip install pymysql</

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值