pycharm连接python,Python Pycharm和SQL Server连接

本文介绍了如何在Python中利用ORM(对象关系映射)库SQLAlchemy来连接和查询SQL Server数据库。首先,你需要通过pip安装SQLAlchemy。然后,创建Engine对象以建立数据库连接,并使用该对象执行查询操作,类似于R中的RODBC包。这使得在Python代码中直接处理数据库数据变得简单。
摘要由CSDN通过智能技术生成

I would like to use data from SQL server in Pycharm using python. I have my database connection set up in Pycharm, but not sure how to access this data within my python code. I would like to query the data within the python code (similar to what I would do in R using the RODBC package).

Any suggestions on what to do or where to look would be much appreciated.

解决方案

There is a concept called OR(Object Relational) Mapping in python, which can be used for database connections. One of the modules that you need to import for the purpose is SQLAlchemy.

First, you will need to install sqlalchemy by:

pip install sqlalchemy

Now, for database connection, we have an Engine class in the sqlalchemy, which is responsible for the database connectivity. We create an object of the Engine class for establishing connection.

from sqlalchemy import create_engine,MetaData,select

engine=create_engine("mysql://user:pwd@localhost/dbname", echo=True)

connection=engine.connect()

The process of reading the database and creating metadata is called Reflection.

metadata=MetaData()

query=select([Student]) #Assuming that my database already has a table named Student

result=connection.execute(query)

row=result.fetchall() #This works similar to the select* query

In this way, you can manipulate data through other queries too, using sqlalchemy!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值