【问题标题】:How do I create a custom Sqlite UDF while using ipython-sql in a Jupyter Notebook?在 Jupyter Notebook 中使用 ipython-sql 时如何创建自定义 Sqlite UDF?
【发布时间】:2019-07-03 13:46:50
【问题描述】:

我将如何使用 ipython-sql (https://github.com/catherinedevlin/ipython-sql) 在 Jupyter/iPython 中创建自定义 SQLite UDF 还是其他 iPython SQL 魔术库?

我正在尝试做这样的事情......

%load_ext sql
%sql sqlite:///

%sql create table example(col text)
%sql insert into example (col) values ('stuff')

to_upper = lambda x: x.upper()

%sql select upper(col) from example

当使用 sqlite3 Python 库时,它会...

from sqlalchemy import create_engine

conn = create_engine('sqlite://')
conn.execute('create table example(col text)')
conn.execute("insert into example values ('stuff')")

to_upper = lambda x: x.upper()

connection = conn.raw_connection()
connection.create_function('to_upper', 1, to_upper)
print(conn.execute("select to_upper(col) col from example").fetchall()[0][0])

有什么想法吗??

【问题讨论】:

    标签: python sql sqlite jupyter-notebook jupyter


    【解决方案1】:
    %load_ext sql
    %sql sqlite:///
    
    %sql create table example(col text)
    %sql insert into example (col) values ('stuff')
    
    to_upper = lambda x: x.upper()
    
    x=%sql -l
    connection=x['sqlite://'].session.connection.connection
    
    connection.create_function('to_upper', 1, to_upper)
    
    %sql select upper(col) from example
    

    【讨论】:

      猜你喜欢
      • 2020-02-14
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多