简体   繁体   English

在 Jupyter Notebook 中使用 ipython-sql 时,如何创建自定义 Sqlite UDF?

[英]How do I create a custom Sqlite UDF while using ipython-sql in a Jupyter Notebook?

How would I go about creating a custom SQLite UDF in Jupyter/iPython with ipython-sql ( https://github.com/catherinedevlin/ipython-sql ) or another iPython SQL magic library?我将如何使用 ipython-sql ( https://github.com/catherinedevlin/ipython-sql ) 或其他 iPython SQL 魔术库在 Jupyter/iPython 中创建自定义 SQLite UDF?

I'm trying to do something like this...我正在尝试做这样的事情......

%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

When using the sqlite3 Python library it would be...使用 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])

Any ideas??有任何想法吗??

%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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM