在Jupyter笔记本中使用ipythonsql时,如何创建自定义Sqlite UDF?

2024-04-28 22:16:55 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用ipythonsql(https://github.com/catherinedevlin/ipython-sql)在Jupyter/iPython中创建自定义SQLite UDF 还是另一个ipythonsql魔法库

我正试着做这样的事

%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])

有什么想法吗


Tags: totextfromexecutesqlitesqlexamplecreate