在Jupyter notebook或者Jupyter Lab中还可以执行SQL
下面来尝试一下
ipython-sql
需要安装一下这个
Github地址:https://github.com/catherinedevlin/ipython-sql
在conda里搜了下,没有

但是,我Google了一下,发现是有的

之前也用过这种命令安装什么,但是一直不知道这个conda-forge是啥

是可以将conda-forge添加到当前环境搜索中的
conda config --add channels conda-forge
然后可以直接使用
conda install ipython-sql
安装之后,重启一下Jupyter就行了
在Jupyter Notebook中使用
- 加载ipython-sql插件
%load_ext sql
- 连接数据库
PostgreSQL: postgresql://scott:tiger@localhost/mydatabase
MySQL: mysql://scott:tiger@localhost/foo
Oracle: oracle://scott:tiger@127.0.0.1:1521/sidname
SQL Server: mssql+pyodbc://scott:tiger@mydsn
SQLite: sqlite:///foo.db
好像sqlalchemy
中可以连接的都可以

- 执行SQL
上面如果连接成功了,这里就可以执行SQL了
result = %sql select * from inoherb_weshop.t_xy_user limit 1

直接输出,就是结果集了
然后,我们可以转换成DataFrame了
result.DataFrame()
这样就可以了
- 执行一组SQL
%%sql
xxx
xxx
像这样:
%%sql
CREATE TABLE EMPLOYEE(firstname varchar(50),lastname varchar(50));
INSERT INTO EMPLOYEE VALUES('Tom','Mitchell');
INSERT INTO EMPLOYEE VALUES('Jack','Ryan');
这样子,的确有点儿方便了,还是有些场景的
网友评论