一个简单的sql连接包装器,使用sqlalchemy和pandas read_sql来标准化sql工作流。

sql-connectors的Python项目详细描述


https://img.shields.io/pypi/v/sql_connectors.svghttps://img.shields.io/travis/aiguofer/sql_connectors.svgDocumentation StatusUpdates

一个简单的sql连接包装器,使用sqlalchemy和pandas read_sql来标准化sql工作流。本项目的主要目标是在使用基于sql的数据源时减少样板代码,并在python中实现对数据源的交互式探索。

功能

  • 用于处理不同SQL数据源的标准化客户端,包括用于定义连接配置的标准化格式
  • 一个基于sqlalchemyEngine的sqlclient接口,带有一些有用的函数,如pandasread_sql和利用sqlalchemy中的reflection的函数

安装

稳定释放

要安装SQL连接器,请在终端中运行此命令:

$ pip install --process-dependency-links sql_connectors

这是安装SQL连接器的首选方法,因为它总是安装最新的稳定版本。

如果您没有安装pip,这个Python installation guide可以指导您 你完成了整个过程。

开发安装

可以从Github repo下载sql连接器的源代码。

您可以克隆公共存储库并在开发模式下安装:

$ git clone git://github.com/aiguofer/sql_connectors
$cd sql_connectors
$ pip install --process-dependency-links -e .[dev]

配置

通过实现自己的Storage,可以将配置存储在任何需要的地方。但是,默认值是LocalStorage~/.config/sql_connectors读取配置文件。

可以使用SQL_CONNECTORS_STORAGE环境变量(例如sql_connectors.storage.LocalStorage)更改Storage类,还可以使用SQL_CONNECTORS_PATH_OR_URI指定不同的配置目录或uri。

example_connection.json文件是作为模板提供的;可以用自己的连接详细信息替换它并重新命名该文件。

示例文件的内容是:

{"drivername":"sqlite","relative_paths":["database"],"default_env":"default","default":{"database":"example_connection.db"}}

这些字段的含义如下:

drivername (string)
This required field is a SQLAlchemy dialect or dialect+driver. See the SQLAlchemy Engine documentation for more details. You may first have to install the required python modules for your dialect+driver to work if it’s a third party plug-in.
relative_paths (list of strings)
This optional field lets you specify if an option for your connection needs to load a file relative to your config directory. For example, if you had a connection that needed to use a cert, you could add ^{tt12}$ to this list, set ^{tt13}$, and drop the cert in ^{tt14}$.
default_env (string)
This optional field lets you specify which environment should be used by default. If not included, it will use ^{tt15}$.
default_schema (string)
This optional field lets you specify which schema should be used by default. If not included, it will use ^{tt16}$.
default_reflect (boolean)
This optional field lets you specify whether it should reflect the data source by default. If not included, it will use ^{tt17}$.
env.username (string)
This optional field specifies the username for the connection. If it’s left out or set to null and the driver is not ‘sqlite’, the user will be prompte when they try to create the client. If the connection doesn’t have credentials, set this to an empty string. Should not be set for ‘sqlite’.
env.password (string)
This optional field specifies the password for the connection. If it’s left out or set to null and the driver is not ‘sqlite’, the user will be prompte when they try to create the client. If the connection doesn’t have credentials, set this to an empty string. Should not be set for ‘sqlite’.
env.host (string)
This optional field specifies the host for the connection. Should not be set for ‘sqlite’.
env.port (string or integer)
This optional field specifies the port for the connection. Should not be set for ‘sqlite’.
env.database (string)
This optional field specifies the database name for the connection. If it’s a ‘sqlite’ connection and left empty, it will use ^{tt18}$. Otherwise, you can specify a relative path or an absolute path; if you want the file in your config directory, you can use the ^{tt19}$ property.
env.query (object)
This optional field is a json object with options to pass onto the dialect and/or DBAPI upon connect.
env.allowed_hosts (list of strings)
This optional field is a list of strings containing hostnames where the given credentials are accepted. If the hostname is not in the list, it will prompt the user for credentials. This was added due to some specific usecase where we share service credentials but they’re only allowed on our common servers.

如何

模块将检查可用的连接配置,并在顶层模块中为每个配置创建变量。它将为每个配置创建2个变量,connection_nameconnection_name_envs;这两个都是函数,第一个函数将返回一个get_client函数,并根据配置设置一些默认值,第二个函数将返回一个get_available_envs函数,当调用该函数时,该函数将返回给定数据源的可用环境。启用reflection时,客户端将保存有关可用表的元数据。

这是一个基本的用法示例,假设示例配置文件存在:

fromsql_connectorsimportconnectionsclient=connections.example_connection()client.read_sql('select 1')

下面是一个更复杂的示例,它相当多余,但显示了更多功能

fromsql_connectorsimportconnectionsavailable_envs=connections.example_connection_envs()client=connections.example_connection(env=available_envs[0],reflect=True)client.read_sql('select 1').to_sql('example_table',client,if_exists='replace')available_tables=client.table_names()table1=client.get_table(available_tables[0])df=client.read_sql(table1.select())

学分

这个包是用Cookiecutteraudreyr/cookiecutter-pypackage项目模板创建的。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java传递实现接口的内部类   java为什么managedbean方法在post请求中的过滤器调用之前调用   java Android应用程序在Android M,O上崩溃,登录A/libc时出错:tid 17938中的致命信号11(SIGSEGV),代码2,故障地址0x343a2944   java JPA存储库。findByAll()返回null,但数据库中存在所有内容   用于发送电子邮件的java Ant脚本   Java小程序HTML页面发生了奇怪的事情   在Postman的请求正文中传递多个JSON数据,并使用Jersy(JXRS)进入Java Rest API   无法强制转换java DAO类   10分钟后重新连接java   正则表达式如何在java中选择字符串直到某个字符   java我的输出有一些问题   Objective中的java时间段   java如何在新屏幕上基于按钮单击在webview中打开url   java哪个selenium版本与Firefox ESR 45.2.0兼容   java无法使用Apache Camel 3.1.0将XML转换为JSON